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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use crate::model::*;
use indicatif::{ProgressBar, ProgressStyle};
use rand::{thread_rng, Rng};
use std::error::Error;
use std::fs::File;
use std::io::Write;
pub struct PSO {
chi: f64,
v_max: f64,
pub model: Model,
neighborhoods: Vec<Vec<usize>>,
velocities: Population,
pub neigh_population: Population,
pub best_f_values: Vec<f64>,
pub best_f_trajectory: Vec<f64>,
pub best_x_trajectory: Vec<Particle>,
}
impl PSO {
pub fn new(model: Model) -> PSO {
let phi = model.config.c1 + model.config.c2;
let phi_squared = phi.powf(2.0);
let tmp = phi_squared - (4.0 * phi);
let tmp = tmp.sqrt();
let chi = 2.0 / (2.0 - phi - tmp).abs();
let v_max = model.config.alpha * 5.0;
let neighborhoods = PSO::create_neighborhoods(&model);
let mut rng = thread_rng();
let mut velocities = vec![];
for _ in 0..model.config.population_size {
let mut tmp = vec![];
for _ in 0..model.flat_dim {
tmp.push(rng.gen_range(v_max * -1.0..v_max * 1.0));
}
velocities.push(tmp);
}
let best_f_values = model.population_f_scores.clone();
let neigh_population = model.population.clone();
let best_f_trajectory = vec![model.f_best];
let best_x_trajectory = vec![model.x_best.clone()];
PSO {
chi,
v_max,
model,
neighborhoods,
velocities,
best_f_values,
neigh_population,
best_f_trajectory,
best_x_trajectory,
}
}
pub fn run(&mut self, terminate: fn(f64) -> bool) -> usize {
let mut bar: Option<ProgressBar> = None;
if self.model.config.progress_bar {
bar = Some(ProgressBar::new(self.model.config.t_max as u64));
match bar {
Some(ref bar) => {
bar.set_style(ProgressStyle::default_bar().template(
"{msg} [{elapsed}] {bar:20.cyan/blue} {pos:>7}/{len:7} ETA: {eta}",
));
}
None => {}
}
}
let mut k = 0;
let pop_size = self.model.config.population_size;
loop {
self.update_velocity_and_pos();
self.model.get_f_values();
self.update_best_positions();
self.model.population = self.model.population.clone();
k += pop_size;
match bar {
Some(ref bar) => {
bar.inc(pop_size as u64);
bar.set_message(format!("{:.6}", self.model.f_best));
}
None => {}
}
if k > self.model.config.t_max || terminate(self.model.f_best) {
break;
}
}
match bar {
Some(ref bar) => {
bar.finish_and_clear();
}
None => {}
}
k
}
fn update_velocity_and_pos(&mut self) {
let mut rng = thread_rng();
for i in 0..self.model.config.population_size {
let lbest = &self.neigh_population[self.local_best(i)];
for j in 0..self.model.flat_dim {
let r1 = rng.gen_range(-1.0..1.0);
let r2 = rng.gen_range(-1.0..1.0);
let cog = self.model.config.c1
* r1
* (self.neigh_population[i][j] - self.model.population[i][j]);
let soc = self.model.config.c2 * r2 * (lbest[j] - self.model.population[i][j]);
let v = self.chi * (self.velocities[i][j] + cog + soc);
self.velocities[i][j] = if v.abs() > self.v_max {
v.signum() * self.v_max
} else {
v
};
let x = self.model.population[i][j] + self.model.config.lr * self.velocities[i][j];
let bound_index =
j % self.model.config.dimensions[self.model.config.dimensions.len() - 1];
let (lower_bound, upper_bound) = self.model.config.bounds[bound_index];
if x > upper_bound {
self.model.population[i][j] = upper_bound;
} else if x < lower_bound {
self.model.population[i][j] = lower_bound;
} else {
self.model.population[i][j] = x;
}
if x.is_nan() {
panic!("A coefficient became NaN!");
}
}
}
}
fn update_best_positions(&mut self) {
for i in 0..self.best_f_values.len() {
let new = self.model.population_f_scores[i];
let old = self.best_f_values[i];
if new < old {
self.best_f_values[i] = new;
self.neigh_population[i] = self.model.population[i].clone();
}
}
self.best_f_trajectory.push(self.model.f_best);
self.best_x_trajectory.push(self.model.x_best.clone());
}
fn local_best(&self, i: usize) -> usize {
let best = PSO::argsort(&self.best_f_values);
for b in best {
if self.neighborhoods[i].iter().any(|&n| n == b) {
return b;
}
}
0
}
fn create_neighborhoods(model: &Model) -> Vec<Vec<usize>> {
let mut neighborhoods;
match model.config.neighborhood_type {
NeighborhoodType::Lbest => {
neighborhoods = vec![];
for i in 0..model.config.population_size {
let mut neighbor = vec![];
let first_neighbor = i as i32 - model.config.rho as i32;
let last_neighbor = i as i32 + model.config.rho as i32;
for neighbor_i in first_neighbor..last_neighbor {
neighbor.push(if neighbor_i < 0 {
(model.config.population_size as i32 - neighbor_i) as usize
} else {
neighbor_i as usize
});
}
neighborhoods.push(neighbor)
}
}
NeighborhoodType::Gbest => {
neighborhoods = vec![];
for _ in 0..model.config.population_size {
let mut tmp = vec![];
for j in 0..model.config.population_size {
tmp.push(j);
}
neighborhoods.push(tmp);
}
}
}
neighborhoods
}
fn argsort(v: &Vec<f64>) -> Vec<usize> {
let mut idx = (0..v.len()).collect::<Vec<_>>();
idx.sort_by(|&i, &j| v[i].partial_cmp(&v[j]).expect("NaN"));
idx
}
pub fn write_f_to_file(&self, filepath: &str) -> Result<(), Box<dyn Error>> {
let best_f_str: Vec<String> = self
.best_f_trajectory
.iter()
.map(|n| n.to_string())
.collect();
let mut file = File::create(filepath)?;
writeln!(file, "{}", best_f_str.join("\n"))?;
Ok(())
}
pub fn write_x_to_file(&self, filepath: &str) -> Result<(), Box<dyn Error>> {
let best_x_str: Vec<String> = self
.best_x_trajectory
.iter()
.map(|x| {
x.iter()
.map(|coef: &f64| coef.to_string())
.collect::<Vec<String>>()
.join(", ")
})
.collect();
let mut file = File::create(filepath)?;
writeln!(file, "{}", best_x_str.join("\n"))?;
Ok(())
}
}