fix indents, add bfs to scheduler

This commit is contained in:
Alexander Bocken 2023-06-27 22:45:40 +02:00
parent ef7646f556
commit 7b5627fafc
Signed by: Alexander
GPG Key ID: 1D237BE83F9B05E8

View File

@ -107,7 +107,6 @@ class ActiveWalkerModel(Model):
self.grid = MultiHexGridScalarFields(width=width, height=height, torus=True, fields=fields) self.grid = MultiHexGridScalarFields(width=width, height=height, torus=True, fields=fields)
if resistance_map_type is None: if resistance_map_type is None:
print("No resistance field")
self.grid.fields["res"] = np.ones((width, height)).astype(float) self.grid.fields["res"] = np.ones((width, height)).astype(float)
elif resistance_map_type == "perlin": elif resistance_map_type == "perlin":
# perlin generates anisotropic noise which may or may not be a good choice # perlin generates anisotropic noise which may or may not be a good choice
@ -139,6 +138,17 @@ class ActiveWalkerModel(Model):
for _ in range(N_f): for _ in range(N_f):
self.grid.add_food(food_size) self.grid.add_food(food_size)
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: m.schedule.get_agent_count(),
"sucessful_walkers": lambda m: m.successful_ants,
"connectivity": lambda m: m.connectivity,
},
agent_reporters={}
)
self.datacollector.collect(self) # keep at end of __init___
# Breadth-first-search algorithm for connectivity # Breadth-first-search algorithm for connectivity
# TODO: Implement pheromone B (take max of the two or sum?) # TODO: Implement pheromone B (take max of the two or sum?)
@ -174,28 +184,9 @@ class ActiveWalkerModel(Model):
connectivity += 1 #then we have found a connected path to a food source connectivity += 1 #then we have found a connected path to a food source
connected_food_sources = connected_food_sources + list([current_node]) #and it is added to the list of connected food sources connected_food_sources = connected_food_sources + list([current_node]) #and it is added to the list of connected food sources
# why not normalize to 0-1 ?
return connectivity #we want the connectivity (0-5) return connectivity #we want the connectivity (0-5)
self.connectivity = bfs(self)
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: m.schedule.get_agent_count(),
"sucessful_walkers": lambda m: m.successful_ants,
"connectivity": lambda m: m.connectivity,
},
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): def agent_density(self):
a = np.zeros((self.grid.width, self.grid.height)) a = np.zeros((self.grid.width, self.grid.height))
for i in range(self.grid.width): for i in range(self.grid.width):
@ -206,12 +197,14 @@ class ActiveWalkerModel(Model):
def step(self): def step(self):
self.schedule.step() # step() and advance() all agents self.schedule.step() # step() and advance() all agents
self.connectivity = self.bfs(self)
# apply decay rate on pheromone levels # apply decay rate on pheromone levels
for key in ("A", "B"): for key in ("A", "B"):
field = self.grid.fields[key] field = self.grid.fields[key]
self.grid.fields[key] = field - self.gamma*field self.grid.fields[key] = field - self.gamma*field
self.datacollector.collect(self) self.datacollector.collect(self)
if self.schedule.steps >= self.max_steps: if self.schedule.steps >= self.max_steps: