Skip to content

Commit

Permalink
split into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperCodec committed Feb 7, 2024
1 parent c0ea416 commit 6f96a6f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 137 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/target/
/.vscode/
5 changes: 5 additions & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use neat::*;

fn main() {

}
141 changes: 5 additions & 136 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,137 +1,6 @@
pub use genetic_rs::prelude::*;
use rand::prelude::*;

#[derive(Debug, Clone)]
pub struct NeuralNetworkTopology {
input_layer: Vec<NeuronTopology>,
hidden_layer: Vec<NeuronTopology>,
output_layer: Vec<NeuronTopology>,
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<NeuronLocation>, rng: &mut impl Rng) -> Self {
let inputs = inputs
.into_iter()
.map(|i| (i, rng.gen::<f32>()))
.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 {

}
pub use genetic_rs::prelude::*;
pub use topology::*;
pub use runnable::*;
7 changes: 7 additions & 0 deletions src/runnable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub struct NeuralNetwork {

}

pub struct Neuron {

}
129 changes: 129 additions & 0 deletions src/topology.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use genetic_rs::prelude::*;
use rand::prelude::*;

#[derive(Debug, Clone)]
pub struct NeuralNetworkTopology {
pub input_layer: Vec<NeuronTopology>,
pub hidden_layer: Vec<NeuronTopology>,
pub output_layer: Vec<NeuronTopology>,
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<NeuronLocation>, rng: &mut impl Rng) -> Self {
let inputs = inputs
.into_iter()
.map(|i| (i, rng.gen::<f32>()))
.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,
}
}
}

0 comments on commit 6f96a6f

Please sign in to comment.