Model
- class ambr.model.Model(parameters: Dict[str, Any])[source]
Bases:
BaseModelBase class for all simulation models, using DataFrames for data storage.
- add_agent(agent: Agent)[source]
Add a single agent. Prefer
add_agents()for bulk creation.
- add_agents(n: int, *, agent_class: Type | None = None, **columns: Any)[source]
Bulk-create
nagents with columnar initial state:self.add_agents(100, wealth=self.nprandom.integers(1, 10, 100), status='S')
Scalar kwargs broadcast; list /
np.ndarray/pl.Seriesvalues must have lengthn. Passagent_class=to also spin up Python instances soAgentList.calland per-agent iteration work.
- property agents_df: DataFrame
- batch_update_agents(agent_ids: list, data: dict)[source]
Batch update multiple agents at once for better performance.
- Parameters:
agent_ids – List of agent IDs to update
data – Dictionary of column names and values (or lists of values)
The Model class is the foundation of any AMBER simulation. It provides the framework for:
Managing simulation time and execution
Storing and updating agent data
Recording model-level metrics
Coordinating agent behaviors
Basic Usage
import ambr as am
class MyModel(am.Model):
def setup(self):
# Initialize agents and environment
pass
def step(self):
# Define what happens each time step
pass
# Run the model
model = MyModel({'steps': 100, 'seed': 42})
results = model.run()
Key Methods
Lifecycle Methods:
setup()- Called once at the beginning to initialize the modelstep()- Called each time step to update agent statesupdate()- Called after step() to update model stateend()- Called once at the end of the simulation
Data Management:
add_agent(agent)- Add a new agent to the modelupdate_agent_data(agent_id, data)- Update data for a specific agentget_agent_data(agent_id)- Retrieve data for a specific agentrecord_model(name, value)- Record a model-level metric
Execution:
run()- Execute the full simulation and return resultsrun_step()- Execute a single time step