Skip to main content

Agent

Struct Agent 

Source
pub struct Agent<V> { /* private fields */ }
Expand description

One BW-TC agent: its valuation, its bundle, and the best bid it knows for every task.

§Examples

use task_allocation::bwtc::Agent;
use task_allocation::sim::{Network, Simulation};
use task_allocation::{AgentId, TaskId, Valuation};

/// Values task `t` at `10 + t` for agent 0 and `20 - t` for agent 1, minus 2 per task held.
struct Skewed(u64);

impl Valuation<TaskId> for Skewed {
    fn marginal_score(&self, path: &[TaskId], task: &TaskId) -> Option<u64> {
        let task = task.index() as u64;
        let base = if self.0 == 0 { 10 + task } else { 20 - task };
        base.checked_sub(2 * path.len() as u64)
    }
}

let agents = (0..2).map(|id| Agent::new(AgentId(id), 2, 4, 2, Skewed(id as u64))).collect();
let network = Network { seed: 1, max_delay: 3, ..Network::default() };
let mut simulation = Simulation::new(agents, network);

simulation.run_until_settled(200).expect("falls quiet in agreement");
assert!(simulation.assignment().is_conflict_free());

Implementations§

Source§

impl<V: Valuation<TaskId>> Agent<V>

Source

pub fn new( id: AgentId, agents: usize, tasks: usize, capacity: usize, valuation: V, ) -> Self

Agent id of a team of agents, bidding on tasks 0..tasks and holding at most capacity of them.

§Panics

Panics when id is not below agents.

Source

pub fn build_bundle(&mut self, now: u64)

The bundle phase at tick now: adds the most valuable tasks this agent can win, then queues a drop bid for every task it no longer holds and a bid for every new one.

Source

pub fn can_bid(&self) -> bool

Whether the bundle phase would add a task now.

Source

pub fn receive(&mut self, packet: &Packet)

The consensus phase for one packet: merges its drop bids, then its bids, queuing what is new and answering bids it knows better ones for, then releases the first task in the bundle this agent was outbid on, and every task added after it.

Packets from this agent itself are ignored, as are entries about unknown tasks or bidders outside the team.

Source

pub fn packet(&mut self) -> Option<Packet>

The packet to broadcast now, if anything is queued; empties the queue.

Source§

impl<V> Agent<V>

Source

pub const fn id(&self) -> AgentId

This agent’s identifier.

Source

pub const fn valuation(&self) -> &V

This agent’s valuation.

Source

pub const fn bundle(&self) -> &Bundle<TaskId>

The tasks this agent holds, with the bid it placed on each.

Source

pub fn best(&self, task: TaskId) -> Option<Bid>

The best bid this agent knows for task: the best of the latest bids of every bidder.

§Panics

Panics when task is not one of this agent’s tasks.

Source

pub fn is_consistent(&self) -> bool

Whether this agent’s bundle holds exactly the tasks whose best known bid is its own.

Trait Implementations§

Source§

impl<V: Clone> Clone for Agent<V>

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<V: Debug> Debug for Agent<V>

Source§

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

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

impl<V: Valuation<TaskId>> Participant for Agent<V>

Source§

fn act(&mut self, now: u64, outbox: &mut Vec<Packet>)

Runs the bundle phase, then broadcasts everything queued, if anything.

Source§

fn receive( &mut self, _now: u64, packet: &Packet, _outbox: &mut Vec<Packet>, ) -> bool

Merges packet; what it calls for goes out at the end of the next round. A change of the best known bid on a task the packet names counts as a change, as does a released task; a newer time for the same bid does not.

Source§

fn message_bytes(packet: &Packet) -> u64

The sender, then every entry after the list’s length: a tag, the task, the bidder, the gain (unused by a drop) and the time.

Source§

fn is_settled(team: &[Self], in_flight: &[&Packet]) -> bool

The network is quiet, nothing is queued, every agent knows the same best bid for every task, every bundle matches, and no bundle phase would add a task.

Source§

type Message = Packet

What the agent broadcasts.
Source§

fn id(&self) -> AgentId

The agent’s identifier.
Source§

fn bundle(&self) -> &Bundle<TaskId>

The tasks the agent holds now.

Auto Trait Implementations§

§

impl<V> Forget for Agent<V>

§

impl<V> Freeze for Agent<V>
where V: Freeze,

§

impl<V> Move for Agent<V>

§

impl<V> RefUnwindSafe for Agent<V>
where V: RefUnwindSafe,

§

impl<V> Send for Agent<V>
where V: Send,

§

impl<V> Sync for Agent<V>
where V: Sync,

§

impl<V> Unpin for Agent<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for Agent<V>
where V: UnsafeUnpin,

§

impl<V> UnwindSafe for Agent<V>
where V: 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.