Skip to main content

LabelCount

Struct LabelCount 

Source
pub struct LabelCount<'a, G, F> { /* private fields */ }
Expand description

A graph whose objective slot counts the distinct labels a path uses: the minimum-label path objective (Pascoal et al. 2013), made additive by carrying the set of labels seen in the state.

label names each edge (from, to, weight); the edge’s cost in slot is replaced by one if its label is new on the path, else zero. The other objectives are kept. States are (state, labels), so the search space grows with the label combinations reached; use it for few labels. Goals are the pairs whose state is a goal.

§Examples

use multiobjective_search::{Graph, LabelCount, ZeroHeuristic, namoa_star};

/// Two routes from 0 to 2: one short with two operators, one long
/// with one. The second objective is a placeholder for the count.
struct Lines;

impl Graph for Lines {
    type State = u8;
    type Weight = [u32; 2];

    fn successors(&self, state: &u8, out: &mut Vec<(u8, [u32; 2])>) {
        match state {
            0 => out.extend([(1, [1, 0]), (2, [9, 0])]),
            1 => out.push((2, [1, 0])),
            _ => {}
        }
    }
}

// Operator: "a" from 0, "b" from 1.
let counted = LabelCount::new(&Lines, 1, |from: &u8, _: &u8, _: &[u32; 2]| *from);
let out = namoa_star(&counted, ZeroHeuristic, counted.start(0), |s: &(u8, _)| s.0 == 2);
let costs: Vec<_> = out.solutions.iter().map(|s| s.cost).collect();
assert_eq!(costs, [[2, 2], [9, 1]]);

Implementations§

Source§

impl<'a, G, F> LabelCount<'a, G, F>

Source

pub const fn new(graph: &'a G, slot: usize, label: F) -> Self

Counts the labels given by label in objective slot of graph.

Source

pub const fn start<S, L>(&self, start: S) -> (S, BTreeSet<L>)

The start pair: start with no labels seen.

Trait Implementations§

Source§

impl<G, F, L, C, const N: usize> Graph for LabelCount<'_, G, F>
where G: Graph<Weight = [C; N]>, F: Fn(&G::State, &G::State, &[C; N]) -> L, L: Clone + Ord, C: Cost + From<u8>,

Source§

type State = (<G as Graph>::State, BTreeSet<L>)

A search state. Ord keys the per-state bookkeeping (a BTreeMap, since the crate is no_std).
Source§

type Weight = [C; N]

The cost vector of one edge, for example [u32; 2].
Source§

fn successors(&self, state: &Self::State, out: &mut Vec<(Self::State, [C; N])>)

Appends every outgoing edge of state to out as (successor, weight). Read more

Auto Trait Implementations§

§

impl<'a, G, F> Forget for LabelCount<'a, G, F>

§

impl<'a, G, F> Freeze for LabelCount<'a, G, F>
where &'a G: Freeze, F: Freeze,

§

impl<'a, G, F> Move for LabelCount<'a, G, F>

§

impl<'a, G, F> RefUnwindSafe for LabelCount<'a, G, F>

§

impl<'a, G, F> Send for LabelCount<'a, G, F>
where F: Send, &'a G: Send,

§

impl<'a, G, F> Sync for LabelCount<'a, G, F>
where F: Sync, &'a G: Sync,

§

impl<'a, G, F> Unpin for LabelCount<'a, G, F>
where F: Unpin, &'a G: Unpin,

§

impl<'a, G, F> UnsafeUnpin for LabelCount<'a, G, F>

§

impl<'a, G, F> UnwindSafe for LabelCount<'a, G, F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.