Skip to main content

StandardModel

Struct StandardModel 

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

Discrete-time agent-based model.

This is the main model type for tick-based simulations. It stores agents in an AgentStore, uses a Scheduler to determine activation order, and calls user-provided step functions each tick.

§Type Parameters

  • S - space type (e.g. NothingSpace, Grid2D)
  • A - agent type implementing Agent
  • Store - agent container implementing AgentStore<A>
  • Props - user-defined model properties (use () if unused)
  • R - RNG type (e.g. StdRng)
  • Sch - scheduler type (e.g. Fastest, Randomly)

§Example

type MyModel = StandardModel<NothingSpace, Particle, HashMapStore<Particle>, (), StdRng, Fastest>;

let mut model = MyModel::new(store, space, Fastest::new(), (), rng, Some(Box::new(step_fn)), None, true);
model.step_n(100);

Implementations§

Source§

impl<S, A, Store, Props, R, Sch> StandardModel<S, A, Store, Props, R, Sch>
where A: PositionedAgent, S: SpaceInteraction<A>, Store: AgentStore<A>, R: RngCore, Sch: Scheduler<Self>,

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_spatial(&mut self) -> Result<(), InteractionError<S::Error>>

Advance one tick while applying deferred add/remove actions to both the agent store and the spatial index.

Source

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

Advance n spatially-consistent ticks.

Source§

impl<S, A, Store, Props, R, Sch> StandardModel<S, A, Store, Props, R, Sch>
where A: Agent, S: Space, Store: AgentStore<A>, R: RngCore, Sch: Scheduler<Self>,

Source

pub fn new( agents: Store, space: S, scheduler: Sch, properties: Props, rng: R, agent_step_ctx: Option<AgentStepFn<S, A, Props, R, Sch>>, model_step: Option<fn(&mut Self)>, agents_first: bool, ) -> Self

Create a new StandardModel.

§Arguments
  • agents - pre-populated agent store.
  • space - the simulation space.
  • scheduler - controls agent activation order.
  • properties - user-defined model properties.
  • rng - seeded random number generator.
  • agent_step_ctx - optional per-agent step function.
  • model_step - optional per-tick model step function.
  • agents_first - if true, agent steps run before the model step.
Source

pub fn new_base( agents: Store, space: S, scheduler: Sch, properties: Props, rng: R, ) -> Self

Create a model with no step functions configured yet.

This is the most ergonomic starting point for external consumers who want to configure the model via builder-style methods.

Defaults:

  • no agent_step_ctx
  • no model_step
  • agents_first = true
Source

pub fn new_with_agent_step( agents: Store, space: S, scheduler: Sch, properties: Props, rng: R, agent_step_ctx: impl for<'a> FnMut(&mut A, &mut StepContext<'a, S, A, Props, R, Sch>) + 'static, agents_first: bool, ) -> Self

Create a model with an agent step configured and no model step.

This avoids Some(Box::new(...)) boilerplate in the common case of a purely agent-driven simulation.

Source

pub fn new_with_model_step( agents: Store, space: S, scheduler: Sch, properties: Props, rng: R, model_step: fn(&mut Self), agents_first: bool, ) -> Self

Create a model with a model step configured and no agent step.

Source

pub fn with_agent_step_ctx( self, agent_step_ctx: impl for<'a> FnMut(&mut A, &mut StepContext<'a, S, A, Props, R, Sch>) + 'static, ) -> Self

Builder-style method to set or replace the agent step function.

Source

pub fn with_model_step(self, model_step: fn(&mut Self)) -> Self

Builder-style method to set or replace the model step function.

Source

pub fn with_agents_first(self, agents_first: bool) -> Self

Builder-style method to configure whether agent steps run before model steps.

Source

pub fn time(&self) -> Time

Current simulation time.

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 (the agent is returned back to the caller).

Source

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

Remove an agent by ID, returning it if found.

Source

pub fn next_id(&mut self) -> AgentId

Generate the next unused agent ID (monotonically increasing).

Source

pub fn step(&mut self)

Advance the simulation by one time step.

Runs agent steps and model step in the order determined by agents_first, then increments time by 1.

Source

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

Advance the simulation by n time steps.

Source

pub fn run(&mut self, steps: usize)

Alias for step_n.

Source

pub fn agents(&self) -> impl Iterator<Item = Ref<'_, A>>

Iterator over all agents in the store.

Returns borrowed references. The order depends on the store implementation and must not be treated as a stable replay contract unless the store explicitly guarantees it.

Source

pub fn agents_len(&self) -> usize

Number of agents currently in the store.

Trait Implementations§

Source§

impl<S, A, Store, Props, R, Sch> HasAgentIds for StandardModel<S, A, Store, Props, R, Sch>
where A: Agent, S: Space, Store: AgentStore<A>, R: RngCore,

Source§

fn agent_ids(&self) -> Vec<AgentId>

Collect all agent IDs into a new Vec.
Source§

fn agent_ids_into(&self, buf: &mut Vec<AgentId>)

Append all agent IDs into buf (does not clear it first).
Source§

impl<S, A, Store, Props, R, Sch> Model for StandardModel<S, A, Store, Props, R, Sch>
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, Sch> !Freeze for StandardModel<S, A, Store, Props, R, Sch>

§

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

§

impl<S, A, Store, Props, R, Sch> !Send for StandardModel<S, A, Store, Props, R, Sch>

§

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

§

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

§

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

§

impl<S, A, Store, Props, R, Sch> !UnwindSafe for StandardModel<S, A, Store, Props, R, Sch>

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