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.
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.
Trait Implementations§
Source§impl<A: Clone + Participant> Clone for Simulation<A>
impl<A: Clone + Participant> Clone for Simulation<A>
Auto Trait Implementations§
impl<A> !Send for Simulation<A>
impl<A> !Sync for Simulation<A>
impl<A> Forget for Simulation<A>
impl<A> Freeze for Simulation<A>
impl<A> Move for Simulation<A>
impl<A> RefUnwindSafe for Simulation<A>where
Vec<A>: RefUnwindSafe,
Vec<InFlight<<A as Participant>::Message>>: RefUnwindSafe,
Vec<Vec<<A as Participant>::Message>>: RefUnwindSafe,
impl<A> Unpin for Simulation<A>
impl<A> UnsafeUnpin for Simulation<A>where
Vec<A>: UnsafeUnpin,
Vec<InFlight<<A as Participant>::Message>>: UnsafeUnpin,
Vec<Vec<<A as Participant>::Message>>: UnsafeUnpin,
impl<A> UnwindSafe for Simulation<A>where
Vec<A>: UnwindSafe,
Vec<InFlight<<A as Participant>::Message>>: UnwindSafe,
Vec<Vec<<A as Participant>::Message>>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more