multiobjective-search 0.1.0

Multi-objective shortest paths over Pareto frontiers: BOA*, BOBA*, EMOA*, NAMOA*dr, A*pex and their approximate, anytime and parallel variants
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! TPA: an exact anytime method for multi-objective combinatorial problems
//! that splits the objective space into boxes and solves one
//! augmented-Tchebycheff subproblem per box.
//!
//! Domínguez-Ríos, Chicano, Alba, "Effective anytime algorithm for
//! multiobjective combinatorial optimization problems", arXiv 2403.08807
//! (Algorithms 2 to 4).

use alloc::collections::{BTreeMap, BinaryHeap};
use alloc::vec::Vec;
use core::cmp::Reverse;

use search_core::{BigUint, Budget, Cost, ExactArith, ExactNumber, Heuristic};

use crate::anytime_boa::{AnytimeFront, ApproxFactor};
use crate::cost::{add, weakly_dominates};
use crate::graph::Graph;
use crate::output::{Solution, Stats};
use crate::pareto::Front;

/// One augmented-Tchebycheff subproblem `P(u, ω)` (Algorithm 2, line 7) in
/// exact integer form.
///
/// With `ω_i = 1 / d_i` and `ϵ = 1 / K`, the objective
/// `max_i ω_i (f_i - z^I_i) + ϵ Σ_i ω_i (f_i - z^I_i)` times `K · Π d_i`
/// is the integer [`Scalarization::score`]
/// `K · max_i W_i (f_i - z^I_i) + Σ_i W_i (f_i - z^I_i)` with
/// `W_i = Π_j d_j / d_i`, and the paper's test `obj < 1` is
/// `score < `[`Scalarization::limit`].
#[derive(Debug, Clone)]
pub struct Scalarization<C, const N: usize> {
    /// The ideal point `z^I`.
    ideal: [C; N],
    /// `W_i`.
    weights: [BigUint; N],
    /// `K = 1 / ϵ`.
    k: BigUint,
    /// `K · Π d_i`.
    limit: BigUint,
}

impl<C: ExactArith<Exact = BigUint>, const N: usize> Scalarization<C, N> {
    /// The integer objective of a cost vector; components below the ideal
    /// point count as zero, which keeps it a lower bound for partial costs.
    #[must_use]
    pub fn score(&self, cost: &[C; N]) -> BigUint {
        let gaps: [BigUint; N] = core::array::from_fn(|i| {
            if cost[i] > self.ideal[i] {
                self.weights[i].clone() * cost[i].exact().abs_diff(&self.ideal[i].exact())
            } else {
                BigUint::zero()
            }
        });
        let max = gaps.iter().max().cloned().unwrap_or_else(BigUint::zero);
        let sum = gaps.into_iter().fold(BigUint::zero(), |s, g| s + g);

        self.k.clone() * max + sum
    }

    /// The score below which a point lies in the box (the paper's `obj < 1`).
    #[must_use]
    pub const fn limit(&self) -> &BigUint {
        &self.limit
    }
}

/// A single-objective solver for TPA's subproblems: the solution with the
/// smallest [`Scalarization::score`] over the whole feasible set, with its
/// cost vector, or `None` if there is none.
pub trait TchebycheffSolver<C, const N: usize> {
    /// The solution type.
    type Solution;

    /// Minimizes `scalarization.score` over all solutions.
    fn minimize(&mut self, scalarization: &Scalarization<C, N>)
    -> Option<(Self::Solution, [C; N])>;
}

/// What [`tpa`] returns: the solutions with their points, and whether the
/// front is complete.
pub type TpaResult<X, C, const N: usize> = (Vec<(X, [C; N])>, bool);

/// Adjacency lists of an explored graph.
type Adjacency<S, W> = BTreeMap<S, Vec<(S, W)>>;

/// A search zone: the half-open box `[lower, upper)` with a direction and
/// a priority.
#[derive(Debug, Clone)]
struct Zone<C, const N: usize> {
    /// Lower bound.
    lower: [C; N],
    /// Upper bound, exclusive.
    upper: [C; N],
    /// The numerator of the paper's (reduced) scaled priority; the
    /// denominator `Π (z^N_i - z^I_i)` is the same for every zone.
    priority: BigUint,
}

