Skip to content

Commit

Permalink
Generic rewrite
Browse files Browse the repository at this point in the history
- Genome is a trait that implements mutation, mating, distance measure etc.
        (opens up for more experimentation and extensibility)
- The default Genome implementor is NeuralNetwork which implements said functionality
        pretty much just as before. So NeuralNetwork is the new Genome.
        Some slight changes in how bias works had to be done to be able to mirror the way
        they do it in some paper that introduced or explained CTRNN iirc.
- Thus now we pass around Organisms not Genomes

- And..:
    - Update rand
    - Removed lots of unnecessary f64
    - Remove CtrnnNeuralNetwork (can just use Ctrnn for setting up the
        network)
    - Fix an error in species
  • Loading branch information
Ploppz committed Feb 1, 2020
1 parent 287d8bc commit 9cf2576
Show file tree
Hide file tree
Showing 17 changed files with 820 additions and 962 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@

# Generated by Cargo
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

# Vim
*.swp
*.swo
rusty-tags.vi

# Fmt
**/*.rs.bk
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ license = "MIT"
name = "rustneat"
repository = "https://github.com/TLmaK0/rustneat"
version = "0.3.0"
edition = "2018"

[dependencies]
conv = "0.3.2"
crossbeam = "0.2"
lazy_static = "0.2.2"
num_cpus = "1.0"
rand = "0.4"
rand = "0.6"
rulinalg = "0.3.4"

rusty_dashed = { version = "0.2.1", optional = true }
Expand Down
18 changes: 8 additions & 10 deletions examples/function_approximation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ extern crate rusty_dashed;
#[cfg(feature = "telemetry")]
mod telemetry_helper;

use rustneat::Environment;
use rustneat::Organism;
use rustneat::Population;
use rustneat::{Environment, Organism, Population, NeuralNetwork};

static mut BEST_FITNESS: f64 = 0.0;
struct FunctionApproximation;

impl Environment for FunctionApproximation {
fn test(&self, organism: &mut Organism) -> f64 {
impl Environment<NeuralNetwork> for FunctionApproximation {
fn test(&self, organism: &mut NeuralNetwork) -> f64 {
let mut output = vec![0f64];
let mut distance = 0f64;

Expand All @@ -28,22 +26,22 @@ impl Environment for FunctionApproximation {
outputs.push([x, (output[0] * 100f64) as i64]);
}

let fitness = 100f64 / (1f64 + (distance / 1000.0));
unsafe {
if organism.fitness > BEST_FITNESS {
BEST_FITNESS = organism.fitness;
if fitness > BEST_FITNESS {
BEST_FITNESS = fitness;
#[cfg(feature = "telemetry")]
telemetry!("approximation1", 1.0, format!("{:?}", outputs));
}
}

100f64 / (1f64 + (distance / 1000.0))
fitness
}
}

fn main() {
let mut population = Population::create_population(150);
let mut environment = FunctionApproximation;
let mut champion: Option<Organism> = None;
let mut champion: Option<Organism<NeuralNetwork>> = None;

#[cfg(feature = "telemetry")]
telemetry_helper::enable_telemetry("?max_fitness=100&ioNeurons=1,2", true);
Expand Down
23 changes: 15 additions & 8 deletions examples/simple_sample.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
extern crate rand;
extern crate rustneat;

use rustneat::Environment;
use rustneat::Organism;
use rustneat::Population;
use rustneat::{Environment, Organism, Population, NeuralNetwork};

#[cfg(feature = "telemetry")]
mod telemetry_helper;

struct XORClassification;

impl Environment for XORClassification {
fn test(&self, organism: &mut Organism) -> f64 {
impl Environment<NeuralNetwork> for XORClassification {
fn test(&self, organism: &mut NeuralNetwork) -> f64 {
let mut output = vec![0f64];
let mut distance: f64;
organism.activate(vec![0f64, 0f64], &mut output);
Expand All @@ -23,7 +21,7 @@ impl Environment for XORClassification {
organism.activate(vec![1f64, 1f64], &mut output);
distance += (0f64 - output[0]).powi(2);

let fitness = 16f64 / (1f64 + distance);
let fitness = 16.0 / (1.0 + distance);

fitness
}
Expand All @@ -38,15 +36,24 @@ fn main() {

let mut population = Population::create_population(150);
let mut environment = XORClassification;
let mut champion: Option<Organism> = None;
let mut champion: Option<Organism<NeuralNetwork>> = None;
let mut i = 0;
while champion.is_none() {
i += 1;
population.evolve();
population.evaluate_in(&mut environment);
let mut best_fitness = 0.0;
for organism in &population.get_organisms() {
if organism.fitness > 15.5f64 {
if organism.fitness > best_fitness {
best_fitness = organism.fitness;
}
if organism.fitness > 15.5 {
champion = Some(organism.clone());
}
}
if i % 10 == 0 {
println!("Gen {}: {}", i, best_fitness);
}
}
println!("{:?}", champion.unwrap().genome);
}
142 changes: 0 additions & 142 deletions src/ctrnn.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use organism::Organism;
use crate::Genome;

/// A trait that is implemented by user to allow test of the Environment.
pub trait Environment: Sync {
/// A trait that is implemented by user to test the fitness of organisms.
pub trait Environment<G: Genome>: Sync {
/// This test will return the value required by this enviroment to test
/// against
fn test(&self, organism: &mut Organism) -> f64;
fn test(&self, organism: &mut G) -> f64;
}
Loading

0 comments on commit 9cf2576

Please sign in to comment.