pub struct StandardModel<S, A, Store, Props, R, Sch>{ /* 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 implementingAgentStore- agent container implementingAgentStore<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>,
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>,
Sourcepub fn insert_positioned_agent(
&mut self,
agent: A,
) -> Result<(), InteractionError<S::Error>>
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.
Sourcepub fn remove_positioned_agent(
&mut self,
id: AgentId,
) -> Result<Option<A>, InteractionError<S::Error>>
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.
Sourcepub fn move_positioned_agent(
&mut self,
id: AgentId,
new_position: A::Position,
) -> Result<(), InteractionError<S::Error>>
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.
Sourcepub fn validate_space_index(&self) -> Result<(), InteractionError<S::Error>>
pub fn validate_space_index(&self) -> Result<(), InteractionError<S::Error>>
Validate that all stored agents are represented by the spatial index.
Sourcepub fn step_spatial(&mut self) -> Result<(), InteractionError<S::Error>>
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.
Sourcepub fn run_spatial(
&mut self,
steps: usize,
) -> Result<(), InteractionError<S::Error>>
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>
impl<S, A, Store, Props, R, Sch> StandardModel<S, A, Store, Props, R, Sch>
Sourcepub 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
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- iftrue, agent steps run before the model step.
Sourcepub fn new_base(
agents: Store,
space: S,
scheduler: Sch,
properties: Props,
rng: R,
) -> Self
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
Sourcepub 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
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.
Sourcepub fn new_with_model_step(
agents: Store,
space: S,
scheduler: Sch,
properties: Props,
rng: R,
model_step: fn(&mut Self),
agents_first: bool,
) -> Self
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.
Sourcepub 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
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.
Sourcepub fn with_model_step(self, model_step: fn(&mut Self)) -> Self
pub fn with_model_step(self, model_step: fn(&mut Self)) -> Self
Builder-style method to set or replace the model step function.
Sourcepub fn with_agents_first(self, agents_first: bool) -> Self
pub fn with_agents_first(self, agents_first: bool) -> Self
Builder-style method to configure whether agent steps run before model steps.
Sourcepub fn properties(&self) -> &Props
pub fn properties(&self) -> &Props
Immutable reference to user-defined properties.
Sourcepub fn properties_mut(&mut self) -> &mut Props
pub fn properties_mut(&mut self) -> &mut Props
Mutable reference to user-defined properties.
Sourcepub fn insert_agent(&mut self, agent: A) -> Result<(), A>
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).
Sourcepub fn remove_agent(&mut self, id: AgentId) -> Option<A>
pub fn remove_agent(&mut self, id: AgentId) -> Option<A>
Remove an agent by ID, returning it if found.
Sourcepub fn next_id(&mut self) -> AgentId
pub fn next_id(&mut self) -> AgentId
Generate the next unused agent ID (monotonically increasing).
Sourcepub fn step(&mut self)
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.
Sourcepub fn agents(&self) -> impl Iterator<Item = Ref<'_, A>>
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.
Sourcepub fn agents_len(&self) -> usize
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>
impl<S, A, Store, Props, R, Sch> HasAgentIds for StandardModel<S, A, Store, Props, R, Sch>
Source§impl<S, A, Store, Props, R, Sch> Model for StandardModel<S, A, Store, Props, R, Sch>
impl<S, A, Store, Props, R, Sch> Model for StandardModel<S, A, Store, Props, R, Sch>
Source§type Properties = Props
type Properties = Props
() if unused).Source§type AgentRef<'a> = Ref<'a, A>
where
Self: 'a
type AgentRef<'a> = Ref<'a, A> where Self: 'a
Ref<'a, A> for RefCell-based stores).Source§type AgentRefMut<'a> = RefMut<'a, A>
where
Self: 'a
type AgentRefMut<'a> = RefMut<'a, A> where Self: 'a
RefMut<'a, A>).