From 44b7fdbc37992f766d322f22c14e7133eab9a481 Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Wed, 15 May 2024 14:24:25 +0000 Subject: [PATCH] create progress bar for plotting example --- Cargo.lock | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++- examples/plot.rs | 14 ++++++++-- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c98cd1..700cf2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,6 +100,19 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys", +] + [[package]] name = "const-cstr" version = "0.3.0" @@ -240,6 +253,12 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "fdeflate" version = "0.3.4" @@ -417,6 +436,28 @@ dependencies = [ "png", ] +[[package]] +name = "indicatif" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "763a5a8f45087d6bcea4222e7b72c291a054edf80e4ef6efd2a4979878c7bea3" +dependencies = [ + "console", + "instant", + "number_prefix", + "portable-atomic", + "unicode-width", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "itoa" version = "1.0.10" @@ -493,6 +534,7 @@ dependencies = [ "bincode", "bitflags 2.5.0", "genetic-rs", + "indicatif", "lazy_static", "plotters", "rand", @@ -511,6 +553,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + [[package]] name = "once_cell" version = "1.19.0" @@ -601,6 +649,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -811,6 +865,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-width" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6" + [[package]] name = "walkdir" version = "2.5.0" @@ -937,6 +997,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.5" diff --git a/Cargo.toml b/Cargo.toml index 8ccd8ca..8305fe4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } @@ -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" \ No newline at end of file +plotters = "0.3.5" +indicatif = "0.17.8" diff --git a/examples/plot.rs b/examples/plot.rs index 2b6a851..af48b01 100644 --- a/examples/plot.rs +++ b/examples/plot.rs @@ -6,6 +6,7 @@ use std::{ use neat::*; use plotters::prelude::*; use rand::prelude::*; +use indicatif::{ProgressBar, ProgressStyle}; #[derive(RandomlyMutable, DivisionReproduction, Clone)] struct AgentDNA { @@ -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> { #[cfg(not(feature = "rayon"))] @@ -94,12 +95,21 @@ fn main() -> Result<(), Box> { 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); @@ -116,7 +126,7 @@ fn main() -> Result<(), Box> { .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()?;