Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indicatif progress bars for examples and increase generation count. #57

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde = ["dep:serde", "dep:serde-big-array"]
[dependencies]
bitflags = "2.5.0"
genetic-rs = { version = "0.5.1", features = ["derive"] }

lazy_static = "1.4.0"
rand = "0.8.5"
rayon = { version = "1.8.1", optional = true }
Expand All @@ -37,4 +38,5 @@ serde-big-array = { version = "0.5.1", optional = true }
[dev-dependencies]
bincode = "1.3.3"
serde_json = "1.0.114"
plotters = "0.3.5"
plotters = "0.3.5"
indicatif = "0.17.8"
16 changes: 15 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A basic example of NEAT with this crate. Enable the `crossover` feature for it to use crossover reproduction

use indicatif::{ProgressBar, ProgressStyle};
use neat::*;
use rand::prelude::*;

Expand Down Expand Up @@ -103,10 +104,23 @@ fn main() {
crossover_pruning_nextgen,
);

for _ in 0..100 {
const GENS: u64 = 1000;
let pb = ProgressBar::new(GENS)
.with_style(
ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} | {msg} {pos}/{len}",
)
.unwrap(),
)
.with_message("gen");

for _ in 0..GENS {
sim.next_generation();
pb.inc(1);
}

pb.finish();

#[cfg(not(feature = "serde"))]
let mut fits: Vec<_> = sim.genomes.iter().map(fitness).collect();

Expand Down
18 changes: 16 additions & 2 deletions examples/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
sync::{Arc, Mutex},
};

use indicatif::{ProgressBar, ProgressStyle};
use neat::*;
use plotters::prelude::*;
use rand::prelude::*;
Expand Down Expand Up @@ -73,7 +74,7 @@ struct PerformanceStats {
}

const OUTPUT_FILE_NAME: &'static str = "fitness-plot.svg";
const GENS: usize = 100;
const GENS: usize = 1000;

fn main() -> Result<(), Box<dyn Error>> {
#[cfg(not(feature = "rayon"))]
Expand All @@ -94,12 +95,25 @@ fn main() -> Result<(), Box<dyn Error>> {
ng,
);

let pb = ProgressBar::new(GENS as u64)
.with_style(
ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} | {msg} {pos}/{len}",
)
.unwrap(),
)
.with_message("gen");

println!("Training...");

for _ in 0..GENS {
sim.next_generation();

pb.inc(1);
}

pb.finish();

// prevent `Arc::into_inner` from failing
drop(sim);

Expand All @@ -116,7 +130,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.margin(5)
.x_label_area_size(30)
.y_label_area_size(30)
.build_cartesian_2d(0usize..100, 0f32..200.0)?;
.build_cartesian_2d(0usize..GENS, 0f32..1000.0)?;

chart.configure_mesh().draw()?;

Expand Down
Loading