tlmak0 / rustneat Goto Github PK
View Code? Open in Web Editor NEWRust Neat - NeuroEvolution of Augmenting Topologies
License: MIT License
Rust Neat - NeuroEvolution of Augmenting Topologies
License: MIT License
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:
[dependencies]
rustneat = "0.1.8"
Most people/crates switched to serde. rustc-serialize was deprecated.
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?
Organism
and Gene
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.Ctrnn
that we take the negative bias, rather than the positive as in the paper. So practically we always have a negative bias.bias: f64
and weight: f64
in Gene
(instead of having a bias and a weight type of gene)?Ctrnn
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)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?
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
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.
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.
Add some kind of compilation feature flag with macros that send events to a realtime datase and integrate it with charts.js or other front-end chart library
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?
~/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.
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
There is no link to this repository on crates.io!
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
Lines 54 to 59 in 18b4fbf
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
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).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.