use alloc::vec::Vec;
use rand_core::Rng;
use crate::evaluator::Evaluator;
use crate::operators::Initializer;
use crate::rng::{below, distinct_indices, standard_normal};
use crate::{Budget, Individual, Objective, Observer, Outcome, Stats, Stop};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Survival {
Comma,
Plus,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepSizes {
Global,
PerCoordinate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Recombination {
Intermediate,
Discrete,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
pub mu: usize,
pub rho: usize,
pub lambda: usize,
pub survival: Survival,
pub step_sizes: StepSizes,
pub recombination: Recombination,
pub initial_sigma: f64,
pub min_sigma: f64,
}
impl Default for Config {
fn default() -> Self {
Self {
mu: 15,
rho: 2,
lambda: 100,
survival: Survival::Comma,
step_sizes: StepSizes::PerCoordinate,
recombination: Recombination::Intermediate,
initial_sigma: 0.3,
min_sigma: 1e-300,
}
}
}
#[derive(Debug, Clone, PartialEq)]
struct Member {
x: Vec<f64>,
sigma: Vec<f64>,
cost: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Es {
pub config: Config,
pub bounds: crate::operators::real::Bounds,
}
impl Es {
#[must_use]
pub fn new(config: Config, bounds: crate::operators::real::Bounds) -> Self {
Self { config, bounds }
}
pub fn run<O, B, Obs, R>(
&mut self,
objective: &mut O,
budget: &mut B,
observer: &mut Obs,
rng: &mut R,
) -> Outcome<Vec<f64>>
where
O: Objective<Vec<f64>> + ?Sized,
B: Budget<Stats> + ?Sized,
Obs: Observer + ?Sized,
R: Rng + ?Sized,
{
self.validate();
let mut evaluator = Evaluator::new(objective, budget, observer);
let Some(mut parents) = self.initial_parents(&mut evaluator, rng) else {
return evaluator.outcome(Vec::new(), Stop::Budget);
};
loop {
let mut offspring = Vec::with_capacity(self.config.lambda);
for _ in 0..self.config.lambda {
let child = self.offspring(&parents, rng);
let Some(cost) = evaluator.evaluate(&child.x) else {
return evaluator.outcome(population(parents), Stop::Budget);
};
offspring.push(Member { cost, ..child });
}
if self.config.survival == Survival::Plus {
offspring.extend(parents);
}
offspring.sort_by(|a, b| a.cost.total_cmp(&b.cost));
offspring.truncate(self.config.mu);
parents = offspring;
evaluator.end_generation(parents[0].cost);
}
}
fn validate(&self) {
let c = &self.config;
assert!(
c.mu > 0 && c.lambda > 0 && c.rho > 0,
"mu, rho and lambda must be positive"
);
assert!(c.rho <= c.mu, "rho must not exceed mu");
assert!(
c.survival == Survival::Plus || c.lambda >= c.mu,
"comma selection needs lambda >= mu"
);
}
fn initial_parents<O, B, Obs, R>(
&mut self,
evaluator: &mut Evaluator<'_, Vec<f64>, O, B, Obs>,
rng: &mut R,
) -> Option<Vec<Member>>
where
O: Objective<Vec<f64>> + ?Sized,
B: Budget<Stats> + ?Sized,
Obs: Observer + ?Sized,
R: Rng + ?Sized,
{
let n = self.bounds.dimension();
let sigma: Vec<f64> = match self.config.step_sizes {
StepSizes::Global => {
let mean_width = (0..n).map(|i| self.bounds.width(i)).sum::<f64>() / dimension(n);
alloc::vec![self.config.initial_sigma * mean_width; 1]
}
StepSizes::PerCoordinate => (0..n)
.map(|i| self.config.initial_sigma * self.bounds.width(i))
.collect(),
};
let mut parents = Vec::with_capacity(self.config.mu);
for _ in 0..self.config.mu {
let x = self.bounds.initialize(rng);
let cost = evaluator.evaluate(&x)?;
parents.push(Member {
x,
sigma: sigma.clone(),
cost,
});
}
Some(parents)
}
fn offspring<R: Rng + ?Sized>(&self, parents: &[Member], rng: &mut R) -> Member {
let chosen = distinct_indices(rng, parents.len(), self.config.rho, None);
let mut child = self.recombine(parents, &chosen, rng);
self.mutate_sigma(&mut child.sigma, rng);
for (i, x) in child.x.iter_mut().enumerate() {
let sigma = if child.sigma.len() == 1 {
child.sigma[0]
} else {
child.sigma[i]
};
*x += sigma * standard_normal(rng);
}
let mut repair = crate::operators::real::Reflect(self.bounds.clone());
crate::operators::Repair::repair(&mut repair, &mut child.x, rng);
child
}
fn recombine<R: Rng + ?Sized>(
&self,
parents: &[Member],
chosen: &[usize],
rng: &mut R,
) -> Member {
let n = parents[0].x.len();
let k = parents[0].sigma.len();
let mut x = alloc::vec![0.0; n];
let mut sigma = alloc::vec![0.0; k];
match self.config.recombination {
Recombination::Intermediate => {
#[expect(clippy::cast_precision_loss, reason = "rho is tiny")]
let weight = 1.0 / chosen.len() as f64;
for &p in chosen {
x.iter_mut()
.zip(&parents[p].x)
.for_each(|(a, b)| *a += weight * b);
sigma
.iter_mut()
.zip(&parents[p].sigma)
.for_each(|(a, b)| *a += weight * b);
}
}
Recombination::Discrete => {
for (i, xi) in x.iter_mut().enumerate() {
*xi = parents[chosen[below(rng, chosen.len())]].x[i];
}
for (j, s) in sigma.iter_mut().enumerate() {
*s = parents[chosen[below(rng, chosen.len())]].sigma[j];
}
}
}
Member {
x,
sigma,
cost: f64::INFINITY,
}
}
fn mutate_sigma<R: Rng + ?Sized>(&self, sigma: &mut [f64], rng: &mut R) {
let n = dimension(self.bounds.dimension());
let floor = self.config.min_sigma;
if sigma.len() == 1 {
let tau = 1.0 / libm::sqrt(n);
sigma[0] = (sigma[0] * libm::exp(tau * standard_normal(rng))).max(floor);
return;
}
let tau_global = 1.0 / libm::sqrt(2.0 * n);
let tau_local = 1.0 / libm::sqrt(2.0 * libm::sqrt(n));
let global = tau_global * standard_normal(rng);
for s in sigma.iter_mut() {
*s = (*s * libm::exp(global + tau_local * standard_normal(rng))).max(floor);
}
}
}
fn dimension(n: usize) -> f64 {
#[expect(clippy::cast_precision_loss, reason = "dimensions are far below 2^52")]
let n = n.max(1) as f64;
n
}
fn population(members: Vec<Member>) -> Vec<Individual<Vec<f64>>> {
members
.into_iter()
.map(|m| Individual {
genome: m.x,
cost: m.cost,
})
.collect()
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OnePlusOne {
pub sigma: f64,
pub min_sigma: f64,
}
impl OnePlusOne {
pub fn run<O, B, Obs, R>(
&mut self,
start: Vec<f64>,
objective: &mut O,
budget: &mut B,
observer: &mut Obs,
rng: &mut R,
) -> Outcome<Vec<f64>>
where
O: Objective<Vec<f64>> + ?Sized,
B: Budget<Stats> + ?Sized,
Obs: Observer + ?Sized,
R: Rng + ?Sized,
{
let mut evaluator = Evaluator::new(objective, budget, observer);
let Some(mut current) = evaluator.individual(start) else {
return evaluator.outcome(Vec::new(), Stop::Budget);
};
let mut sigma = self.sigma;
loop {
let candidate: Vec<f64> = current
.genome
.iter()
.map(|x| x + sigma * standard_normal(rng))
.collect();
let Some(cost) = evaluator.evaluate(&candidate) else {
break;
};
if cost <= current.cost {
current = Individual {
genome: candidate,
cost,
};
sigma *= libm::exp(1.0 / 3.0);
} else {
sigma = (sigma * libm::exp(-1.0 / 12.0)).max(self.min_sigma);
}
evaluator.end_generation(current.cost);
}
evaluator.outcome(alloc::vec![current], Stop::Budget)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::operators::real::Bounds;
use crate::problems::single::{ellipsoid, rosenbrock, sphere};
use crate::{Limits, SplitMix64};
#[test]
fn variants_converge_on_sphere() {
for survival in [Survival::Comma, Survival::Plus] {
for step_sizes in [StepSizes::Global, StepSizes::PerCoordinate] {
for recombination in [Recombination::Intermediate, Recombination::Discrete] {
let config = Config {
survival,
step_sizes,
recombination,
..Config::default()
};
let mut es = Es::new(config, Bounds::uniform(4, -5.0, 5.0));
let mut objective = |x: &Vec<f64>| sphere(x);
let outcome = es.run(
&mut objective,
&mut Limits::evaluations(30_000),
&mut (),
&mut SplitMix64::new(3),
);
assert!(
outcome.best_cost() < 1e-6,
"{config:?}: {}",
outcome.best_cost()
);
}
}
}
}
#[test]
fn per_coordinate_on_ellipsoid() {
let mut es = Es::new(Config::default(), Bounds::uniform(5, -3.0, 3.0));
let mut objective = |x: &Vec<f64>| ellipsoid(x);
let outcome = es.run(
&mut objective,
&mut Limits::evaluations(100_000),
&mut (),
&mut SplitMix64::new(1),
);
assert!(outcome.best_cost() < 1e-6, "{}", outcome.best_cost());
}
#[test]
fn one_plus_one() {
let mut es = OnePlusOne {
sigma: 1.0,
min_sigma: 1e-300,
};
let mut objective = |x: &Vec<f64>| sphere(x);
let outcome = es.run(
alloc::vec![3.0; 10],
&mut objective,
&mut Limits::evaluations(5_000),
&mut (),
&mut SplitMix64::new(2),
);
assert!(outcome.best_cost() < 1e-10, "{}", outcome.best_cost());
let mut objective = |x: &Vec<f64>| rosenbrock(x);
let outcome = es.run(
alloc::vec![0.0; 2],
&mut objective,
&mut Limits::evaluations(20_000),
&mut (),
&mut SplitMix64::new(2),
);
assert!(outcome.best_cost() < 1e-6, "{}", outcome.best_cost());
}
#[test]
fn deterministic() {
let run = || {
let mut es = Es::new(Config::default(), Bounds::uniform(3, -1.0, 1.0));
let mut objective = |x: &Vec<f64>| sphere(x);
es.run(
&mut objective,
&mut Limits::evaluations(777),
&mut (),
&mut SplitMix64::new(9),
)
};
assert_eq!(run(), run());
assert_eq!(run().stats.evaluations, 777);
}
}