Skip to content

Commit

Permalink
added neat config for pole balancing and cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
samyhaff committed Apr 20, 2024
1 parent 4dcbfe2 commit 5a4e38a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::f64::consts::PI;
use crate::constants::POLE_BALANCING_STEPS;
use crate::constants::{POLE_BALANCING_STEPS, POLE_BALANCING_MAX_FORCE};
use crate::neuroevolution_algorithm::*;
use crate::pole_balancing::State;

Expand Down Expand Up @@ -62,19 +62,19 @@ fn pole_balancing(alg: &Algorithm) -> f64 {
let mut state = State::new(
0.,
0.,
vec![1.],
vec![0.],
vec![0.],
vec![1., 0.1],
vec![0.017, 0.],
vec![0., 0.],
1.,
vec![0.5],
vec![0.5, 0.05],
);

let mut count = 0;

for _ in 0..POLE_BALANCING_STEPS {
let input = state.to_vec();
let output = alg.evaluate(&input);
let force = 20. * output - 10.;
let force = 2. * POLE_BALANCING_MAX_FORCE * output - POLE_BALANCING_MAX_FORCE;
state.update(force);
if state.are_poles_balanced() && !state.is_cart_out_of_bounds() {
count += 1;
Expand Down
5 changes: 5 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ fn main() {
Problem::Quarter => Benchmark::Classification(ClassificationProblem::SphereProblem(SphereClassificationProblem::Quarter(UNIT_CIRCLE_STEPS))),
Problem::TwoQuarters => Benchmark::Classification(ClassificationProblem::SphereProblem(SphereClassificationProblem::TwoQuarters(UNIT_CIRCLE_STEPS))),
Problem::Xor => Benchmark::Classification(ClassificationProblem::Xor),
Problem::PoleBalancing => Benchmark::PoleBalancing,
};

if cli.problem == Problem::PoleBalancing {
panic!("Not implemented yet!");
}

match cli.algorithm {
AlgorithmType::Oneplusonena => {
match cli.continuous {
Expand Down
12 changes: 6 additions & 6 deletions src/bin/neat.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use neuroevolution::neat::*;
use neuroevolution::benchmarks::{Benchmark, ClassificationProblem};
use neuroevolution::benchmarks::Benchmark;
use neuroevolution::neuroevolution_algorithm::*;

fn main() {
let config = Config {
population_size: 150,
n_inputs: 2,
population_size: 1000,
n_inputs: 4,
n_outputs: 1,
weights_mean: 0.,
weights_stddev: 0.8,
Expand All @@ -19,12 +19,12 @@ fn main() {
similarity_threshold: 15.0,
excess_weight: 1.,
disjoint_weight: 1.,
matching_weight: 0.3,
champion_copy_threshold: 5,
matching_weight: 3.0,
champion_copy_threshold: 4,
stagnation_threshold: 1500,
};

let mut neat = Neat::new(config);
neat.optimize(&Benchmark::Classification(ClassificationProblem::Xor), 1500);
neat.optimize(&Benchmark::PoleBalancing, 1000);
println!("Fitness: {:.2}", neat.get_best_individual_fitness());
}
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ pub enum Problem {
Quarter,
TwoQuarters,
Xor,
PoleBalancing,
}
3 changes: 2 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub const N_ITERATIONS: u32 = 1000;
pub const RESOLUTION: usize = 1000;
pub const UNIT_CIRCLE_STEPS: u32 = 100;
pub const POLE_BALANCING_STEPS: usize = 1000000;
pub const POLE_BALANCING_STEPS: usize = 1000;
pub const POLE_BALANCING_MAX_FORCE: f64 = 10.;

0 comments on commit 5a4e38a

Please sign in to comment.