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
260
261
262
263
264
265
266
//! Traits for types that can be used as an objective function, as well as wrapper types that
//! modify the behavior of objective functions.
use DVector;
/// A trait for types that can be used as an objective function.
///
/// This trait is implemented for functions and closures with the correct signature, so the
/// following can all be used in a [`CMAES`][crate::CMAES]:
///
/// ```
/// use cmaes::DVector;
///
/// fn foo(x: &DVector<f64>) -> f64 { x.magnitude() }
/// let bar = |x: &DVector<f64>| x.magnitude();
/// ```
///
/// Objective functions do not necessarily have to be `'static`, so they may borrow other values:
///
/// ```
/// # use cmaes::{CMAESOptions, DVector};
/// let mut n = 0.0;
/// let baz = |_: &DVector<f64>| {
/// n += 1.0;
/// n
/// };
///
/// let mut state = CMAESOptions::new(vec![0.0; 2], 1.0).build(baz).unwrap();
/// ```
///
/// Or be references themselves:
///
/// ```
/// # use cmaes::{CMAESOptions, DVector};
/// # let mut n = 0.0;
/// # let mut baz = |_: &DVector<f64>| {
/// # n += 1.0;
/// # n
/// # };
/// let mut state = CMAESOptions::new(vec![0.0; 2], 1.0).build(&mut baz).unwrap();
/// ```
///
/// The trait may also be implemented for custom types:
///
/// ```
/// use cmaes::{CMAESOptions, DVector, ObjectiveFunction};
///
/// struct Custom {
/// counter: f64,
/// }
///
/// impl ObjectiveFunction for Custom {
/// fn evaluate(&mut self, x: &DVector<f64>) -> f64 {
/// self.counter += 1.0;
/// x.magnitude() + self.counter
/// }
/// }
///
/// impl<'a> ObjectiveFunction for &'a mut Custom {
/// fn evaluate(&mut self, x: &DVector<f64>) -> f64 {
/// Custom::evaluate(*self, x)
/// }
/// }
///
/// // The objective function's state can be retrieved after optimization
/// let mut custom = Custom { counter: 0.0 };
///
/// let mut cmaes_state = CMAESOptions::new(vec![0.0; 2], 1.0).build(&mut custom).unwrap();
/// let solution = cmaes_state.run();
///
/// println!("{}", custom.counter);
/// ```
///
/// Wrapper types for modifying objective functions are provided in the
/// [`objective_function`][crate::objective_function] module, and more can be implemented manually.
///
/// For objective functions that can be executed in parallel, see
/// [`ParallelObjectiveFunction`][ParallelObjectiveFunction].
/// Like [`ObjectiveFunction`][ObjectiveFunction], but for objective functions that can be executed
/// in parallel.
///
/// The key difference is that it requires [`Sync`][std::marker::Sync] and has a `&self` parameter
/// instead of `&mut self`, meaning that mutable state cannot be used except for with interior
/// mutability, such as through [`Mutex`][std::sync::Mutex] or [atomic types][std::sync::atomic].
///
/// The trait is implemented for functions and closures with the correct signature, so the following
/// can all be used in a [`CMAES`][crate::CMAES]:
///
/// ```
/// use cmaes::DVector;
///
/// fn foo(x: &DVector<f64>) -> f64 { x.magnitude() }
/// let bar = |x: &DVector<f64>| x.magnitude();
/// ```
///
/// It can also be implemented for custom types:
///
/// ```
/// use cmaes::{CMAESOptions, DVector, ObjectiveFunction, ParallelObjectiveFunction};
///
/// use std::sync::atomic::{AtomicUsize, Ordering};
///
/// struct Custom {
/// counter: AtomicUsize,
/// }
///
/// impl ParallelObjectiveFunction for Custom {
/// fn evaluate_parallel(&self, x: &DVector<f64>) -> f64 {
/// // Interior mutability must be used to modify any state
/// self.counter.fetch_add(1, Ordering::SeqCst);
/// x.magnitude()
/// }
/// }
///
/// impl<'a> ParallelObjectiveFunction for &'a Custom {
/// fn evaluate_parallel(&self, x: &DVector<f64>) -> f64 {
/// Custom::evaluate_parallel(*self, x)
/// }
/// }
///
/// // `ObjectiveFunction` can be implemented as well for use with `CMAES::run`
/// impl ObjectiveFunction for Custom {
/// fn evaluate(&mut self, x: &DVector<f64>) -> f64 {
/// self.evaluate_parallel(x)
/// }
/// }
///
/// impl<'a> ObjectiveFunction for &'a mut Custom {
/// fn evaluate(&mut self, x: &DVector<f64>) -> f64 {
/// self.evaluate_parallel(x)
/// }
/// }
///
/// // The objective function's state can be retrieved after optimization
/// let custom = Custom { counter: 0.into() };
///
/// let mut cmaes_state = CMAESOptions::new(vec![0.0; 2], 1.0).build(&custom).unwrap();
/// let solution = cmaes_state.run_parallel();
///
/// println!("{}", custom.counter.load(Ordering::SeqCst));
/// ```
/// A type that wraps any [`ObjectiveFunction`] and scales the input vectors before passing them to
/// the wrapped function.
///
/// Can be used to restrict or widen the the search space in one or more
/// dimensions in case it is known that the solution likely lies within a narrower or wider range in
/// them.
///
/// # Examples
///
/// ```
/// # use cmaes::{CMAESOptions, DVector};
/// use cmaes::objective_function::Scale;
///
/// let mut function = |x: &DVector<f64>| x.magnitude();
/// let scale = Scale::new(function, vec![0.2, 0.2, 1.0]);
///
/// let mut state = CMAESOptions::new(vec![0.0; 2], 1.0).build(scale).unwrap();
/// ```