-
Notifications
You must be signed in to change notification settings - Fork 0
/
aco.rs
172 lines (161 loc) · 4.97 KB
/
aco.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
169
170
171
172
//! Ant Colony Optimization (ACO).
//!
//! This module currently defines ACO only in its original domain, i.e. for [TSP].
//!
//! [TSP]: TravellingSalespersonProblem
//!
//! # References
//!
//! \[1\] Marco Dorigo, Mauro Birattari, and Thomas Stützle. 2006.
//! Ant colony optimization. IEEE Computational Intelligence Magazine 1, 4 (November 2006), 28–39.
//! DOI:<https://doi.org/10/dq339r>
//!
//! \[2\] Marco Dorigo and Gianni Di Caro. 1999.
//! Ant colony optimization: a new meta-heuristic.
//! In Proceedings of the 1999 Congress on Evolutionary Computation-CEC99 (Cat. No. 99TH8406), 1470-1477 Vol. 2.
//! DOI:<https://doi.org/10/b5h9xz>
use eyre::WrapErr;
use crate::{
components::{generative, initialization},
identifier::{Global, Identifier},
logging::Logger,
problems::TravellingSalespersonProblem,
Component, Condition, Configuration, ExecResult, SingleObjectiveProblem,
};
/// Parameters for [`ant_system`].
pub struct ASParameters {
/// The number of ants in the colony, i.e. how many individuals should be sampled.
num_ants: usize,
/// Relative importance of pheromones (usually called τ).
alpha: f64,
/// Relative importance of heuristic information (usually called η).
beta: f64,
/// Initial pheromone values in the matrix.
default_pheromones: f64,
/// Pheromone evaporation rate.
evaporation: f64,
/// Constant for scaling the pheromone update.
decay_coefficient: f64,
}
/// Ant System (AS).
///
/// Uses the [`aco`] component internally.
pub fn ant_system<P>(
params: ASParameters,
condition: Box<dyn Condition<P>>,
) -> ExecResult<Configuration<P>>
where
P: TravellingSalespersonProblem,
{
let ASParameters {
num_ants,
alpha,
beta,
default_pheromones,
evaporation,
decay_coefficient,
} = params;
Ok(Configuration::builder()
.do_(initialization::Empty::new())
.do_(aco::<P, Global>(
Parameters {
generation: generative::AcoGeneration::new(
num_ants,
alpha,
beta,
default_pheromones,
),
pheromone_update: generative::AsPheromoneUpdate::new(
evaporation,
decay_coefficient,
),
},
condition,
))
.build())
}
/// Parameters for [`max_min_ant_system`].
pub struct MMASParameters {
/// The number of ants in the colony, i.e. how many individuals should be sampled.
num_ants: usize,
/// Relative importance of pheromones (usually called τ).
alpha: f64,
/// Relative importance of heuristic information (usually called η).
beta: f64,
/// Initial pheromone values in the matrix.
default_pheromones: f64,
/// Pheromone evaporation rate.
evaporation: f64,
/// Maximal allowed pheromone value.
max_pheromones: f64,
/// Minimal allowed pheromone value.
min_pheromones: f64,
}
/// MAX-MIN Ant System (MMAS).
///
/// Uses the [`aco`] component internally.
pub fn max_min_ant_system<P>(
params: MMASParameters,
condition: Box<dyn Condition<P>>,
) -> ExecResult<Configuration<P>>
where
P: TravellingSalespersonProblem,
{
let MMASParameters {
num_ants,
alpha,
beta,
default_pheromones,
evaporation,
max_pheromones,
min_pheromones,
} = params;
Ok(Configuration::builder()
.do_(initialization::Empty::new())
.do_(aco::<P, Global>(
Parameters {
generation: generative::AcoGeneration::new(
num_ants,
alpha,
beta,
default_pheromones,
),
pheromone_update: generative::MinMaxPheromoneUpdate::new(
evaporation,
max_pheromones,
min_pheromones,
)
.wrap_err("failed to construct the min max pheromone update")?,
},
condition,
))
.build())
}
/// Basic building blocks of [`aco`].
pub struct Parameters<P> {
/// Generates a population using the pheromone matrix.
generation: Box<dyn Component<P>>,
/// Updates the pheromone matrix using the objective values of the sampled population.
pheromone_update: Box<dyn Component<P>>,
}
/// A generic single-objective Ant Colony Optimization (ACO) template.
pub fn aco<P, I>(params: Parameters<P>, condition: Box<dyn Condition<P>>) -> Box<dyn Component<P>>
where
P: SingleObjectiveProblem,
I: Identifier,
{
let Parameters {
generation,
pheromone_update,
} = params;
Configuration::builder()
.while_(condition, |builder| {
builder
.do_(generation)
.evaluate_with::<I>()
.update_best_individual()
.do_(pheromone_update)
.do_(Logger::new())
})
.build_component()
}