Skip to main content

EventQueueModel

Struct EventQueueModel 

Source
pub struct EventQueueModel<S, A, Store, Props, R>
where A: Agent, S: Space, Store: AgentStore<A>, R: RngCore,
{ /* private fields */ }
Expand description

Continuous-time agent-based model driven by an event queue.

§Type Parameters

  • S - space type
  • A - agent type implementing Agent
  • Store - agent container implementing AgentStore<A>
  • Props - user-defined model properties (use () if unused)
  • R - RNG type

§Example

let actions: Vec<fn(&mut MyAgent, &mut EventContext<...>)> = vec![tick_action];
let mut model = EventQueueModel::new(store, space, (), rng, actions);
model.add_event(agent_id, 0, 1.0); // fire action 0 at t+1.0
model.step_until(100.0);

Implementations§

Source§

impl<S, A, Store, Props, R> EventQueueModel<S, A, Store, Props, R>
where A: PositionedAgent, S: SpaceInteraction<A>, Store: AgentStore<A>, R: RngCore,

Source

pub fn insert_positioned_agent( &mut self, agent: A, ) -> Result<(), InteractionError<S::Error>>

Insert a positioned agent into both the store and the space atomically.

Source

pub fn remove_positioned_agent( &mut self, id: AgentId, ) -> Result<Option<A>, InteractionError<S::Error>>

Remove a positioned agent from both the store and the space atomically.

Source

pub fn move_positioned_agent( &mut self, id: AgentId, new_position: A::Position, ) -> Result<(), InteractionError<S::Error>>

Move a positioned agent, updating both the agent value and spatial index.

Source

pub fn validate_space_index(&self) -> Result<(), InteractionError<S::Error>>

Validate that all stored agents are represented by the spatial index.

Source

pub fn step_event_spatial(&mut self) -> Result<bool, InteractionError<S::Error>>

Process the next event and apply deferred add/remove actions to both the agent store and spatial index.

Source

pub fn step_until_spatial( &mut self, t_end: f64, ) -> Result<(), InteractionError<S::Error>>

Process all events up to and including time t_end with spatially consistent deferred lifecycle updates.

Source

pub fn run_events_spatial( &mut self, n: usize, ) -> Result<(), InteractionError<S::Error>>

Process up to n events with spatially consistent lifecycle updates.

Source§

impl<S, A, Store, Props, R> EventQueueModel<S, A, Store, Props, R>
where A: Agent, S: Space, Store: AgentStore<A>, R: RngCore,

Source

