use alloc::vec::Vec;
use crate::MultiObjective;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Schaffer;
impl MultiObjective<Vec<f64>> for Schaffer {
fn objectives(&self) -> usize {
2
}
fn evaluate(&mut self, genome: &Vec<f64>, out: &mut [f64]) -> f64 {
let x = genome[0];
out[0] = x * x;
out[1] = (x - 2.0) * (x - 2.0);
0.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Zdt {
Zdt1,
Zdt2,
Zdt3,
}
impl Zdt {
#[must_use]
pub fn front(self, f1: f64) -> f64 {
self.shape(f1, 1.0)
}
fn shape(self, f1: f64, g: f64) -> f64 {
let ratio = f1 / g;
let h = match self {
Self::Zdt1 => 1.0 - libm::sqrt(ratio),
Self::Zdt2 => 1.0 - ratio * ratio,
Self::Zdt3 => {
1.0 - libm::sqrt(ratio) - ratio * libm::sin(10.0 * core::f64::consts::PI * f1)
}
};
g * h
}
}
impl MultiObjective<Vec<f64>> for Zdt {
fn objectives(&self) -> usize {
2
}
fn evaluate(&mut self, genome: &Vec<f64>, out: &mut [f64]) -> f64 {
let n = genome.len();
#[expect(clippy::cast_precision_loss, reason = "dimensions are far below 2^52")]
let g = if n > 1 {
1.0 + 9.0 * genome[1..].iter().sum::<f64>() / (n - 1) as f64
} else {
1.0
};
out[0] = genome[0];
out[1] = self.shape(genome[0], g);
0.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dtlz {
pub kind: DtlzKind,
pub objectives: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DtlzKind {
Dtlz1,
Dtlz2,
}
impl MultiObjective<Vec<f64>> for Dtlz {
fn objectives(&self) -> usize {
self.objectives
}
fn evaluate(&mut self, x: &Vec<f64>, out: &mut [f64]) -> f64 {
let m = self.objectives;
let tail = &x[m - 1..];
let half_pi = core::f64::consts::FRAC_PI_2;
match self.kind {
DtlzKind::Dtlz1 => {
#[expect(clippy::cast_precision_loss, reason = "dimensions are far below 2^52")]
let k = tail.len() as f64;
let g = 100.0
* (k + tail
.iter()
.map(|v| {
(v - 0.5) * (v - 0.5)
- libm::cos(20.0 * core::f64::consts::PI * (v - 0.5))
})
.sum::<f64>());
for (i, f) in out.iter_mut().enumerate().take(m) {
let mut value = f64::midpoint(1.0, g);
value *= x[..m - 1 - i].iter().product::<f64>();
if i > 0 {
value *= 1.0 - x[m - 1 - i];
}
*f = value;
}
}
DtlzKind::Dtlz2 => {
let g: f64 = tail.iter().map(|v| (v - 0.5) * (v - 0.5)).sum();
for (i, f) in out.iter_mut().enumerate().take(m) {
let mut value = 1.0 + g;
value *= x[..m - 1 - i]
.iter()
.map(|v| libm::cos(v * half_pi))
.product::<f64>();
if i > 0 {
value *= libm::sin(x[m - 1 - i] * half_pi);
}
*f = value;
}
}
}
0.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Knapsack {
pub items: Vec<(u32, u32, u32)>,
pub capacity: u32,
}
impl Knapsack {
#[must_use]
pub fn generate(n: usize, seed: u64) -> Self {
let mut rng = crate::SplitMix64::new(seed);
let mut draw = || 1 + u32::try_from(crate::below(&mut rng, 30)).expect("below 30");
let items: Vec<(u32, u32, u32)> = (0..n).map(|_| (draw(), draw(), draw())).collect();
let capacity = items.iter().map(|i| i.0).sum::<u32>() / 2;
Self { items, capacity }
}
#[must_use]
pub fn assess(&self, take: &[bool]) -> ([f64; 2], f64) {
let (mut weight, mut v1, mut v2) = (0, 0, 0);
for (&(w, a, b), _) in self.items.iter().zip(take).filter(|(_, t)| **t) {
weight += w;
v1 += a;
v2 += b;
}
(
[-f64::from(v1), -f64::from(v2)],
f64::from(weight.saturating_sub(self.capacity)),
)
}
}
impl MultiObjective<Vec<bool>> for Knapsack {
fn objectives(&self) -> usize {
2
}
fn evaluate(&mut self, genome: &Vec<bool>, out: &mut [f64]) -> f64 {
let (costs, violation) = self.assess(genome);
out.copy_from_slice(&costs);
violation
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Lotz;
impl MultiObjective<Vec<bool>> for Lotz {
fn objectives(&self) -> usize {
2
}
fn evaluate(&mut self, genome: &Vec<bool>, out: &mut [f64]) -> f64 {
let leading = genome.iter().take_while(|&&b| b).count();
let trailing = genome.iter().rev().take_while(|&&b| !b).count();
#[expect(clippy::cast_precision_loss, reason = "lengths are far below 2^52")]
{
out[0] = -(leading as f64);
out[1] = -(trailing as f64);
}
0.0
}
}
#[must_use]
pub fn enumerate_bits<M: MultiObjective<Vec<bool>>>(
problem: &mut M,
n: usize,
) -> Vec<(Vec<bool>, Vec<f64>, f64)> {
assert!(n <= 20, "too many genomes to enumerate");
let m = problem.objectives();
(0..1u32 << n)
.map(|mask| {
let genome: Vec<bool> = (0..n).map(|i| mask >> i & 1 == 1).collect();
let mut out = alloc::vec![0.0; m];
let violation = problem.evaluate(&genome, &mut out);
(genome, out, violation)
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fronts() {
let mut out = [0.0; 3];
let mut zdt = Zdt::Zdt1;
zdt.evaluate(&alloc::vec![0.25, 0.0, 0.0], &mut out[..2]);
assert!((out[1] - 0.5).abs() < 1e-12);
let mut dtlz2 = Dtlz {
kind: DtlzKind::Dtlz2,
objectives: 3,
};
dtlz2.evaluate(&alloc::vec![0.3, 0.7, 0.5, 0.5], &mut out);
assert!((out.iter().map(|f| f * f).sum::<f64>() - 1.0).abs() < 1e-12);
let mut dtlz1 = Dtlz {
kind: DtlzKind::Dtlz1,
objectives: 3,
};
dtlz1.evaluate(&alloc::vec![0.3, 0.7, 0.5, 0.5], &mut out);
assert!((out.iter().sum::<f64>() - 0.5).abs() < 1e-12);
}
}