Skip to main content

Agent

Struct Agent 

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

One CBBA agent: its valuation, its bundle, and what it knows about every task’s winner.

§Examples

Two agents that can each take one task, exchanging messages directly:

use task_allocation::{AgentId, TaskId, Valuation, cbba::Agent};

/// Values the two tasks at fixed rewards, for at most one task.
struct Single([u64; 2]);

impl Valuation<TaskId> for Single {
    fn marginal_score(&self, path: &[TaskId], task: &TaskId) -> Option<u64> {
        path.is_empty().then_some(self.0[task.index()])
    }
}

let mut a = Agent::new(AgentId(0), 2, 2, 1, Single([5, 4]));
let mut b = Agent::new(AgentId(1), 2, 2, 1, Single([6, 1]));

for _ in 0..3 {
    a.build_bundle();
    b.build_bundle();
    let (to_b, to_a) = (a.message(), b.message());
    b.receive(&to_b);
    a.receive(&to_a);
}

assert_eq!(a.bundle().path(), [TaskId(1)]);
assert_eq!(b.bundle().path(), [TaskId(0)]);
assert_eq!(a.winner(TaskId(0)), Some(AgentId(1)));

Implementations§

Source§

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

Source

pub const fn with_cadence(self, cadence: Cadence) -> Self

This agent, broadcasting as cadence says.

§Panics

Panics when cadence refreshes after zero rounds.

Source

pub fn broadcast_due(&mut self) -> bool

Whether this round calls for a broadcast under the agent’s Cadence. Call it once per round, after the bundle phase; a round without a broadcast counts towards the refresh interval.

Source§

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

Source

pub fn add_task(&mut self) -> TaskId

Adds a task after the existing ones and returns its identifier; the next bundle phase may bid on it. Existing bundles stay as they are.

Source

pub fn retire_task(&mut self, task: TaskId) -> bool

Retires task, because it is done or cancelled: nobody wins it any more, and this agent never bids on it again. If this agent holds it, it gives it up together with the tasks it added after it, whose gains assumed it. Returns whether this agent held it.

§Panics

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

Source

pub fn forget_agent(&mut self, agent: AgentId)

Records that agent left the team: its claims are void, now and when relayed later, and its messages are ignored. Its tasks become free to bid on.

§Panics

Panics when agent is not part of the team.

Source

pub fn leave(&mut self)

Makes this agent leave the team: it gives up its whole bundle and bids no more. The others should forget it.

Source§

impl<V> Agent<V>

Source

pub const fn has_left(&self) -> bool

Whether this agent left the team.

Source

pub fn has_departed(&self, agent: AgentId) -> bool

Whether this agent was told that agent left the team.

§Panics

Panics when agent is not part of the team.

Source§

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

Source

pub const fn with_bid_warping(self) -> Self

This agent, warping its bids so that they never grow along its bundle: for valuations whose marginal gains do not diminish.

The agent ranks tasks by their true marginal gain, but bids at most what it bid on the task it added last; see the module docs for the method and its limits.

With diminishing gains and Replan::Full, warping changes nothing. With Replan::Keep it can: a task another agent held while this one filled later slots may be worth more, once freed, than the last bid, and is then bid at most that.

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 const fn with_replan(self, replan: Replan) -> Self

This agent, releasing part of its bundle before every bundle phase as replan says.

The default, Replan::Keep, is CBBA as published in 2009.

§Panics

Panics when a bundle budget below the capacity is set and replan releases as much per phase as the budget re-adds, or more: Replan::Full, or Replan::Last of the budget or more.

Source

pub fn with_bundle_budget(self, additions: usize) -> Self

This agent, adding at most additions tasks per bundle phase, so that the phase’s work fits a frame: each added task costs one pass over every task the agent can still win. A bundle then fills over several rounds, and the team keeps negotiating meanwhile.

The limit counts tasks added after the release. A budget below the capacity therefore only works with Replan::Keep, or with Replan::Last of fewer than additions: a replan that releases as many or more would stop the bundle from growing for good, while the team counts as settled.

§Panics

Panics when additions is zero, since the agent could never bid, and when it is below the capacity while the agent replans as much as it allows or more (Replan::Full, or Replan::Last of additions or more).

Source

pub fn build_bundle(&mut self) -> usize

The bundle phase: releases what Replan says, then adds the most valuable tasks this agent can win until its bundle is full or nothing is left to win; returns the number of tasks added after the release.

Each added task’s bid is its marginal gain, capped at the previous bid when this agent warps its bids, and this agent becomes its winner in its own tables. With Replan::Keep, the bundle grows in place, without allocating once the bundle has reached its capacity before.

Each call counts as a round for quiet_rounds.

Source

pub fn can_bid(&self) -> bool

Whether the bundle phase would change anything now: this agent would add a task, or, when it replans, rebuild its bundle differently.

With Replan::Keep this looks for one winnable task. Otherwise it runs a whole bundle phase on a copy, as costly as build_bundle.

Source

pub fn message(&mut self) -> Message

The message this agent broadcasts now: its winner tables and what it has seen of every agent. Advances this agent’s clock.

Source

pub fn receive(&mut self, message: &Message) -> bool

The consensus phase for one message: merges message into this agent’s winner tables, and drops the tasks it was outbid on along with every task added after them. Returns whether this agent’s tables or bundle changed.

Messages from this agent itself, from agents outside the team or departed, or older than what this agent has already seen of the sender are ignored. A message about fewer or more tasks than this agent knows is merged over the tasks both know.

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 winner(&self, task: TaskId) -> Option<AgentId>

The winner this agent believes in for task; None when it knows of no bid.

§Panics

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

Source

pub fn winning_bid(&self, task: TaskId) -> u64

The winning bid this agent believes in for task; zero when it knows of no bid.

§Panics

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

Source

pub fn winners(&self) -> impl ExactSizeIterator<Item = Option<AgentId>> + '_

The winner this agent believes in for every task, in task order.

Source

pub const fn quiet_rounds(&self) -> u64

How many bundle phases in a row this agent’s winners and bids stayed the same, counting changes from messages, team changes and its own bids.

Source

pub const fn is_locally_settled(&self, span: u64) -> bool

This agent’s own guess that the team has settled: its winners and bids stayed the same for at least span bundle phases, the separation time of ACBBA’s sources (Johnson’s 2012 MS thesis, pp. 82–84).

Unlike Participant::is_settled, which sees the whole team and every message in flight, this needs nothing but the agent, so a deployed agent can use it. It is a heuristic and can be wrong both ways. A lost message, a partition or a slow relay looks just like agreement, so it can report settled too early; Rantanen et al. (IEEE SAM 2018) found ACBBA agents taking lost packets for consensus. And it waits out the whole span after the team agreed. A safe span exceeds the longest time news takes to cross the network, which grows with its diameter, delays and losses.

Source

pub fn is_consistent(&self) -> bool

Whether this agent’s bundle holds exactly the tasks it believes it wins.

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<Message>)

Runs the bundle phase and broadcasts the agent’s full state when its Cadence calls for it; by default, every round.

Source§

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

Runs the consensus phase for message; CBBA answers in the next round, not now.

Source§

fn message_bytes(message: &Message) -> u64

The sender, every task’s winner and bid, and every agent’s clock.

Source§

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

The team agrees on every winner, every bundle matches, no agent’s bundle phase would change anything, and every message in flight carries the agreed winners. A team that has not bid yet is not settled.

Agents that left the team, and messages they sent, are disregarded.

Source§

type Message = Message

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.