Skip to content

boltzmann wealth model: Switch to experimental data collection #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading