multiobjective-search 0.1.1

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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! SIP-MOSP: asynchronous parallel exact multi-objective search with
//! versioned frontier snapshots and indexed dominance pruning.
//!
//! Xu, Zhang, Qian, Liu, Wu, Tang: "Asynchronous Parallel Search for Exact
//! Multi-Objective Shortest Paths with Versioned Frontier Snapshots and
//! Indexed Dominance Pruning", arXiv 2609.11944, 2026.
//!
//! *Search workers* expand labels from private best-first queues, each
//! under its own cyclic objective order, and prune the children against
//! published, immutable snapshots of the endpoint's and the goals'
//! frontiers. A surviving child goes to the *update worker* that owns its
//! endpoint (one owner per state, by hash), or to the single *goal worker*.
//! An owner alone modifies the exact ("canonical") frontier of its states:
//! it re-checks the candidate unless the snapshot it was checked against
//! is still current, inserts it, publishes the next snapshot version and
//! hands the label to the less loaded of two search workers. Snapshots are
//! a lexicographically sorted, indexed base ([`IndexedFront`]) plus a short
//! delta of recent additions, so most publications share the base.
//!
//! ```mermaid
//! graph LR
//!     sw["search workers<br/>(private queues)"] -- "child, snapshot version" --> uw["update worker<br/>owning the endpoint"]
//!     sw -- "goal child" --> gw[goal worker]
//!     uw -- "admitted label" --> sw
//!     uw -. publish .-> snap[("snapshots<br/>base + delta + index")]
//!     gw -. publish .-> snap
//!     snap -. read .-> sw
//! ```
//!
//! The result is the exact Pareto-optimal front whatever the thread timing
//! (the paper's Theorem 1); which path represents a cost vector may
//! differ between runs.

use alloc::collections::{BTreeMap, BinaryHeap};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::cmp::{Ordering, Reverse};
use core::hash::{Hash, Hasher};
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering as Atomic};
use std::collections::HashMap;
use std::hash::DefaultHasher;
use std::sync::{Condvar, Mutex, RwLock};

use search_core::{Cost, Heuristic};

use crate::cost::add;
use crate::graph::Graph;
use crate::output::{Output, Solution, Stats};
use crate::pareto::{Front, FrontIndex, IndexedFront};

/// Worker counts, the snapshot index and its thresholds for [`sip_mosp`].
///
/// The thresholds only decide when a snapshot is consolidated and how its
/// base is summarized; they never change the answer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SipConfig {
    /// Search workers, at least 1.
    pub search_workers: usize,
    /// Update workers for non-goal states, at least 1. A goal worker runs
    /// in addition.
    pub update_workers: usize,
    /// The index over snapshot bases: [`FrontIndex::Blocks`] is SIP-MOSP-BM,
    /// [`FrontIndex::Tree`] SIP-MOSP-ST.
    pub index: FrontIndex,
    /// Frontiers smaller than this are published as an unindexed base only
    /// (the paper's `θ_I`). Default 64.
    pub index_threshold: usize,
    /// The delta is folded into a rebuilt base once it holds
    /// `max(delta_min, |F| / delta_divisor)` vectors (the paper's adaptive
    /// `θ_Δ(|F|)`). Default 8.
    pub delta_min: usize,
    /// See [`SipConfig::delta_min`]; at least 1. Default 8.
    pub delta_divisor: usize,
    /// Vectors per block of [`FrontIndex::Blocks`]; at least 1. Default 16.
    pub block_size: usize,
}

impl SipConfig {
    /// Two search and two update workers, the block-minimum index, and the
    /// default thresholds.
    pub const DEFAULT: Self = Self {
        search_workers: 2,
        update_workers: 2,
        index: FrontIndex::Blocks,
        index_threshold: 64,
        delta_min: 8,
        delta_divisor: 8,
        block_size: 16,
    };

    /// The delta capacity for a frontier of `frontier` vectors.
    fn delta_capacity(&self, frontier: usize) -> usize {
        (frontier / self.delta_divisor).max(self.delta_min)
    }
}

impl Default for SipConfig {
    /// [`SipConfig::DEFAULT`].
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// SIP-MOSP: finds one path for every Pareto-optimal cost vector from
/// `start` to a state that satisfies `is_goal`, with one thread per search
/// and update worker plus a goal worker.
///
/// Requirements, not checked at run time:
///
/// - edge costs are non-negative;
/// - `heuristic` is admissible in every objective and zero on every goal
///   (the paper uses exact per-objective distances, which are
///   consistent). Each search worker uses its own clone.
///
/// Solutions are sorted lexicographically, one per Pareto-optimal cost
/// vector; the same vectors as [`namoa_star`](crate::namoa_star) in every
/// run. Goal states are not expanded.
///
/// # Panics
///
/// Panics if a worker count, `delta_divisor` or `block_size` is zero, or if a
/// worker thread panics.
///
/// # Examples
///
/// ```
/// use multiobjective_search::{Graph, SipConfig, ZeroHeuristic, namoa_star, sip_mosp};
///
/// /// A small diamond with three objectives.
/// struct Diamond;
///
/// impl Graph for Diamond {
///     type State = u8;
///     type Weight = [u32; 3];
///
///     fn successors(&self, state: &u8, out: &mut Vec<(u8, [u32; 3])>) {
///         match state {
///             0 => out.extend([(1, [1, 2, 3]), (2, [3, 2, 1]), (3, [5, 5, 5])]),
///             1 | 2 => out.push((3, [1, 1, 1])),
///             _ => {}
///         }
///     }
/// }
///
/// let out = sip_mosp(&Diamond, ZeroHeuristic, 0, |s| *s == 3, SipConfig::default());
/// let exact = namoa_star(&Diamond, ZeroHeuristic, 0, |s| *s == 3);
/// let costs = |o: &multiobjective_search::Output<u8, [u32; 3]>| {
///     o.solutions.iter().map(|s| s.cost).collect::<Vec<_>>()
/// };
/// assert_eq!(costs(&out), costs(&exact));
/// ```
pub fn sip_mosp<G, H, C, P, const N: usize>(
    graph: &G,
    mut heuristic: H,
    start: G::State,
    is_goal: P,
    config: SipConfig,
) -> Output<G::State, [C; N]>
where
    G: Graph<Weight = [C; N]> + Sync,
    G::State: Hash + Send + Sync,
    H: Heuristic<G::State, [C; N]> + Clone + Send,
    C: Cost + Send + Sync,
    P: Fn(&G::State) -> bool + Sync,
{
    assert!(
        config.search_workers > 0 && config.update_workers > 0,
        "SIP-MOSP needs at least one search and one update worker"
    );
    assert!(
        config.delta_divisor > 0 && config.block_size > 0,
        "the delta divisor and the block size must be positive"
    );

    let Some(h) = heuristic.estimate(&start) else {
        return Output {
            solutions: Vec::new(),
            stats: Stats::default(),
        };
    };

    let shared = Shared::new(config);
    let start_label = Arc::new(Label {
        state: start,
        g: [C::ZERO; N],
        f: h,
        parent: None,
        seen: 0,
    });
    shared.generated.fetch_add(1, Atomic::Relaxed);

    let goal_labels = std::thread::scope(|scope| {
        for r in 0..config.search_workers {
            let (shared, heuristic, is_goal) = (&shared, heuristic.clone(), &is_goal);
            scope.spawn(move || search_worker(shared, graph, heuristic, is_goal, r));
        }

        for p in 0..config.update_workers {
            let shared = &shared;
            scope.spawn(move || update_worker(shared, p));
        }

        let goal = scope.spawn(|| goal_worker(&shared));
        shared.route(start_label, &is_goal);

        goal.join().expect("the goal worker panicked")
    });

    let stats = Stats {
        expanded: shared.expanded.load(Atomic::Relaxed),
        generated: shared.generated.load(Atomic::Relaxed),
    };

    Output {
        solutions: goal_labels
            .into_iter()
            .map(|label| rebuild(&label))
            .collect(),
        stats,
    }
}

/// A label: one concrete path, shared by its children through `parent`.
struct Label<S, C, const N: usize> {
    /// The state the path ends in.
    state: S,
    /// Cost of the path.
    g: [C; N],
    /// `g + h`.
    f: [C; N],
    /// The path without its last edge; `None` for the start.
    parent: Option<Arc<Label<S, C, N>>>,
    /// Version of the endpoint's (or, for a goal, the goal) snapshot the
    /// label was checked against when generated.
    seen: u64,
}

/// A label in a private queue, ordered by `key` and then arrival.
struct Entry<S, C, const N: usize> {
    /// `f` under the worker's objective order.
    key: [C; N],
    /// Arrival number, for a deterministic order among equal keys.
    seq: usize,
    /// The label.
    label: Arc<Label<S, C, N>>,
}

impl<S, C: Ord, const N: usize> PartialEq for Entry<S, C, N> {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl<S, C: Ord, const N: usize> Eq for Entry<S, C, N> {}

impl<S, C: Ord, const N: usize> PartialOrd for Entry<S, C, N> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<S, C: Ord, const N: usize> Ord for Entry<S, C, N> {
    fn cmp(&self, other: &Self) -> Ordering {
        (&self.key, self.seq).cmp(&(&other.key, other.seq))
    }
}

/// An immutable published frontier: the indexed base plus the recent
/// additions (`S_v^k = (B, Δ, I, k)`).
struct Snapshot<C, const N: usize> {
    /// The consolidated, indexed base, shared between versions.
    base: Arc<IndexedFront<C, N>>,
    /// Vectors added since the base was built.
    delta: Vec<[C; N]>,
    /// Component-wise minimum of `delta`; `None` when it is empty.
    delta_min: Option<[C; N]>,
    /// The version `k`, counted per frontier from 0.
    version: u64,
}

impl<C: Cost, const N: usize> Snapshot<C, N> {
    /// The empty snapshot, version 0.
    fn empty() -> Self {
        Self {
            base: Arc::new(IndexedFront::new(Vec::new(), FrontIndex::None)),
            delta: Vec::new(),
            delta_min: None,
            version: 0,
        }
    }

