-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update pip statement for zsh & advanced tutorial notebook (#1648)
- Loading branch information
1 parent
89374ee
commit cea5521
Showing
2 changed files
with
186 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env python3 | ||
|
||
|
||
import mesa | ||
|
||
|
||
def compute_gini(model): | ||
agent_wealths = [agent.wealth for agent in model.schedule.agents] | ||
x = sorted(agent_wealths) | ||
N = model.num_agents | ||
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x)) | ||
return 1 + (1 / N) - 2 * B | ||
|
||
|
||
class MoneyAgent(mesa.Agent): | ||
"""An agent with fixed initial wealth.""" | ||
|
||
def __init__(self, unique_id, model): | ||
super().__init__(unique_id, model) | ||
self.wealth = 1 | ||
|
||
def move(self): | ||
possible_steps = self.model.grid.get_neighborhood( | ||
self.pos, moore=True, include_center=False | ||
) | ||
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): | ||
self.move() | ||
if self.wealth > 0: | ||
self.give_money() | ||
|
||
|
||
class MoneyModel(mesa.Model): | ||
"""A model with some number of agents.""" | ||
|
||
def __init__(self, N, width, height): | ||
self.num_agents = N | ||
self.grid = mesa.space.MultiGrid(width, height, True) | ||
self.schedule = mesa.time.RandomActivation(self) | ||
|
||
# Create agents | ||
for i in range(self.num_agents): | ||
a = MoneyAgent(i, self) | ||
self.schedule.add(a) | ||
# Add the agent to a random grid cell | ||
x = self.random.randrange(self.grid.width) | ||
y = self.random.randrange(self.grid.height) | ||
self.grid.place_agent(a, (x, y)) | ||
|
||
self.datacollector = mesa.DataCollector( | ||
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"} | ||
) | ||
|
||
def step(self): | ||
self.datacollector.collect(self) | ||
self.schedule.step() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters