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
use anyhow::Result;
use nalgebra::DVector;
/// Parameters for CMA-ES (Covariance Matrix Adaptation Evolution Strategy).
#[derive(Debug, Clone)]
pub struct CmaesParams {
pub popsize: i32, // Population size
pub xstart: Vec<f32>, // Initial guess (mean vector)
pub sigma: f32, // Step-size (standard deviation)
pub tol: f32, // Tolerance for convergence, optional
pub zs: f32, // Enforce zero sparsity for quicker computational results, optional
pub n: f32, // Dimension of the problem space (xstart size)
pub mu: i32, // Number of parents (best individuals)
pub weights: DVector<f32>, // Weights for recombination
pub mueff: f32, // Effective number of parents
pub cc: f32, // Cumulation constant for the rank-one update
pub cs: f32, // Cumulation constant for the rank-mu update
pub c1: f32, // Learning rate for the rank-one update
pub cmu: f32, // Learning rate for the rank-mu update
pub damps: f32, // Damping for step-size adaptation
// pub lazy_gap_evals: f32, // Gap to postpone eigendecomposition
}
/// Trait defining validation methods for CMA-ES parameters.
pub trait CmaesParamsValidator {
type Output;
fn new() -> Result<Self::Output>;
// Fundamental
fn set_popsize(self, popsize: i32) -> Result<Self::Output>;
fn set_xstart(self, xstart: Vec<f32>) -> Result<Self::Output>;
fn set_sigma(self, sigma: f32) -> Result<Self::Output>;
// Helper
fn update_dependent_params(&mut self);
}
/// Trait for Validated Cmaes Params
impl CmaesParamsValidator for CmaesParams {
type Output = CmaesParams;
/// Creates default parameters for the CMA-ES algorithm based on the provided parameters.
///
/// Example
///
/// ```rust
/// use haru_cmaes::params::{CmaesParams, CmaesParamsValidator};
///
/// let params = CmaesParams::new();
///
/// assert!(params.is_ok());
/// ```
fn new() -> Result<Self::Output> {
let popsize: i32 = 10;
let xstart = vec![0.0; 6];
let sigma = 0.75;
let tol = 0.01;
let zs = 0.01;
// Update these after setters
let n = xstart.len() as f32;
let mu = popsize / 2;
let k = popsize as f32;
let iterable: Vec<f32> = (0..popsize)
.map(|x| {
if x < mu {
(k / 2.0 + 0.5).ln() - ((x + 1) as f32).ln()
} else {
0.0
}
})
.collect();
let weights: DVector<f32> = DVector::from_vec(iterable);
let w_sum: f32 = weights.rows(0, mu as usize).iter().sum();
let weights: DVector<f32> = weights.map(|x| x / w_sum);
let weights_mu = weights.rows(0, mu as usize).into_owned();
let mueff: f32 = (weights_mu.iter().sum::<f32>().powi(2)) / weights_mu.map(|x| x * x).sum();
let cc = (4. + mueff / n) / (n + 4. + 2. * mueff / n);
let cs = (mueff + 2.) / (n + mueff + 5.);
let c1 = 2. / ((n + 1.3) * (n + 1.3) + mueff);
let cmu = (1. - c1).min(2. * (mueff - 2. + 1. / mueff) / ((n + 2.) * (n + 2.) + mueff));
let damps = 2. * mueff / k + 0.3 + cs;
let params = CmaesParams {
// Fundamental
popsize,
xstart,
sigma,
// Objective's
tol,
// Computational's
zs,
// Others
n,
mu,
weights,
mueff,
cc,
cs,
c1,
cmu,
damps,
};
Ok(params)
}
/// Updates all parameters that depend on other fields.
fn update_dependent_params(&mut self) {
self.n = self.xstart.len() as f32;
self.mu = self.popsize / 2;
let k = self.popsize as f32;
let iterable: Vec<f32> = (0..self.popsize)
.map(|x| {
if x < self.mu {
(k / 2.0 + 0.5).ln() - ((x + 1) as f32).ln()
} else {
0.0
}
})
.collect();
let weights: DVector<f32> = DVector::from_vec(iterable);
let w_sum: f32 = weights.rows(0, self.mu as usize).iter().sum();
self.weights = weights.map(|x| x / w_sum);
let weights_mu = &self.weights.rows(0, self.mu as usize).into_owned();
self.mueff = (weights_mu.iter().sum::<f32>().powi(2)) / weights_mu.map(|x| x * x).sum();
self.cc = (4. + self.mueff / self.n) / (self.n + 4. + 2. * self.mueff / self.n);
self.cs = (self.mueff + 2.) / (self.n + self.mueff + 5.);
self.c1 = 2. / ((self.n + 1.3).powi(2) + self.mueff);
self.cmu = (1. - self.c1)
.min(2. * (self.mueff - 2. + 1. / self.mueff) / ((self.n + 2.).powi(2) + self.mueff));
self.damps = 2. * self.mueff / k + 0.3 + self.cs;
}
/// Sets population size
///
/// Example
///
/// ```rust
/// use haru_cmaes::params::{CmaesParams, CmaesParamsValidator};
///
/// let params = CmaesParams::new()
/// .and_then(|p| p.set_popsize(15));
///
/// assert!(params.is_ok());
/// ```
fn set_popsize(mut self, popsize: i32) -> Result<Self> {
self.popsize = popsize;
self.update_dependent_params();
Ok(self)
}
/// Sets origin of search
///
/// Example
///
/// ```rust
/// use haru_cmaes::params::{CmaesParams, CmaesParamsValidator};
///
/// let params = CmaesParams::new()
/// .and_then(|p| p.set_xstart(vec![0.0; 60]));
///
/// assert!(params.is_ok());
/// ```
fn set_xstart(mut self, xstart: Vec<f32>) -> Result<Self::Output> {
self.xstart = xstart;
self.update_dependent_params();
Ok(self)
}
/// Sets step size (sigma)
///
/// Example
///
/// ```rust
/// use haru_cmaes::params::{CmaesParams, CmaesParamsValidator};
///
/// let params = CmaesParams::new()
/// .and_then(|p| p.set_sigma(0.85));
///
/// assert!(params.is_ok());
/// ```
fn set_sigma(mut self, sigma: f32) -> Result<Self::Output> {
self.sigma = sigma;
Ok(self)
}
}