Skip to content

Commit b819f00

Browse files
VolodymyrOrlovstefan-k
authored andcommitted
Switches CMA-ES to bulk_cost
1 parent 62d6e7a commit b819f00

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

argmin/src/solver/cma_es/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//! For details see [`CMAES`].
1111
1212
use crate::core::{
13-
ArgminFloat, CostFunction, Error, PopulationState, Problem, SerializeAlias, Solver, KV,
13+
ArgminFloat, CostFunction, Error, PopulationState, Problem, SerializeAlias, Solver, SyncAlias,
14+
KV,
1415
};
1516
use argmin_math::{
1617
ArgminAdd, ArgminArgsort, ArgminAxisIter, ArgminBroadcast, ArgminDiv, ArgminDot,
@@ -45,10 +46,10 @@ use std::ops::{AddAssign, MulAssign};
4546
/// }
4647
///
4748
/// impl CostFunction for Rosenbrock {
48-
/// type Param = Vec<f32>;
49-
/// type Output = f32;
49+
/// type Param = Vec<f32>;
50+
/// type Output = f32;
5051
///
51-
/// fn cost(&self, p: &Self::Param) -> Result<Self::Output, Error> {
52+
/// fn cost(&self, p: &Self::Param) -> Result<Self::Output, Error> {
5253
/// Ok(rosenbrock_2d(p, self.a, self.b))
5354
/// }
5455
/// }
@@ -203,11 +204,12 @@ where
203204

204205
impl<O, P, F> Solver<O, PopulationState<P, F, P::Array2D>> for CMAES<P, F>
205206
where
206-
O: CostFunction<Param = P, Output = F>,
207+
O: CostFunction<Param = P, Output = F> + SyncAlias,
207208
Vec<F>: ArgminArgsort,
208209
F: ArgminFloat + MulAssign + AddAssign + NumCast + ArgminDiv<P, P>,
209210
P: SerializeAlias
210211
+ Clone
212+
+ SyncAlias
211213
+ ArgminTransition
212214
+ ArgminSize<usize>
213215
+ ArgminZeroLike
@@ -251,12 +253,9 @@ where
251253

252254
state.population = Some(self.generate());
253255

254-
let fitness: Vec<F> = state
255-
.get_population()
256-
.unwrap()
257-
.row_iterator()
258-
.map(|p| problem.cost(&p).unwrap())
259-
.collect();
256+
let fitness: Vec<F> = problem
257+
.bulk_cost(&state.get_population().unwrap().row_iterator().collect())
258+
.unwrap();
260259

261260
let fitness_indices = fitness.argsort();
262261

@@ -411,7 +410,6 @@ mod tests {
411410
assert!(state.best_individual.is_some());
412411

413412
let solution = state.best_individual.unwrap();
414-
println!("{:?}", solution);
415413
assert!((solution[0] - 1.0).abs() <= precision);
416414
assert!((solution[1] - 1.0).abs() <= precision);
417415
}

media/book/src/concept.md

-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ There are three components needed for solving an optimization problem in argmin:
99
The [Executor](https://docs.rs/argmin/latest/argmin/core/struct.Executor.html) applies the solver to the optimization problem.
1010
It also accepts observers and checkpointing mechanisms, as well as an initial guess of the parameter vector, the cost function value at that initial guess, gradient, and so on.
1111

12-
<<<<<<< HEAD:media/book/src/concept.md
1312
A solver is anything that implements the [Solver](https://docs.rs/argmin/latest/argmin/core/trait.Solver.html) trait.
1413
This trait defines how the optimization algorithm is initialized, how a single iteration is performed and when and how to terminate the iterations.
15-
=======
16-
A solver is anything that implements the [Solver](https://docs.rs/argmin/latest/argmin/core/trait.Solver.html) trait. This trait defines how the optimization algorithm is initialized, how a single iteration is performed and when and how to terminate the iterations.
17-
>>>>>>> 90a03788 (Adds CMA-ES algorithm):docs/book/src/concept.md
1814

1915
The optimization problem needs to implement a subset of the traits
2016
[`CostFunction`](https://docs.rs/argmin/latest/argmin/core/trait.CostFunction.html),

0 commit comments

Comments
 (0)