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
use rand::{thread_rng, Rng};
use rayon::prelude::*;
use std::fmt;
pub type Particle = Vec<f64>;
pub type Population = Vec<Particle>;
pub struct Model {
pub config: Config,
pub flat_dim: usize,
pub population: Population,
pub population_f_scores: Vec<f64>,
pub x_best: Particle,
pub f_best: f64,
obj_f: fn(&Particle, usize, &Vec<usize>) -> f64,
}
impl Model {
pub fn new(
config: Config,
obj_f: fn(p: &Particle, flat_dim: usize, dim: &Vec<usize>) -> f64,
) -> Model {
let mut rng = thread_rng();
let mut flat_dim = 1;
for d in config.dimensions.clone() {
flat_dim *= d;
}
let mut population: Population = vec![];
for _ in 0..config.population_size {
let mut particle: Particle = vec![];
for _ in 0..flat_dim {
particle.push(rng.gen_range(config.bounds.0..config.bounds.1));
}
population.push(particle);
}
let population_f_scores = vec![0.0; config.population_size];
let x_best = population[0].clone();
let f_best = population_f_scores[0].clone();
let mut model = Model {
config,
flat_dim,
population,
population_f_scores,
x_best,
f_best,
obj_f: obj_f,
};
model.get_f_values();
model
}
pub fn get_f_values(&mut self) -> Vec<f64> {
let iter = self.population.par_iter();
self.population_f_scores = iter
.map(|particle| {
(self.obj_f)(particle, self.flat_dim, &self.config.dimensions)
})
.collect();
let mut f_best = self.population_f_scores[0];
let mut x_best_index = 0;
for (index, &score) in self.population_f_scores.iter().enumerate() {
if score < f_best {
f_best = score;
x_best_index = index;
}
}
self.f_best = f_best;
self.x_best = self.population[x_best_index].clone();
self.population_f_scores.to_owned()
}
pub fn get_f_best(&self) -> f64 {
self.f_best
}
pub fn get_x_best(&self) -> Particle {
self.x_best.clone()
}
}
#[derive(Debug)]
pub struct Config {
pub dimensions: Vec<usize>,
pub population_size: usize,
pub neighborhood_type: NeighborhoodType,
pub rho: usize,
pub alpha: f64,
pub c1: f64,
pub c2: f64,
pub lr: f64,
pub bounds: (f64, f64),
pub t_max: usize,
}
impl Config {
pub fn new() -> Config {
Self::default()
}
}
impl Default for Config {
fn default() -> Self {
Self {
dimensions: vec![2],
population_size: 1000,
neighborhood_type: NeighborhoodType::Lbest,
rho: 2,
alpha: 0.1,
lr: 0.5,
c1: 2.05,
c2: 2.05,
bounds: (-1.0, 1.0),
t_max: 1000,
}
}
}
#[derive(Debug)]
pub enum NeighborhoodType {
Lbest,
Gbest,
}
impl fmt::Display for NeighborhoodType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NeighborhoodType::Lbest => write!(f, "Local neighborhood (lbest)"),
NeighborhoodType::Gbest => write!(f, "Global neighborhood (gbest)"),
}
}
}