Skip to content

Commit

Permalink
Reformatting ColorPatches to color_patches and adding money model files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiekazil committed Oct 30, 2016
1 parent 6e69936 commit c2ced38
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
70 changes: 70 additions & 0 deletions examples/boltzmann_wealth_model/money_model.py
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()
30 changes: 30 additions & 0 deletions examples/boltzmann_wealth_model/viz_money_model.py
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.

0 comments on commit c2ced38

Please sign in to comment.