Comparison

There are numerous modeling and simulation tools for ABMs, each with their own particular focus and style (find an overview here). The three main distinguishing features of agentpy are the following:

  • Agentpy integrates the multiple tasks of agent-based modeling - model design, interactive simulations, numerical experiments, and data analysis - within a single environment and is optimized for interactive computing with IPython and Jupyter.

  • Agentpy is designed for scientific use with experiments over multiple runs. It provides tools for parameter sampling (similar to NetLogo’s BehaviorSpace), Monte Carlo experiments, stochastic processes, parallel computing, and sensitivity analysis.

  • Agentpy is written in Python, one of the world’s most popular programming languages that offers a vast number of tools and libraries for scientific use. It is further designed for compatibility with established packages like numpy, scipy, networkx, pandas, ema_workbench, seaborn, and SALib.

The main alternative to agentpy in Python is Mesa. To allow for an comparison of the syntax, here are two examples for a simple model of wealth transfer, both of which realize exactly the same operations. More information on the two models can be found in the documentation of each framework (Agentpy & Mesa).

Agentpy

Mesa

import agentpy as ap





class MoneyAgent(ap.Agent):

    def setup(self):
        self.wealth = 1

    def wealth_transfer(self):
        if self.wealth == 0:
            return
        a = self.model.agents.random()
        a.wealth += 1
        self.wealth -= 1



class MoneyModel(ap.Model):

    def setup(self):
        self.agents = ap.AgentList(
            self, self.p.n, MoneyAgent)

    def step(self):
        self.agents.record('wealth')
        self.agents.wealth_transfer()










# Perform single run
parameters = {'n': 10, 'steps': 10}
model = MoneyModel(parameters)
results = model.run()

# Perform multiple runs
variable_params = {
    'n': ap.IntRange(10, 500), 
    'steps': 10
}
sample = ap.Sample(variable_params, n=49)
exp = ap.Experiment(
    MoneyModel,
    sample,
    iterations=5,
    record=True
)
results = exp.run()
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.batchrunner import BatchRunner
from mesa.datacollection \
    import DataCollector

class MoneyAgent(Agent):

    def __init__(self, unique_id, model):
        super().__init__(unique_id, model)
        self.wealth = 1

    def step(self):
        if self.wealth == 0:
            return
        other_agent = self.random.choice(
            self.model.schedule.agents)
        other_agent.wealth += 1
        self.wealth -= 1

class MoneyModel(Model):

    def __init__(self, N):
        self.running = True
        self.num_agents = N
        self.schedule = \
            RandomActivation(self)
        for i in range(self.num_agents):
            a = MoneyAgent(i, self)
            self.schedule.add(a)

        self.collector = DataCollector(
            agent_reporters={
                "Wealth": "wealth"})

    def step(self):
        self.collector.collect(self)
        self.schedule.step()

# Perform single run
model = MoneyModel(10)
for i in range(10):
    model.step()

# Perform multiple runs
variable_params = {
    "N": range(10, 500, 10)}

batch_run = BatchRunner(
    MoneyModel,
    variable_params,
    iterations=5,
    max_steps=10,
    agent_reporters={"Wealth": "wealth"}
)

batch_run.run_all()

The following table further provides a comparison of the main features of each framework.

Feature

Agentpy

Mesa

Containers
Sequence classes
like AgentList and AgentDList
Scheduler classes for
different activation orders
Topologies
Spatial grid, continuous space,
network
Spatial grid, continuous space,
network
Data recording
Recording methods for variables
of agents, environments, and
model; as well as reporters
DataCollector class that can
collect variables of agents
and model
Parameter sampling
Classes for sample generation
and different types of
parameter ranges
Multi-run experiments
Experiment class that supports
multiple iterations, parameter
samples, randomization,
and parallel processing
BatchRunner class that supports
multiple iterations and parameter
ranges
Output data
DataDict class to store, save,
load, and re-arrange output data
Methods to generate dataframes

Visualization
Gridplots, animations,
and interactive visualization
within Jupyter Notebooks
Plots and interactive visualization
in a separate web-server
Analysis
Tools for data arrangement and
sensitivity analysis