use alloc::vec::Vec;
use rand_core::Rng;
use super::{Crossover, Initializer, Mutation, Repair};
use crate::rng::{below, chance, shuffle, standard_normal, uniform, unit_f64};
#[derive(Debug, Clone, PartialEq)]
pub struct Bounds {
lower: Vec<f64>,
upper: Vec<f64>,
}
impl Bounds {
#[must_use]
pub fn new(lower: Vec<f64>, upper: Vec<f64>) -> Self {
assert_eq!(lower.len(), upper.len(), "bounds of different dimension");
for (lo, hi) in lower.iter().zip(&upper) {
assert!(
lo.is_finite() && hi.is_finite() && lo <= hi,
"invalid bound [{lo}, {hi}]"
);
}
Self { lower, upper }
}
#[must_use]
pub fn uniform(dimension: usize, low: f64, high: f64) -> Self {
Self::new(alloc::vec![low; dimension], alloc::vec![high; dimension])
}
#[must_use]
pub fn dimension(&self) -> usize {
self.lower.len()
}
#[must_use]
pub fn lower(&self) -> &[f64] {
&self.lower
}
#[must_use]
pub fn upper(&self) -> &[f64] {
&self.upper
}
#[must_use]
pub fn width(&self, i: usize) -> f64 {
self.upper[i] - self.lower[i]
}
#[must_use]
pub fn contains(&self, x: &[f64]) -> bool {
x.len() == self.dimension()
&& x.iter()
.enumerate()
.all(|(i, v)| (self.lower[i]..=self.upper[i]).contains(v))
}
pub fn clamp(&self, x: &mut [f64]) {
for (i, v) in x.iter_mut().enumerate() {
*v = v.clamp(self.lower[i], self.upper[i]);
}
}
#[must_use]
pub fn center(&self) -> Vec<f64> {
self.lower
.iter()
.zip(&self.upper)
.map(|(lo, hi)| 0.5 * (lo + hi))
.collect()
}
}
impl Initializer<Vec<f64>> for Bounds {
fn initialize<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Vec<f64> {
(0..self.dimension())
.map(|i| uniform(rng, self.lower[i], self.upper[i]))
.collect()
}
}
pub fn latin_hypercube<R: Rng + ?Sized>(
bounds: &Bounds,
count: usize,
rng: &mut R,
) -> Vec<Vec<f64>> {
let mut points = alloc::vec![alloc::vec![0.0; bounds.dimension()]; count];
let mut strata: Vec<usize> = (0..count).collect();
#[expect(
clippy::cast_precision_loss,
reason = "sample counts are far below 2^52"
)]
let n = count as f64;
for i in 0..bounds.dimension() {
shuffle(rng, &mut strata);
for (point, &stratum) in points.iter_mut().zip(&strata) {
#[expect(
clippy::cast_precision_loss,
reason = "sample counts are far below 2^52"
)]
let offset = (stratum as f64 + unit_f64(rng)) / n;
point[i] = (bounds.lower[i] + offset * bounds.width(i)).min(bounds.upper[i]);
}
}
points
}
fn gene_rate(rate: Option<f64>, n: usize) -> f64 {
#[expect(clippy::cast_precision_loss, reason = "dimensions are far below 2^52")]
let default = 1.0 / n.max(1) as f64;
rate.unwrap_or(default)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Gaussian {
pub sigma: f64,
pub rate: Option<f64>,
}
impl Mutation<Vec<f64>> for Gaussian {
fn mutate<R: Rng + ?Sized>(&mut self, genome: &mut Vec<f64>, rng: &mut R) {
let rate = gene_rate(self.rate, genome.len());
for_some_genes(genome.len(), rate, rng, |i, rng| {
genome[i] += self.sigma * standard_normal(rng);
});
}
}
fn for_some_genes<R: Rng + ?Sized>(
n: usize,
rate: f64,
rng: &mut R,
mut f: impl FnMut(usize, &mut R),
) {
if n == 0 {
return;
}
let mut any = false;
for i in 0..n {
if chance(rng, rate) {
f(i, rng);
any = true;
}
}
if !any {
let i = below(rng, n);
f(i, rng);
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Polynomial {
pub bounds: Bounds,
pub eta: f64,
pub rate: Option<f64>,
}
impl Mutation<Vec<f64>> for Polynomial {
fn mutate<R: Rng + ?Sized>(&mut self, genome: &mut Vec<f64>, rng: &mut R) {
let rate = gene_rate(self.rate, genome.len());
let bounds = &self.bounds;
let eta = self.eta;
for_some_genes(genome.len(), rate, rng, |i, rng| {
genome[i] = polynomial_step(
genome[i],
bounds.lower[i],
bounds.upper[i],
eta,
unit_f64(rng),
);
});
}
}
fn polynomial_step(y: f64, low: f64, high: f64, eta: f64, u: f64) -> f64 {
let width = high - low;
if width <= 0.0 {
return low;
}
let power = 1.0 / (eta + 1.0);
let delta_q = if u <= 0.5 {
let xy = 1.0 - (y - low) / width;
let value = 2.0 * u + (1.0 - 2.0 * u) * libm::pow(xy, eta + 1.0);
libm::pow(value, power) - 1.0
} else {
let xy = 1.0 - (high - y) / width;
let value = 2.0 * (1.0 - u) + 2.0 * (u - 0.5) * libm::pow(xy, eta + 1.0);
1.0 - libm::pow(value, power)
};
(y + delta_q * width).clamp(low, high)
}
#[derive(Debug, Clone, PartialEq)]
pub struct UniformReset {
pub bounds: Bounds,
pub rate: Option<f64>,
}
impl Mutation<Vec<f64>> for UniformReset {
fn mutate<R: Rng + ?Sized>(&mut self, genome: &mut Vec<f64>, rng: &mut R) {
let rate = gene_rate(self.rate, genome.len());
let bounds = &self.bounds;
for_some_genes(genome.len(), rate, rng, |i, rng| {
genome[i] = uniform(rng, bounds.lower[i], bounds.upper[i]);
});
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Sbx {
pub bounds: Bounds,
pub eta: f64,
}
impl Crossover<Vec<f64>> for Sbx {
fn crossover<R: Rng + ?Sized>(
&mut self,
a: &Vec<f64>,
b: &Vec<f64>,
rng: &mut R,
) -> [Vec<f64>; 2] {
let mut c1 = a.clone();
let mut c2 = b.clone();
for i in 0..a.len().min(b.len()) {
if unit_f64(rng) > 0.5 || (a[i] - b[i]).abs() <= 1e-14 {
continue;
}
let (low, high) = (self.bounds.lower[i], self.bounds.upper[i]);
let (y1, y2) = if a[i] < b[i] {
(a[i], b[i])
} else {
(b[i], a[i])
};
let u = unit_f64(rng);
let lower_child = sbx_child(
y1,
y2,
1.0 + 2.0 * (y1 - low) / (y2 - y1),
self.eta,
u,
-1.0,
);
let upper_child = sbx_child(
y1,
y2,
1.0 + 2.0 * (high - y2) / (y2 - y1),
self.eta,
u,
1.0,
);
let (x1, x2) = (lower_child.clamp(low, high), upper_child.clamp(low, high));
if unit_f64(rng) < 0.5 {
c1[i] = x2;
c2[i] = x1;
} else {
c1[i] = x1;
c2[i] = x2;
}
}
[c1, c2]
}
}
fn sbx_child(y1: f64, y2: f64, beta: f64, eta: f64, u: f64, sign: f64) -> f64 {
let alpha = 2.0 - libm::pow(beta, -(eta + 1.0));
let beta_q = if u <= 1.0 / alpha {
libm::pow(u * alpha, 1.0 / (eta + 1.0))
} else {
libm::pow(1.0 / (2.0 - u * alpha), 1.0 / (eta + 1.0))
};
0.5 * ((y1 + y2) + sign * beta_q * (y2 - y1))
}
#[derive(Debug, Clone, PartialEq)]
pub struct Blx {
pub bounds: Bounds,
pub alpha: f64,
}
impl Crossover<Vec<f64>> for Blx {
fn crossover<R: Rng + ?Sized>(
&mut self,
a: &Vec<f64>,
b: &Vec<f64>,
rng: &mut R,
) -> [Vec<f64>; 2] {
let mut children = [a.clone(), b.clone()];
for child in &mut children {
for i in 0..a.len().min(b.len()) {
let (lo, hi) = (a[i].min(b[i]), a[i].max(b[i]));
let spread = self.alpha * (hi - lo);
let value = uniform(rng, lo - spread, hi + spread);
child[i] = value.clamp(self.bounds.lower[i], self.bounds.upper[i]);
}
}
children
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Arithmetic;
impl Crossover<Vec<f64>> for Arithmetic {
fn crossover<R: Rng + ?Sized>(
&mut self,
a: &Vec<f64>,
b: &Vec<f64>,
rng: &mut R,
) -> [Vec<f64>; 2] {
let lambda = unit_f64(rng);
let blend = |x: f64, y: f64, l: f64| l * x + (1.0 - l) * y;
[
a.iter()
.zip(b)
.map(|(&x, &y)| blend(x, y, lambda))
.collect(),
a.iter()
.zip(b)
.map(|(&x, &y)| blend(x, y, 1.0 - lambda))
.collect(),
]
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IsoLineDd {
pub sigma_iso: f64,
pub sigma_line: f64,
}
impl Crossover<Vec<f64>> for IsoLineDd {
fn crossover<R: Rng + ?Sized>(
&mut self,
a: &Vec<f64>,
b: &Vec<f64>,
rng: &mut R,
) -> [Vec<f64>; 2] {
let child = |x: &[f64], y: &[f64], rng: &mut R| {
let line = self.sigma_line * standard_normal(rng);
x.iter()
.zip(y)
.map(|(p, q)| p + self.sigma_iso * standard_normal(rng) + line * (q - p))
.collect()
};
[child(a, b, rng), child(b, a, rng)]
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Clamp(pub Bounds);
impl Repair<Vec<f64>> for Clamp {
fn repair<R: Rng + ?Sized>(&mut self, genome: &mut Vec<f64>, _rng: &mut R) {
self.0.clamp(genome);
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Reflect(pub Bounds);
impl Repair<Vec<f64>> for Reflect {
fn repair<R: Rng + ?Sized>(&mut self, genome: &mut Vec<f64>, _rng: &mut R) {
for (i, value) in genome.iter_mut().enumerate() {
*value = reflect(*value, self.0.lower[i], self.0.upper[i]);
}
}
}
fn reflect(x: f64, low: f64, high: f64) -> f64 {
let width = high - low;
if width <= 0.0 || !x.is_finite() {
return x.clamp(low, high);
}
let period = 2.0 * width;
let mut offset = (x - low) % period;
if offset < 0.0 {
offset += period;
}
if offset <= width {
low + offset
} else {
high - (offset - width)
}
}
#[cfg(test)]
mod tests {
use alloc::vec;
use super::*;
use crate::SplitMix64;
#[test]
fn bounded_operators_stay_inside() {
let bounds = Bounds::uniform(5, -1.0, 2.0);
let mut rng = SplitMix64::new(7);
let mut init = bounds.clone();
let mut sbx = Sbx {
bounds: bounds.clone(),
eta: 15.0,
};
let mut blx = Blx {
bounds: bounds.clone(),
alpha: 0.5,
};
let mut poly = Polynomial {
bounds: bounds.clone(),
eta: 20.0,
rate: Some(1.0),
};
for _ in 0..500 {
let a = init.initialize(&mut rng);
let b = init.initialize(&mut rng);
for mut child in sbx
.crossover(&a, &b, &mut rng)
.into_iter()
.chain(blx.crossover(&a, &b, &mut rng))
{
assert!(bounds.contains(&child), "{child:?}");
poly.mutate(&mut child, &mut rng);
assert!(bounds.contains(&child), "{child:?}");
}
}
}
#[test]
fn reflection_folds() {
assert!((reflect(2.5, 0.0, 2.0) - 1.5).abs() < 1e-12);
assert!((reflect(-0.5, 0.0, 2.0) - 0.5).abs() < 1e-12);
assert!((reflect(5.0, 0.0, 2.0) - 1.0).abs() < 1e-12);
assert!((reflect(1.25, 0.0, 2.0) - 1.25).abs() < 1e-12);
}
#[test]
fn sbx_of_equal_parents_is_identity() {
let mut sbx = Sbx {
bounds: Bounds::uniform(3, 0.0, 1.0),
eta: 15.0,
};
let p = vec![0.2, 0.5, 0.9];
assert_eq!(
sbx.crossover(&p, &p, &mut SplitMix64::new(1)),
[p.clone(), p]
);
}
#[test]
fn latin_hypercube_stratifies() {
let bounds = Bounds::uniform(3, -2.0, 2.0);
let points = latin_hypercube(&bounds, 10, &mut SplitMix64::new(3));
for i in 0..3 {
let mut strata: Vec<usize> = points
.iter()
.map(|p| {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "test"
)]
let s = ((p[i] + 2.0) / 4.0 * 10.0) as usize;
s
})
.collect();
strata.sort_unstable();
assert_eq!(strata, (0..10).collect::<Vec<_>>());
}
}
}