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>
impl<V: Valuation<TaskId>> Agent<V>
Sourcepub fn new(
id: AgentId,
agents: usize,
tasks: usize,
capacity: usize,
valuation: V,
) -> Self
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.
Sourcepub const fn with_replan(self, replan: Replan) -> Self
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.
Sourcepub fn build_bundle(&mut self) -> usize
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, and this agent becomes its winner in its own tables.
Sourcepub fn can_bid(&self) -> bool
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.
Sourcepub fn message(&mut self) -> Message
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.
Sourcepub fn receive(&mut self, message: &Message) -> bool
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, of a different task count, or older than what this agent has already seen of the sender are ignored.
Source§impl<V> Agent<V>
impl<V> Agent<V>
Sourcepub const fn bundle(&self) -> &Bundle<TaskId>
pub const fn bundle(&self) -> &Bundle<TaskId>
The tasks this agent holds, with the bid it placed on each.
Sourcepub fn winner(&self, task: TaskId) -> Option<AgentId>
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.
Sourcepub fn winning_bid(&self, task: TaskId) -> u64
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.
Sourcepub fn winners(&self) -> impl ExactSizeIterator<Item = Option<AgentId>> + '_
pub fn winners(&self) -> impl ExactSizeIterator<Item = Option<AgentId>> + '_
The winner this agent believes in for every task, in task order.
Sourcepub fn is_consistent(&self) -> bool
pub fn is_consistent(&self) -> bool
Whether this agent’s bundle holds exactly the tasks it believes it wins.
Trait Implementations§
Source§impl<V: Valuation<TaskId>> Participant for Agent<V>
impl<V: Valuation<TaskId>> Participant for Agent<V>
Source§fn act(&mut self, _now: u64, outbox: &mut Vec<Message>)
fn act(&mut self, _now: u64, outbox: &mut Vec<Message>)
Runs the bundle phase and broadcasts the agent’s full state, every round.
Source§fn receive(&mut self, _now: u64, message: &Message, _outbox: &mut Vec<Message>)
fn receive(&mut self, _now: u64, message: &Message, _outbox: &mut Vec<Message>)
Runs the consensus phase for message; CBBA answers in the next round, not now.
Source§fn is_settled(team: &[Self], in_flight: &[&Message]) -> bool
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.