/// TPA (Algorithm 2) over any solver: the exact Pareto front of the problem
/// whose ideal point is `ideal` and whose costs all lie below `nadir`
/// (strictly, in every objective), found box by box.
///
/// The objective space starts as one box `[ideal, nadir)`. Each step
/// selects a box from the next direction's queue in turn (Algorithm 3), by
/// highest priority, and solves the augmented-Tchebycheff subproblem with
/// weights `ω_i = 1 / max(1, u_i - z^I_i)` and `ϵ = 1 / (2p(r - 1))`
/// (`r` the widest range). A score below the limit means a new
/// non-dominated point inside the box (Holzmann and Smith's Theorem 5); it
/// is reported, and every box whose upper bound lies above it is split by
/// the p-partition (Definition 1, Algorithm 4), boxes with comparable
/// upper bounds are joined (Definition 3), and the new boxes get the
/// reduced scaled priority (Definition 4). Otherwise the box is dropped.
/// Every point found is efficient, none twice, and the front is complete
/// when no box is left (Theorem 1).
///
/// `on_point` gets each new solution and point; `budget` sees the solver
/// and is checked before each subproblem.
///
/// Returns the solutions found and whether the front is complete.
///
/// # Panics
///
/// Panics if `ideal` is not below `nadir` in every objective.
pub fn tpa<C, S, const N: usize>(
    solver: &mut S,
    ideal: [C; N],
    nadir: [C; N],
    mut on_point: impl FnMut(&S::Solution, [C; N]),
    mut budget: impl FnMut(&S) -> bool,
) -> TpaResult<S::Solution, C, N>
where
    C: Cost + ExactArith<Exact = BigUint>,
    S: TchebycheffSolver<C, N>,
{
    assert!(
        (0..N).all(|i| ideal[i] < nadir[i]),
        "the ideal point must lie below the nadir bound"
    );
    let e = |c: C| c.exact();
    let range = |i: usize| e(nadir[i]).abs_diff(&e(ideal[i]));
    let widest = (0..N).map(range).max().unwrap_or_else(BigUint::one);
    let p = BigUint::from(u128::try_from(2 * N).expect("small"));
    // K = 2p(r - 1), at least 1.
    let k = (p * widest.abs_diff(&BigUint::one())).max(BigUint::one());

    let mut queues: Vec<Vec<Zone<C, N>>> = (0..N).map(|_| Vec::new()).collect();
    let whole = Zone {
        lower: ideal,
        upper: nadir,
        priority: volume(&ideal, &nadir),
    };
    queues[0].push(whole);
    let mut direction = N - 1;
    let mut found = Vec::new();

    while queues.iter().any(|q| !q.is_empty()) {
        if budget(solver) {
            return (found, false);
        }

        // Algorithm 3: the next non-empty direction, its highest priority.
        direction = (direction + 1) % N;
        while queues[direction].is_empty() {
            direction = (direction + 1) % N;
        }

        let pick = (0..queues[direction].len())
            .max_by(|&a, &b| {
                queues[direction][a]
                    .priority
                    .cmp(&queues[direction][b].priority)
            })
            .expect("non-empty");
        let upper = queues[direction][pick].upper;
        let scalarization = scalarize(&ideal, &upper, &k);

        let inside = (0..N).all(|i| ideal[i] < upper[i]);
        match solver.minimize(&scalarization) {
            Some((solution, point))
                if inside && scalarization.score(&point) < scalarization.limit =>
            {
                on_point(&solution, point);
                update(&mut queues, &point);
                found.push((solution, point));
            }
            _ => {
                queues[direction].swap_remove(pick);
            }
        }
    }

    (found, true)
}

/// The subproblem for a box with upper bound `upper`.
fn scalarize<C: Cost + ExactArith<Exact = BigUint>, const N: usize>(
    ideal: &[C; N],
    upper: &[C; N],
    k: &BigUint,
) -> Scalarization<C, N> {
    let d: [BigUint; N] = core::array::from_fn(|i| {
        upper[i]
            .exact()
            .abs_diff(&ideal[i].exact())
            .max(BigUint::one())
    });
    let product = d.iter().fold(BigUint::one(), |p, x| p * x.clone());
    let weights = core::array::from_fn(|i| {
        d.iter()
            .enumerate()
            .filter(|&(j, _)| j != i)
            .fold(BigUint::one(), |p, (_, x)| p * x.clone())
    });

    Scalarization {
        ideal: *ideal,
        weights,
        k: k.clone(),
        limit: k.clone() * product,
    }
}