    /// `SnapshotDominates` (Algorithm 4): the delta first, guarded by its
    /// minimum, then the base through its index.
    fn dominates(&self, q: &[C; N]) -> bool {
        let below = |p: &[C; N]| p.iter().zip(q).all(|(a, b)| a <= b);
        if self.delta_min.as_ref().is_some_and(below) && self.delta.iter().any(below) {
            return true;
        }

        self.base.weakly_dominates(q)
    }

    /// The next version after admitting `g` into `front` (lines 13 to 24
    /// of Algorithm 3): a fresh unindexed base for a small frontier, a
    /// rebuilt indexed base when the delta is full or the frontier just
    /// outgrew the threshold, and otherwise the shared base with `g`
    /// appended to the delta.
    fn next(&self, front: &Front<C, N>, g: [C; N], config: &SipConfig) -> Self {
        let version = self.version + 1;
        let rebuild = |index| Self {
            base: Arc::new(IndexedFront::with_block_size(
                front.as_slice().to_vec(),
                index,
                config.block_size,
            )),
            delta: Vec::new(),
            delta_min: None,
            version,
        };

        if front.len() < config.index_threshold {
            return rebuild(FrontIndex::None);
        }

        let unindexed = self.base.len() < config.index_threshold;
        if unindexed || self.delta.len() + 1 >= config.delta_capacity(front.len()) {
            return rebuild(config.index);
        }

        let mut delta = self.delta.clone();
        delta.push(g);
        let delta_min = Some(match self.delta_min {
            Some(m) => core::array::from_fn(|i| m[i].min(g[i])),
            None => g,
        });

        Self {
            base: Arc::clone(&self.base),
            delta,
            delta_min,
            version,
        }
    }
}

/// A worker's incoming queue: many producers, one consumer.
struct Inbox<T> {
    /// Arrivals not yet moved into the worker's private queue.
    items: Mutex<Vec<T>>,
    /// Signalled on every arrival and on termination.
    ready: Condvar,
    /// Labels sent to this worker and not yet processed; the search
    /// workers' load for the power-of-two-choices placement.
    load: AtomicUsize,
}

impl<T> Inbox<T> {
    /// An empty inbox.
    fn new() -> Self {
        Self {
            items: Mutex::new(Vec::new()),
            ready: Condvar::new(),
            load: AtomicUsize::new(0),
        }
    }
}

/// A published-snapshot table for the states one update worker owns.
type Shard<S, C, const N: usize> = RwLock<HashMap<S, Arc<Snapshot<C, N>>>>;

/// Everything the workers share.
struct Shared<S, C, const N: usize> {
    /// The configuration.
    config: SipConfig,
    /// Incoming queues of the search workers.
    search: Vec<Inbox<Arc<Label<S, C, N>>>>,
    /// Incoming queues of the update workers.
    update: Vec<Inbox<Arc<Label<S, C, N>>>>,
    /// Incoming queue of the goal worker.
    goal: Inbox<Arc<Label<S, C, N>>>,
    /// Published snapshots per update worker.
    shards: Vec<Shard<S, C, N>>,
    /// The published goal snapshot.
    goal_snapshot: RwLock<Arc<Snapshot<C, N>>>,
    /// Labels sent and not yet fully processed; the search has ended when
    /// it drops to zero.
    pending: AtomicUsize,
    /// Set once `pending` reached zero.
    done: AtomicBool,
    /// Labels expanded by search workers.
    expanded: AtomicUsize,
    /// Labels generated, the start included.
    generated: AtomicUsize,
}

impl<S: Clone + Hash + Eq, C: Cost, const N: usize> Shared<S, C, N> {
    /// Queues and snapshot tables for `config`.
    fn new(config: SipConfig) -> Self {
        Self {
            config,
            search: (0..config.search_workers).map(|_| Inbox::new()).collect(),
            update: (0..config.update_workers).map(|_| Inbox::new()).collect(),
            goal: Inbox::new(),
            shards: (0..config.update_workers)
                .map(|_| RwLock::new(HashMap::new()))
                .collect(),
            goal_snapshot: RwLock::new(Arc::new(Snapshot::empty())),
            pending: AtomicUsize::new(0),
            done: AtomicBool::new(false),
            expanded: AtomicUsize::new(0),
            generated: AtomicUsize::new(0),
        }
    }

