Skip to main content

KnapsackGraph

Struct KnapsackGraph 

Source
pub struct KnapsackGraph<C, const N: usize, const M: usize> { /* private fields */ }
Expand description

A multi-objective, multi-constraint 0/1 knapsack as a layered graph.

State (i, used, took): items 0..i decided, used the resources they take, took whether item i - 1 was taken. From each state an edge skips item i at cost values[i] (the value lost) and, if the item fits every capacity, an edge takes it at cost zero. A path from KnapsackGraph::start to a goal (KnapsackGraph::is_goal) is a feasible selection, and minimizing the lost values maximizes the kept ones (Captivo et al. 2003 solve the bi-objective knapsack this way). KnapsackGraph::taken reads the selection off a path.

§Examples

use multiobjective_search::{KnapsackGraph, ZeroHeuristic, namoa_star};

// Three items with weights and two values; capacity 5.
let knapsack = KnapsackGraph::new(vec![([3_u32], [5, 1]), ([3], [1, 5]), ([2], [2, 2])], [5]);
let out = namoa_star(&knapsack, ZeroHeuristic, knapsack.start(), |s| knapsack.is_goal(s));
let picks: Vec<_> = out.solutions.iter().map(|s| knapsack.taken(&s.path)).collect();
assert_eq!(picks, [vec![0, 2], vec![1, 2]]); // values (7, 3) and (3, 7)

Implementations§

Source§

impl<C: Cost, const N: usize, const M: usize> KnapsackGraph<C, N, M>

Source

pub const fn new(items: Vec<([C; M], [C; N])>, capacity: [C; M]) -> Self

A knapsack with items as (weights, values) and capacity per resource.

Source

pub const fn start(&self) -> (usize, [C; M], bool)

The start state: nothing decided, nothing used.

Source

pub fn is_goal(&self, state: &(usize, [C; M], bool)) -> bool

Returns true once every item is decided.

Source

pub fn taken(&self, path: &[(usize, [C; M], bool)]) -> Vec<usize>

The indices of the items a path takes.

Trait Implementations§

Source§

impl<C: Clone, const N: usize, const M: usize> Clone for KnapsackGraph<C, N, M>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug, const N: usize, const M: usize> Debug for KnapsackGraph<C, N, M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: Cost, const N: usize, const M: usize> Graph for KnapsackGraph<C, N, M>

Source§

type State = (usize, [C; M], bool)

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<C, const N: usize, const M: usize> Forget for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> Freeze for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> Move for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> RefUnwindSafe for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> Send for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> Sync for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> Unpin for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> UnsafeUnpin for KnapsackGraph<C, N, M>

§

impl<C, const N: usize, const M: usize> UnwindSafe for KnapsackGraph<C, N, M>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.