/// `Π (upper_i - lower_i)`, zero for an empty box.
fn volume<C: Cost + ExactArith<Exact = BigUint>, const N: usize>(
    lower: &[C; N],
    upper: &[C; N],
) -> BigUint {
    if (0..N).any(|i| lower[i] >= upper[i]) {
        return BigUint::zero();
    }

    (0..N).fold(BigUint::one(), |p, i| {
        p * upper[i].exact().abs_diff(&lower[i].exact())
    })
}

/// Algorithm 4: p-partition of every box whose upper bound lies above `z`,
/// then joins of boxes with comparable upper bounds per direction.
fn update<C: Cost + ExactArith<Exact = BigUint>, const N: usize>(
    queues: &mut [Vec<Zone<C, N>>],
    z: &[C; N],
) {
    let boxes: Vec<Zone<C, N>> = queues
        .iter_mut()
        .flat_map(|q| {
            let (split, kept): (Vec<_>, Vec<_>) = core::mem::take(q)
                .into_iter()
                .partition(|b| (0..N).all(|i| z[i] < b.upper[i]));
            *q = kept;
            split
        })
        .collect();

    for b in boxes {
        let hat: [C; N] = core::array::from_fn(|i| z[i].max(b.lower[i]));
        for (i, queue) in queues.iter_mut().enumerate() {
            // B_i(z) = [(l_1..l_i, ẑ_{i+1}..ẑ_p), (u_1..u_{i-1}, ẑ_i, u_{i+1}..u_p)].
            let lower = core::array::from_fn(|j| if j <= i { b.lower[j] } else { hat[j] });
            let upper = core::array::from_fn(|j| if j == i { hat[j] } else { b.upper[j] });
            let mut priority = volume(&lower, &upper);
            if priority == BigUint::zero() {
                continue;
            }

            if i == N - 1 {
                // The part [l, z] cannot hold a non-dominated point.
                priority = priority.abs_diff(&volume(&b.lower, z));
            }

            queue.push(Zone {
                lower,
                upper,
                priority,
            });
        }
    }

    for queue in queues.iter_mut() {
        join(queue);
    }
}

/// Joins boxes with comparable upper bounds (Definition 3) until none are.
fn join<C: Cost + ExactArith<Exact = BigUint>, const N: usize>(queue: &mut Vec<Zone<C, N>>) {
    'outer: loop {
        for a in 0..queue.len() {
            for b in 0..queue.len() {
                if a != b && weakly_dominates(&queue[a].upper, &queue[b].upper) {
                    let (small, large) = (queue[a].clone(), queue[b].clone());
                    let lower = core::array::from_fn(|i| small.lower[i].min(large.lower[i]));
                    let priority = volume(&lower, &large.upper);
                    let (hi, lo) = (a.max(b), a.min(b));
                    queue.swap_remove(hi);
                    queue.swap_remove(lo);
                    queue.push(Zone {
                        lower,
                        upper: large.upper,
                        priority,
                    });
                    continue 'outer;
                }
            }
        }

        return;
    }
}

/// The TPA subproblem on a graph: the path to a goal that minimizes the
/// scalarized score, by best-first search on the score of `g + h` with
/// per-state dominance pruning.
pub struct PathTchebycheff<'a, G: Graph, H, P> {
    /// The graph.
    graph: &'a G,
    /// An admissible heuristic.
    heuristic: H,
    /// The start state.
    start: <G as Graph>::State,
    /// Goal test.
    is_goal: P,
    /// Effort counters over all subproblems.
    stats: Stats,
}

impl<G: Graph, H, P> core::fmt::Debug for PathTchebycheff<'_, G, H, P> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("PathTchebycheff")
            .field("stats", &self.stats)
            .finish_non_exhaustive()
    }
}