    /// The update worker that owns `state`.
    fn owner(&self, state: &S) -> usize {
        bucket(state, 0, self.config.update_workers)
    }

    /// The published snapshot of a non-goal `state`.
    fn snapshot(&self, state: &S) -> Arc<Snapshot<C, N>> {
        let shard = self.shards[self.owner(state)]
            .read()
            .expect("no worker panics while holding a shard");

        shard
            .get(state)
            .map_or_else(|| Arc::new(Snapshot::empty()), Arc::clone)
    }

    /// The published goal snapshot.
    fn goal_snapshot(&self) -> Arc<Snapshot<C, N>> {
        Arc::clone(
            &self
                .goal_snapshot
                .read()
                .expect("no worker panics while publishing"),
        )
    }

    /// Sends a candidate to the goal worker or to its endpoint's owner.
    fn route(&self, label: Arc<Label<S, C, N>>, is_goal: impl Fn(&S) -> bool) {
        if is_goal(&label.state) {
            self.send(&self.goal, label);
        } else {
            self.send(&self.update[self.owner(&label.state)], label);
        }
    }

    /// Hands an admitted label to the less loaded of two search workers
    /// chosen by hashes of its state (power of two choices).
    fn dispatch(&self, label: Arc<Label<S, C, N>>) {
        let n = self.config.search_workers;
        let (a, b) = (bucket(&label.state, 1, n), bucket(&label.state, 2, n));
        let load = |r: usize| self.search[r].load.load(Atomic::Relaxed);
        let r = if load(a) <= load(b) { a } else { b };
        self.send(&self.search[r], label);
    }

    /// Appends `item` to `inbox`, counting it as pending.
    fn send<T>(&self, inbox: &Inbox<T>, item: T) {
        self.pending.fetch_add(1, Atomic::SeqCst);
        inbox.load.fetch_add(1, Atomic::Relaxed);
        inbox
            .items
            .lock()
            .expect("no worker panics while holding an inbox")
            .push(item);
        inbox.ready.notify_one();
    }

    /// Marks one item of `inbox` as processed, and ends the search when
    /// nothing is pending any more.
    fn finish<T>(&self, inbox: &Inbox<T>) {
        inbox.load.fetch_sub(1, Atomic::Relaxed);
        if self.pending.fetch_sub(1, Atomic::SeqCst) == 1 {
            self.done.store(true, Atomic::SeqCst);
            Self::wake(&self.goal);
            for inbox in self.search.iter().chain(&self.update) {
                Self::wake(inbox);
            }
        }
    }

    /// Wakes a worker waiting on `inbox`, holding its lock so that the
    /// wake-up cannot be missed.
    fn wake<T>(inbox: &Inbox<T>) {
        let _guard = inbox
            .items
            .lock()
            .expect("no worker panics while holding an inbox");
        inbox.ready.notify_all();
    }

    /// Moves the arrivals of `inbox` into `queue`, waiting while both are
    /// empty. Returns `false` once the search has ended.
    fn refill<T>(&self, inbox: &Inbox<T>, mut queue: impl FnMut(T), queued: bool) -> bool {
        let mut items = inbox
            .items
            .lock()
            .expect("no worker panics while holding an inbox");
        while items.is_empty() && !queued {
            if self.done.load(Atomic::SeqCst) {
                return false;
            }

            items = inbox
                .ready
                .wait(items)
                .expect("no worker panics while holding an inbox");
        }

        for item in items.drain(..) {
            queue(item);
        }

        true
    }
}

/// A deterministic bucket in `0..n` for `state`, salted by `seed`.
fn bucket<S: Hash>(state: &S, seed: u64, n: usize) -> usize {
    let mut hasher = DefaultHasher::new();
    seed.hash(&mut hasher);
    state.hash(&mut hasher);
    let n = u64::try_from(n).expect("worker counts fit in u64");
    usize::try_from(hasher.finish() % n).expect("a bucket is below the worker count")
}

/// A private best-first queue under one cyclic objective order.
struct Private<S, C, const N: usize> {
    /// The queue.
    heap: BinaryHeap<Reverse<Entry<S, C, N>>>,
    /// The first objective of the order.
    first: usize,
    /// Arrivals so far.
    seq: usize,
}

impl<S, C: Cost, const N: usize> Private<S, C, N> {
    /// An empty queue ordering `f` from objective `first` on, cyclically.
    fn new(first: usize) -> Self {
        Self {
            heap: BinaryHeap::new(),
            first,
            seq: 0,
        }
    }

    /// Queues a label.
    fn push(&mut self, label: Arc<Label<S, C, N>>) {
        let key = core::array::from_fn(|i| label.f[(i + self.first) % N]);
        self.seq += 1;
        self.heap.push(Reverse(Entry {
            key,
            seq: self.seq,
            label,
        }));
    }

    /// Takes the label with the smallest key.
    fn pop(&mut self) -> Option<Arc<Label<S, C, N>>> {
        self.heap.pop().map(|Reverse(entry)| entry.label)
    }
}

/// A search worker (Algorithm 2): expands admitted labels and sends the
/// children that survive snapshot pruning to their owners.
fn search_worker<G, H, C, P, const N: usize>(
    shared: &Shared<G::State, C, N>,
    graph: &G,
    mut heuristic: H,
    is_goal: &P,
    worker: usize,
) where
    G: Graph<Weight = [C; N]>,
    G::State: Hash,
    H: Heuristic<G::State, [C; N]>,
    C: Cost,
    P: Fn(&G::State) -> bool,
{
    let inbox = &shared.search[worker];
    let mut queue = Private::new(worker % N);
    let mut successors = Vec::new();

    loop {
        let queued = !queue.heap.is_empty();
        if !shared.refill(inbox, |label| queue.push(label), queued) {
            break;
        }

        let Some(parent) = queue.pop() else { continue };
        shared.expanded.fetch_add(1, Atomic::Relaxed);

        let goals = shared.goal_snapshot();
        graph.successors(&parent.state, &mut successors);
        for (state, weight) in successors.drain(..) {
            let Some(estimate) = heuristic.estimate(&state) else {
                continue;
            };

            let (g, goal) = (add(parent.g, weight), is_goal(&state));
            let f = add(g, estimate);
            let endpoint = if goal {
                Arc::clone(&goals)
            } else {
                shared.snapshot(&state)
            };

            if endpoint.dominates(&g) || goals.dominates(&f) {
                continue;
            }

            let child = Arc::new(Label {
                state,
                g,
                f,
                parent: Some(Arc::clone(&parent)),
                seen: endpoint.version,
            });
            shared.generated.fetch_add(1, Atomic::Relaxed);
            shared.route(child, is_goal);
        }

        shared.finish(inbox);
    }
}

/// The canonical frontier of one state and its last published snapshot.
struct Canonical<C, const N: usize> {
    /// The exact non-dominated costs admitted so far.
    front: Front<C, N>,
    /// The last snapshot published for it.
    snapshot: Arc<Snapshot<C, N>>,
}

impl<C: Cost, const N: usize> Canonical<C, N> {
    /// An empty frontier with the version-0 snapshot.
    fn new() -> Self {
        Self {
            front: Front::new(),
            snapshot: Arc::new(Snapshot::empty()),
        }
    }

