Skip to main content

evolutionary_planning/
objective.rs

1//! What is optimised: [`Objective`] for one cost, [`MultiObjective`] for a cost vector with a
2//! constraint violation.
3
4/// A cost to minimise for each genome.
5///
6/// Implemented for every `FnMut(&G) -> f64`, so a closure is an objective. Evaluation may
7/// have side effects (a simulator, a counter); the algorithms call it exactly once per
8/// evaluated individual and count those calls against the budget.
9///
10/// ```
11/// use evolutionary_planning::Objective;
12///
13/// let mut sphere = |x: &Vec<f64>| x.iter().map(|v| v * v).sum::<f64>();
14/// assert_eq!(sphere.evaluate(&vec![1.0, 2.0]), 5.0);
15/// ```
16pub trait Objective<G: ?Sized> {
17    /// Returns the cost of `genome`; smaller is better. NaN is treated as `+∞`.
18    fn evaluate(&mut self, genome: &G) -> f64;
19}
20
21impl<G: ?Sized, F: FnMut(&G) -> f64> Objective<G> for F {
22    fn evaluate(&mut self, genome: &G) -> f64 {
23        self(genome)
24    }
25}
26
27/// Several costs to minimise at once, plus a constraint violation.
28///
29/// The violation is `0` for a feasible genome and grows with how far it is from feasibility
30/// (Jakob's rule: never flatten infeasibility). Constrained domination prefers feasible over
31/// infeasible genomes and, among infeasible ones, the smaller violation (Deb 2000).
32///
33/// ```
34/// use evolutionary_planning::{MultiObjective, multi_objective_fn};
35///
36/// // Schaffer's problem: minimise x^2 and (x - 2)^2.
37/// let mut schaffer = multi_objective_fn(2, |x: &f64, out: &mut [f64]| {
38///     out[0] = x * x;
39///     out[1] = (x - 2.0) * (x - 2.0);
40///     0.0
41/// });
42/// let mut out = [0.0; 2];
43/// assert_eq!(schaffer.evaluate(&1.0, &mut out), 0.0);
44/// assert_eq!(out, [1.0, 1.0]);
45/// ```
46pub trait MultiObjective<G: ?Sized> {
47    /// The number of objectives, `m >= 1`; constant for the lifetime of the problem.
48    fn objectives(&self) -> usize;
49
50    /// Writes the `m` costs of `genome` into `out` (of length [`MultiObjective::objectives`])
51    /// and returns its constraint violation (`0` if feasible, NaN treated as `+∞`).
52    fn evaluate(&mut self, genome: &G, out: &mut [f64]) -> f64;
53}
54
55impl<G: ?Sized, M: MultiObjective<G> + ?Sized> MultiObjective<G> for &mut M {
56    fn objectives(&self) -> usize {
57        (**self).objectives()
58    }
59
60    fn evaluate(&mut self, genome: &G, out: &mut [f64]) -> f64 {
61        (**self).evaluate(genome, out)
62    }
63}
64
65/// Turns a closure `FnMut(&G, &mut [f64]) -> f64` with `objectives` costs into a
66/// [`MultiObjective`].
67pub fn multi_objective_fn<G: ?Sized, F: FnMut(&G, &mut [f64]) -> f64>(
68    objectives: usize,
69    f: F,
70) -> MultiObjectiveFn<F> {
71    MultiObjectiveFn { objectives, f }
72}
73
74/// A closure used as a [`MultiObjective`]; created by [`multi_objective_fn`].
75#[derive(Debug, Clone, Copy)]
76pub struct MultiObjectiveFn<F> {
77    /// The number of objectives the closure writes.
78    objectives: usize,
79    /// The closure: writes the costs and returns the violation.
80    f: F,
81}
82
83impl<G: ?Sized, F: FnMut(&G, &mut [f64]) -> f64> MultiObjective<G> for MultiObjectiveFn<F> {
84    fn objectives(&self) -> usize {
85        self.objectives
86    }
87
88    fn evaluate(&mut self, genome: &G, out: &mut [f64]) -> f64 {
89        (self.f)(genome, out)
90    }
91}