Skip to content

Commit

Permalink
Merge pull request #3 from inflectrix/dev
Browse files Browse the repository at this point in the history
NEAT 0.2.0
  • Loading branch information
HyperCodec authored Feb 22, 2024
2 parents a52f3f3 + e7f3eca commit 4f1a227
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 45 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: CI-CD

on: [push, pull_request]
on:
push:
branches: [main]
pull_request:

jobs:
test:
Expand Down
82 changes: 79 additions & 3 deletions Cargo.lock

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

16 changes: 13 additions & 3 deletions 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.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Inflectrix"]
repository = "https://github.com/inflectrix/neat"
Expand All @@ -11,16 +11,26 @@ keywords = ["genetic", "machine-learning", "ai", "algorithm", "evolution"]
categories = ["algorithms", "science", "simulation"]
license = "MIT"

[package.metadata.docs.rs]
features = ["serde"]
rustdoc-args = ["--cfg", "docsrs"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["max-index"]
#crossover = ["genetic-rs/crossover"]
rayon = ["genetic-rs/rayon", "dep:rayon"]
max-index = []
serde = ["dep:serde", "dep:serde-big-array"]


[dependencies]
genetic-rs = "0.2.1"
genetic-rs = "0.3"
rand = "0.8.5"
rayon = { version = "1.8.1", optional = true }
rayon = { version = "1.8.1", optional = true }
serde = { version = "1.0.197", features = ["derive"], optional = true }
serde-big-array = { version = "0.5.1", optional = true }

[dev-dependencies]
bincode = "1.3.3"
6 changes: 3 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl RandomlyMutable for AgentDNA {
impl Prunable for AgentDNA {}

impl DivisionReproduction for AgentDNA {
fn spawn_child(&self, rng: &mut impl Rng) -> Self {
fn divide(&self, rng: &mut impl Rng) -> Self {
let mut child = self.clone();
child.mutate(self.network.mutation_rate, rng);
child
Expand Down Expand Up @@ -112,7 +112,7 @@ fn main() {
sim.next_generation();
}

let fits: Vec<_> = sim.entities.iter().map(fitness).collect();
let fits: Vec<_> = sim.genomes.iter().map(fitness).collect();

let maxfit = fits
.iter()
Expand All @@ -130,7 +130,7 @@ fn main() {
sim.next_generation();
}

let fits: Vec<_> = sim.entities.iter().map(fitness).collect();
let fits: Vec<_> = sim.genomes.iter().map(fitness).collect();

let maxfit = fits
.iter()
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
//! ### Feature Roadmap:
//! - [x] base (single-core) crate
//! - [x] rayon
//! - [x] serde
//! - [ ] crossover
//!
//! You can get started by looking at [genetic-rs docs](https://docs.rs/genetic-rs) and checking the examples for this crate.
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]

/// A module containing the [`NeuralNetworkTopology`] struct. This is what you want to use in the DNA of your agent, as it is the thing that goes through nextgens and suppors mutation.
pub mod topology;
Expand All @@ -18,3 +20,6 @@ pub mod runnable;
pub use genetic_rs::prelude::*;
pub use runnable::*;
pub use topology::*;

#[cfg(feature = "serde")]
pub use nnt_serde::*;
16 changes: 10 additions & 6 deletions src/runnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rayon::prelude::*;
#[cfg(feature = "rayon")]
use std::sync::{Arc, RwLock};

/// A runnable, stated Neural Network generated from a [NeuralNetworkToplogy]. Use [`NeuralNetwork::from`] to go from stateles to runnable.
/// A runnable, stated Neural Network generated from a [NeuralNetworkTopology]. Use [`NeuralNetwork::from`] to go from stateles to runnable.
/// Because this has state, you need to run [`NeuralNetwork::flush_state`] between [`NeuralNetwork::predict`] calls.
#[derive(Debug)]
#[cfg(not(feature = "rayon"))]
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<const I: usize, const O: usize> NeuralNetwork<I, O> {
n.state.value += self.process_neuron(l) * w;
}

n.sigmoid();
n.activate();

n.state.value
}
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<const I: usize, const O: usize> NeuralNetwork<I, O> {

let mut nw = n.write().unwrap();
nw.state.value += val;
nw.sigmoid();
nw.activate();

nw.state.value
}
Expand Down Expand Up @@ -240,6 +240,9 @@ pub struct Neuron {

/// The current state of the neuron.
pub state: NeuronState,

/// The neuron's activation function
pub activation: ActivationFn,
}

impl Neuron {
Expand All @@ -248,9 +251,9 @@ impl Neuron {
self.state.value = self.bias;
}

/// Applies the sigoid activation function to the state's current value.
pub fn sigmoid(&mut self) {
self.state.value = 1. / (1. + std::f32::consts::E.powf(-self.state.value))
/// Applies the activation function to the neuron
pub fn activate(&mut self) {
self.state.value = (self.activation.func)(self.state.value);
}
}

Expand All @@ -263,6 +266,7 @@ impl From<&NeuronTopology> for Neuron {
value: value.bias,
..Default::default()
},
activation: value.activation.clone(),
}
}
}
Expand Down
Loading

0 comments on commit 4f1a227

Please sign in to comment.