Skip to main content

dissimilar_paths

Function dissimilar_paths 

Source
pub fn dissimilar_paths<G, C, P, F>(
    graph: &G,
    start: G::State,
    is_goal: P,
    cost: F,
    k: usize,
    max_shared: usize,
) -> Vec<RankedPath<G::State, G::Edge, C>>
where G: EdgeGraph, C: Cost, P: Fn(&G::State) -> bool, F: Fn(&G::Weight) -> C,
Expand description

Up to k loopless paths from start to a goal in order of the scalar cost(weight), each sharing at most max_shared edges with every earlier one: the greedy k-shortest dissimilar paths.

Paths are ranked best-first by (cost, edge sequence), so equal costs are taken in a fixed order, and a path is kept when it is dissimilar enough from all kept ones (in the spirit of Moghanni et al. 2022, who trade cost against dissimilarity). Each result is (states, edges, cost). Exponential in the worst case, like every ranking.

Requirements: non-negative costs; goals end paths.

§Panics

Never: partial paths are never empty.

§Examples

use multiobjective_search::{EdgeGraph, Graph, dissimilar_paths};

/// 0 → 1 by two edges, then 1 → 2 by two edges.
struct Ladder;

const EDGES: [(u8, u8, u32); 4] = [(0, 1, 1), (0, 1, 2), (1, 2, 1), (1, 2, 2)];

impl Graph for Ladder {
    type State = u8;
    type Weight = u32;

    fn successors(&self, state: &u8, out: &mut Vec<(u8, u32)>) {
        out.extend(EDGES.iter().filter(|e| e.0 == *state).map(|e| (e.1, e.2)));
    }
}

impl EdgeGraph for Ladder {
    type Edge = usize;

    fn edges(&self, state: &u8, out: &mut Vec<(usize, u8, u32)>) {
        out.extend(EDGES.iter().enumerate().filter(|(_, e)| e.0 == *state).map(|(i, e)| (i, e.1, e.2)));
    }
}

// Fully disjoint: the cheapest path, then the one sharing nothing.
let paths = dissimilar_paths(&Ladder, 0, |s| *s == 2, |w: &u32| *w, 3, 0);
let costs: Vec<_> = paths.iter().map(|p| p.2).collect();
assert_eq!(costs, [2, 4]);