pub fn new( agents: Store, space: S, properties: Props, rng: R, actions: Vec<fn(&mut A, &mut EventContext<'_, S, A, Props, R>)>, ) -> Self

Create a new EventQueueModel.

§Arguments
  • agents - pre-populated agent store.
  • space - the simulation space.
  • properties - user-defined model properties.
  • rng - seeded random number generator.
  • actions - vector of event-action functions, indexed by event_idx.
Source

pub fn time_f64(&self) -> f64

Current model time as f64.

Source

pub fn rng_mut(&self) -> RefMut<'_, R>

Mutable access to the model’s RNG (via RefCell).

Source

pub fn space(&self) -> &S

Immutable reference to the simulation space.

Source

pub fn space_mut(&mut self) -> &mut S

Mutable reference to the simulation space.

Source

pub fn properties(&self) -> &Props

Immutable reference to user-defined properties.

Source

pub fn properties_mut(&mut self) -> &mut Props

Mutable reference to user-defined properties.

Source

pub fn agent(&self, id: AgentId) -> Option<Ref<'_, A>>

Borrow an agent immutably by ID.

Source

pub fn agent_mut(&self, id: AgentId) -> Option<RefMut<'_, A>>

Borrow an agent mutably by ID.

Source

pub fn insert_agent(&mut self, agent: A) -> Result<(), A>

Insert an agent into the store.

Returns Err(agent) if an agent with the same ID already exists.

Source

pub fn remove_agent(&mut self, id: AgentId) -> Option<A>

Remove an agent by ID, returning it if found.

Events targeting this agent will be silently skipped when they fire.

Source

pub fn next_id(&mut self) -> AgentId

Generate the next unused agent ID (monotonically increasing).

Source

pub fn add_event(&mut self, agent_id: AgentId, event_idx: usize, dt: f64)

Schedule a new event at self.time + dt.

§Panics

Panics if dt is negative, NaN, or infinite.

Source

pub fn queue_len(&self) -> usize

Number of events currently in the queue.

Source

pub fn queue_is_empty(&self) -> bool

Returns true if the event queue is empty.

Source

pub fn peek_time(&self) -> Option<f64>

Peek at the timestamp of the next (earliest) event, if any.

Source

pub fn step_event(&mut self) -> bool

Process the next event in the queue.

Returns true if an event was popped (even if the target agent was removed), false if the queue was empty.

Source

pub fn step_until(&mut self, t_end: f64)

Process all events up to and including time t_end.

After this call, self.time_f64() == t_end. Events scheduled after t_end remain in the queue.

Source

pub fn run_events(&mut self, n: usize)

Process up to n events from the queue.

Stops early if the queue is exhausted.

Trait Implementations§

Source§

impl<S, A, Store, Props, R> Model for EventQueueModel<S, A, Store, Props, R>
where A: Agent, S: Space, Store: AgentStore<A>, R: RngCore,

Source§

type Agent = A

The agent type stored in this model.
Source§

type Space = S

The space type used by this model.
Source§

type Properties = Props

User-defined model properties (can be () if unused).
Source§

type Rng = R

The random number generator type.
Source§

type AgentRef<'a> = Ref<'a, A> where Self: 'a

Borrowed reference to an agent (may be Ref<'a, A> for RefCell-based stores).
Source§

type AgentRefMut<'a> = RefMut<'a, A> where Self: 'a

Mutably borrowed reference to an agent (may be RefMut<'a, A>).
Source§

fn time(&self) -> Time

Current simulation time.
Source§

fn rng_mut(&self) -> impl DerefMut<Target = Self::Rng> + '_

Mutable access to the model’s RNG. Read more
Source§

fn space(&self) -> &Self::Space

Immutable reference to the simulation space.
Source§

fn properties(&self) -> &Self::Properties

Immutable reference to user-defined properties.
Source§

fn properties_mut(&mut self) -> &mut Self::Properties

Mutable reference to user-defined properties.
Source§

fn agent(&self, id: AgentId) -> Option<Self::AgentRef<'_>>

Look up an agent by ID, returning a borrowed reference.
Source§

fn agent_mut(&self, id: AgentId) -> Option<Self::AgentRefMut<'_>>

Look up an agent by ID, returning a mutably borrowed reference.
Source§

fn has_id(&self, id: AgentId) -> bool

Check whether an agent with the given ID exists.

Auto Trait Implementations§

§

impl<S, A, Store, Props, R> !Freeze for EventQueueModel<S, A, Store, Props, R>

§

impl<S, A, Store, Props, R> !RefUnwindSafe for EventQueueModel<S, A, Store, Props, R>

§

impl<S, A, Store, Props, R> Send for EventQueueModel<S, A, Store, Props, R>
where Store: Send, Props: Send, R: Send, A: Send,

§

impl<S, A, Store, Props, R> !Sync for EventQueueModel<S, A, Store, Props, R>

§

impl<S, A, Store, Props, R> Unpin for EventQueueModel<S, A, Store, Props, R>
where Store: Unpin, S: Unpin, Props: Unpin, A: Unpin, R: Unpin,

§

impl<S, A, Store, Props, R> UnsafeUnpin for EventQueueModel<S, A, Store, Props, R>
where Store: UnsafeUnpin, S: UnsafeUnpin, Props: UnsafeUnpin, R: UnsafeUnpin,

§

impl<S, A, Store, Props, R> UnwindSafe for EventQueueModel<S, A, Store, Props, R>
where Store: UnwindSafe, S: UnwindSafe, Props: UnwindSafe, A: UnwindSafe, R: 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more