Git Product home page Git Product logo

rustneat's Introduction

Working-in-progress

Rust NEAT

travis-ci Gitter

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

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

telemetry

Install Rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh source $HOME/.cargo/env

Run test

To speed up tests, run them with --release (XOR classification/simple_sample should take less than a minute)

cargo test --release

Run example

cargo run --release --example simple_sample --features=telemetry

then go to http://localhost:3000 to see how neural network evolves

telemetry

Run openai tests

telemetry

Install python dependencies

sudo apt install python3
sudo apt install python3-pip
sudo apt install libpython3.8-dev
sudo apt-get remove swig
sudo apt-get install swig3.0
sudo ln -s /usr/bin/swig3.0 /usr/bin/swig
sudo pip3 install gym
sudo pip3 install gym[box2d]
sudo apt install python3-opengl

Run test

cargo run --release --example openai --features=openai,telemetry

Windows Openai

openai/gym#11 (comment)

Update to the latest version of Windows (>version 1607, "Anniversary Update")
Enable Windows Subsystem for Linux (WSL)
Open cmd, run bash
Install python & gym (using sudo, and NOT PIP to install gym). So by now you should probably be able to run things and get really nasty graphics related errors. This is because WSL doesn't support any displays, so we need to fake it.
Install vcXsrv, and run it (you should just have a little tray icon)
In bash run "export DISPLAY=:0" Now when you run it you should get a display to pop-up, there may be issues related to graphics drivers. Sadly, this is where the instructions diverge if you don't have an NVIDIA graphics card.
Get the drivers: "sudo apt-get install nvidia-319 nvidia-settings-319 nvidia-prime"
Run!

Sample usage

Create a new cargo project:

Add rustneat to Cargo.toml

[dependencies]
rustneat = "0.2.1"

Then use the library i.e. to implement the above example, use the library as follows:

extern crate rustneat;
use rustneat::Environment;
use rustneat::Organism;
use rustneat::Population;

struct XORClassification;

impl Environment for XORClassification {
    fn test(&self, organism: &mut Organism) -> 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();
        (4f64 - distance).powi(2)
    }
}

fn main() {
    let mut population = Population::create_population(150);
    let mut environment = XORClassification;
    let mut champion: Option<Organism> = None;
    while champion.is_none() {
        population.evolve();
        population.evaluate_in(&mut environment);
        for organism in &population.get_organisms() {
            if organism.fitness > 15.9f64 {
                champion = Some(organism.clone());
            }
        }
    }
    println!("{:?}", champion.unwrap().genome);
}

Develop

Check style guidelines with:

rustup component add rustfmt-preview cargo fmt

Thanks

Thanks for the icon nerves by Delwar Hossain from the Noun Project

References:

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

On the Dynamics of Small Continuous-Time Recurrent Neural Network (Beer, 1995) http://www.cs.uvm.edu/~jbongard/2014_CS206/Beer_CTRNNs.pdf

An Investigation into the Dynamics of a Continuous Time Recurrent Neural Network Node http://www.tinyblueplanet.com/easy/FCSReport.pdf

http://indy9000.github.io/posts/analysis-of-a-simple-ctrnn.html http://indy9000.github.io/posts/analysis-of-a-simple-ctrnn.html

rustneat's People

Contributors

dirvine avatar tlmak0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rustneat's Issues

Panics with multiple outputs

Hello! I'm using this library to try to make bot for a game as a learning experience but struggle with generating multiple outputs. Might be something that I do wrong but the following example code panics:

impl Environment for XORClassification {
    fn test(&self, organism: &mut Organism) -> f64 {
        let mut output = vec![0f64, 0f64]; // Doesn't panic with vec![0f64]

        let mut distance: f64;

        organism.activate(&vec![0f64, 0f64], &mut output);
        distance = (0f64 - output[0]).abs(); // [0, 0] should generate output 0
        organism.activate(&vec![0f64, 1f64], &mut output);
        distance += (1f64 - output[0]).abs(); // [0, 1] should generate output 1
        organism.activate(&vec![1f64, 0f64], &mut output);
        distance += (1f64 - output[0]).abs(); // [1, 0] should generate output 1
        organism.activate(&vec![1f64, 1f64], &mut output);
        distance += (0f64 - output[0]).abs(); // [1, 1] should generate output 0

        (4f64 - distance).powi(2)
    }
}

