From 15efd64c3e39cdad78ded71d6d9e5ce9de0bf02f Mon Sep 17 00:00:00 2001 From: AlexBocken Date: Mon, 26 Jun 2023 10:23:19 +0200 Subject: [PATCH 1/3] vivianes changes --- agent.py | 4 ++- main.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- model.py | 56 +++++++++++++++++++++++++++++++-- 3 files changed, 151 insertions(+), 5 deletions(-) mode change 100755 => 100644 main.py diff --git a/agent.py b/agent.py index 48e6e7c..60feeaf 100644 --- a/agent.py +++ b/agent.py @@ -17,6 +17,7 @@ import numpy.typing as npt from mesa.agent import Agent from mesa.space import Coordinate + class RandomWalkerAnt(Agent): def __init__(self, unique_id, model, look_for_pheromone=None, @@ -127,12 +128,13 @@ class RandomWalkerAnt(Agent): self.sensitivity = self.model.s_0 self.energy = self.model.e_0 - self.look_for_pheromone = "A" # Is this a correct interpretation? + self.look_for_pheromone = "B" self.drop_pheromone = "A" self._prev_pos = neighbor self._next_pos = self.pos + # recruit new ants for agent_id in self.model.get_unique_ids(self.model.N_r): if self.model.schedule.get_agent_count() < self.model.N_m: diff --git a/main.py b/main.py old mode 100755 new mode 100644 index 0d780ce..bcd979c --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ + #!/bin/python """ main.py - Part of ants project @@ -14,7 +15,7 @@ import matplotlib.pyplot as plt from mesa.space import Coordinate from mesa.datacollection import DataCollector -from multihex import MultiHexGrid +#from multihex import MultiHexGrid def main(): pass @@ -213,3 +214,96 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY You should have received a copy of the GNU Affero General Public License along with this program. If not, see """ + + + +# |%%--%%| + +# Access the DataCollector +datacollector = model.datacollector + + +# |%%--%%| + +# Get the data from the DataCollector +model_data = datacollector.get_model_vars_dataframe() + +# |%%--%%| + +print(model_data.columns) + +# |%%--%%| <74OaeOltqi|WpQLCA0RuP> + +# Plot the number of alive ants over time +plt.plot(model_data.index, model_data['alive_ants']) +plt.xlabel('Time') +plt.ylabel('Number of Alive Ants') #this should probably be "active" ants, since it is not considering those in the nest +plt.title('Number of Alive Ants Over Time') +plt.grid(True) +plt.show() + +# |%%--%%| + +# Plot the number of sucessful walkers over time +plt.plot(model_data.index, model_data['sucessful_walkers']) +plt.xlabel('Time') +plt.ylabel('Number of Sucessful Walkers') +plt.title('Number of Sucessful Walkers Over Time') +plt.grid(True) +plt.show() + +# |%%--%%| + +# Calculate the cumulative sum +model_data['cumulative_sucessful_walkers'] = model_data['sucessful_walkers'].cumsum() + +# Plot the cumulative sum of sucessful walkers over time +plt.plot(model_data.index, model_data['cumulative_sucessful_walkers']) +plt.xlabel('Time') +plt.ylabel('Cumulative Sucessful Walkers') +plt.title('Cumulative Sucessful Walkers Over Time') +plt.grid(True) +plt.show() + +# Values over 100 are to be interpreted as walkers being sucessfull several times since the total max number of ants is 100 + +# |%%--%%| + + # Connectivity measure + def check_food_source_connectivity(food_sources, paths): #food_sources = nodes.is_nest, paths=result from BFS + connected_food_sources = set() + + for source in food_sources: + if source in paths: + connected_food_sources.add(source) + + connectivity = len(connected_food_sources) + + + return connectivity + + + # Calculate connectivity through BFS + + current_paths = bfs(self.grid, self.grid.fields["nests"], 0.000001) + + +# |%%--%%| <64kmoHYvCD|JEzmDy4wuX> + + + +# |%%--%%| + + + + + + + +# |%%--%%| + + + +# |%%--%%| + + diff --git a/model.py b/model.py index ae29610..db2c567 100644 --- a/model.py +++ b/model.py @@ -15,6 +15,7 @@ from multihex import MultiHexGridScalarFields from mesa.time import SimultaneousActivation from mesa.datacollection import DataCollector from agent import RandomWalkerAnt +from collections import deque kwargs_paper_setup1 = { "width": 100, @@ -62,6 +63,7 @@ kwargs_paper_setup2 = { "resistance_map_type" : None, } + class ActiveWalkerModel(Model): def __init__(self, width : int, height : int, N_0 : int, # number of initial roamers @@ -120,8 +122,6 @@ class ActiveWalkerModel(Model): raise NotImplemented(f"{resistance_map_type=} is not implemented.") - - self._unique_id_counter = -1 self.max_steps = max_steps @@ -135,16 +135,66 @@ class ActiveWalkerModel(Model): for _ in range(N_f): self.grid.add_food(food_size) + + # Breadth-first-search algorithm for connectivity + #def bfs(graph, start_node, threshold): #graph=grid, start_node=nest, threshold=TBD? + # visited = set() + # queue = deque([(start_node, [])]) + # paths = {} + # connected_food_sources = set() + + # while queue: + # current_node, path = queue.popleft() + #current_node = tuple(current_node) + # visited.add(current_node) + + # if current_node in graph: + # for neighbor, m.grid.fields["A"] in graph[current_node].items(): + # if neighbor not in visited and m.grid.fields["A"] >= threshold: + # new_path = path + [neighbor] + # queue.append((neighbor, new_path)) + + # Check if the neighbor is a food source + # if neighbor in self.grid_food: + # if neighbor not in paths: + # paths[neighbor] = new_path + # connected_food_sources.add(neighbor) + + # connectivity = len(connected_food_sources) + + # return connectivity + + + # Calculate connectivity through BFS + + # current_paths = bfs(self.grid, self.grid.fields["nests"], 0.000001) + + + + def subset_agent_count(self): + subset_agents = [agent for agent in self.schedule.agents if agent.sensitivity == self.s_0 and agent.look_for_pheromone == "B"] + count = float(len(subset_agents)) + return count + + self.datacollector = DataCollector( # model_reporters={"agent_dens": lambda m: m.agent_density()}, model_reporters = {"pheromone_a": lambda m: m.grid.fields["A"], "pheromone_b": lambda m: m.grid.fields["B"], + "alive_ants": lambda m: self.schedule.get_agent_count(), + "sucessful_walkers": lambda m: subset_agent_count(self), + #"connectivity": lambda m: check_food_source_connectivity(self.grid_food,current_paths), }, agent_reporters={} ) self.datacollector.collect(self) # keep at end of __init___ + #def subset_agent_count(self): + # subset_agents = [agent for agent in self.schedule.agents if agent.sensitivity == self.s_0] + # count = float(len(subset_agents)) + # return count + def agent_density(self): a = np.zeros((self.grid.width, self.grid.height)) for i in range(self.grid.width): @@ -180,4 +230,4 @@ This program is free software: you can redistribute it and/or modify it under th This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see -""" +""" \ No newline at end of file From 74d59ecc2e95307ffc59cc2abcfd204cd458d386 Mon Sep 17 00:00:00 2001 From: AlexBocken Date: Mon, 26 Jun 2023 10:27:53 +0200 Subject: [PATCH 2/3] fix indents from ipynb to py --- main.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) mode change 100644 => 100755 main.py diff --git a/main.py b/main.py old mode 100644 new mode 100755 index bcd979c..448c1be --- a/main.py +++ b/main.py @@ -270,23 +270,23 @@ plt.show() # |%%--%%| # Connectivity measure - def check_food_source_connectivity(food_sources, paths): #food_sources = nodes.is_nest, paths=result from BFS - connected_food_sources = set() - - for source in food_sources: - if source in paths: - connected_food_sources.add(source) - - connectivity = len(connected_food_sources) - - - return connectivity - - - # Calculate connectivity through BFS - - current_paths = bfs(self.grid, self.grid.fields["nests"], 0.000001) - +def check_food_source_connectivity(food_sources, paths): #food_sources = nodes.is_nest, paths=result from BFS + connected_food_sources = set() + + for source in food_sources: + if source in paths: + connected_food_sources.add(source) + + connectivity = len(connected_food_sources) + + + return connectivity + + + # Calculate connectivity through BFS + + current_paths = bfs(self.grid, self.grid.fields["nests"], 0.000001) + # |%%--%%| <64kmoHYvCD|JEzmDy4wuX> @@ -298,12 +298,10 @@ plt.show() - + # |%%--%%| # |%%--%%| - - From 960b4b1ca0b6bf66280185d4d392059f30c9a900 Mon Sep 17 00:00:00 2001 From: AlexBocken Date: Mon, 26 Jun 2023 10:55:23 +0200 Subject: [PATCH 3/3] alternative cummulative successful ants measurement --- agent.py | 3 ++- main.py | 1 + model.py | 42 +++++++++++++++++++----------------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/agent.py b/agent.py index 60feeaf..105fcae 100644 --- a/agent.py +++ b/agent.py @@ -113,8 +113,8 @@ class RandomWalkerAnt(Agent): self.energy = self.model.e_0 #now look for other pheromone - self.drop_pheromone = "B" self.look_for_pheromone = "A" + self.drop_pheromone = "B" self._prev_pos = neighbor self._next_pos = self.pos @@ -133,6 +133,7 @@ class RandomWalkerAnt(Agent): self._prev_pos = neighbor self._next_pos = self.pos + self.model.successful_ants += 1 # recruit new ants diff --git a/main.py b/main.py index 448c1be..1d91e90 100755 --- a/main.py +++ b/main.py @@ -187,6 +187,7 @@ def check_ants_follow_gradient(): # main() from model import kwargs_paper_setup1 as kwargs +# kwargs["N_m"] = 10000 model = ActiveWalkerModel(**kwargs) from hexplot import plot_hexagon diff --git a/model.py b/model.py index db2c567..357d95b 100644 --- a/model.py +++ b/model.py @@ -100,6 +100,7 @@ class ActiveWalkerModel(Model): self.q_tr : float = q_tr # threshold under which ant cannot distinguish concentrations self.e_min : float = e_min # energy at which walker dies self.N_f : int = N_f #num food sources + self.successful_ants = 0 # for viviane's graph fields=["A", "B", "nests", "food", "res"] self.schedule = SimultaneousActivation(self) @@ -135,7 +136,7 @@ class ActiveWalkerModel(Model): for _ in range(N_f): self.grid.add_food(food_size) - + # Breadth-first-search algorithm for connectivity #def bfs(graph, start_node, threshold): #graph=grid, start_node=nest, threshold=TBD? @@ -143,7 +144,7 @@ class ActiveWalkerModel(Model): # queue = deque([(start_node, [])]) # paths = {} # connected_food_sources = set() - + # while queue: # current_node, path = queue.popleft() #current_node = tuple(current_node) @@ -154,36 +155,31 @@ class ActiveWalkerModel(Model): # if neighbor not in visited and m.grid.fields["A"] >= threshold: # new_path = path + [neighbor] # queue.append((neighbor, new_path)) - + # Check if the neighbor is a food source # if neighbor in self.grid_food: # if neighbor not in paths: # paths[neighbor] = new_path # connected_food_sources.add(neighbor) - # connectivity = len(connected_food_sources) - - # return connectivity - - - # Calculate connectivity through BFS - - # current_paths = bfs(self.grid, self.grid.fields["nests"], 0.000001) - - - - def subset_agent_count(self): - subset_agents = [agent for agent in self.schedule.agents if agent.sensitivity == self.s_0 and agent.look_for_pheromone == "B"] - count = float(len(subset_agents)) - return count + # connectivity = len(connected_food_sources) + + # return connectivity + + + # Calculate connectivity through BFS + + # current_paths = bfs(self.grid, self.grid.fields["nests"], 0.000001) + + + - self.datacollector = DataCollector( # model_reporters={"agent_dens": lambda m: m.agent_density()}, model_reporters = {"pheromone_a": lambda m: m.grid.fields["A"], "pheromone_b": lambda m: m.grid.fields["B"], - "alive_ants": lambda m: self.schedule.get_agent_count(), - "sucessful_walkers": lambda m: subset_agent_count(self), + "alive_ants": lambda m: m.schedule.get_agent_count(), + "sucessful_walkers": lambda m: m.successful_ants, #"connectivity": lambda m: check_food_source_connectivity(self.grid_food,current_paths), }, agent_reporters={} @@ -194,7 +190,7 @@ class ActiveWalkerModel(Model): # subset_agents = [agent for agent in self.schedule.agents if agent.sensitivity == self.s_0] # count = float(len(subset_agents)) # return count - + def agent_density(self): a = np.zeros((self.grid.width, self.grid.height)) for i in range(self.grid.width): @@ -230,4 +226,4 @@ This program is free software: you can redistribute it and/or modify it under th This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see -""" \ No newline at end of file +"""