    /// The admission step of Algorithm 3 (lines 8 to 24): rejects `g` if it
    /// was checked against an older snapshot and the current one dominates
    /// it, else inserts it and returns the next snapshot to publish.
    fn admit(&mut self, g: [C; N], seen: u64, config: &SipConfig) -> Option<Arc<Snapshot<C, N>>> {
        if seen != self.snapshot.version && self.snapshot.dominates(&g) {
            return None;
        }

        if !self.front.insert(g) {
            return None;
        }

        self.snapshot = Arc::new(self.snapshot.next(&self.front, g, config));
        Some(Arc::clone(&self.snapshot))
    }
}

/// An update worker (Algorithm 3) for the non-goal states it owns.
fn update_worker<S, C, const N: usize>(shared: &Shared<S, C, N>, p: usize)
where
    S: Clone + Hash + Eq,
    C: Cost,
{
    let inbox = &shared.update[p];
    let mut queue = Private::new(0);
    let mut frontiers: HashMap<S, Canonical<C, N>> = HashMap::new();

    loop {
        let queued = !queue.heap.is_empty();
        if !shared.refill(inbox, |label| queue.push(label), queued) {
            break;
        }

        let Some(y) = queue.pop() else { continue };
        let canonical = frontiers
            .entry(y.state.clone())
            .or_insert_with(Canonical::new);

        if let Some(snapshot) = canonical.admit(y.g, y.seen, &shared.config) {
            shared.shards[p]
                .write()
                .expect("no worker panics while holding a shard")
                .insert(y.state.clone(), snapshot);
            shared.dispatch(y);
        }

        shared.finish(inbox);
    }
}

/// The goal worker: keeps the goal frontier and one representative per
/// admitted cost, and returns the representatives of the final front in
/// lexicographic order.
fn goal_worker<S, C, const N: usize>(shared: &Shared<S, C, N>) -> Vec<Arc<Label<S, C, N>>>
where
    S: Clone + Hash + Eq,
    C: Cost,
{
    let inbox = &shared.goal;
    let mut queue = Private::new(0);
    let mut frontier = Canonical::new();
    let mut representatives = BTreeMap::new();

    loop {
        let queued = !queue.heap.is_empty();
        if !shared.refill(inbox, |label| queue.push(label), queued) {
            break;
        }

        let Some(y) = queue.pop() else { continue };

        if let Some(snapshot) = frontier.admit(y.g, y.seen, &shared.config) {
            *shared
                .goal_snapshot
                .write()
                .expect("no worker panics while publishing") = snapshot;
            representatives.insert(y.g, y);
        }

        shared.finish(inbox);
    }

    frontier
        .front
        .as_slice()
        .iter()
        .map(|cost| Arc::clone(&representatives[cost]))
        .collect()
}

/// The solution ending in `label`.
fn rebuild<S: Clone, C: Copy, const N: usize>(label: &Arc<Label<S, C, N>>) -> Solution<S, [C; N]> {
    let mut path = Vec::new();
    let mut current = Some(label);

    while let Some(l) = current {
        path.push(l.state.clone());
        current = l.parent.as_ref();
    }

    path.reverse();

    Solution {
        cost: label.g,
        path,
    }
}