Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing generics #44

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ before_script:
- |
(which cargo-install-update && cargo install-update cargo-update) || cargo install cargo-update &&
(which cargo-prune && cargo install-update cargo-prune) || cargo install cargo-prune
- rustup component add rustfmt-preview
- rustup component add rustfmt
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then export OPENSSL_INCLUDE_DIR=`brew --prefix openssl`/include; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then export OPENSSL_LIB_DIR=`brew --prefix openssl`/lib; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then export DEP_OPENSSL_INCLUDE=`brew --prefix openssl`/include; fi
script:
- if [ "${TRAVIS_RUST_VERSION}" = stable ]; then
(
set -x;
cargo fmt -- --write-mode=diff
cargo fmt
);
fi
- cargo test --verbose --release
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ keywords = ["neuroevolution", "neat", "aumenting-topologies", "genetic", "algori
license = "MIT"
name = "rustneat"
repository = "https://github.com/TLmaK0/rustneat"
version = "0.2.1"
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 All @@ -38,3 +39,6 @@ required-features = ["cpython"]

[[example]]
name = "simple_sample"

[[example]]
name = "function_approximation"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Working-in-progress

Implementation of **NeuroEvolution of Augmenting Topologies NEAT** http://nn.cs.utexas.edu/downloads/papers/stanley.ec02.pdf

This implementations uses a **Continuous-Time Recurrent Neural Network** (**CTRNN**) (Yamauchi and Beer, 1994).
This implementations uses a **CTRNN** based on**On the Dynamics of Small Continuous-Time Recurrent Neural Network** (Beer, 1995) http://www.cs.uvm.edu/~jbongard/2014_CS206/Beer_CTRNNs.pdf

![telemetry](docs/img/rustneat.png)

Expand Down
72 changes: 72 additions & 0 deletions examples/function_approximation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
extern crate rand;
extern crate rustneat;

#[cfg(feature = "telemetry")]
#[macro_use]
extern crate rusty_dashed;

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

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

static mut BEST_FITNESS: f64 = 0.0;
struct FunctionApproximation;

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

let mut outputs = Vec::new();

for x in -10..11 {
organism.activate(vec![x as f64 / 10f64], &mut output);
distance += ((x as f64).powf(2f64) - (output[0] * 100f64)).abs();
outputs.push([x, (output[0] * 100f64) as i64]);
}

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

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

#[cfg(feature = "telemetry")]
telemetry_helper::enable_telemetry("?max_fitness=100&ioNeurons=1,2", true);

#[cfg(feature = "telemetry")]
std::thread::sleep(std::time::Duration::from_millis(2000));

#[cfg(feature = "telemetry")]
telemetry!(
"approximation1",
1.0,
format!("{:?}", (-10..11).map(|x| [x, x * x]).collect::<Vec<_>>())
);

#[cfg(feature = "telemetry")]
std::thread::sleep(std::time::Duration::from_millis(2000));

while champion.is_none() {
population.evolve();
population.evaluate_in(&mut environment);
for organism in &population.get_organisms() {
if organism.fitness >= 96f64 {
champion = Some(organism.clone());
}
}
}
println!("{:?}", champion.unwrap().genome);
}
44 changes: 27 additions & 17 deletions examples/simple_sample.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
extern crate rand;
extern crate rustneat;

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

#[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);
distance = (0f64 - output[0]).abs();
organism.activate(&vec![0f64, 1f64], &mut output);
distance += (1f64 - output[0]).abs();
organism.activate(&vec![1f64, 0f64], &mut output);
distance += (1f64 - output[0]).abs();
organism.activate(&vec![1f64, 1f64], &mut output);
distance += (0f64 - output[0]).abs();
organism.activate(vec![0f64, 0f64], &mut output);
distance = (0f64 - output[0]).powi(2);
organism.activate(vec![0f64, 1f64], &mut output);
distance += (1f64 - output[0]).powi(2);
organism.activate(vec![1f64, 0f64], &mut output);
distance += (1f64 - output[0]).powi(2);
organism.activate(vec![1f64, 1f64], &mut output);
distance += (0f64 - output[0]).powi(2);

