Skip to main content

Overview

This example models opinion dynamics — a classic ABM scenario where each agent holds a real-valued opinion and gradually shifts toward the average opinion of its neighbors. Over time, agents converge toward consensus. It demonstrates:
  • Setting up parameters and a custom graph
  • Writing initial data and timestep functions
  • Running to convergence
  • Reading final state

The simulation

What to expect

On a cycle graph, information only travels between immediate neighbors, so convergence is slower than on a complete graph. With 30 nodes and a convergence threshold of 0.005, you should see the simulation settle in a few hundred timesteps with all agents very close to a shared mean opinion.

Variations to try

Change graph_type to "complete" to see how convergence speed changes when every agent can directly influence every other agent.
Replace the built-in graph with a Barabási–Albert graph to model opinion dynamics on a social network with hubs.
Introduce a small random perturbation at each step to prevent perfect convergence and model real-world uncertainty.