pub struct Simulation<A: Participant> { /* private fields */ }Expand description
A team of agents on a simulated network.
§Examples
use task_allocation::{AgentId, TaskId, Valuation, cbba::Agent, sim::{Network, Simulation}};
/// Values task `t` at `10 + t`, minus 3 for every task already held.
struct Worker;
impl Valuation<TaskId> for Worker {
fn marginal_score(&self, path: &[TaskId], task: &TaskId) -> Option<u64> {
(10 + task.index() as u64).checked_sub(3 * path.len() as u64)
}
}
let agents = (0..3).map(|id| Agent::new(AgentId(id), 3, 5, 2, Worker)).collect();
let network = Network { seed: 7, max_delay: 3, loss_per_mille: 200, ..Network::default() };
let mut simulation = Simulation::new(agents, network);
let ticks = simulation.run_until_settled(500).expect("settles");
assert!(simulation.assignment().is_conflict_free());
assert!(ticks <= 500);Implementations§
Source§impl<A: Participant> Simulation<A>
impl<A: Participant> Simulation<A>
Sourcepub fn new(agents: Vec<A>, network: Network) -> Self
pub fn new(agents: Vec<A>, network: Network) -> Self
A simulation of agents on network, at tick zero with nothing in flight.
§Panics
Panics when the agents are not identified 0..agents.len() in order, and when the
network’s Clusters::period is zero.
Sourcepub fn step(&mut self)
pub fn step(&mut self)
Runs one tick: delivers the messages due, runs every agent’s round, and broadcasts what the agents put in their outboxes.
Sourcepub fn run_until_settled(&mut self, max_ticks: u64) -> Option<u64>
pub fn run_until_settled(&mut self, max_ticks: u64) -> Option<u64>
Steps until the team is settled, at most max_ticks times;
returns the tick at which it settled, or None when it did not within the limit.
Sourcepub fn is_settled(&self) -> bool
pub fn is_settled(&self) -> bool
Whether the team has settled: see Participant::is_settled. Nothing changes after
that.
Sourcepub fn agents_mut(&mut self) -> &mut [A]
pub fn agents_mut(&mut self) -> &mut [A]
The team, to change it between steps: add a task, retire one, or let an agent leave.
Sourcepub fn assignment(&self) -> Assignment
pub fn assignment(&self) -> Assignment
Every agent’s bundle, as one assignment. It is conflict-free once the team is settled; before, agents may still hold the same task.