impl<G, H, C, P, const N: usize> TchebycheffSolver<C, N> for PathTchebycheff<'_, G, H, P>
where
    G: Graph<Weight = [C; N]>,
    H: Heuristic<G::State, [C; N]>,
    C: Cost + ExactArith<Exact = BigUint>,
    P: Fn(&G::State) -> bool,
{
    type Solution = Vec<G::State>;

    fn minimize(&mut self, scalarization: &Scalarization<C, N>) -> Option<(Vec<G::State>, [C; N])> {
        let mut nodes: Vec<(G::State, [C; N], Option<usize>)> = Vec::new();
        let mut open = BinaryHeap::new();
        let mut closed: BTreeMap<G::State, Front<C, N>> = BTreeMap::new();
        let mut successors = Vec::new();

        let h = self.heuristic.estimate(&self.start)?;
        nodes.push((self.start.clone(), [C::ZERO; N], None));
        open.push(Reverse((scalarization.score(&h), h, 0)));

        while let Some(Reverse((_, _, index))) = open.pop() {
            let (state, g) = (nodes[index].0.clone(), nodes[index].1);
            if closed.get(&state).is_some_and(|c| c.weakly_dominates(&g)) {
                continue;
            }

            closed.entry(state.clone()).or_default().insert(g);
            if (self.is_goal)(&state) {
                let mut path = Vec::new();
                let mut at = Some(index);
                while let Some(i) = at {
                    path.push(nodes[i].0.clone());
                    at = nodes[i].2;
                }

                path.reverse();
                return Some((path, g));
            }

            self.stats.expanded += 1;
            self.graph.successors(&state, &mut successors);
            for (next, w) in successors.drain(..) {
                let g_next = add(g, w);
                if closed
                    .get(&next)
                    .is_some_and(|c| c.weakly_dominates(&g_next))
                {
                    continue;
                }

                let Some(h) = self.heuristic.estimate(&next) else {
                    continue;
                };
                let f = add(g_next, h);
                nodes.push((next, g_next, Some(index)));
                open.push(Reverse((scalarization.score(&f), f, nodes.len() - 1)));
                self.stats.generated += 1;
            }
        }

        None
    }
}

/// TPA for shortest paths: the exact Pareto front from `start` to a state
/// that satisfies `is_goal`, found well spread over the objective space
/// first and completed if the budget allows.
///
/// The ideal point and a strict upper bound of the nadir point (the sum of
/// all reachable edge costs plus one, since Pareto-optimal paths are
/// simple) come from one exploration of the finite reachable graph and one
/// Dijkstra per objective; each subproblem is a [`PathTchebycheff`] search
/// with `heuristic`. `on_front` gets the solutions after every new point,
/// with an infinite factor until the front is complete (TPA has no
/// approximation guarantee before that) and zero when it is. `budget` sees
/// the expansions so far and is checked before each subproblem.
///
/// Requirements: non-negative integer-like costs (`ExactArith` into
/// `BigUint`), a finite reachable graph, an admissible `heuristic` zero on
/// goals.
///
/// # Examples
///
/// ```
/// use multiobjective_search::{Graph, ZeroHeuristic, boa_star, tpa_paths};
///
/// /// Four routes from 0 to 1.
/// struct Routes;
///
/// impl Graph for Routes {
///     type State = u8;
///     type Weight = [u32; 2];
///
///     fn successors(&self, state: &u8, out: &mut Vec<(u8, [u32; 2])>) {
///         if *state == 0 {
///             out.extend([(1, [1, 9]), (1, [3, 5]), (1, [5, 3]), (1, [9, 1])]);
///         }
///     }
/// }
///
/// let result = tpa_paths(&Routes, ZeroHeuristic, 0, |s| *s == 1, |_, _| {}, |_: &_| false);
/// assert!(result.complete);
/// assert_eq!(result.solutions, boa_star(&Routes, ZeroHeuristic, 0, |s| *s == 1).solutions);
/// ```
pub fn tpa_paths<G, H, C, P, const N: usize>(
    graph: &G,
    heuristic: H,
    start: G::State,
    is_goal: P,
    mut on_front: impl FnMut(&[Solution<G::State, [C; N]>], ApproxFactor<C>),
    mut budget: impl Budget<Stats>,
) -> AnytimeFront<G::State, C, N>
where
    G: Graph<Weight = [C; N]>,
    H: Heuristic<G::State, [C; N]>,
    C: Cost + ExactArith<Exact = BigUint>,
    P: Fn(&G::State) -> bool,
{
    let Some((ideal, nadir)) = bounds(graph, &start, &is_goal) else {
        return AnytimeFront {
            solutions: Vec::new(),
            factor: ApproxFactor::Zero,
            complete: true,
            stats: Stats::default(),
        };
    };

    let mut solver = PathTchebycheff {
        graph,
        heuristic,
        start,
        is_goal,
        stats: Stats::default(),
    };
    let mut front: Vec<Solution<G::State, [C; N]>> = Vec::new();

    // Without any positive edge cost every path costs zero: one subproblem
    // finds the only point.
    if (0..N).any(|i| nadir[i] <= ideal[i]) {
        let only = solver.minimize(&scalarize(&ideal, &ideal, &BigUint::one()));
        front.extend(only.map(|(path, cost)| Solution { cost, path }));
        on_front(&front, ApproxFactor::Zero);
        return AnytimeFront {
            solutions: front,
            factor: ApproxFactor::Zero,
            complete: true,
            stats: solver.stats,
        };
    }

    let (_, complete) = tpa(
        &mut solver,
        ideal,
        nadir,
        |path, cost| {
            front.push(Solution {
                cost,
                path: path.clone(),
            });
            front.sort_by_key(|s| s.cost);
            on_front(&front, ApproxFactor::Infinite);
        },
        |solver| budget.exhausted(&solver.stats),
    );

    let factor = if complete {
        ApproxFactor::Zero
    } else {
        ApproxFactor::Infinite
    };
    if complete {
        on_front(&front, factor);
    }

    AnytimeFront {
        solutions: front,
        factor,
        complete,
        stats: solver.stats,
    }
}

