Skip to content

Commit

Permalink
Merge pull request #46 from inflectrix/dev
Browse files Browse the repository at this point in the history
hotfix
  • Loading branch information
HyperCodec authored Apr 16, 2024
2 parents fd65396 + b96ad33 commit 228f7af
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
82 changes: 17 additions & 65 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,92 +87,44 @@ 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),
#[cfg(feature = "rayon")]
Vec::gen_random(100),
fitness,
#[cfg(not(feature = "crossover"))]
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);
}

#[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);
}

#[cfg(all(feature = "crossover", not(feature = "rayon")))]
fn main() {
let mut rng = rand::thread_rng();

let mut sim = GeneticSim::new(
Vec::gen_random(&mut rng, 100),
fitness,
#[cfg(feature = "crossover")]
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);
}
}

#[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();
}
#[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();

fits.sort_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());
#[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);

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: {}", serialized);
println!("{}", serialized);
}
}
10 changes: 5 additions & 5 deletions src/topology/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivationFn> {
self.fns.values().cloned().collect()
}
Expand All @@ -77,7 +77,7 @@ 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()
}
}
Expand All @@ -101,7 +101,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;
Expand All @@ -112,8 +112,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;
}
}

Expand Down

0 comments on commit 228f7af

Please sign in to comment.