add num_max_agents, cleanup

This commit is contained in:
Alexander Bocken 2023-05-18 12:46:48 +02:00
parent a8cb347e4a
commit 30661542b5
Signed by: Alexander
GPG Key ID: 1D237BE83F9B05E8
4 changed files with 16 additions and 32 deletions

View File

@ -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):

21
main.py
View File

@ -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()

View File

@ -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)

View File

@ -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,
}