rust_monster/ga/
ga_core.rs1use super::ga_population::{GAPopulation, GAPopulationSortOrder};
4
5bitflags!
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
21pub 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
31pub trait GASolution
33{
34 fn new() -> Self;
36
37 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 fn fitness(&self) -> f32;
44 fn score(&self) -> f32;
46}
47
48
49pub trait GAFactory<T: GASolution>
51{
52 fn initial_population(&mut self) -> GAPopulation<T>
53 {
54 GAPopulation::new(vec![], GAPopulationSortOrder::HighIsBest)
55 }
56}
57
58
59pub trait GeneticAlgorithm<T: GASolution>
61{
62 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 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}