extern crate rsgenetic;
use rsgenetic::pheno::*;
use rsgenetic::sim::select::*;
use rsgenetic::sim::seq::Simulator;
use rsgenetic::sim::*;
#[derive(Clone, Copy, Debug)]
struct MyPhenotype {
variant: MyVariant,
value: i32,
}
#[derive(Clone, Copy, Debug)]
enum MyVariant {
Variant1,
Variant2,
}
impl Phenotype<i32> for MyPhenotype {
fn fitness(&self) -> i32 {
self.value
}
fn crossover(&self, other: &MyPhenotype) -> MyPhenotype {
match self.variant {
MyVariant::Variant1 => MyPhenotype {
variant: self.variant,
value: self.value + other.value,
},
MyVariant::Variant2 => MyPhenotype {
variant: self.variant,
value: self.value - other.value,
},
}
}
fn mutate(&self) -> MyPhenotype {
match self.variant {
MyVariant::Variant1 => MyPhenotype {
variant: self.variant,
value: self.value / 2,
},
MyVariant::Variant2 => MyPhenotype {
variant: self.variant,
value: self.value * 2,
},
}
}
}
fn main() {
let mut population: Vec<MyPhenotype> = Vec::with_capacity(300);
for i in 0..150 {
population.push(MyPhenotype {
variant: MyVariant::Variant1,
value: i,
});
population.push(MyPhenotype {
variant: MyVariant::Variant2,
value: i,
})
}
#[allow(deprecated)]
let mut builder = Simulator::builder(&mut population);
builder
.with_selector(Box::new(UnstableMaximizeSelector::new(10)))
.with_max_iters(100);
let mut s = builder.build();
s.run();
let result = s.get().unwrap();
let time = s.time();
println!("Execution time: {} ns.", time.unwrap());
println!("Result: {:?} | Fitness: {}.", result, result.fitness());
}