From 15efd64c3e39cdad78ded71d6d9e5ce9de0bf02f Mon Sep 17 00:00:00 2001 From: AlexBocken Date: Mon, 26 Jun 2023 10:23:19 +0200 Subject: [PATCH] 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