/// The ideal point and a strict nadir bound of the paths from `start` to a
/// goal; `None` if no goal is reachable.
fn bounds<G, C, P, const N: usize>(
    graph: &G,
    start: &G::State,
    is_goal: &P,
) -> Option<([C; N], [C; N])>
where
    G: Graph<Weight = [C; N]>,
    C: Cost,
    P: Fn(&G::State) -> bool,
{
    // Explore, summing every edge's costs.
    let mut seen = alloc::collections::BTreeSet::from([start.clone()]);
    let mut stack = alloc::vec![start.clone()];
    let mut sum = [C::ZERO; N];
    let mut edges = Vec::new();
    let mut adjacency: Adjacency<G::State, [C; N]> = BTreeMap::new();
    while let Some(s) = stack.pop() {
        if is_goal(&s) {
            continue;
        }

        graph.successors(&s, &mut edges);
        for (t, w) in &edges {
            sum = add(sum, *w);
            if seen.insert(t.clone()) {
                stack.push(t.clone());
            }
        }

        adjacency.insert(s, core::mem::take(&mut edges));
    }

    let ideal: Option<[C; N]> = (0..N)
        .map(|i| {
            let mut dist: BTreeMap<G::State, C> = BTreeMap::new();
            let mut heap = BinaryHeap::from([Reverse((C::ZERO, start.clone()))]);
            while let Some(Reverse((d, s))) = heap.pop() {
                if dist.contains_key(&s) {
                    continue;
                }

                dist.insert(s.clone(), d);
                if is_goal(&s) {
                    return Some(d);
                }

                for (t, w) in adjacency.get(&s).into_iter().flatten() {
                    heap.push(Reverse((d + w[i], t.clone())));
                }
            }

            None
        })
        .collect::<Option<Vec<C>>>()
        .map(|v| core::array::from_fn(|i| v[i]));

    // Pareto-optimal paths are simple, so no cost exceeds the edge sums;
    // `2 · sum + unit` is strictly above them, with `unit` the smallest
    // positive edge cost (Cost has no one).
    let unit = adjacency
        .values()
        .flatten()
        .flat_map(|(_, w)| *w)
        .filter(|&c| c > C::ZERO)
        .min();
    ideal.map(|ideal| {
        let unit = unit.unwrap_or(C::ZERO);
        (ideal, sum.map(|c| c.saturating_add(c).saturating_add(unit)))
    })
}