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>
impl<C: Cost, const N: usize, const M: usize> KnapsackGraph<C, N, M>
Sourcepub const fn new(items: Vec<([C; M], [C; N])>, capacity: [C; M]) -> Self
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.
Sourcepub const fn start(&self) -> (usize, [C; M], bool)
pub const fn start(&self) -> (usize, [C; M], bool)
The start state: nothing decided, nothing used.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more