From 072ae7d14b8dc2221fe06410bf0f99ba8ddadc77 Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:06:56 +0000 Subject: [PATCH 1/6] change cfg macros --- examples/basic.rs | 78 ++++++++--------------------------------------- 1 file changed, 13 insertions(+), 65 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index bcd5d6d..5d21054 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -87,55 +87,23 @@ fn fitness(dna: &AgentDNA) -> f32 { fitness } -#[cfg(all(not(feature = "crossover"), not(feature = "rayon")))] fn main() { + #[cfg(not(feature = "rayon"))] let mut rng = rand::thread_rng(); let mut sim = GeneticSim::new( + #[cfg(not(feature = "rayon"))] Vec::gen_random(&mut rng, 100), - fitness, - division_pruning_nextgen, - ); - for _ in 0..100 { - sim.next_generation(); - } + #[cfg(feature = "rayon")] + Vec::gen_random(100), - let fits: Vec<_> = sim.genomes.iter().map(fitness).collect(); - - let maxfit = fits - .iter() - .max_by(|a, b| a.partial_cmp(b).unwrap()) - .unwrap(); - - dbg!(&fits, maxfit); -} - -#[cfg(all(not(feature = "crossover"), feature = "rayon"))] -fn main() { - let mut sim = GeneticSim::new(Vec::gen_random(100), fitness, division_pruning_nextgen); - - for _ in 0..100 { - sim.next_generation(); - } - - let fits: Vec<_> = sim.genomes.iter().map(fitness).collect(); - - let maxfit = fits - .iter() - .max_by(|a, b| a.partial_cmp(b).unwrap()) - .unwrap(); - - dbg!(&fits, maxfit); -} + fitness, -#[cfg(all(feature = "crossover", not(feature = "rayon")))] -fn main() { - let mut rng = rand::thread_rng(); + #[cfg(not(feature = "crossover"))] + division_pruning_nextgen, - let mut sim = GeneticSim::new( - Vec::gen_random(&mut rng, 100), - fitness, + #[cfg(feature = "crossover")] crossover_pruning_nextgen, ); @@ -143,36 +111,16 @@ fn main() { sim.next_generation(); } - let mut fits: Vec<_> = sim.genomes.iter().map(|e| (e, fitness(e))).collect(); + let mut fits: Vec<_> = sim.genomes.iter().map(fitness).collect(); - fits.sort_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap()); + fits.sort_by(|a, b| a.partial_cmp(&b).unwrap()); dbg!(&fits); - if cfg!(feature = "serde") { + #[cfg(feature = "serde")] + { let intermediate = NNTSerde::from(&fits[0].0.network); let serialized = serde_json::to_string(&intermediate).unwrap(); println!("{}", serialized); } -} - -#[cfg(all(feature = "crossover", feature = "rayon"))] -fn main() { - let mut sim = GeneticSim::new(Vec::gen_random(100), fitness, crossover_pruning_nextgen); - - for _ in 0..100 { - sim.next_generation(); - } - - let mut fits: Vec<_> = sim.genomes.iter().map(|e| (e, fitness(e))).collect(); - - fits.sort_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap()); - - dbg!(&fits); - - if cfg!(feature = "serde") { - let intermediate = NNTSerde::from(&fits[0].0.network); - let serialized = serde_json::to_string(&intermediate).unwrap(); - println!("serialized: {}", serialized); - } -} +} \ No newline at end of file From fb806ae9bf2b080c2ed81d9de0e2f1a00683f395 Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:11:39 +0000 Subject: [PATCH 2/6] fix serde feature comp error --- examples/basic.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/examples/basic.rs b/examples/basic.rs index 5d21054..922f9d5 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -111,10 +111,18 @@ fn main() { sim.next_generation(); } + #[cfg(not(feature = "serde"))] let mut fits: Vec<_> = sim.genomes.iter().map(fitness).collect(); + + #[cfg(feature = "serde")] + let mut fits: Vec<_> = sim.genomes.iter().map(|e| (e, fitness(e))).collect(); + #[cfg(not(feature = "serde"))] fits.sort_by(|a, b| a.partial_cmp(&b).unwrap()); + #[cfg(feature = "serde")] + fits.sort_by(|(_, a), (_, b)| a.partial_cmp(&b).unwrap()); + dbg!(&fits); #[cfg(feature = "serde")] From 12727b86ea356f0c4e37c579d6702fe8beb25378 Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:14:24 +0000 Subject: [PATCH 3/6] cargo fmt --- examples/basic.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 922f9d5..9ad0419 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -94,15 +94,11 @@ fn main() { let mut sim = GeneticSim::new( #[cfg(not(feature = "rayon"))] Vec::gen_random(&mut rng, 100), - #[cfg(feature = "rayon")] Vec::gen_random(100), - fitness, - #[cfg(not(feature = "crossover"))] division_pruning_nextgen, - #[cfg(feature = "crossover")] crossover_pruning_nextgen, ); @@ -113,7 +109,7 @@ fn main() { #[cfg(not(feature = "serde"))] let mut fits: Vec<_> = sim.genomes.iter().map(fitness).collect(); - + #[cfg(feature = "serde")] let mut fits: Vec<_> = sim.genomes.iter().map(|e| (e, fitness(e))).collect(); @@ -131,4 +127,4 @@ fn main() { let serialized = serde_json::to_string(&intermediate).unwrap(); println!("{}", serialized); } -} \ No newline at end of file +} From aee433f657cecb25d34084bea89c7e91fc378412 Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:43:59 +0000 Subject: [PATCH 4/6] solve errors with activations_in_scope --- src/topology/activation.rs | 12 +++++++----- src/topology/mod.rs | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/topology/activation.rs b/src/topology/activation.rs index d216cb1..a774c5f 100644 --- a/src/topology/activation.rs +++ b/src/topology/activation.rs @@ -67,7 +67,7 @@ impl ActivationRegistry { } } - /// Gets a Vec of all the + /// Gets a Vec of all the activation functions registered. Unless you need an owned value, use [fns][ActivationRegistry::fns].values() instead. pub fn activations(&self) -> Vec { self.fns.values().cloned().collect() } @@ -77,7 +77,9 @@ impl ActivationRegistry { let acts = self.activations(); acts.into_iter() - .filter(|a| !scope.contains(ActivationScope::NONE) && scope.contains(a.scope)) + .filter(|a| { + a.scope != ActivationScope::NONE && a.scope.contains(scope) + }) .collect() } } @@ -101,7 +103,7 @@ impl Default for ActivationRegistry { bitflags! { /// Specifies where an activation function can occur - #[derive(Copy, Clone)] + #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct ActivationScope: u8 { /// Whether the activation can be applied to the input layer. const INPUT = 0b001; @@ -112,8 +114,8 @@ bitflags! { /// Whether the activation can be applied to the output layer. const OUTPUT = 0b100; - /// If this flag is true, it ignores all the rest and does not make the function naturally occur. - const NONE = 0b1000; + /// The activation function will not be randomly placed anywhere + const NONE = 0b000; } } diff --git a/src/topology/mod.rs b/src/topology/mod.rs index 02ad296..b1badf7 100644 --- a/src/topology/mod.rs +++ b/src/topology/mod.rs @@ -559,6 +559,8 @@ impl NeuronTopology { ) -> Self { let mut activations: Vec<_> = activations.into_iter().collect(); + + Self::new_with_activation( inputs, activations.remove(rng.gen_range(0..activations.len())), From 9369c11b03d9cc3e10c7426a93955bfe1c74ccb0 Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:44:49 +0000 Subject: [PATCH 5/6] cargo fmt --- src/topology/activation.rs | 4 +--- src/topology/mod.rs | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/topology/activation.rs b/src/topology/activation.rs index a774c5f..a711851 100644 --- a/src/topology/activation.rs +++ b/src/topology/activation.rs @@ -77,9 +77,7 @@ impl ActivationRegistry { let acts = self.activations(); acts.into_iter() - .filter(|a| { - a.scope != ActivationScope::NONE && a.scope.contains(scope) - }) + .filter(|a| a.scope != ActivationScope::NONE && a.scope.contains(scope)) .collect() } } diff --git a/src/topology/mod.rs b/src/topology/mod.rs index b1badf7..02ad296 100644 --- a/src/topology/mod.rs +++ b/src/topology/mod.rs @@ -559,8 +559,6 @@ impl NeuronTopology { ) -> Self { let mut activations: Vec<_> = activations.into_iter().collect(); - - Self::new_with_activation( inputs, activations.remove(rng.gen_range(0..activations.len())), From d40cb9eb45ac21823f4776cae5e866db3cd0377c Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Tue, 16 Apr 2024 14:49:21 +0000 Subject: [PATCH 6/6] change version number for hotfix --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7f3551e..be4d7b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,7 +118,7 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "neat" -version = "0.5.0" +version = "0.5.1" dependencies = [ "bincode", "bitflags", diff --git a/Cargo.toml b/Cargo.toml index be73813..91247ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "neat" description = "Crate for working with NEAT in rust" -version = "0.5.0" +version = "0.5.1" edition = "2021" authors = ["Inflectrix"] repository = "https://github.com/inflectrix/neat"