pub struct Bundle<Task> { /* private fields */ }Expand description
The tasks an agent holds, in bundle order and in path order.
§Invariants
- The bundle and the path hold the same tasks, each at most once.
- Gains are those the tasks had when they were added, given the tasks added before them.
§Examples
use task_allocation::{Bundle, Valuation};
/// Values task `t` at `t`, but each task costs one more point than the last.
struct Busy;
impl Valuation<u64> for Busy {
fn marginal_score(&self, path: &[u64], task: &u64) -> Option<u64> {
task.checked_sub(path.len() as u64)
}
}
let mut bundle = Bundle::new();
let added = bundle.extend_greedily(&Busy, &[3, 9, 5], 2, |_, gain| gain > 0);
assert_eq!(added, 2);
assert_eq!(bundle.tasks().collect::<Vec<_>>(), [&9, &5]);
assert_eq!(bundle.value(), 9 + 4);Implementations§
Source§impl<Task> Bundle<Task>
impl<Task> Bundle<Task>
Sourcepub fn tasks(&self) -> impl ExactSizeIterator<Item = &Task> + '_
pub fn tasks(&self) -> impl ExactSizeIterator<Item = &Task> + '_
The tasks in the order they were added.
Sourcepub fn entries(&self) -> impl ExactSizeIterator<Item = (&Task, u64)> + '_
pub fn entries(&self) -> impl ExactSizeIterator<Item = (&Task, u64)> + '_
The tasks with the gain each had when it was added, in the order they were added.
Sourcepub fn value(&self) -> u64
pub fn value(&self) -> u64
The bundle’s value: the sum of the recorded gains.
Saturates at u64::MAX.
Sourcepub fn insert(&mut self, task: Task, insertion: Insertion)where
Task: Clone,
pub fn insert(&mut self, task: Task, insertion: Insertion)where
Task: Clone,
Adds task at the end of the bundle order and at insertion.position in the path,
recording insertion.gain.
The caller keeps the bundle free of duplicates; see Bundle::contains.
§Panics
Panics when insertion.position is greater than the path’s length.
Sourcepub fn release_from(&mut self, index: usize) -> Vec<Task>where
Task: PartialEq,
pub fn release_from(&mut self, index: usize) -> Vec<Task>where
Task: PartialEq,
Removes every task added at bundle index index or later, and returns them in the
order they were added. An index at or past the end removes nothing.
Use it when the agent loses the task at index: the gains of later tasks assumed that
task was still held.
Source§impl<Task: Clone + PartialEq> Bundle<Task>
impl<Task: Clone + PartialEq> Bundle<Task>
Sourcepub fn best_candidate<'c, V, F>(
&self,
valuation: &V,
candidates: &'c [Task],
accept: F,
) -> Option<(&'c Task, Insertion)>
pub fn best_candidate<'c, V, F>( &self, valuation: &V, candidates: &'c [Task], accept: F, ) -> Option<(&'c Task, Insertion)>
The candidate with the highest gain on the current path that accept allows, with
its insertion; None when no candidate is feasible and accepted.
Tasks the bundle already holds are skipped. Of equal gains, the candidate listed first
wins. accept is asked about each feasible task, with its gain, that would beat the
best so far; a bidding agent uses it to skip tasks it cannot outbid.
Sourcepub fn extend_greedily<V, F>(
&mut self,
valuation: &V,
candidates: &[Task],
capacity: usize,
accept: F,
) -> usize
pub fn extend_greedily<V, F>( &mut self, valuation: &V, candidates: &[Task], capacity: usize, accept: F, ) -> usize
Adds the best accepted candidate, again and again, until the bundle holds capacity
tasks or no candidate is left; returns the number of tasks added.
This is the greedy bundle construction of CBBA’s bundle phase: each round adds the
task with the highest marginal gain on the current path (see
best_candidate) at its best position.