multiobjective_search/mvh.rs
1//! Multi-valued heuristics: a set of cost-to-go estimates per state.
2
3mod table;
4
5use alloc::vec::Vec;
6
7use search_core::Heuristic;
8
9pub use table::{MvhTable, apex_mvh};
10
11/// A multi-valued heuristic (MVH): for every state, a set of cost vectors
12/// that bound the remaining cost to a goal from below.
13///
14/// Wolff, Felner, Salzman, "Bridging Multi-Valued Heuristics and
15/// Dimensionality Reduction in Multi-Objective Search", arXiv 2606.20644.
16///
17/// - **Admissible:** for every path from `state` to a goal, some estimate
18/// weakly dominates its cost. In particular every goal has the zero
19/// vector among its estimates.
20/// - **Consistent** (monotone): every goal's estimates are `{0}`, and for
21/// every edge `(s, t)` with cost `c` and every estimate `h'` of `t`, some
22/// estimate `h` of `s` has `h <= c + h'` component-wise (Mandow and Pérez
23/// de la Cruz 2010, Definition 5.7). The exact Pareto sets of the
24/// remaining cost are consistent in this sense.
25///
26/// Wolff et al. state consistency with the quantifiers the other way round
27/// (every `h` of `s` has some `h'` of `t`), which their own Table 1 and the
28/// exact Pareto sets do not satisfy. What
29/// [`namoa_star_dr_mvh`](crate::namoa_star_dr_mvh) needs is that every path
30/// to a goal carries a chain of estimates `h_k <= c_k + h_(k+1)` ending in
31/// the goal's `0`; either form provides one.
32///
33/// A single-valued [`Heuristic`] is the special case of one estimate per
34/// state; wrap it in [`SingleValued`]. Implemented for closures through
35/// [`mv_heuristic_fn`] and for `&mut M`.
36///
37/// # Examples
38///
39/// ```
40/// use multiobjective_search::{MvHeuristic, mv_heuristic_fn};
41///
42/// // Two trade-offs from state 0, the goal 1 is free.
43/// let mut h = mv_heuristic_fn(|s: &u8, out: &mut Vec<[u32; 2]>| match s {
44/// 0 => out.extend([[1, 5], [4, 2]]),
45/// _ => out.push([0, 0]),
46/// });
47///
48/// let mut out = Vec::new();
49/// h.estimates(&0, &mut out);
50/// assert_eq!(out, [[1, 5], [4, 2]]);
51/// ```
52pub trait MvHeuristic<S, V> {
53 /// Appends the estimates of `state` to `out`.
54 ///
55 /// `out` is empty on entry. Appending nothing marks `state` as a dead
56 /// end from which no goal is reachable. The estimates need not be
57 /// mutually non-dominated or sorted; the searches handle both.
58 fn estimates(&mut self, state: &S, out: &mut Vec<V>);
59}
60
61impl<S, V, M: MvHeuristic<S, V> + ?Sized> MvHeuristic<S, V> for &mut M {
62 fn estimates(&mut self, state: &S, out: &mut Vec<V>) {
63 (**self).estimates(state, out);
64 }
65}
66
67/// A single-valued [`Heuristic`] as a [`MvHeuristic`] with one estimate
68/// per state, none for a dead end.
69///
70/// Admissible or consistent exactly when the wrapped heuristic is.
71#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
72pub struct SingleValued<H>(pub H);
73
74impl<S, V, H: Heuristic<S, V>> MvHeuristic<S, V> for SingleValued<H> {
75 fn estimates(&mut self, state: &S, out: &mut Vec<V>) {
76 out.extend(self.0.estimate(state));
77 }
78}
79
80/// A [`MvHeuristic`] backed by a closure; see [`mv_heuristic_fn`].
81#[derive(Debug, Clone, Copy)]
82pub struct MvHeuristicFn<F>(F);
83
84impl<S, V, F: FnMut(&S, &mut Vec<V>)> MvHeuristic<S, V> for MvHeuristicFn<F> {
85 fn estimates(&mut self, state: &S, out: &mut Vec<V>) {
86 (self.0)(state, out);
87 }
88}
89
90/// Wraps a closure `|state, out| ...` that appends the estimates of
91/// `state` to `out` as a [`MvHeuristic`].
92pub const fn mv_heuristic_fn<S, V, F: FnMut(&S, &mut Vec<V>)>(f: F) -> MvHeuristicFn<F> {
93 MvHeuristicFn(f)
94}