use alloc::vec::Vec;
use rand_core::Rng;
use crate::multi::{MultiEvaluator, MultiOutcome, first_front};
use crate::operators::{Crossover, Initializer, Mutation, Repair, Variation};
use crate::pareto::{Evaluated, ParetoArchive};
use crate::rng::{below, chance, shuffle};
use crate::{Budget, MultiObjective, Observer, Stats, Stop};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Decomposition {
WeightedSum,
Tchebycheff,
Pbi {
theta: f64,
},
}
impl Decomposition {
#[must_use]
pub fn cost(&self, f: &[f64], lambda: &[f64], ideal: &[f64]) -> f64 {
match *self {
Self::WeightedSum => f.iter().zip(lambda).map(|(a, w)| a * w).sum(),
Self::Tchebycheff => f
.iter()
.zip(lambda)
.zip(ideal)
.map(|((a, w), z)| w.max(1e-6) * (a - z).abs())
.fold(f64::NEG_INFINITY, f64::max),
Self::Pbi { theta } => {
let norm = libm::sqrt(lambda.iter().map(|w| w * w).sum());
let d1 = f
.iter()
.zip(ideal)
.zip(lambda)
.map(|((a, z), w)| (a - z) * w)
.sum::<f64>()
/ norm;
let d2 = libm::sqrt(
f.iter()
.zip(ideal)
.zip(lambda)
.map(|((a, z), w)| {
let off = a - (z + d1 * w / norm);
off * off
})
.sum(),
);
d1 + theta * d2
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Config {
pub neighbourhood: usize,
pub delta: f64,
pub max_replacements: usize,
pub decomposition: Decomposition,
pub crossover_rate: f64,
pub mutation_rate: f64,
pub archive: Option<usize>,
}
impl Default for Config {
fn default() -> Self {
Self {
neighbourhood: 20,
delta: 0.9,
max_replacements: 2,
decomposition: Decomposition::Tchebycheff,
crossover_rate: 1.0,
mutation_rate: 1.0,
archive: None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Moead<I, X, M, P> {
pub config: Config,
pub weights: Vec<Vec<f64>>,
pub variation: Variation<I, X, M, P>,
}
impl<I, X, M, P> Moead<I, X, M, P> {
#[must_use]
pub fn new(config: Config, weights: Vec<Vec<f64>>, variation: Variation<I, X, M, P>) -> Self {
Self {
config,
weights,
variation,
}
}
pub fn run<G, Pr, B, Obs, R>(
&mut self,
problem: &mut Pr,
budget: &mut B,
observer: &mut Obs,
rng: &mut R,
) -> MultiOutcome<G>
where
G: Clone,
I: Initializer<G>,
X: Crossover<G>,
M: Mutation<G>,
P: Repair<G>,
Pr: MultiObjective<G> + ?Sized,
B: Budget<Stats> + ?Sized,
Obs: Observer + ?Sized,
R: Rng + ?Sized,
{
let n = self.weights.len();
assert!(n >= 2, "MOEA/D needs at least two weight vectors");
assert!(
self.weights.iter().all(|w| w.len() == problem.objectives()),
"weights must have one coordinate per objective"
);
let neighbours = neighbourhoods(&self.weights, self.config.neighbourhood.clamp(2, n));
let mut archive = self.config.archive.map(|cap| ParetoArchive::new(Some(cap)));
let mut evaluator = MultiEvaluator::new(problem, budget, observer);
let mut population = Vec::with_capacity(n);
for _ in 0..n {
let Some(member) = evaluator.evaluate(self.variation.init.initialize(rng)) else {
return finish(&evaluator, population, archive);
};
offer(&mut archive, &member);
population.push(member);
}
let mut ideal = ideal_point(&population);
evaluator.end_generation();
loop {
for neighbourhood in &neighbours {
let pool: Vec<usize> = if chance(rng, self.config.delta) {
neighbourhood.clone()
} else {
(0..n).collect()
};
let child = self.child(&population, &pool, rng);
let Some(child) = evaluator.evaluate(child) else {
return finish(&evaluator, population, archive);
};
for (z, f) in ideal.iter_mut().zip(&child.objectives) {
*z = z.min(*f);
}
offer(&mut archive, &child);
self.replace(&mut population, &child, pool, &ideal, rng);
}
evaluator.end_generation();
}
}
fn child<G, R>(&mut self, population: &[Evaluated<G>], pool: &[usize], rng: &mut R) -> G
where
G: Clone,
X: Crossover<G>,
M: Mutation<G>,
P: Repair<G>,
R: Rng + ?Sized,
{
let a = pool[below(rng, pool.len())];
let b = pool[below(rng, pool.len())];
let [child, _] = self.variation.breed(
&population[a].genome,
&population[b].genome,
self.config.crossover_rate,
self.config.mutation_rate,
rng,
);
child
}
fn replace<G: Clone, R: Rng + ?Sized>(
&self,
population: &mut [Evaluated<G>],
child: &Evaluated<G>,
mut pool: Vec<usize>,
ideal: &[f64],
rng: &mut R,
) {
shuffle(rng, &mut pool);
let mut replaced = 0;
for j in pool {
if replaced >= self.config.max_replacements {
break;
}
let current = &population[j];
let better = if child.violation.total_cmp(¤t.violation).is_ne() {
child.violation < current.violation
} else {
let d = &self.config.decomposition;
d.cost(&child.objectives, &self.weights[j], ideal)
<= d.cost(¤t.objectives, &self.weights[j], ideal)
};
if better {
population[j] = child.clone();
replaced += 1;
}
}
}
}
fn neighbourhoods(weights: &[Vec<f64>], t: usize) -> Vec<Vec<usize>> {
weights
.iter()
.map(|w| {
let mut order: Vec<(f64, usize)> = weights
.iter()
.enumerate()
.map(|(j, v)| {
(
w.iter().zip(v).map(|(a, b)| (a - b) * (a - b)).sum::<f64>(),
j,
)
})
.collect();
order.sort_by(|a, b| a.0.total_cmp(&b.0).then(a.1.cmp(&b.1)));
order.into_iter().take(t).map(|(_, j)| j).collect()
})
.collect()
}
fn ideal_point<G>(population: &[Evaluated<G>]) -> Vec<f64> {
let m = population[0].objectives.len();
(0..m)
.map(|j| {
population
.iter()
.map(|e| e.objectives[j])
.fold(f64::INFINITY, f64::min)
})
.collect()
}
fn offer<G: Clone>(archive: &mut Option<ParetoArchive<G>>, member: &Evaluated<G>) {
if let Some(archive) = archive {
archive.insert(
member.genome.clone(),
member.objectives.clone(),
member.violation,
);
}
}
fn finish<G: Clone, Pr: ?Sized, B, Obs>(
evaluator: &MultiEvaluator<'_, Pr, B, Obs>,
population: Vec<Evaluated<G>>,
archive: Option<ParetoArchive<G>>,
) -> MultiOutcome<G>
where
B: Budget<Stats> + ?Sized,
Obs: Observer + ?Sized,
{
let mut outcome = evaluator.outcome(population, Stop::Budget);
if let Some(archive) = archive {
outcome.front = first_front(archive.members());
}
outcome
}
#[cfg(test)]
mod tests {
use super::*;
use crate::nsga2::tests::{brute_front, distinct};
use crate::operators::NoRepair;
use crate::operators::discrete::{BitFlip, RandomBits};
use crate::operators::sequence::UniformCrossover;
use crate::pareto::das_dennis;
use crate::problems::multi::Knapsack;
use crate::{Limits, SplitMix64};
#[test]
fn knapsack_against_brute_force() {
let mut problem = Knapsack::generate(12, 7);
let expected = brute_front(&mut problem, 12);
for decomposition in [
Decomposition::Tchebycheff,
Decomposition::Pbi { theta: 5.0 },
Decomposition::WeightedSum,
] {
let config = Config {
decomposition,
archive: Some(200),
..Config::default()
};
let mut moead = Moead::new(
config,
das_dennis(2, 59),
Variation {
init: RandomBits { len: 12 },
crossover: UniformCrossover::default(),
mutation: BitFlip::default(),
repair: NoRepair,
},
);
let outcome = moead.run(
&mut problem,
&mut Limits::evaluations(30_000),
&mut (),
&mut SplitMix64::new(4),
);
let found = distinct(&outcome.front);
assert!(
found.iter().all(|p| expected.contains(p)),
"{decomposition:?}: {found:?}"
);
if decomposition == Decomposition::Tchebycheff {
assert_eq!(found, expected);
}
}
}
#[test]
fn scalarisations() {
let f = [2.0, 1.0];
let w = [0.5, 0.5];
let z = [0.0, 0.0];
assert_eq!(Decomposition::WeightedSum.cost(&f, &w, &z), 1.5);
assert_eq!(Decomposition::Tchebycheff.cost(&f, &w, &z), 1.0);
let pbi = Decomposition::Pbi { theta: 5.0 }.cost(&f, &w, &z);
let d1 = 1.5 / libm::sqrt(0.5);
let d2 = libm::sqrt(0.5);
assert!((pbi - (d1 + 5.0 * d2)).abs() < 1e-12);
}
}