run_to_convergence() manage the loop automatically, or call is_converged() yourself to decide what to do at each step.
How convergence is measured
At the end of every timestep (when running automatically), Emergent collects the value ofconvergence_data_key from every node and computes the standard deviation across the population:
convergence_std_dev, the simulation is considered converged.
Convergence only makes sense for numeric node attributes. The standard deviation calculation will raise an error if the attribute values cannot be collected into a numpy array.
Parameters
convergence_data_key
convergence_data_key
The node attribute to monitor. Must be set before calling
run_to_convergence(). The attribute must exist on every node after initialize_graph() completes.convergence_std_dev
convergence_std_dev
Standard deviation threshold. The model is converged when
std(values) <= convergence_std_dev. A higher value makes convergence easier to reach; a lower value demands tighter agreement.run_to_convergence
run_to_convergence() calls your timestep_function repeatedly until the population converges or the timestep limit is reached. It returns the number of timesteps executed.
MAX_TIMESTEPS without converging, run_to_convergence() stops and returns MAX_TIMESTEPS. It does not raise an exception.
is_converged
is_converged(data_key, std_dev) checks convergence at any point without advancing the simulation. Use it for manual control loops or conditional logic:
is_converged takes the data key and threshold as explicit arguments rather than reading from model parameters, so you can check multiple attributes with different thresholds in the same loop.
Adjusting the timestep limit
The default maximum is 100,000 timesteps. Change it withchange_max_timesteps():
run_to_convergence(). Manual loops using timestep() and is_converged() are not subject to this limit.
Tuning convergence settings
Simulation never converges
Simulation never converges
If
run_to_convergence() always returns MAX_TIMESTEPS, your threshold may be too tight for the dynamics of your model.- Raise
convergence_std_dev: A larger threshold is easier to meet. Try doubling it and see whether the simulation converges earlier. - Check your timestep function: Make sure agents are actually moving toward agreement. Print the standard deviation every 1,000 steps to see if it is decreasing.
- Check your graph topology: A cycle graph with many nodes can be very slow to converge. Try switching to
"complete"as a sanity check.
Simulation converges too quickly
Simulation converges too quickly
If convergence happens in very few timesteps,
convergence_std_dev may be too high — the initial node values might already satisfy the threshold.- Lower
convergence_std_dev: Require tighter agreement between agents. - Check your initial data function: If all nodes start with the same value, the simulation is converged before it begins.
Choosing the right convergence_std_dev
Choosing the right convergence_std_dev
The right threshold depends on the range of values your node attribute can take:
- Opinions in [0, 1]: A threshold of
0.01to0.1gives precise convergence.0.5would mean agents with opinions of0and1are considered converged. - Integer counts: Use a threshold proportional to the expected final spread — e.g.,
1.0for counts in the hundreds. - Unbounded values: Consider normalizing values before convergence checks, or use a threshold relative to the initial standard deviation.
convergence_data_key and convergence_std_dev, and Agent model for the full simulation lifecycle.