Skip to content

Commit

Permalink
boltzmann wealth model: Switch to experimental data collection
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Aug 10, 2024
1 parent 17c65ce commit a81eaff
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions examples/boltzmann_wealth_model_experimental/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import mesa
from mesa.experimental import DataCollector


def compute_gini(model):
agent_wealths = [agent.wealth for agent in model.schedule.agents]
x = sorted(agent_wealths)
N = model.num_agents
x = sorted(model.agents.get("wealth"))
N = len(x)
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
return 1 + (1 / N) - 2 * B

Expand All @@ -22,9 +22,6 @@ def __init__(self, N=100, width=10, height=10):
self.num_agents = N
self.grid = mesa.space.MultiGrid(width, height, True)
self.schedule = mesa.time.RandomActivation(self)
self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
Expand All @@ -34,13 +31,19 @@ def __init__(self, N=100, width=10, height=10):
y = self.random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))

self.running = True
self.datacollector.collect(self)
self.datacollector = DataCollector(
self,
{
"model": {"Gini": compute_gini},
"agents": {"Wealth": "wealth"},
},
)
self.datacollector.collect()

def step(self):
self.schedule.step()
# collect data
self.datacollector.collect(self)
self.datacollector.collect()

def run_model(self, n):
for i in range(n):
Expand Down

0 comments on commit a81eaff

Please sign in to comment.