forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reformatting ColorPatches to color_patches and adding money model files.
- Loading branch information
1 parent
6e69936
commit c2ced38
Showing
7 changed files
with
100 additions
and
0 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,70 @@ | ||
import random | ||
|
||
from mesa import Agent, Model | ||
from mesa.time import RandomActivation | ||
from mesa.space import MultiGrid | ||
from mesa.datacollection import DataCollector | ||
|
||
|
||
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 MoneyModel(Model): | ||
"""A model with some number of agents.""" | ||
|
||
def __init__(self, N, width, height): | ||
self.num_agents = N | ||
self.running = True | ||
self.grid = MultiGrid(height, width, True) | ||
self.schedule = RandomActivation(self) | ||
self.datacollector = DataCollector( | ||
model_reporters={"Gini": compute_gini}, | ||
agent_reporters={"Wealth": lambda a: a.wealth} | ||
) | ||
# 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 = random.randrange(self.grid.width) | ||
y = random.randrange(self.grid.height) | ||
self.grid.place_agent(a, (x, y)) | ||
|
||
def step(self): | ||
self.datacollector.collect(self) | ||
self.schedule.step() | ||
|
||
def run_model(self, n): | ||
for i in range(n): | ||
self.step() | ||
|
||
|
||
class MoneyAgent(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 = 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 = random.choice(cellmates) | ||
other.wealth += 1 | ||
self.wealth -= 1 | ||
|
||
def step(self): | ||
self.move() | ||
if self.wealth > 0: | ||
self.give_money() |
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,30 @@ | ||
from mesa.visualization.modules import CanvasGrid | ||
from mesa.visualization.modules import ChartModule | ||
from mesa.visualization.ModularVisualization import ModularServer | ||
|
||
from money_model import MoneyModel | ||
|
||
|
||
def agent_portrayal(agent): | ||
portrayal = {"Shape": "circle", | ||
"Filled": "true", | ||
"r": 0.5} | ||
|
||
if agent.wealth > 0: | ||
portrayal["Color"] = "red" | ||
portrayal["Layer"] = 0 | ||
else: | ||
portrayal["Color"] = "grey" | ||
portrayal["Layer"] = 1 | ||
portrayal["r"] = 0.2 | ||
return portrayal | ||
|
||
grid = CanvasGrid(agent_portrayal, 10, 10, 500, 500) | ||
chart = ChartModule([ | ||
{"Label": "Gini", "Color": "Black"}], | ||
data_collector_name='datacollector' | ||
) | ||
|
||
server = ModularServer(MoneyModel, [grid, chart], "Money Model", 100, 10, 10) | ||
server.port = 8521 | ||
server.launch() |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.