Skip to content

Commit

Permalink
Minor pep updates to boltzmann.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiekazil committed Jun 26, 2016
1 parent b828636 commit 5776caa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 14 additions & 6 deletions examples/Boltzmann_Wealth_Model/MoneyModel.py
Expand Up @@ -6,23 +6,27 @@

import random


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)
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})
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)
Expand All @@ -35,19 +39,23 @@ def __init__(self, N, width, height):
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):
self.unique_id = unique_id
self.wealth = 1

def move(self, model):
possible_steps = model.grid.get_neighborhood(self.pos, moore=True, include_center=False)
possible_steps = model.grid.get_neighborhood(
self.pos, moore=True, include_center=False
)
new_position = random.choice(possible_steps)
model.grid.move_agent(self, new_position)

Expand Down
8 changes: 5 additions & 3 deletions examples/Boltzmann_Wealth_Model/Viz_MoneyModel.py
Expand Up @@ -19,9 +19,11 @@ def agent_portrayal(agent):
return portrayal

grid = CanvasGrid(agent_portrayal, 10, 10, 500, 500)
chart = ChartModule([{"Label": "Gini", "Color": "Black"}],
data_collector_name='datacollector')
chart = ChartModule([
{"Label": "Gini", "Color": "Black"}],
data_collector_name='datacollector'
)

server = ModularServer(MoneyModel, [grid, chart], "Money Model", 100, 10, 10)
server.port = 8889
server.launch()
server.launch()

0 comments on commit 5776caa

Please sign in to comment.