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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::aco::FMatrix;
use push_trait::Push;
use rand::prelude::ThreadRng;
use rand::{thread_rng, Rng};
use std::collections::HashSet;
pub trait Ant {
/// Clears iteration specific data like visited vertices or path.
fn clear(&mut self);
/// Returns vector of vertices in order of visiting
fn path(&self) -> &[usize];
/// Selects an vertex to start from
fn chose_staring_place(&mut self);
/// Returns true when there is no valid next vertex with path not fully constructed.
fn is_stuck(&self) -> bool;
/// Chooses and goes to the next vertex. Returns traversed edge.
fn go_to_next_place(&mut self, edges_goodness: &FMatrix) -> (usize, usize);
}
macro_rules! standard_ant_impl {
() => {
/// Clears iteration specific data like visited vertices or path.
fn clear(&mut self) {
for i in 0..self.solution_size {
self.unvisited.push(i);
}
self.path.clear();
self.stuck = false;
}
/// Returns vector of vertices in order of visiting
fn path(&self) -> &[usize] {
&self.path
}
/// Selects an vertex to start from
fn chose_staring_place(&mut self) {
let start: usize = self.rng.gen_range(0..self.solution_size);
self.unvisited.remove(&start);
self.path.push(start);
}
/// Returns true when there is no valid next vertex with path not fully constructed.
fn is_stuck(&self) -> bool {
self.stuck
}
};
}
/// # Canonical Ant
///
/// Represent a single ant.
/// Used to build a solution.
/// Related to [AntsBehavior]
pub struct CanonicalAnt<R: Rng> {
unvisited: HashSet<usize>,
path: Vec<usize>,
solution_size: usize,
stuck: bool,
rng: R,
}
impl<R: Rng> CanonicalAnt<R> {
/// Create a new instance of [CanonicalAnt] with user specified RNG.
///
/// ## Arguments
/// * `solution_size` - Numer of graph vertices.
/// * `rng` - Random numbers generator.
pub fn with_rng(solution_size: usize, rng: R) -> Self {
Self {
unvisited: HashSet::with_capacity(solution_size),
path: Vec::with_capacity(solution_size),
stuck: false,
solution_size,
rng,
}
}
}
impl<R: Rng> Ant for CanonicalAnt<R> {
standard_ant_impl!();
/// Chooses and goes to the next vertex. Returns traversed edge.
///
/// Panic when starting vertex wasn't decided ([CanonicalAnt::chose_staring_place]) or when all vertices
/// are already visited
fn go_to_next_place(&mut self, edges_goodness: &FMatrix) -> (usize, usize) {
let last = *self
.path
.last()
.expect("Path is empty. Did you forget to call Ant::chose_staring_place");
if self.is_stuck() {
return (last, last);
}
let row = edges_goodness.row(last);
if self.unvisited.is_empty() {
panic!("Ant had already visited every place");
}
let mut goodness_sum = 0.0f64;
for v in self.unvisited.iter() {
goodness_sum += row[*v];
}
let mut random: f64 = self.rng.gen_range(0.0..=goodness_sum);
let mut next: usize = last;
for v in self.unvisited.iter() {
random -= row[*v];
if random <= 0.0 {
next = *v;
break;
}
}
if next == last {
self.stuck = true;
}
self.unvisited.remove(&next);
self.path.push(next);
(last, next)
}
}
impl CanonicalAnt<ThreadRng> {
/// Create a new instance of [CanonicalAnt] with default RNG.
///
/// ## Arguments
/// * `solution_size` - Numer of graph vertices
pub fn new(solution_size: usize) -> Self {
Self::with_rng(solution_size, thread_rng())
}
}
/// # Exploitation Ant
///
/// Represent a single exploiting ant.
///
/// With given probability it will chose the path with most pheromone.
///
/// Used to build a solution.
/// Related to [AntsBehavior]
pub struct ExploitingAnt<R: Rng> {
unvisited: HashSet<usize>,
path: Vec<usize>,
solution_size: usize,
stuck: bool,
exploitation_rate: f64,
rng: R,
}
impl<R: Rng> ExploitingAnt<R> {
/// Create a new instance of [ExploitingAnt] with user specified RNG.
///
/// ## Arguments
/// * `solution_size` - Numer of graph vertices.
/// * `exploitation_rate` - Number between 0.0 and 1.0. Probability of exploiting pheromone information.
/// * `rng` - Random numbers generator.
pub fn with_rng(solution_size: usize, exploitation_rate: f64, rng: R) -> Self {
assert!(
(0.0..1.0).contains(&exploitation_rate),
"Exploitation rate must be in range (0.0..1.0)"
);
Self {
unvisited: HashSet::with_capacity(solution_size),
path: Vec::with_capacity(solution_size),
stuck: false,
solution_size,
exploitation_rate,
rng,
}
}
}
impl<R: Rng> Ant for ExploitingAnt<R> {
standard_ant_impl!();
fn go_to_next_place(&mut self, edges_goodness: &FMatrix) -> (usize, usize) {
let last = *self
.path
.last()
.expect("Path is empty. Did you forget to call Ant::chose_staring_place");
if self.is_stuck() {
return (last, last);
}
let row = edges_goodness.row(last);
if self.unvisited.is_empty() {
panic!("Ant had already visited every place");
}
let should_exploit = self.rng.gen::<f64>() < self.exploitation_rate;
let mut next = last;
if should_exploit {
let mut value = f64::MIN;
for v in self.unvisited.iter() {
if value < row[*v] {
next = *v;
value = row[*v];
}
}
} else {
let mut goodness_sum = 0.0f64;
for v in self.unvisited.iter() {
goodness_sum += row[*v];
}
let mut random: f64 = self.rng.gen_range(0.0..=goodness_sum);
for v in self.unvisited.iter() {
random -= row[*v];
if random <= 0.0 {
next = *v;
break;
}
}
}
if next == last {
self.stuck = true;
}
self.unvisited.remove(&next);
self.path.push(next);
(last, next)
}
}
impl ExploitingAnt<ThreadRng> {
/// Create a new instance of [CanonicalAnt] with default RNG.
///
/// ## Arguments
/// * `solution_size` - Numer of graph vertices.
/// * `exploitation_rate` - Number between 0.0 and 1.0. Probability of exploiting pheromone information.
pub fn new(solution_size: usize, exploitation_rate: f64) -> Self {
Self::with_rng(solution_size, exploitation_rate, thread_rng())
}
}