let fitness = (4f64 - distance).powi(2);
let fitness = 16.0 / (1.0 + distance);

fitness
}
}

fn main() {
#[cfg(feature = "telemetry")]
telemetry_helper::enable_telemetry("?max_fitness=17");
telemetry_helper::enable_telemetry("?max_fitness=16", true);

#[cfg(feature = "telemetry")]
std::thread::sleep(std::time::Duration::from_millis(2000));

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.9f64 {
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);
}
10 changes: 8 additions & 2 deletions examples/telemetry_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ extern crate rusty_dashed;
use self::rusty_dashed::Dashboard;

#[allow(dead_code)]
pub fn main(){}
pub fn main() {}

#[cfg(feature = "telemetry")]
pub fn enable_telemetry(query_string: &str) {
pub fn enable_telemetry(query_string: &str, open: bool) {
let mut dashboard = Dashboard::new();
dashboard.add_graph("fitness1", "fitness", 0, 0, 4, 4);
dashboard.add_graph("network1", "network", 4, 0, 4, 4);
dashboard.add_graph("approximation1", "approximation", 0, 4, 2, 2);

rusty_dashed::Server::serve_dashboard(dashboard);

let url = format!("http://localhost:3000{}", query_string);

if !open {
return;
}

match open::that(url.clone()) {
Err(_) => println!(
"\nOpen browser and go to {:?} to see how neural network evolves\n",
Expand Down
8 changes: 8 additions & 0 deletions graphs/approximation.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#approximation1 svg {
width: 100%;
height: 100%;
}

#approximation1 path {
fill: none;
}
40 changes: 40 additions & 0 deletions graphs/approximation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var svg, paths = [];

var lineChart;

function approximation_init(id){
svg = d3.select('#' + id).append('svg');
var width = $('#' + id).children().width() / 2;
var rangeX = [-width, width];
var height = $('#' + id).children().height();
var rangeY = [0, height];

for(var n=0; n<10; n++ ) {
paths[n] = svg.append('path');
paths[n].attr("transform", "translate(" + width + ")");
paths[n].attr("stroke", "rgb(" + (50 + n * 20) + ",0,0)");
}
paths[0].attr("stroke", "rgb(200,200,200)");
scaleX = d3.scaleLinear().domain([-10, 10]).range(rangeX);
scaleY = d3.scaleLinear().domain([0, 100]).range(rangeY);

lineChart = d3.line()
.x(function(d){ return scaleX(d[0]); })
.y(function(d){ return height - scaleY(d[1]); });
}

var functionToApproximate = [];
var functionApproximations = [];

function approximation(id, value){
if (functionToApproximate.length == 0) functionToApproximate = value;
if (functionApproximations.length == 9) functionApproximations.shift();

functionApproximations.push(value);

paths[0].attr("d", lineChart(functionToApproximate));

for(var n=0; n < functionApproximations.length; n++ ){
paths[n + 1].attr("d", lineChart(functionApproximations[n]));
}
}
4 changes: 2 additions & 2 deletions graphs/fitness.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
svg {
#fitness1 svg {
width: 100%;
height: 100%;
}

svg path {
#fitness1 svg path {
stroke: black;
fill: none;
}
17 changes: 16 additions & 1 deletion graphs/network.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var ioNeurons = (getParameterByName('ioNeurons') || '0,0').split(',');
console.log(ioNeurons);

var simulation;
var color;
var link;
Expand Down Expand Up @@ -45,7 +48,10 @@ var allLinks = {};

function addIfNewNode(nodes, newNodes, node_id){
if (nodes.indexOf(node_id) < 0) {
newNodes.push({id: "node" + node_id, group: 0});
var group = 0;
if (node_id < ioNeurons[0]) group = 3;
else if (node_id < ioNeurons[1]) group = 7;
newNodes.push({id: "node" + node_id, group: group});
nodes.push(node_id);
}
}
Expand Down Expand Up @@ -210,3 +216,12 @@ function network(id, genes){
}
}

function getParameterByName(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
Loading