rust_monster/ga/
ga_core.rs

1// TODO: COPYRIGHT, USE & AUTHORS
2
3use super::ga_population::{GAPopulation, GAPopulationSortOrder};
4
5/// Bit Flags for Genetic Algorithm Configuration 
6/// 
7///
8bitflags!
9{
10    pub flags GAFlags: u32
11    {
12        const DEBUG_FLAG = 0b00000001
13    }
14}
15impl Default for GAFlags
16{
17    fn default() -> GAFlags { GAFlags {bits : 0} }
18}
19
20
21/// Genetic Algorithm Configuration
22pub trait GAConfig
23{
24    fn flags(&self) -> GAFlags;
25    fn max_generations(&self) -> i32;
26    fn probability_crossover(&self) -> f32;
27    fn probability_mutation(&self) -> f32;
28}
29
30
31/// Genetic Algorithm Solution
32pub trait GASolution
33{
34    //Static
35    fn new() -> Self;
36
37    // Instance
38    fn clone(&self) -> Self;
39    fn crossover(&self, other : &Self) -> Self;
40    fn mutate(&mut self, pMutation : f32);
41    fn evaluate(&mut self) -> f32;
42    // Scaled fitness score
43    fn fitness(&self) -> f32;
44    // Raw objective score
45    fn score(&self) -> f32;
46}
47
48
49/// Genetic Algorithm Solution Factory
50pub trait GAFactory<T: GASolution>
51{
52    fn initial_population(&mut self) -> GAPopulation<T> 
53    {
54        GAPopulation::new(vec![], GAPopulationSortOrder::HighIsBest)
55    }
56}
57
58
59/// Genetic Algorithm
60pub trait GeneticAlgorithm<T: GASolution>
61{
62    // GENERIC GA METHODS - Should not be overriden frequently
63    fn initialize(&mut self)
64    {
65        debug!("Genetic Algorithm - Initialized");
66        self.initialize_internal()
67    }
68
69    fn step(&mut self) -> i32
70    { 
71        debug!("Genetic Algorithm - Step");
72        self.step_internal()
73    }
74
75    fn done(&mut self) -> bool
76    {
77        debug!("Genetic Algorithm - Done");
78        self.done_internal()
79    }
80
81    // IMPLEMENTATION SPECIFIC
82    fn config(&mut self) -> &GAConfig;
83
84    fn population(&mut self) -> &mut GAPopulation<T>;
85
86    fn initialize_internal(&mut self) {}
87    fn step_internal(&mut self) -> i32 { 0 }
88    fn done_internal(&mut self) -> bool { true }
89}