2022-10-17 18:23:57 +00:00
|
|
|
"""
|
2021-10-14 15:37:06 +00:00
|
|
|
This is an example that adds soil agents and environment in a normal
|
|
|
|
mesa workflow.
|
2022-10-17 18:23:57 +00:00
|
|
|
"""
|
2021-10-14 15:37:06 +00:00
|
|
|
from mesa import Agent as MesaAgent
|
|
|
|
from mesa.space import MultiGrid
|
2022-10-17 18:23:57 +00:00
|
|
|
|
2021-10-14 15:37:06 +00:00
|
|
|
# from mesa.time import RandomActivation
|
|
|
|
from mesa.datacollection import DataCollector
|
|
|
|
from mesa.batchrunner import BatchRunner
|
|
|
|
|
|
|
|
import networkx as nx
|
|
|
|
|
2022-10-16 15:54:03 +00:00
|
|
|
from soil import NetworkAgent, Environment, serialization
|
2021-10-14 15:37:06 +00:00
|
|
|
|
2022-10-17 18:23:57 +00:00
|
|
|
|
2021-10-14 15:37:06 +00:00
|
|
|
def compute_gini(model):
|
|
|
|
agent_wealths = [agent.wealth for agent in model.agents]
|
|
|
|
x = sorted(agent_wealths)
|
|
|
|
N = len(list(model.agents))
|
2022-10-17 18:23:57 +00:00
|
|
|
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
|
|
|
|
return 1 + (1 / N) - 2 * B
|
2021-10-14 15:37:06 +00:00
|
|
|
|
2022-10-16 15:54:03 +00:00
|
|
|
|
2021-10-14 15:37:06 +00:00
|
|
|
class MoneyAgent(MesaAgent):
|
|
|
|
"""
|
|
|
|
A MESA agent with fixed initial wealth.
|
|
|
|
It will only share wealth with neighbors based on grid proximity
|
|
|
|
"""
|
|
|
|
|
2022-10-16 15:54:03 +00:00
|
|
|
def __init__(self, unique_id, model, wealth=1):
|
2021-10-14 15:37:06 +00:00
|
|
|
super().__init__(unique_id=unique_id, model=model)
|
2022-10-16 15:54:03 +00:00
|
|
|
self.wealth = wealth
|
2021-10-14 15:37:06 +00:00
|
|
|
|
|
|
|
def move(self):
|
|
|
|
possible_steps = self.model.grid.get_neighborhood(
|
2022-10-17 18:23:57 +00:00
|
|
|
self.pos, moore=True, include_center=False
|
|
|
|
)
|
2021-10-14 15:37:06 +00:00
|
|
|
new_position = self.random.choice(possible_steps)
|
|
|
|
self.model.grid.move_agent(self, new_position)
|
|
|
|
|
|
|
|
def give_money(self):
|
|
|
|
cellmates = self.model.grid.get_cell_list_contents([self.pos])
|
|
|
|
if len(cellmates) > 1:
|
|
|
|
other = self.random.choice(cellmates)
|
|
|
|
other.wealth += 1
|
|
|
|
self.wealth -= 1
|
|
|
|
|
|
|
|
def step(self):
|
2022-10-16 15:54:03 +00:00
|
|
|
print("Crying wolf", self.pos)
|
2021-10-14 15:37:06 +00:00
|
|
|
self.move()
|
|
|
|
if self.wealth > 0:
|
|
|
|
self.give_money()
|
|
|
|
|
|
|
|
|
|
|
|
class SocialMoneyAgent(NetworkAgent, MoneyAgent):
|
|
|
|
wealth = 1
|
|
|
|
|
|
|
|
def give_money(self):
|
|
|
|
cellmates = set(self.model.grid.get_cell_list_contents([self.pos]))
|
2022-10-17 19:36:21 +00:00
|
|
|
friends = set(self.get_neighbors())
|
2021-10-14 15:37:06 +00:00
|
|
|
self.info("Trying to give money")
|
2022-10-16 15:54:03 +00:00
|
|
|
self.info("Cellmates: ", cellmates)
|
|
|
|
self.info("Friends: ", friends)
|
2021-10-14 15:37:06 +00:00
|
|
|
|
|
|
|
nearby_friends = list(cellmates & friends)
|
|
|
|
|
|
|
|
if len(nearby_friends):
|
|
|
|
other = self.random.choice(nearby_friends)
|
|
|
|
other.wealth += 1
|
|
|
|
self.wealth -= 1
|
|
|
|
|
2022-10-17 18:23:57 +00:00
|
|
|
|
2022-10-16 15:54:03 +00:00
|
|
|
def graph_generator(n=5):
|
|
|
|
G = nx.Graph()
|
|
|
|
for ix in range(n):
|
|
|
|
G.add_edge(0, ix)
|
|
|
|
return G
|
|
|
|
|
2021-10-14 15:37:06 +00:00
|
|
|
|
|
|
|
class MoneyEnv(Environment):
|
|
|
|
"""A model with some number of agents."""
|
2022-10-17 18:23:57 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
N,
|
|
|
|
generator=graph_generator,
|
|
|
|
agent_class=SocialMoneyAgent,
|
|
|
|
topology=None,
|
|
|
|
**kwargs
|
|
|
|
):
|
2022-10-16 15:54:03 +00:00
|
|
|
|
|
|
|
generator = serialization.deserialize(generator)
|
|
|
|
agent_class = serialization.deserialize(agent_class, globs=globals())
|
|
|
|
topology = generator(n=N)
|
2022-10-17 18:23:57 +00:00
|
|
|
super().__init__(topology=topology, N=N, **kwargs)
|
2021-10-14 15:37:06 +00:00
|
|
|
self.grid = MultiGrid(width, height, False)
|
|
|
|
|
2022-10-16 15:54:03 +00:00
|
|
|
self.populate_network(agent_class=agent_class)
|
|
|
|
|
2021-10-14 15:37:06 +00:00
|
|
|
# Create agents
|
|
|
|
for agent in self.agents:
|
|
|
|
x = self.random.randrange(self.grid.width)
|
|
|
|
y = self.random.randrange(self.grid.height)
|
|
|
|
self.grid.place_agent(agent, (x, y))
|
|
|
|
|
|
|
|
self.datacollector = DataCollector(
|
2022-10-17 18:23:57 +00:00
|
|
|
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
|
|
|
|
)
|
2021-10-14 15:37:06 +00:00
|
|
|
|
|
|
|
|
2022-10-17 18:23:57 +00:00
|
|
|
if __name__ == "__main__":
|
2021-10-14 15:37:06 +00:00
|
|
|
|
2022-10-17 18:23:57 +00:00
|
|
|
fixed_params = {
|
|
|
|
"generator": nx.complete_graph,
|
|
|
|
"width": 10,
|
|
|
|
"network_agents": [{"agent_class": SocialMoneyAgent, "weight": 1}],
|
|
|
|
"height": 10,
|
|
|
|
}
|
2021-10-14 15:37:06 +00:00
|
|
|
|
|
|
|
variable_params = {"N": range(10, 100, 10)}
|
|
|
|
|
2022-10-17 18:23:57 +00:00
|
|
|
batch_run = BatchRunner(
|
|
|
|
MoneyEnv,
|
|
|
|
variable_parameters=variable_params,
|
|
|
|
fixed_parameters=fixed_params,
|
|
|
|
iterations=5,
|
|
|
|
max_steps=100,
|
|
|
|
model_reporters={"Gini": compute_gini},
|
|
|
|
)
|
2021-10-14 15:37:06 +00:00
|
|
|
batch_run.run_all()
|
|
|
|
|
|
|
|
run_data = batch_run.get_model_vars_dataframe()
|
|
|
|
run_data.head()
|
|
|
|
print(run_data.Gini)
|