Skip to main content

Overview

AgentModel is the single object that ties your simulation together. It holds:
  • A parameter store for controlling model behavior
  • A NetworkX graph where each node represents one agent
  • A pluggable initial data function and timestep function
  • Built-in convergence detection

Parameters

Parameters are stored in an internal key-value dictionary. Four keys are built-in and always present: You can add any additional parameters your simulation needs:
The four default parameters (num_nodes, graph_type, convergence_data_key, convergence_std_dev) cannot be deleted via delete_parameters(). Attempting to do so raises a KeyError.

Accessing parameters

Use dictionary-style access to read or set individual parameters:
Or list all current parameter keys:

The graph

Emergent uses NetworkX graphs internally. Each node is an agent; node data is a dictionary you populate via your initial data function and mutate in your timestep function.

Built-in graph types

Set graph_type before calling initialize_graph():

Custom graphs

You can bypass the built-in types and provide your own NetworkX graph directly:
When using a custom graph, call set_graph() before initialize_graph(). The initialize_graph() call will populate nodes with data using your initial data function but will not recreate the graph topology.

Simulation functions

Initial data function

Called once per node during initialize_graph(). Returns a dictionary that is merged into the node’s data store.

Timestep function

Called once per call to timestep(). Receives the model and should mutate graph node data directly.

Convergence

run_to_convergence() runs timesteps in a loop and stops when the standard deviation of a tracked node attribute drops to or below convergence_std_dev.
The default max timesteps cap is 100,000. Change it with:
You can also check convergence manually at any time: