-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.rs
168 lines (158 loc) · 4.83 KB
/
ga.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! Genetic Algorithm (GA).
//!
//! # References
//!
//! \[1\] John H. Holland. 1992.
//! Genetic Algorithms.
//! Scientific American 267, 1 (1992), 66–73.
//! DOI:<https://doi.org/10/bmbqnb>
//!
//! \[2\] Kumara Sastry, David E. Goldberg, and Graham Kendall. 2014.
//! Genetic Algorithms.
//! In Search Methodologies: Introductory Tutorials in Optimization and Decision Support Techniques,
//! Edmund K. Burke and Graham Kendall (eds.). Springer US, Boston, MA, 93–117.
//! DOI:<https://doi.org/10.1007/978-1-4614-6940-7_4>
use crate::{
component::ExecResult,
components::{
boundary, initialization, mutation, recombination, replacement, selection, utils,
},
conditions,
configuration::Configuration,
identifier::{Global, Identifier},
logging::Logger,
problems::{LimitedVectorProblem, SingleObjectiveProblem, VectorProblem},
Component, Condition,
};
/// Parameters for [`binary_ga`].
#[derive(Clone, Copy, Debug)]
pub struct BinaryProblemParameters {
pub population_size: u32,
pub tournament_size: u32,
pub rm: f64,
pub pc: f64,
pub pm: f64,
}
/// An example single-objective GA operating on a binary search space.
///
/// Uses the [`ga`] component internally.
pub fn binary_ga<P>(
params: BinaryProblemParameters,
condition: Box<dyn Condition<P>>,
) -> ExecResult<Configuration<P>>
where
P: SingleObjectiveProblem + VectorProblem<Element = bool>,
{
let BinaryProblemParameters {
population_size,
tournament_size,
rm,
pc,
pm,
} = params;
Ok(Configuration::builder()
.do_(initialization::RandomBitstring::new_uniform(
population_size,
))
.evaluate()
.update_best_individual()
.do_(ga::<P, Global>(
Parameters {
selection: selection::Tournament::new(population_size, tournament_size),
crossover: recombination::UniformCrossover::new_insert_both(pc),
pm,
mutation: mutation::BitFlipMutation::new(rm),
constraints: utils::Noop::new(),
archive: None,
replacement: replacement::Generational::new(population_size),
},
condition,
))
.build())
}
/// Parameters for [`real_ga`].
#[derive(Clone, Copy, Debug)]
pub struct RealProblemParameters {
pub population_size: u32,
pub tournament_size: u32,
pub pm: f64,
pub deviation: f64,
pub pc: f64,
}
/// An example single-objective GA operating on a real search space.
///
/// Uses the [`ga`] component internally.
pub fn real_ga<P>(
params: RealProblemParameters,
condition: Box<dyn Condition<P>>,
) -> ExecResult<Configuration<P>>
where
P: SingleObjectiveProblem + LimitedVectorProblem<Element = f64>,
{
let RealProblemParameters {
population_size,
tournament_size,
pm,
deviation,
pc,
} = params;
Ok(Configuration::builder()
.do_(initialization::RandomSpread::new(population_size))
.evaluate()
.update_best_individual()
.do_(ga::<P, Global>(
Parameters {
selection: selection::Tournament::new(population_size, tournament_size),
crossover: recombination::UniformCrossover::new_insert_both(pc),
pm,
mutation: mutation::NormalMutation::new_dev(deviation),
constraints: boundary::Saturation::new(),
archive: None,
replacement: replacement::Generational::new(population_size),
},
condition,
))
.build())
}
/// Basic building blocks of [`ga`].
pub struct Parameters<P> {
pub selection: Box<dyn Component<P>>,
pub crossover: Box<dyn Component<P>>,
pub pm: f64,
pub mutation: Box<dyn Component<P>>,
pub constraints: Box<dyn Component<P>>,
pub archive: Option<Box<dyn Component<P>>>,
pub replacement: Box<dyn Component<P>>,
}
/// A generic single-objective Genetic Algorithm (GA) template.
pub fn ga<P, I>(params: Parameters<P>, condition: Box<dyn Condition<P>>) -> Box<dyn Component<P>>
where
P: SingleObjectiveProblem,
I: Identifier,
{
let Parameters {
selection,
crossover,
pm,
mutation,
constraints,
archive,
replacement,
} = params;
Configuration::builder()
.while_(condition, |builder| {
builder
.do_(selection)
.do_(crossover)
.if_(conditions::RandomChance::new(pm), |builder| {
builder.do_(mutation)
})
.do_(constraints)
.evaluate_with::<I>()
.update_best_individual()
.do_if_some_(archive)
.do_(replacement)
.do_(Logger::new())
})
.build_component()
}