Comparison

Agentpy vs. Mesa

An alternative framework for agent-based modeling in Python is Mesa. The stated goal of Mesa is “to be the Python 3-based counterpart to NetLogo, Repast, or MASON”. The focus of these frameworks is traditionally on spatial environments, with an interface where one can observe live dynamics and adjust parameters while the model is running.

Agentpy, in contrast, is more focused on networks and multi-run experiments, with tools to generate and analyze output data from these experiments. Agentpy further has a different model structure that is built around agent lists, which allow for simple selection and manipulation of agent groups; and environments, which can contain agents but also act as agents themselves.

To allow for an comparison of the syntax of each framework, 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 (link for 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.add_agents(
            self.p.agents, MoneyAgent)

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










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

# Perform multiple runs
parameters['agents'] = (10, 500, int)
sample = ap.sample(parameters, 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()

Finally, the following table provides a comparison of the main features of each framework.

Feature

Agentpy

Mesa

Customizable objects
Agent, Environment, Model
Agent, Model
Container classes
AgentList and EnvDict for
selection and manipulation
of agent and environment groups
Scheduler (see below)
Time management
Custom activation order has to be
defined in the Model.step method
Multiple scheduler classes for
different activation orders
Supported topologies
Spatial grid, networkx graph

Spatial grid, network grid,
continuous space
Data recording
Recording methods for variables
(of agents, environments, and
model) and evaluation measures
DataCollector class that can
collect variables of agents
and model
Parameter sampling
Multiple sampling functions
Custom sample has to be defined
Multi-run experiments
Experiment class that supports
multiple iterations, parameter
samples, scenario comparison,
and parallel processing
BatchRunner class that supports
multiple iterations and parameter
samples
Output data
DataDict class that can save,
load, and re-arrange output data
Multiple methods to generate
dataframes
Visualization
Tools for plots and animations,
and interactive visualization in
Python
Extensive browser-based
visualization module
Analysis
Tools for data arrangement and
sensitivity analysis