Skip to content

Commit 9c4daf4

Browse files
authored
Merge pull request #57 from HyperCodec/examples-indicatif
Add indicatif progress bars for examples and increase generation count.
2 parents d150af4 + d3a9c40 commit 9c4daf4

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

Cargo.lock

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ serde = ["dep:serde", "dep:serde-big-array"]
2828
[dependencies]
2929
bitflags = "2.5.0"
3030
genetic-rs = { version = "0.5.1", features = ["derive"] }
31+
3132
lazy_static = "1.4.0"
3233
rand = "0.8.5"
3334
rayon = { version = "1.8.1", optional = true }
@@ -37,4 +38,5 @@ serde-big-array = { version = "0.5.1", optional = true }
3738
[dev-dependencies]
3839
bincode = "1.3.3"
3940
serde_json = "1.0.114"
40-
plotters = "0.3.5"
41+
plotters = "0.3.5"
42+
indicatif = "0.17.8"

examples/basic.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! A basic example of NEAT with this crate. Enable the `crossover` feature for it to use crossover reproduction
22
3+
use indicatif::{ProgressBar, ProgressStyle};
34
use neat::*;
45
use rand::prelude::*;
56

@@ -103,10 +104,23 @@ fn main() {
103104
crossover_pruning_nextgen,
104105
);
105106

106-
for _ in 0..100 {
107+
const GENS: u64 = 1000;
108+
let pb = ProgressBar::new(GENS)
109+
.with_style(
110+
ProgressStyle::with_template(
111+
"[{elapsed_precise}] {bar:40.cyan/blue} | {msg} {pos}/{len}",
112+
)
113+
.unwrap(),
114+
)
115+
.with_message("gen");
116+
117+
for _ in 0..GENS {
107118
sim.next_generation();
119+
pb.inc(1);
108120
}
109121

122+
pb.finish();
123+
110124
#[cfg(not(feature = "serde"))]
111125
let mut fits: Vec<_> = sim.genomes.iter().map(fitness).collect();
112126

examples/plot.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{
33
sync::{Arc, Mutex},
44
};
55

6+
use indicatif::{ProgressBar, ProgressStyle};
67
use neat::*;
78
use plotters::prelude::*;
89
use rand::prelude::*;
@@ -73,7 +74,7 @@ struct PerformanceStats {
7374
}
7475

7576
const OUTPUT_FILE_NAME: &'static str = "fitness-plot.svg";
76-
const GENS: usize = 100;
77+
const GENS: usize = 1000;
7778

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

98+
let pb = ProgressBar::new(GENS as u64)
99+
.with_style(
100+
ProgressStyle::with_template(
101+
"[{elapsed_precise}] {bar:40.cyan/blue} | {msg} {pos}/{len}",
102+
)
103+
.unwrap(),
104+
)
105+
.with_message("gen");
106+
97107
println!("Training...");
98108

99109
for _ in 0..GENS {
100110
sim.next_generation();
111+
112+
pb.inc(1);
101113
}
102114

115+
pb.finish();
116+
103117
// prevent `Arc::into_inner` from failing
104118
drop(sim);
105119

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

121135
chart.configure_mesh().draw()?;
122136

0 commit comments

Comments
 (0)