Skip to main content

Bundle

Struct Bundle 

Source
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>

Source

pub const fn new() -> Self

An empty bundle.

Source

pub fn len(&self) -> usize

The number of tasks held.

Source

pub fn is_empty(&self) -> bool

Whether the bundle holds no task.

Source

pub fn path(&self) -> &[Task]

The tasks in execution order.

Source

pub fn tasks(&self) -> impl ExactSizeIterator<Item = &Task> + '_

The tasks in the order they were added.

Source

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.

Source

pub fn value(&self) -> u64

The bundle’s value: the sum of the recorded gains.

Saturates at u64::MAX.

Source

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.

Source

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

pub fn clear(&mut self)

Removes every task.

Source§

impl<Task: PartialEq> Bundle<Task>

Source

pub fn contains(&self, task: &Task) -> bool

Whether the bundle holds task.

Source

pub fn index_of(&self, task: &Task) -> Option<usize>

The bundle index of task: how many tasks were added before it; None when the bundle does not hold it.

Source§

impl<Task: Clone + PartialEq> Bundle<Task>

Source

pub fn best_candidate<'c, V, F>( &self, valuation: &V, candidates: &'c [Task], accept: F, ) -> Option<(&'c Task, Insertion)>
where V: Valuation<Task> + ?Sized, F: FnMut(&Task, u64) -> bool,

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.

Source

pub fn extend_greedily<V, F>( &mut self, valuation: &V, candidates: &[Task], capacity: usize, accept: F, ) -> usize
where V: Valuation<Task> + ?Sized, F: FnMut(&Task, u64) -> bool,

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.

Trait Implementations§

Source§

impl<Task: Clone> Clone for Bundle<Task>

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<Task: Debug> Debug for Bundle<Task>

Source§

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

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

impl<Task> Default for Bundle<Task>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<Task: Eq> Eq for Bundle<Task>

Source§

impl<Task: PartialEq> PartialEq for Bundle<Task>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<Task: PartialEq> StructuralPartialEq for Bundle<Task>

Auto Trait Implementations§

§

impl<Task> Forget for Bundle<Task>

§

impl<Task> Freeze for Bundle<Task>
where Vec<(Task, u64)>: Freeze, Vec<Task>: Freeze,

§

impl<Task> Move for Bundle<Task>

§

impl<Task> RefUnwindSafe for Bundle<Task>
where Vec<(Task, u64)>: RefUnwindSafe, Vec<Task>: RefUnwindSafe,

§

impl<Task> Send for Bundle<Task>
where Vec<(Task, u64)>: Send, Vec<Task>: Send,

§

impl<Task> Sync for Bundle<Task>
where Vec<(Task, u64)>: Sync, Vec<Task>: Sync,

§

impl<Task> Unpin for Bundle<Task>
where Vec<(Task, u64)>: Unpin, Vec<Task>: Unpin,

§

impl<Task> UnsafeUnpin for Bundle<Task>
where Vec<(Task, u64)>: UnsafeUnpin, Vec<Task>: UnsafeUnpin,

§

impl<Task> UnwindSafe for Bundle<Task>
where Vec<(Task, u64)>: UnwindSafe, Vec<Task>: UnwindSafe,

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.