Skip to main content

constrained_shortest_path

Function constrained_shortest_path 

Source
pub fn constrained_shortest_path<G, H, C, P>(
    graph: &G,
    heuristic: H,
    start: G::State,
    is_goal: P,
    budget: C,
    stats: &mut Stats,
) -> Option<Solution<G::State, [C; 2]>>
where G: Graph<Weight = [C; 2]>, H: Heuristic<G::State, [C; 2]>, C: Cost, P: Fn(&G::State) -> bool,
Expand description

Finds the path from start to a state that satisfies is_goal with the smallest first cost among those whose second cost is at most budget, the smallest second cost breaking ties; None if no path fits the budget.

Labels leave the open list in lexicographic f order. A label is dropped when f2 > budget, or, as in BOA*, when an earlier label at its state had a second cost no larger (it reaches everything this one does, no dearer). The first goal label is the answer: every label still open costs at least as much in the first objective.

Requirements, not checked at run time: non-negative costs and a heuristic consistent in both objectives and zero on every goal. Expansion counters go to stats.

ยงExamples

use multiobjective_search::{Graph, Stats, ZeroHeuristic, constrained_shortest_path};

/// Three routes from 0 to 1: (time, fuel).
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, [8, 1])]);
        }
    }
}

let mut stats = Stats::default();
let best = constrained_shortest_path(&Routes, ZeroHeuristic, 0, |s| *s == 1, 6, &mut stats);
assert_eq!(best.map(|s| s.cost), Some([3, 5]));