Error:

thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', C:\projects\rust\src\libcore\slice\mod.rs:865:10
note: Run with `RUST_BACKTRACE=1` for a backtrace

While this example generates outputs as expected:

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

        let mut distance: f64;

        organism.activate(&vec![0f64, 0f64], &mut output);
        distance = (0f64 - output[0]).abs(); // [0, 0] should generate output 0
        organism.activate(&vec![0f64, 1f64], &mut output);
        distance += (1f64 - output[0]).abs(); // [0, 1] should generate output 1
        organism.activate(&vec![1f64, 0f64], &mut output);
        distance += (1f64 - output[0]).abs(); // [1, 0] should generate output 1
        organism.activate(&vec![1f64, 1f64], &mut output);
        distance += (0f64 - output[0]).abs(); // [1, 1] should generate output 0

        (4f64 - distance).powi(2)
    }
}

Why is this?
Also, great library otherwise, will see if I can solve it and close this issue if it's some blunder on my part which is highly likely.

Here's the backtrace:

thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', C:\projects\rust\src\libcore\slice\mod.rs:865:10
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:

  ...

   9: core::panicking::panic_bounds_check
             at C:\projects\rust\src\libcore\panicking.rs:58
  10: core::slice::{{impl}}::index<f64>
             at C:\projects\rust\src\libcore\slice\mod.rs:865
  11: core::slice::{{impl}}::index<f64,usize>
             at C:\projects\rust\src\libcore\slice\mod.rs:767
  12: alloc::vec::{{impl}}::index<f64,usize>
             at C:\projects\rust\src\liballoc\vec.rs:1706
  13: rustneat::organism::Organism::activate
             at E:\prog\rust\rustneat\src\organism.rs:57
  14: neatctrl::{{impl}}::test
             at .\src\main.rs:50
  15: rustneat::species_evaluator::{{impl}}::evaluate_organisms::{{closure}}
             at E:\prog\rust\rustneat\src\species_evaluator.rs:93
  16: crossbeam::scoped::{{impl}}::spawn::{{closure}}<closure,()>
             at C:\Users\Henrik\.cargo\registry\src\github.com-1ecc6299db9ec823\crossbeam-0.2.12\src\scoped.rs:237
  17: crossbeam::{{impl}}::call_box<closure>
             at C:\Users\Henrik\.cargo\registry\src\github.com-1ecc6299db9ec823\crossbeam-0.2.12\src\lib.rs:44
  18: crossbeam::spawn_unsafe::{{closure}}<closure>
             at C:\Users\Henrik\.cargo\registry\src\github.com-1ecc6299db9ec823\crossbeam-0.2.12\src\lib.rs:53

EDIT:
I think I solved it! (EDIT: Nvm I didn't) By changing

rustneat/src/organism.rs

Lines 54 to 59 in 18b4fbf

if sensors.len() < neurons_len {
let outputs_activations = activations.split_at(sensors.len()).1.to_vec();
for n in 0..outputs.len() {
outputs[n] = outputs_activations[n];
}
}

to

        if sensors.len() < neurons_len {
            let outputs_activations = activations.split_at(sensors.len()-1).1.to_vec();
            for n in 0..outputs.len()-1 {
                outputs[n] = outputs_activations[n];
            }
        }

I managed to get the XOR example with multiple outputs to work and generate a fitness > 15.9
EDIT: This comes with a lot of issues however, first, all other outputs expect output[0] remains zero and doesn't evolve and it doesn't work with single outputs anymore. But it did solve the panic, so I atleast came one step closer to solve it :) ๐Ÿ‘

EDIT:
Fixed it! With:
https://github.com/henke1010/rustneat/blob/c813b555a714c5b85b7c84fc5ee103e1d60a8ae3/src/organism.rs#L54-L63

