From 30661542b54c755db31d33883c258c939b1d4f70 Mon Sep 17 00:00:00 2001 From: AlexBocken Date: Thu, 18 May 2023 12:46:48 +0200 Subject: [PATCH] add num_max_agents, cleanup --- agent.py | 16 +++++++++------- main.py | 21 +-------------------- model.py | 7 ++++--- server.py | 4 ++-- 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/agent.py b/agent.py index 784c891..95c94cb 100644 --- a/agent.py +++ b/agent.py @@ -88,6 +88,7 @@ class RandomWalkerAnt(Agent): for neighbor in self.front_neighbors: if self.model.grid.is_food(neighbor): self.drop_pheromone = "B" + self.look_for_pheromone = "A" self.sensitivity = self.sensitivity_0 self.prev_pos = neighbor @@ -100,17 +101,16 @@ class RandomWalkerAnt(Agent): self.drop_pheromone = "A" self.sensitivity = self.sensitivity_0 - #TODO: Do we flip the ant here or reset prev pos? - # For now, flip ant just like at food self.prev_pos = neighbor self._next_pos = self.pos # recruit new ants for agent_id in self.model.get_unique_ids(self.model.num_new_recruits): - agent = RandomWalkerAnt(unique_id=agent_id, model=self.model, look_for_pheromone="B", drop_pheromone="A") - agent._next_pos = self.pos - self.model.schedule.add(agent) - self.model.grid.place_agent(agent, pos=neighbor) + if self.model.schedule.get_agent_count() < self.model.num_max_agents: + agent = RandomWalkerAnt(unique_id=agent_id, model=self.model, look_for_pheromone="B", drop_pheromone="A") + agent._next_pos = self.pos + self.model.schedule.add(agent) + self.model.grid.place_agent(agent, pos=neighbor) # follow positive gradient if self.look_for_pheromone is not None: @@ -176,7 +176,9 @@ class RandomWalkerAnt(Agent): assert(self.prev_pos is not None) all_neighbors = self.neighbors() neighbors_at_the_back = self.neighbors(pos=self.prev_pos, include_center=True) - return list(filter(lambda i: i not in neighbors_at_the_back, all_neighbors)) + front_neighbors = list(filter(lambda i: i not in neighbors_at_the_back, all_neighbors)) + assert(len(front_neighbors) == 3) # not sure whether always the case, used for debugging + return front_neighbors @property def front_neighbor(self): diff --git a/main.py b/main.py index a4d19ab..0e0c596 100755 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from agent import RandomWalkerAnt import numpy as np import matplotlib.pyplot as plt from mesa.space import Coordinate +from mesa.datacollection import DataCollector def main(): check_pheromone_exponential_decay() @@ -22,9 +23,6 @@ def check_pheromone_exponential_decay(): Check whether wanted exponential decay of pheromones on grid is done correctly shows plot of pheromone placed on grid vs. equivalent exponential decay function """ - - from mesa.datacollection import DataCollector - width = 21 height = width num_initial_roamers = 0 @@ -46,9 +44,6 @@ def check_pheromone_exponential_decay(): model.run_model() a_test = model.datacollector.get_model_vars_dataframe()["pheromone_a"] - import matplotlib.pyplot as plt - import numpy as np - plt.figure() xx = np.linspace(0,1000, 10000) yy = a_test[0]*np.exp(-model.decay_rates["A"]*xx) @@ -66,8 +61,6 @@ def check_ant_sensitivity_linear_decay(): shows plot of ant sensitivity placed on grid vs. equivalent linear decay function not food sources are on the grid for this run to not reset sensitivities """ - from mesa.datacollection import DataCollector - width = 50 height = width num_initial_roamers = 1 @@ -91,9 +84,6 @@ def check_ant_sensitivity_linear_decay(): model.run_model() a_test = model.datacollector.get_agent_vars_dataframe().reset_index()["sensitivity"] - import matplotlib.pyplot as plt - import numpy as np - plt.figure() xx = np.linspace(0,1000, 10000) yy = a_test[0] - start*xx @@ -109,9 +99,6 @@ def check_ant_pheromone_exponential_decay(): Check whether wanted exponential decay of pheromone drop rate for ants is correctly modeled shows plot of pheromone placed on grid vs. equivalent exponential decay function """ - - from mesa.datacollection import DataCollector - width = 50 height = width num_initial_roamers = 1 @@ -134,10 +121,6 @@ def check_ant_pheromone_exponential_decay(): model.run_model() a_test = model.datacollector.get_agent_vars_dataframe().reset_index()["pheromone_drop_rate"] - import matplotlib.pyplot as plt - import numpy as np - - plt.figure() xx = np.linspace(0,1000, 10000) yy = a_test[0]*np.exp(-model.schedule.agents[0].betas["A"]*xx) @@ -148,8 +131,6 @@ def check_ant_pheromone_exponential_decay(): plt.show() - - if __name__ == "__main__": main() diff --git a/model.py b/model.py index 5145bfd..8634e61 100644 --- a/model.py +++ b/model.py @@ -39,9 +39,10 @@ class ActiveWalkerModel(Model): } for agent_id in self.get_unique_ids(num_initial_roamers): - agent = RandomWalkerAnt(unique_id=agent_id, model=self, look_for_pheromone="A", drop_pheromone="A") - self.schedule.add(agent) - self.grid.place_agent(agent, pos=nest_position) + if self.schedule.get_agent_count() < self.num_max_agents: + agent = RandomWalkerAnt(unique_id=agent_id, model=self, look_for_pheromone="A", drop_pheromone="A") + self.schedule.add(agent) + self.grid.place_agent(agent, pos=nest_position) for _ in range(num_food_sources): self.grid.add_food(food_size) diff --git a/server.py b/server.py index a81d5aa..4d14e63 100755 --- a/server.py +++ b/server.py @@ -23,9 +23,9 @@ def setup(params=None): if params is None: params = { "width": 50, "height": 50, - "num_max_agents" : 100, + "num_max_agents" : 1000, "nest_position" : (25,25), - "num_initial_roamers" : 5, + "num_initial_roamers" : 20, }