From 5776caa9dba79fc2ffa7c16dd75368aab80feda2 Mon Sep 17 00:00:00 2001 From: jackiekazil Date: Sun, 26 Jun 2016 05:33:04 -0700 Subject: [PATCH] Minor pep updates to boltzmann. --- examples/Boltzmann_Wealth_Model/MoneyModel.py | 20 +++++++++++++------ .../Boltzmann_Wealth_Model/Viz_MoneyModel.py | 8 +++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/examples/Boltzmann_Wealth_Model/MoneyModel.py b/examples/Boltzmann_Wealth_Model/MoneyModel.py index 82765e4d6aa..1df47a346f1 100644 --- a/examples/Boltzmann_Wealth_Model/MoneyModel.py +++ b/examples/Boltzmann_Wealth_Model/MoneyModel.py @@ -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) @@ -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) diff --git a/examples/Boltzmann_Wealth_Model/Viz_MoneyModel.py b/examples/Boltzmann_Wealth_Model/Viz_MoneyModel.py index 910114642a0..7527a91bf45 100644 --- a/examples/Boltzmann_Wealth_Model/Viz_MoneyModel.py +++ b/examples/Boltzmann_Wealth_Model/Viz_MoneyModel.py @@ -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() \ No newline at end of file +server.launch()