Made a pull request

Sample code crashes

~/rustneat$ cargo run --release --example simple_sample --features=telemetry
    Finished release [optimized] target(s) in 0.0 secs
     Running `target/release/examples/simple_sample`

Go to http://localhost:3000 to see how neural network evolves

thread '<unnamed>' panicked at 'not yet implemented', /home/pk/.cargo/registry/src/github.com-1ecc6299db9ec823/rusty_dashed-0.1.2/src/server.rs:46:17
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Could not find `environment` in `rustneat`

The sample code in readme.md fails for me with this error:

error[E0432]: unresolved import rustneat::Environment
--> src\main.rs:3:5
|
3 | use rustneat::Environment;
| ^^^^^^^^^^^^^^^^^^^^^ no Environment in the root

error[E0432]: unresolved import rustneat::Organism
--> src\main.rs:4:5
|
4 | use rustneat::Organism;
| ^^^^^^^^^^^^^^^^^^ no Organism in the root

error[E0432]: unresolved import rustneat::Population
--> src\main.rs:5:5
|
5 | use rustneat::Population;
| ^^^^^^^^^^^^^^^^^^^^ no Population in the root

error: cannot continue compilation due to previous error

I haven't programmed in rust for some time so I hope it's not some easy noob misstake on my part but I've tried to fix it for several hours now with no progress.

The things I've done is this:

  1. Created a new directory
  2. cargo init --bin
  3. changed cargo.toml to
    [dependencies]
    rustneat = "0.1.8"
  4. Copied sample code from README.md exactly as it is to main.rs and compiled.
  5. Generated above error

Some concerns about our neural networks and Ctrnn

Organism and Gene

  • In mutation_add_connection, there is no check whether the connection already exists. In Organism::get_weights, we only use the weight of one connection, so any other similar connections are useless.
  • Bias can only be positive. I'm not sure if this is good? I also noticed in Ctrnn that we take the negative bias, rather than the positive as in the paper. So practically we always have a negative bias.
  • What about having both bias: f64 and weight: f64 in Gene (instead of having a bias and a weight type of gene)?

Ctrnn

  • Shouldn't tau be bigger than 1.0? I think it sort of represents how fast y decays. And when it is 1.0, y decays in only one timestep. (you can verify this by looking at how we calculate the new y (however look at my commit where I fixed a small thing in Ctrnn)

panic in Specie::generate_offspring()

After running it on my problem for like an hour (with no improvement), I got this panic:

thread 'main' panicked at 'Range::new called with `low >= high`', C:\Users\me\.c
argo\registry\src\github.com-1ecc6299db9ec823\rand-0.3.18\src\distributions\rang
e.rs:60:8
stack backtrace:
   0: std::sys_common::backtrace::_print
             at C:\projects\rust\src\libstd\sys_common\backtrace.rs:92
   1: std::panicking::default_hook::{{closure}}
             at C:\projects\rust\src\libstd\panicking.rs:380
   2: std::panicking::default_hook
             at C:\projects\rust\src\libstd\panicking.rs:397
   3: std::panicking::rust_panic_with_hook
             at C:\projects\rust\src\libstd\panicking.rs:577
   4: <rand::ThreadRng as rand::Rng>::next_u64
   5: rustneat::specie::Specie::generate_offspring
   6: rustneat::population::Population::evolve
   7: ZIG_NORM_X
   8: ZIG_NORM_X
   9: panic_unwind::__rust_maybe_catch_panic
             at C:\projects\rust\src\libpanic_unwind\lib.rs:99
  10: std::rt::lang_start
             at C:\projects\rust\src\libstd\rt.rs:52
  11: __scrt_common_main_seh
             at f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:253
  12: BaseThreadInitThunk

How to evolve based on head to head battles?

If I want to evolve organisms that compete in head to head 2 player contests (like tic tac toe), is there any guidence on how to set this up. An example would be great. I note that the test function for the Environment trait has a parameter for only a single organism. Is there a convenient way to set up 2 player games?

