genotype-rs
An abstraction layer between genotype and phenotype, with in-place mutation.
use genotype::{Param, ParamHolder, RangedParam};
use genotype::param_set::{ParamSet2d};
use genotype::mutation::*;
#[derive(Debug)]
struct Dimension(Param);
#[derive(Debug)]
struct Rotation(Param);
#[derive(Debug)]
struct Shape {
dimensions: ParamSet2d<Dimension>,
rotation: Rotation,
}
impl RangedParam for Dimension {
fn range(&self) -> (Param, Param) {
(1.0, 20.0)
}
fn get(&self) -> Param { self.0 }
fn get_mut(&mut self) -> &mut Param { &mut self.0 }
}
impl RangedParam for Rotation {
fn range(&self) -> (Param, Param) {
(0.0, 360.0)
}
fn get(&self) -> Param { self.0 }
fn get_mut(&mut self) -> &mut Param { &mut self.0 }
}
impl ParamHolder for Shape {
fn param_count(&self) -> usize { self.dimensions.param_count() + 1 }
fn get_param(&mut self, index: usize) -> &mut RangedParam {
match index {
0...1 => self.dimensions.get_param(index),
2 => &mut self.rotation,
_ => panic!("Bad index"),
}
}
}
struct ConstGen(Param);
impl MutationGen for ConstGen {
fn gen(&mut self) -> Param { self.0 }
}
let shape = Rc::new(RefCell::new(Shape {
dimensions: ParamSet2d::new(Dimension(0.5), Dimension(0.5)),
rotation: Rotation(0.0),
}));
println!("shape: {:?}", shape);
mutate(shape.clone(), &mut ConstGen(0.1));
println!("mutated shape: {:?}", shape);