From 6f96a6f04892c178bfc5a462c79a4371c183750c Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+inflectrix@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:28:47 +0000 Subject: [PATCH] split into modules --- .gitignore | 3 +- examples/basic.rs | 5 ++ src/lib.rs | 141 ++-------------------------------------------- src/runnable.rs | 7 +++ src/topology.rs | 129 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 137 deletions(-) create mode 100644 examples/basic.rs create mode 100644 src/runnable.rs create mode 100644 src/topology.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..1b71596 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target +/target/ +/.vscode/ \ No newline at end of file diff --git a/examples/basic.rs b/examples/basic.rs new file mode 100644 index 0000000..830c7d8 --- /dev/null +++ b/examples/basic.rs @@ -0,0 +1,5 @@ +use neat::*; + +fn main() { + +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index acc3ff4..cb0e524 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,137 +1,6 @@ -pub use genetic_rs::prelude::*; -use rand::prelude::*; - -#[derive(Debug, Clone)] -pub struct NeuralNetworkTopology { - input_layer: Vec, - hidden_layer: Vec, - output_layer: Vec, - pub mutation_rate: f32, -} - -impl NeuralNetworkTopology { - pub fn new(inputs: usize, outputs: usize, mutation_rate: f32, rng: &mut impl Rng) -> Self { - let mut input_layer = Vec::with_capacity(inputs); - - for _ in 0..inputs { - input_layer.push(NeuronTopology::new(vec![], rng)); - } - - let mut output_layer = Vec::with_capacity(outputs); - let input_locs: Vec<_> = input_layer - .iter() - .enumerate() - .map(|(i, _n)| NeuronLocation::Input(i)) - .collect(); - - for _ in 0..outputs { - let mut already_chosen = Vec::new(); - - // random number of connections to random input neurons. - let input = (0..rng.gen_range(0..inputs)) - .map(|_| { - let mut i = rng.gen_range(0..inputs); - while already_chosen.contains(&i) { - i = rng.gen_range(0..inputs); - } - - already_chosen.push(i); - - input_locs[i] - }) - .collect(); - - output_layer.push(NeuronTopology::new(input, rng)); - } - - Self { - input_layer, - hidden_layer: vec![], - output_layer, - mutation_rate, - } - } -} - -impl RandomlyMutable for NeuralNetworkTopology { - fn mutate(&mut self, rate: f32, rng: &mut impl rand::Rng) { - todo!(); - } -} - -#[cfg(not(feature = "crossover"))] -impl DivisionReproduction for NeuralNetworkTopology { - fn spawn_child(&self, rng: &mut impl rand::Rng) -> Self { - let mut child = self.clone(); - child.mutate(self.mutation_rate, rng); - child - } -} +pub mod topology; +pub mod runnable; -impl Prunable for NeuralNetworkTopology {} - -#[derive(Debug, Clone)] -pub struct NeuronTopology { - inputs: Vec<(NeuronLocation, f32)>, - bias: f32, -} - -impl NeuronTopology { - pub fn new(inputs: Vec, rng: &mut impl Rng) -> Self { - let inputs = inputs - .into_iter() - .map(|i| (i, rng.gen::())) - .collect(); - - Self { - inputs, - bias: rng.gen(), - } - } -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum NeuronLocation { - Input(usize), - Hidden(usize), - Output(usize), -} - -impl NeuronLocation { - pub fn is_input(&self) -> bool { - match self { - Self::Input(_) => true, - _ => false, - } - } - - pub fn is_hidden(&self) -> bool { - match self { - Self::Hidden(_) => true, - _ => false, - } - } - - pub fn is_output(&self) -> bool { - match self { - Self::Output(_) => true, - _ => false, - } - } - - pub fn unwrap(&self) -> usize { - match self { - Self::Input(i) => *i, - Self::Hidden(i) => *i, - Self::Output(i) => *i, - } - } -} - -pub struct NeuralNetwork { - -} - -pub struct Neuron { - -} \ No newline at end of file +pub use genetic_rs::prelude::*; +pub use topology::*; +pub use runnable::*; \ No newline at end of file diff --git a/src/runnable.rs b/src/runnable.rs new file mode 100644 index 0000000..be27fb4 --- /dev/null +++ b/src/runnable.rs @@ -0,0 +1,7 @@ +pub struct NeuralNetwork { + +} + +pub struct Neuron { + +} \ No newline at end of file diff --git a/src/topology.rs b/src/topology.rs new file mode 100644 index 0000000..656dc37 --- /dev/null +++ b/src/topology.rs @@ -0,0 +1,129 @@ +use genetic_rs::prelude::*; +use rand::prelude::*; + +#[derive(Debug, Clone)] +pub struct NeuralNetworkTopology { + pub input_layer: Vec, + pub hidden_layer: Vec, + pub output_layer: Vec, + pub mutation_rate: f32, +} + +impl NeuralNetworkTopology { + pub fn new(inputs: usize, outputs: usize, mutation_rate: f32, rng: &mut impl Rng) -> Self { + let mut input_layer = Vec::with_capacity(inputs); + + for _ in 0..inputs { + input_layer.push(NeuronTopology::new(vec![], rng)); + } + + let mut output_layer = Vec::with_capacity(outputs); + let input_locs: Vec<_> = input_layer + .iter() + .enumerate() + .map(|(i, _n)| NeuronLocation::Input(i)) + .collect(); + + for _ in 0..outputs { + let mut already_chosen = Vec::new(); + + // random number of connections to random input neurons. + let input = (0..rng.gen_range(0..inputs)) + .map(|_| { + let mut i = rng.gen_range(0..inputs); + while already_chosen.contains(&i) { + i = rng.gen_range(0..inputs); + } + + already_chosen.push(i); + + input_locs[i] + }) + .collect(); + + output_layer.push(NeuronTopology::new(input, rng)); + } + + Self { + input_layer, + hidden_layer: vec![], + output_layer, + mutation_rate, + } + } +} + +impl RandomlyMutable for NeuralNetworkTopology { + fn mutate(&mut self, rate: f32, rng: &mut impl rand::Rng) { + todo!(); + } +} + +#[cfg(not(feature = "crossover"))] +impl DivisionReproduction for NeuralNetworkTopology { + fn spawn_child(&self, rng: &mut impl rand::Rng) -> Self { + let mut child = self.clone(); + child.mutate(self.mutation_rate, rng); + child + } +} + +impl Prunable for NeuralNetworkTopology {} + +#[derive(Debug, Clone)] +pub struct NeuronTopology { + inputs: Vec<(NeuronLocation, f32)>, + bias: f32, +} + +impl NeuronTopology { + pub fn new(inputs: Vec, rng: &mut impl Rng) -> Self { + let inputs = inputs + .into_iter() + .map(|i| (i, rng.gen::())) + .collect(); + + Self { + inputs, + bias: rng.gen(), + } + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum NeuronLocation { + Input(usize), + Hidden(usize), + Output(usize), +} + +impl NeuronLocation { + pub fn is_input(&self) -> bool { + match self { + Self::Input(_) => true, + _ => false, + } + } + + pub fn is_hidden(&self) -> bool { + match self { + Self::Hidden(_) => true, + _ => false, + } + } + + pub fn is_output(&self) -> bool { + match self { + Self::Output(_) => true, + _ => false, + } + } + + pub fn unwrap(&self) -> usize { + match self { + Self::Input(i) => *i, + Self::Hidden(i) => *i, + Self::Output(i) => *i, + } + } +} \ No newline at end of file