Skip to main content
Every AgentModel maintains an internal parameter dictionary. Built-in parameters control graph construction and convergence detection. You can also add custom parameters to pass data into your behavior functions without relying on global variables.

Built-in parameters

These four parameters are always present and cannot be deleted:
num_nodes
number
default:"3"
Number of nodes to create when initialize_graph() builds the graph. Ignored if you supply a custom graph via set_graph().
graph_type
string
default:"complete"
Graph topology for initialize_graph(). Accepted values: "complete", "cycle", "wheel". See Graph structures.
convergence_data_key
string
default:"None"
Node attribute monitored by run_to_convergence() and is_converged(). Must be set before running to convergence. See Convergence.
convergence_std_dev
number
default:"100"
Standard deviation threshold for convergence. The simulation stops when the std dev of convergence_data_key across all nodes is at or below this value.

Managing parameters

update_parameters

Add new parameters or update existing ones by passing a dictionary:
model.update_parameters({
    "num_nodes": 50,
    "graph_type": "cycle",
    "convergence_data_key": "opinion",
    "convergence_std_dev": 0.5,
    # Custom parameters
    "mutation_rate": 0.01,
    "environment": "hostile",
})
Calling update_parameters with a key that already exists overwrites the previous value. Calling it with a new key adds the parameter.

delete_parameters

Remove one or more custom parameters by passing a list of keys:
model.delete_parameters(["mutation_rate", "environment"])
Calling delete_parameters with no arguments (or None) resets the entire parameter dictionary to its default state:
# Reset to defaults — removes all custom parameters
model.delete_parameters()
Attempting to delete a built-in parameter (num_nodes, graph_type, convergence_data_key, or convergence_std_dev) raises a KeyError. You can update their values but not remove them from the dictionary.

list_parameters

Returns a list of all current parameter keys, including any custom ones you have added:
model.update_parameters({"alpha": 0.9, "beta": 1.2})
print(model.list_parameters())
# ['num_nodes', 'graph_type', 'convergence_data_key', 'convergence_std_dev', 'alpha', 'beta']
This is useful for inspecting the model state at runtime or before passing it to a function.

Subscript access

You can read and write any parameter using dictionary-style subscript notation:
# Read
print(model["num_nodes"])      # 3
print(model["graph_type"])     # "complete"
print(model["alpha"])          # 0.9

# Write
model["num_nodes"] = 100
model["alpha"] = 0.5
Subscript writes are equivalent to calling update_parameters({key: value}). There is no restriction on which parameters you can write this way — including the built-in ones.

Custom parameters in behavior functions

Both initial_data_function and timestep_function receive the AgentModel instance as their first argument. This means they can read any parameter directly:
model.update_parameters({
    "infection_rate": 0.3,
    "recovery_rate": 0.05,
})

def timestep(model):
    infection_rate = model["infection_rate"]
    recovery_rate = model["recovery_rate"]
    graph = model.get_graph()
    for node, data in graph.nodes(data=True):
        if data["state"] == "susceptible":
            infected_neighbors = sum(
                1 for n in graph.neighbors(node)
                if graph.nodes[n]["state"] == "infected"
            )
            if infected_neighbors > 0 and random.random() < infection_rate:
                graph.nodes[node]["state"] = "infected"
        elif data["state"] == "infected":
            if random.random() < recovery_rate:
                graph.nodes[node]["state"] = "recovered"

model.set_timestep_function(timestep)
Prefer storing runtime values as model parameters over global variables. Parameters travel with the model object, making your simulation easier to serialize, reproduce, and pass to helper functions.
You can also update parameters between simulation runs to sweep across values:
results = {}
for rate in [0.1, 0.2, 0.3, 0.5]:
    model.update_parameters({"infection_rate": rate})
    model.initialize_graph()
    steps = model.run_to_convergence()
    results[rate] = steps
See Agent model for the full lifecycle context, and Convergence for how convergence_data_key and convergence_std_dev are used at runtime.

Build docs developers (and LLMs) love