Starting with a single neuron, no useful evolution can happen for many generations

Both in the XOR example and in my own attempts, I noticed something: The first 100-200 generations, the output of organism.activate is 0.0. So we are essentially waiting for any connection between input and output, while no evolution other than random mutation can happen because fitness will be constant for the organisms that only output 0.0.

So I suggest either starting from a slightly more connected starting point, or finding a way to make the alg more 'eager' to add connections early on (but maybe this is not in line with the original alg), or let the user specify a starting point (that is, a genome or NN architecture to start with).
Maybe it would already be a good improvement to connect the one start neuron with all inputs and outputs.

Remove neat dir and move files down a level

I think it may be cleaner to not have the neat dir indirection. Then the remaining parts can be made into mods a little easier (ctrnn etc.). This may make it cleaner. Doing this before documenting and looking at visibility etc. may be helpful. I am happy to do this if required.

Quite slow compared to original

This library takes around 200 generations to solve XOR and it usually takes around several minutes, yet according to the research papers NEAT should be able to solve XOR in around 30 generations.

Is there a particular reason for this deficiency?

Netwrok does not produce output

Hello! I want to implement a snake game that will use NEAT to learn and got these inputs:

            let inputs = vec![
                snake.head.x as f64,
                snake.head.y as f64,
                (snake.body.contains(&next_pos) as i32 as f64), // snake collides with body
                (next_pos.x == 0) as i32 as f64, // collides with left wall 
                (next_pos.x + 1 == rb.width() as i16) as i32 as f64, // collides with right wall
                (next_pos.y == 1) as i32 as f64, // collides with upper wall
                (next_pos.y + 1 == rb.height() as i16) as i32 as f64, // collides with down wall
                is_food_up(&snake,&apple),
                is_food_down(&snake,&apple),
                is_food_left(&snake,&apple),
                is_food_right(&snake,&apple),
            ];
         let mut output = vec![0.0f64];

            organism.activate(inputs, &mut output);
            out = output[0];
            match output[0] {
                x if x < 0.2 => 
                {
                    if snake.direction != direction::Direction::Up {
                        snake.direction = direction::Direction::Down;
                    }
                }
                x if x < 0.5 => {
                    if snake.direction != direction::Direction::Down {
                        snake.direction = direction::Direction::Up;
                    }
                }
                x if x < 0.7 => {
                    if snake.direction != direction::Direction::Right {
                        snake.direction = direction::Direction::Left;
                    }
                }
                x if x < 1.0 => {
                    if snake.direction != direction::Direction::Left {
                        snake.direction = direction::Direction::Right;
                    }
                }
                _ => {}
            }

I always get output equal to zero and never get something other, what is wrong?
P.S I return score as fitness: return score as f64

Parallelization

I could not find any thread parallelization in the code, please correct me if I'm wrong.
How should this be done? I'm willing to do the work.
What I'm thinking is just parallelize the calls to Environment::test for evaluating fitness. This is most important to me, as my fitness calculation can be quite heavy.
I suggest using rayon. Any thoughts?

Some algorithm considerations

I will keep updating it. Some of these are quite subtle, but I think we should address everything.

  • 1. Should mutate_add_connection() be allowed to add i -> i connections? (right now I would say yes. These can have an effect on the NN.) edit: I will allow it for now

  • 2. Adding a connection that already exists: should it keep the old weight or the new? edit: I will keep the new weight for now

  • 3. Creating offspring: there is 25% chance to just mutate the parent, and 75% chance to mate two organisms, but in that case no mutations happen. Is this inspired by literature? Another idea is to always mate two organisms, followed by mutation (say, by 25% chance).

  • 4. Interspecies mating - is this supported by literature? (doesn't have to be, just wondering about the justifications)

  • 5. Specie::generate_offspring() currently just picks N organisms randomly, but the NEAT paper seems to say that we should pick the N best-performing organisms. Also, currently the champion organism within the specie is added (if specie size > 5). Why is that?

  • 6. Doesn't seem like we use shared fitness. (look at "explicit fitness sharing" in the NEAT paper).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.