From 41545ca378225981d3e014a94943071ff7a39a23 Mon Sep 17 00:00:00 2001 From: cauemello Date: Thu, 7 Dec 2017 13:04:19 -0200 Subject: [PATCH 1/6] bug correction Corrects: visualization of ticks on x-axis when chart has too many values (#77) and a bug occurring when the reset button is pressed --- .../visualization/templates/js/ChartModule.js | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/mesa/visualization/templates/js/ChartModule.js b/mesa/visualization/templates/js/ChartModule.js index f08e069aabf..ae477b6e895 100644 --- a/mesa/visualization/templates/js/ChartModule.js +++ b/mesa/visualization/templates/js/ChartModule.js @@ -43,7 +43,7 @@ var ChartModule = function(series, canvas_width, canvas_height) { responsive: true, tooltips: { mode: 'index', - intersect: false, + intersect: false }, hover: { mode: 'nearest', @@ -53,13 +53,16 @@ var ChartModule = function(series, canvas_width, canvas_height) { xAxes: [{ display: true, scaleLabel: { - display: true, + display: true + }, + ticks: { + maxTicksLimit: 11 } }], yAxes: [{ display: true, scaleLabel: { - display: true, + display: true } }] } @@ -80,12 +83,10 @@ var ChartModule = function(series, canvas_width, canvas_height) { }; this.reset = function() { - chart.destroy(); - chartData.labels = []; - chart = new Chart(context, { - type: 'line', - data: chartData, - options: chartOptions + while (chart.data.labels.length) { chart.data.labels.pop(); } + chart.data.datasets.forEach((dataset) => { + while (dataset.data.length) { dataset.data.pop(); } }); + chart.update(); }; -}; \ No newline at end of file +}; From 33d5c7d09490a66df81f16f3915ba8101e006cb3 Mon Sep 17 00:00:00 2001 From: cauemello Date: Thu, 7 Dec 2017 13:08:25 -0200 Subject: [PATCH 2/6] Color code must have # Color code must have # to work properly. --- examples/bank_reserves/bank_reserves/server.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/bank_reserves/bank_reserves/server.py b/examples/bank_reserves/bank_reserves/server.py index 2c3547d3f38..818c3b6fe97 100644 --- a/examples/bank_reserves/bank_reserves/server.py +++ b/examples/bank_reserves/bank_reserves/server.py @@ -13,11 +13,11 @@ """ # Green -RICH_COLOR = "46FF33" +RICH_COLOR = "#46FF33" # Red -POOR_COLOR = "FF3C33" +POOR_COLOR = "#FF3C33" # Blue -MID_COLOR = "3349FF" +MID_COLOR = "#3349FF" def person_portrayal(agent): From e0fb730eb5423d4c683cc8815bca4cc6efa0f547 Mon Sep 17 00:00:00 2001 From: cauemello Date: Thu, 7 Dec 2017 13:57:11 -0200 Subject: [PATCH 3/6] Corrects datacollector position In order to work properly, we must collect data after defining the initial state of the model (step 0) and after each step. So, we must collect data as the last line of model __init__ and after each schedule.step(). Corrects #268 --- examples/Schelling/model.py | 3 +++ examples/bank_reserves/bank_reserves/model.py | 6 ++++-- examples/boltzmann_wealth_model/wealth_model/model.py | 6 +++++- .../wealth_model/model.py | 4 ++++ .../epstein_civil_violence/civil_violence/model.py | 10 +++++++--- examples/forest_fire/forest_fire/model.py | 3 +++ examples/pd_grid/pd_grid/model.py | 5 ++++- examples/sugarscape_cg/sugarscape/model.py | 3 +++ examples/virus_on_network/virus_on_network/model.py | 4 ++++ examples/wolf_sheep/wolf_sheep/model.py | 3 +++ 10 files changed, 40 insertions(+), 7 deletions(-) diff --git a/examples/Schelling/model.py b/examples/Schelling/model.py index fe9a6915068..90ffa461e8f 100644 --- a/examples/Schelling/model.py +++ b/examples/Schelling/model.py @@ -78,6 +78,8 @@ def __init__(self, height, width, density, minority_pc, homophily): agent = SchellingAgent((x, y), self, agent_type) self.grid.position_agent(agent, (x, y)) self.schedule.add(agent) + # collect initial data + self.datacollector.collect(self) def step(self): ''' @@ -85,6 +87,7 @@ def step(self): ''' self.happy = 0 # Reset counter of happy agents self.schedule.step() + # collect data self.datacollector.collect(self) if self.happy == self.schedule.get_agent_count(): diff --git a/examples/bank_reserves/bank_reserves/model.py b/examples/bank_reserves/bank_reserves/model.py index 2ad97b9f651..a94da780b07 100644 --- a/examples/bank_reserves/bank_reserves/model.py +++ b/examples/bank_reserves/bank_reserves/model.py @@ -124,12 +124,14 @@ def __init__(self, height=grid_h, width=grid_w, init_people=2, rich_threshold=10 self.schedule.add(p) self.running = True + # collect initial data + self.datacollector.collect(self) def step(self): - # collect data - self.datacollector.collect(self) # tell all the agents in the model to run their step function self.schedule.step() + # collect data + self.datacollector.collect(self) def run_model(self): for i in range(self.run_time): diff --git a/examples/boltzmann_wealth_model/wealth_model/model.py b/examples/boltzmann_wealth_model/wealth_model/model.py index 89a16a845aa..b2bf2d28d57 100644 --- a/examples/boltzmann_wealth_model/wealth_model/model.py +++ b/examples/boltzmann_wealth_model/wealth_model/model.py @@ -40,9 +40,13 @@ def __init__(self, N, width, height): y = random.randrange(self.grid.height) self.grid.place_agent(a, (x, y)) - def step(self): + # collect initial data self.datacollector.collect(self) + + def step(self): self.schedule.step() + # collect data + self.datacollector.collect(self) def run_model(self, n): for i in range(n): diff --git a/examples/boltzmann_wealth_model_network/wealth_model/model.py b/examples/boltzmann_wealth_model_network/wealth_model/model.py index ff39212ef70..a008de23701 100644 --- a/examples/boltzmann_wealth_model_network/wealth_model/model.py +++ b/examples/boltzmann_wealth_model_network/wealth_model/model.py @@ -41,8 +41,12 @@ def __init__(self, num_agents, num_nodes): # Add the agent to a random node self.grid.place_agent(a, list_of_random_nodes[i]) + # collect initial data + self.datacollector.collect(self) + def step(self): self.schedule.step() + # collect data self.datacollector.collect(self) def run_model(self, n): diff --git a/examples/epstein_civil_violence/civil_violence/model.py b/examples/epstein_civil_violence/civil_violence/model.py index 05dcc1592b9..845df67c272 100644 --- a/examples/epstein_civil_violence/civil_violence/model.py +++ b/examples/epstein_civil_violence/civil_violence/model.py @@ -69,8 +69,8 @@ def __init__(self, height, width, citizen_density, cop_density, "arrest_probability": lambda a: getattr(a, "arrest_probability", None) } - self.dc = DataCollector(model_reporters=model_reporters, - agent_reporters=agent_reporters) + self.datacollector = DataCollector(model_reporters=model_reporters, + agent_reporters=agent_reporters) unique_id = 0 if self.cop_density + self.citizen_density > 1: raise ValueError( @@ -93,12 +93,16 @@ def __init__(self, height, width, citizen_density, cop_density, self.grid[y][x] = citizen self.schedule.add(citizen) + # collect initial data + self.datacollector.collect(self) + def step(self): """ Advance the model by one step and collect data. """ self.schedule.step() - self.dc.collect(self) + # collect data + self.datacollector.collect(self) self.iteration += 1 if self.iteration > self.max_iters: self.running = False diff --git a/examples/forest_fire/forest_fire/model.py b/examples/forest_fire/forest_fire/model.py index a34bec8470b..acaa17a5c5c 100644 --- a/examples/forest_fire/forest_fire/model.py +++ b/examples/forest_fire/forest_fire/model.py @@ -45,12 +45,15 @@ def __init__(self, height, width, density): self.grid._place_agent((x, y), new_tree) self.schedule.add(new_tree) self.running = True + # collect initial data + self.datacollector.collect(self) def step(self): """ Advance the model by one step. """ self.schedule.step() + # collect data self.datacollector.collect(self) # Halt if no more fire diff --git a/examples/pd_grid/pd_grid/model.py b/examples/pd_grid/pd_grid/model.py index 421e28c8b3b..b07fad5497c 100644 --- a/examples/pd_grid/pd_grid/model.py +++ b/examples/pd_grid/pd_grid/model.py @@ -47,10 +47,13 @@ def __init__(self, height, width, schedule_type, payoffs=None): "Cooperating_Agents": lambda m: len([a for a in m.schedule.agents if a.move == "C"]) }) + # collect initial data + self.datacollector.collect(self) def step(self): - self.datacollector.collect(self) self.schedule.step() + # collect initial data + self.datacollector.collect(self) def run(self, n): ''' Run the model for n steps. ''' diff --git a/examples/sugarscape_cg/sugarscape/model.py b/examples/sugarscape_cg/sugarscape/model.py index 2a997b23489..9805b2dc25f 100644 --- a/examples/sugarscape_cg/sugarscape/model.py +++ b/examples/sugarscape_cg/sugarscape/model.py @@ -65,9 +65,12 @@ def __init__(self, height=50, width=50, self.schedule.add(ssa) self.running = True + # collect initial data + self.datacollector.collect(self) def step(self): self.schedule.step() + # collect data self.datacollector.collect(self) if self.verbose: print([self.schedule.time, diff --git a/examples/virus_on_network/virus_on_network/model.py b/examples/virus_on_network/virus_on_network/model.py index 7a0b7f14446..3539cf877d9 100644 --- a/examples/virus_on_network/virus_on_network/model.py +++ b/examples/virus_on_network/virus_on_network/model.py @@ -66,6 +66,9 @@ def __init__(self, num_nodes, avg_node_degree, initial_outbreak_size, virus_spre for a in self.grid.get_cell_list_contents(infected_nodes): a.state = State.INFECTED + # collect initial data + self.datacollector.collect(self) + def resistant_susceptible_ratio(self): try: return number_state(self, State.RESISTANT) / number_state(self, State.SUSCEPTIBLE) @@ -74,6 +77,7 @@ def resistant_susceptible_ratio(self): def step(self): self.schedule.step() + # collect data self.datacollector.collect(self) def run_model(self, n): diff --git a/examples/wolf_sheep/wolf_sheep/model.py b/examples/wolf_sheep/wolf_sheep/model.py index da6ab7c0751..44ea4339676 100644 --- a/examples/wolf_sheep/wolf_sheep/model.py +++ b/examples/wolf_sheep/wolf_sheep/model.py @@ -115,9 +115,12 @@ def __init__(self, height=20, width=20, self.schedule.add(patch) self.running = True + # collect initial data + self.datacollector.collect(self) def step(self): self.schedule.step() + # collect data self.datacollector.collect(self) if self.verbose: print([self.schedule.time, From 872a83be20d16fa139f9d9cf636a2011601373d4 Mon Sep 17 00:00:00 2001 From: cauemello Date: Thu, 7 Dec 2017 14:04:21 -0200 Subject: [PATCH 4/6] Corrects datacollector position --- examples/pd_grid/pd_grid/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pd_grid/pd_grid/model.py b/examples/pd_grid/pd_grid/model.py index b07fad5497c..af010e078f0 100644 --- a/examples/pd_grid/pd_grid/model.py +++ b/examples/pd_grid/pd_grid/model.py @@ -52,7 +52,7 @@ def __init__(self, height, width, schedule_type, payoffs=None): def step(self): self.schedule.step() - # collect initial data + # collect data self.datacollector.collect(self) def run(self, n): From 9ab0397ba766ae9f4badc197ccf3019699e404b0 Mon Sep 17 00:00:00 2001 From: cauemello Date: Thu, 7 Dec 2017 14:51:35 -0200 Subject: [PATCH 5/6] Using the long-form declaration of function --- mesa/visualization/templates/js/ChartModule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/visualization/templates/js/ChartModule.js b/mesa/visualization/templates/js/ChartModule.js index ae477b6e895..99c3935c7e6 100644 --- a/mesa/visualization/templates/js/ChartModule.js +++ b/mesa/visualization/templates/js/ChartModule.js @@ -84,7 +84,7 @@ var ChartModule = function(series, canvas_width, canvas_height) { this.reset = function() { while (chart.data.labels.length) { chart.data.labels.pop(); } - chart.data.datasets.forEach((dataset) => { + chart.data.datasets.forEach(function(dataset) { while (dataset.data.length) { dataset.data.pop(); } }); chart.update(); From 1df1ff6babf37f0830541ae561640c711eb0f2d2 Mon Sep 17 00:00:00 2001 From: cauemello Date: Sun, 10 Dec 2017 22:35:39 -0200 Subject: [PATCH 6/6] some adjustments some adjustments --- examples/Schelling/model.py | 5 ++--- examples/bank_reserves/bank_reserves/model.py | 1 - examples/boltzmann_wealth_model/wealth_model/model.py | 3 +-- .../boltzmann_wealth_model_network/wealth_model/model.py | 3 +-- examples/conways_game_of_life/game_of_life/model.py | 1 + examples/epstein_civil_violence/civil_violence/model.py | 3 +-- examples/forest_fire/forest_fire/model.py | 2 +- examples/pd_grid/pd_grid/model.py | 4 ++-- examples/shape_example/shape_model/model.py | 2 +- examples/sugarscape_cg/sugarscape/model.py | 1 - examples/virus_on_network/virus_on_network/model.py | 3 +-- examples/wolf_sheep/wolf_sheep/model.py | 1 - 12 files changed, 11 insertions(+), 18 deletions(-) diff --git a/examples/Schelling/model.py b/examples/Schelling/model.py index 90ffa461e8f..c7147fc3b99 100644 --- a/examples/Schelling/model.py +++ b/examples/Schelling/model.py @@ -60,8 +60,6 @@ def __init__(self, height, width, density, minority_pc, homophily): # For testing purposes, agent's individual x and y {"x": lambda a: a.pos[0], "y": lambda a: a.pos[1]}) - self.running = True - # Set up agents # We use a grid iterator that returns # the coordinates of a cell as well as @@ -78,7 +76,8 @@ def __init__(self, height, width, density, minority_pc, homophily): agent = SchellingAgent((x, y), self, agent_type) self.grid.position_agent(agent, (x, y)) self.schedule.add(agent) - # collect initial data + + self.running = True self.datacollector.collect(self) def step(self): diff --git a/examples/bank_reserves/bank_reserves/model.py b/examples/bank_reserves/bank_reserves/model.py index a94da780b07..f54a0fec7f1 100644 --- a/examples/bank_reserves/bank_reserves/model.py +++ b/examples/bank_reserves/bank_reserves/model.py @@ -124,7 +124,6 @@ def __init__(self, height=grid_h, width=grid_w, init_people=2, rich_threshold=10 self.schedule.add(p) self.running = True - # collect initial data self.datacollector.collect(self) def step(self): diff --git a/examples/boltzmann_wealth_model/wealth_model/model.py b/examples/boltzmann_wealth_model/wealth_model/model.py index b2bf2d28d57..c9626932cc2 100644 --- a/examples/boltzmann_wealth_model/wealth_model/model.py +++ b/examples/boltzmann_wealth_model/wealth_model/model.py @@ -24,7 +24,6 @@ class MoneyModel(Model): 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( @@ -40,7 +39,7 @@ def __init__(self, N, width, height): y = random.randrange(self.grid.height) self.grid.place_agent(a, (x, y)) - # collect initial data + self.running = True self.datacollector.collect(self) def step(self): diff --git a/examples/boltzmann_wealth_model_network/wealth_model/model.py b/examples/boltzmann_wealth_model_network/wealth_model/model.py index a008de23701..b7cdb1829d1 100644 --- a/examples/boltzmann_wealth_model_network/wealth_model/model.py +++ b/examples/boltzmann_wealth_model_network/wealth_model/model.py @@ -24,7 +24,6 @@ def __init__(self, num_agents, num_nodes): self.num_agents = num_agents self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5) - self.running = True self.grid = NetworkGrid(self.G) self.schedule = RandomActivation(self) self.datacollector = DataCollector( @@ -41,7 +40,7 @@ def __init__(self, num_agents, num_nodes): # Add the agent to a random node self.grid.place_agent(a, list_of_random_nodes[i]) - # collect initial data + self.running = True self.datacollector.collect(self) def step(self): diff --git a/examples/conways_game_of_life/game_of_life/model.py b/examples/conways_game_of_life/game_of_life/model.py index 72fcf29c562..5c5d4aeb666 100644 --- a/examples/conways_game_of_life/game_of_life/model.py +++ b/examples/conways_game_of_life/game_of_life/model.py @@ -37,6 +37,7 @@ def __init__(self, height, width): cell.state = cell.ALIVE self.grid.place_agent(cell, (x, y)) self.schedule.add(cell) + self.running = True def step(self): diff --git a/examples/epstein_civil_violence/civil_violence/model.py b/examples/epstein_civil_violence/civil_violence/model.py index 845df67c272..50f4b8818f9 100644 --- a/examples/epstein_civil_violence/civil_violence/model.py +++ b/examples/epstein_civil_violence/civil_violence/model.py @@ -51,7 +51,6 @@ def __init__(self, height, width, citizen_density, cop_density, self.active_threshold = active_threshold self.arrest_prob_constant = arrest_prob_constant self.movement = movement - self.running = True self.max_iters = max_iters self.iteration = 0 self.schedule = RandomActivation(self) @@ -93,7 +92,7 @@ def __init__(self, height, width, citizen_density, cop_density, self.grid[y][x] = citizen self.schedule.add(citizen) - # collect initial data + self.running = True self.datacollector.collect(self) def step(self): diff --git a/examples/forest_fire/forest_fire/model.py b/examples/forest_fire/forest_fire/model.py index acaa17a5c5c..f0f2921261d 100644 --- a/examples/forest_fire/forest_fire/model.py +++ b/examples/forest_fire/forest_fire/model.py @@ -44,8 +44,8 @@ def __init__(self, height, width, density): new_tree.condition = "On Fire" self.grid._place_agent((x, y), new_tree) self.schedule.add(new_tree) + self.running = True - # collect initial data self.datacollector.collect(self) def step(self): diff --git a/examples/pd_grid/pd_grid/model.py b/examples/pd_grid/pd_grid/model.py index af010e078f0..c4565540561 100644 --- a/examples/pd_grid/pd_grid/model.py +++ b/examples/pd_grid/pd_grid/model.py @@ -31,7 +31,6 @@ def __init__(self, height, width, schedule_type, payoffs=None): Determines the agent activation regime. payoffs: (optional) Dictionary of (move, neighbor_move) payoffs. ''' - self.running = True self.grid = SingleGrid(height, width, torus=True) self.schedule_type = schedule_type self.schedule = self.schedule_types[self.schedule_type](self) @@ -47,7 +46,8 @@ def __init__(self, height, width, schedule_type, payoffs=None): "Cooperating_Agents": lambda m: len([a for a in m.schedule.agents if a.move == "C"]) }) - # collect initial data + + self.running = True self.datacollector.collect(self) def step(self): diff --git a/examples/shape_example/shape_model/model.py b/examples/shape_example/shape_model/model.py index 31209429a51..4e8afcb1d26 100644 --- a/examples/shape_example/shape_model/model.py +++ b/examples/shape_example/shape_model/model.py @@ -15,12 +15,12 @@ def __init__(self, unique_id, model, pos, heading=(1, 0)): class ShapesModel(Model): def __init__(self, N, width=20, height=10): - self.running = True self.N = N # num of agents self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1)) # tuples are fast self.grid = SingleGrid(width, height, torus=False) self.schedule = RandomActivation(self) self.make_walker_agents() + self.running = True def make_walker_agents(self): unique_id = 0 diff --git a/examples/sugarscape_cg/sugarscape/model.py b/examples/sugarscape_cg/sugarscape/model.py index 9805b2dc25f..d82df33ed2f 100644 --- a/examples/sugarscape_cg/sugarscape/model.py +++ b/examples/sugarscape_cg/sugarscape/model.py @@ -65,7 +65,6 @@ def __init__(self, height=50, width=50, self.schedule.add(ssa) self.running = True - # collect initial data self.datacollector.collect(self) def step(self): diff --git a/examples/virus_on_network/virus_on_network/model.py b/examples/virus_on_network/virus_on_network/model.py index 3539cf877d9..08215258b9a 100644 --- a/examples/virus_on_network/virus_on_network/model.py +++ b/examples/virus_on_network/virus_on_network/model.py @@ -47,7 +47,6 @@ def __init__(self, num_nodes, avg_node_degree, initial_outbreak_size, virus_spre self.virus_check_frequency = virus_check_frequency self.recovery_chance = recovery_chance self.gain_resistance_chance = gain_resistance_chance - self.running = True self.datacollector = DataCollector({"Infected": number_infected, "Susceptible": number_susceptible, @@ -66,7 +65,7 @@ def __init__(self, num_nodes, avg_node_degree, initial_outbreak_size, virus_spre for a in self.grid.get_cell_list_contents(infected_nodes): a.state = State.INFECTED - # collect initial data + self.running = True self.datacollector.collect(self) def resistant_susceptible_ratio(self): diff --git a/examples/wolf_sheep/wolf_sheep/model.py b/examples/wolf_sheep/wolf_sheep/model.py index 44ea4339676..7a73f9c5e39 100644 --- a/examples/wolf_sheep/wolf_sheep/model.py +++ b/examples/wolf_sheep/wolf_sheep/model.py @@ -115,7 +115,6 @@ def __init__(self, height=20, width=20, self.schedule.add(patch) self.running = True - # collect initial data self.datacollector.collect(self) def step(self):