Skip to main content

Simulation

Struct Simulation 

Source
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>

Source

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.

Source

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.

Source

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.

Source

pub fn is_settled(&self) -> bool

Whether the team has settled: see Participant::is_settled. Nothing changes after that.

Source

pub fn agents(&self) -> &[A]

The team, indexed by agent.

Source

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.

Source

pub const fn tick(&self) -> u64

The current tick: how many steps have run.

Source

pub const fn stats(&self) -> Stats

Message counts so far.

Source

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>
where A::Message: Clone,

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<A: Debug + Participant> Debug for Simulation<A>
where A::Message: Debug,

Source§

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

Formats the value using the given formatter. Read more

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>
where Vec<A>: Freeze, Vec<InFlight<<A as Participant>::Message>>: Freeze, Vec<Vec<<A as Participant>::Message>>: Freeze,

§

impl<A> Move for Simulation<A>

§

impl<A> RefUnwindSafe for Simulation<A>

§

impl<A> Unpin for Simulation<A>
where Vec<A>: Unpin, Vec<InFlight<<A as Participant>::Message>>: Unpin, Vec<Vec<<A as Participant>::Message>>: Unpin,

§

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> 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.