2023-04-26 23:45:14 +02:00
|
|
|
"""
|
|
|
|
agent.py - Part of ants project
|
|
|
|
|
|
|
|
This model implements the actual agents on the grid (a.k.a. the ants)
|
|
|
|
|
|
|
|
License: AGPL 3 (see end of file)
|
2023-05-17 15:57:23 +02:00
|
|
|
(C) Alexander Bocken, Viviane Fahrni, Grace Kagho
|
2023-04-26 23:45:14 +02:00
|
|
|
"""
|
|
|
|
import numpy as np
|
2023-05-07 15:24:27 +02:00
|
|
|
import numpy.typing as npt
|
2023-04-26 23:45:14 +02:00
|
|
|
from mesa.agent import Agent
|
|
|
|
from mesa.space import Coordinate
|
|
|
|
|
|
|
|
class RandomWalkerAnt(Agent):
|
2023-05-17 19:32:08 +02:00
|
|
|
def __init__(self, unique_id, model, look_for_pheromone=None,
|
2023-05-11 16:45:20 +02:00
|
|
|
energy_0=1,
|
2023-05-17 19:32:08 +02:00
|
|
|
pheromone_drop_rate_0 : dict[str, float]={"A": 80, "B": 80},
|
2023-05-11 16:45:20 +02:00
|
|
|
sensitivity_0=0.99,
|
2023-05-17 19:32:08 +02:00
|
|
|
alpha=0.6, drop_pheromone=None,
|
2023-05-11 16:45:20 +02:00
|
|
|
betas : dict[str, float]={"A": 0.0512, "B": 0.0512},
|
|
|
|
sensitivity_decay_rate=0.01,
|
2023-05-31 00:48:41 +02:00
|
|
|
sensitivity_max = 300,
|
|
|
|
sensitivity_min = 0.001,
|
|
|
|
sensitivity_steepness = 1
|
2023-05-07 15:24:27 +02:00
|
|
|
) -> None:
|
2023-05-11 16:45:20 +02:00
|
|
|
|
2023-04-26 23:45:14 +02:00
|
|
|
super().__init__(unique_id=unique_id, model=model)
|
|
|
|
|
|
|
|
self._next_pos : None | Coordinate = None
|
2023-05-18 16:00:43 +02:00
|
|
|
self._prev_pos : None | Coordinate = None
|
2023-05-11 16:45:20 +02:00
|
|
|
|
2023-05-17 19:32:08 +02:00
|
|
|
self.look_for_pheromone = look_for_pheromone
|
|
|
|
self.drop_pheromone = drop_pheromone
|
2023-05-11 16:45:20 +02:00
|
|
|
self.energy = energy_0 #TODO: use
|
|
|
|
self.sensitivity_0 = sensitivity_0
|
|
|
|
self.sensitivity = self.sensitivity_0
|
2023-05-17 19:32:08 +02:00
|
|
|
self.pheromone_drop_rate = pheromone_drop_rate_0
|
2023-04-26 23:45:14 +02:00
|
|
|
self.alpha = alpha
|
2023-05-11 16:45:20 +02:00
|
|
|
self.sensitivity_max = sensitivity_max
|
2023-05-31 00:48:41 +02:00
|
|
|
self.sensitivity_min = sensitivity_min
|
2023-05-11 16:45:20 +02:00
|
|
|
self.sensitivity_decay_rate = sensitivity_decay_rate
|
2023-05-31 00:48:41 +02:00
|
|
|
self.sensitivity_steepness = sensitivity_steepness
|
2023-05-11 16:45:20 +02:00
|
|
|
self.betas = betas
|
2023-05-18 16:00:43 +02:00
|
|
|
self.threshold : dict[str, float] = {"A": 0, "B": 0}
|
2023-05-31 00:48:41 +02:00
|
|
|
|
2023-04-26 23:45:14 +02:00
|
|
|
|
2023-04-28 14:45:56 +02:00
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
def sens_adj(self, props, key) -> npt.NDArray[np.float_] | float:
|
|
|
|
"""
|
|
|
|
returns the adjusted value of any property dependent on the current
|
|
|
|
sensitivity.
|
|
|
|
The idea is to have a nonlinear response, where any opinion below a
|
|
|
|
threshold (here: self.threshold[key]) is ignored, otherwise it returns
|
|
|
|
the property
|
|
|
|
Long-term this function should be adjusted to return the property up
|
|
|
|
to a upper threshold as well.
|
|
|
|
|
|
|
|
|
|
|
|
returns ^
|
|
|
|
|
|
|
|
|
sens_max| __________
|
|
|
|
| /
|
|
|
|
| /
|
|
|
|
q^tr| /
|
|
|
|
|
|
|
|
|
0|________
|
|
|
|
-----------------------> prop
|
2023-05-31 00:48:41 +02:00
|
|
|
For the nonlinear sensitivity, the idea is to use a logistic function that has
|
|
|
|
a characteristic sigmoidal shape that starts from a low value, increases rapidly,
|
|
|
|
and then gradually approaches a saturation level.
|
|
|
|
|
|
|
|
f(x) = L / (1 + exp(-k*(x - x0)))
|
|
|
|
|
|
|
|
f(x) = return value
|
|
|
|
L = sens_max
|
|
|
|
k is a parameter that controls the steepness of the curve. We can start with 1
|
|
|
|
A higher value of k leads to a steeper curve.
|
|
|
|
x0 is the midpoint of the curve, where the sensitivity starts to increase significantly.
|
|
|
|
We can make X0 the threshold value
|
|
|
|
|
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
"""
|
2023-05-07 15:24:27 +02:00
|
|
|
# if props iterable create array, otherwise return single value
|
|
|
|
try:
|
|
|
|
iter(props)
|
|
|
|
except TypeError:
|
2023-05-31 00:48:41 +02:00
|
|
|
#TODO: proper nonlinear response, not just clamping
|
|
|
|
|
|
|
|
non_linear_sens = True
|
|
|
|
if non_linear_sens:
|
|
|
|
L = self.sensitivity_max
|
|
|
|
k = self.sensitivity_steepness
|
|
|
|
mid = self.threshold[key]
|
|
|
|
if props > self.sensitivity_max:
|
|
|
|
return self.sensitivity_max
|
|
|
|
|
|
|
|
#Should we still keep these conditions?
|
|
|
|
# if props > self.threshold[key]:
|
|
|
|
# return props
|
|
|
|
else:
|
|
|
|
adjusted_sensitivity = L / (1 + np.exp(-k * (props - mid)))
|
|
|
|
print(f'props: {props}, adjusted_value: {adjusted_sensitivity}')
|
|
|
|
return adjusted_sensitivity
|
2023-05-07 15:24:27 +02:00
|
|
|
else:
|
2023-05-31 00:48:41 +02:00
|
|
|
if props > self.sensitivity_max:
|
|
|
|
return self.sensitivity_max #Should we still keep these conditions
|
|
|
|
if props > self.threshold[key]:
|
|
|
|
return props
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2023-05-07 15:24:27 +02:00
|
|
|
|
|
|
|
arr : list[float] = []
|
|
|
|
for prop in props:
|
2023-05-11 16:45:20 +02:00
|
|
|
arr.append(self.sens_adj(prop, key))
|
2023-05-07 15:24:27 +02:00
|
|
|
return np.array(arr)
|
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
def _choose_next_pos(self):
|
2023-05-18 16:00:43 +02:00
|
|
|
if self._prev_pos is None:
|
2023-04-29 11:00:42 +02:00
|
|
|
i = np.random.choice(range(6))
|
2023-05-18 16:00:43 +02:00
|
|
|
assert(self.pos is not self.neighbors()[i])
|
2023-04-29 11:00:42 +02:00
|
|
|
self._next_pos = self.neighbors()[i]
|
2023-05-18 16:00:43 +02:00
|
|
|
self._prev_pos = self.pos
|
2023-04-29 11:00:42 +02:00
|
|
|
return
|
2023-05-07 15:24:27 +02:00
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
if self.searching_food:
|
2023-05-07 15:24:27 +02:00
|
|
|
for neighbor in self.front_neighbors:
|
|
|
|
if self.model.grid.is_food(neighbor):
|
2023-05-17 19:32:08 +02:00
|
|
|
self.drop_pheromone = "B"
|
2023-05-18 12:46:48 +02:00
|
|
|
self.look_for_pheromone = "A"
|
2023-05-11 16:45:20 +02:00
|
|
|
self.sensitivity = self.sensitivity_0
|
|
|
|
|
2023-05-18 16:00:43 +02:00
|
|
|
self._prev_pos = neighbor
|
2023-05-07 15:24:27 +02:00
|
|
|
self._next_pos = self.pos
|
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
elif self.searching_nest:
|
2023-05-07 15:24:27 +02:00
|
|
|
for neighbor in self.front_neighbors:
|
|
|
|
if self.model.grid.is_nest(neighbor):
|
2023-05-17 19:32:08 +02:00
|
|
|
self.look_for_pheromone = "A" # Is this a correct interpretation?
|
|
|
|
self.drop_pheromone = "A"
|
2023-05-11 16:45:20 +02:00
|
|
|
self.sensitivity = self.sensitivity_0
|
|
|
|
|
2023-05-18 16:00:43 +02:00
|
|
|
self._prev_pos = neighbor
|
2023-05-07 15:24:27 +02:00
|
|
|
self._next_pos = self.pos
|
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
# recruit new ants
|
2023-05-07 15:24:27 +02:00
|
|
|
for agent_id in self.model.get_unique_ids(self.model.num_new_recruits):
|
2023-05-18 12:46:48 +02:00
|
|
|
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)
|
2023-05-07 15:24:27 +02:00
|
|
|
|
|
|
|
# follow positive gradient
|
2023-05-17 19:32:08 +02:00
|
|
|
if self.look_for_pheromone is not None:
|
|
|
|
front_concentration = [self.model.grid.fields[self.look_for_pheromone][cell] for cell in self.front_neighbors ]
|
|
|
|
front_concentration = self.sens_adj(front_concentration, self.look_for_pheromone)
|
|
|
|
current_pos_concentration = self.sens_adj(self.model.grid.fields[self.look_for_pheromone][self.pos], self.look_for_pheromone)
|
2023-05-18 16:00:43 +02:00
|
|
|
gradient = front_concentration - np.repeat(current_pos_concentration, 3).astype(np.float_)
|
|
|
|
# TODO: if two or more neighbors have same concentration randomize? Should be unlikely with floats though
|
2023-04-28 19:10:33 +02:00
|
|
|
index = np.argmax(gradient)
|
|
|
|
if gradient[index] > 0:
|
|
|
|
self._next_pos = self.front_neighbors[index]
|
2023-05-18 16:00:43 +02:00
|
|
|
self._prev_pos = self.pos
|
2023-04-28 19:10:33 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# do biased random walk
|
|
|
|
p = np.random.uniform()
|
|
|
|
if p < self.alpha:
|
|
|
|
self._next_pos = self.front_neighbor
|
2023-05-18 16:00:43 +02:00
|
|
|
self._prev_pos = self.pos
|
2023-04-28 19:10:33 +02:00
|
|
|
else:
|
|
|
|
# need copy() as we would otherwise remove the tuple from all possible lists (aka python "magic")
|
|
|
|
other_neighbors = self.neighbors().copy()
|
|
|
|
other_neighbors.remove(self.front_neighbor)
|
|
|
|
random_index = np.random.choice(range(len(other_neighbors)))
|
|
|
|
self._next_pos = other_neighbors[random_index]
|
2023-05-18 16:00:43 +02:00
|
|
|
self._prev_pos = self.pos
|
2023-04-28 14:45:56 +02:00
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
|
|
|
|
def step(self):
|
|
|
|
self.sensitivity -= self.sensitivity_decay_rate
|
|
|
|
self._choose_next_pos()
|
2023-05-17 19:32:08 +02:00
|
|
|
self._adjust_pheromone_drop_rate()
|
2023-05-11 16:45:20 +02:00
|
|
|
|
2023-05-31 00:48:41 +02:00
|
|
|
#kill agent if sensitivity is low
|
|
|
|
if self.sensitivity < self.sensitivity_min:
|
|
|
|
self._kill_agent()
|
|
|
|
|
2023-05-17 19:32:08 +02:00
|
|
|
def _adjust_pheromone_drop_rate(self):
|
|
|
|
if(self.drop_pheromone is not None):
|
|
|
|
self.pheromone_drop_rate[self.drop_pheromone] -= self.pheromone_drop_rate[self.drop_pheromone] * self.betas[self.drop_pheromone]
|
2023-05-11 16:45:20 +02:00
|
|
|
|
2023-05-17 19:32:08 +02:00
|
|
|
def drop_pheromones(self) -> None:
|
2023-04-28 19:10:33 +02:00
|
|
|
# should only be called in advance() as we do not use hidden fields
|
2023-05-17 19:32:08 +02:00
|
|
|
if self.drop_pheromone is not None:
|
|
|
|
self.model.grid.fields[self.drop_pheromone][self.pos] += self.pheromone_drop_rate[self.drop_pheromone]
|
2023-04-26 23:45:14 +02:00
|
|
|
|
2023-05-31 00:48:41 +02:00
|
|
|
def _kill_agent(self):
|
|
|
|
#update dead_agent list
|
|
|
|
self.model.dead_agents.append(self)
|
|
|
|
|
|
|
|
|
2023-04-26 23:45:14 +02:00
|
|
|
def advance(self) -> None:
|
2023-05-17 19:32:08 +02:00
|
|
|
self.drop_pheromones()
|
2023-04-29 11:00:42 +02:00
|
|
|
self.model.grid.move_agent(self, self._next_pos)
|
2023-05-18 16:00:43 +02:00
|
|
|
self._next_pos = None # so that we rather crash than use wrong data
|
2023-04-28 14:45:56 +02:00
|
|
|
|
2023-04-28 19:10:33 +02:00
|
|
|
# TODO: find out how to decorate with property properly
|
|
|
|
def neighbors(self, pos=None, include_center=False):
|
|
|
|
if pos is None:
|
|
|
|
pos = self.pos
|
|
|
|
return self.model.grid.get_neighborhood(pos, include_center=include_center)
|
|
|
|
|
2023-05-11 16:45:20 +02:00
|
|
|
@property
|
|
|
|
def searching_nest(self) -> bool:
|
2023-05-17 19:32:08 +02:00
|
|
|
return self.drop_pheromone == "B"
|
2023-05-11 16:45:20 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def searching_food(self) -> bool:
|
2023-05-17 19:32:08 +02:00
|
|
|
return self.drop_pheromone == "A"
|
2023-05-11 16:45:20 +02:00
|
|
|
|
2023-04-26 23:45:14 +02:00
|
|
|
@property
|
|
|
|
def front_neighbors(self):
|
2023-04-28 19:10:33 +02:00
|
|
|
"""
|
|
|
|
returns all three neighbors which the ant can see
|
|
|
|
"""
|
|
|
|
all_neighbors = self.neighbors()
|
2023-05-18 16:00:43 +02:00
|
|
|
neighbors_at_the_back = self.neighbors(pos=self._prev_pos, include_center=True)
|
2023-05-18 12:46:48 +02:00
|
|
|
front_neighbors = list(filter(lambda i: i not in neighbors_at_the_back, all_neighbors))
|
2023-05-18 16:00:43 +02:00
|
|
|
|
|
|
|
########## DEBUG
|
|
|
|
try:
|
|
|
|
assert(self._prev_pos is not None)
|
|
|
|
assert(self._prev_pos is not self.pos)
|
|
|
|
assert(self._prev_pos in all_neighbors)
|
|
|
|
assert(len(front_neighbors) == 3)
|
|
|
|
except AssertionError:
|
|
|
|
print(f"{self._prev_pos=}")
|
|
|
|
print(f"{self.pos=}")
|
|
|
|
print(f"{all_neighbors=}")
|
|
|
|
print(f"{neighbors_at_the_back=}")
|
|
|
|
print(f"{front_neighbors=}")
|
|
|
|
raise AssertionError
|
|
|
|
else:
|
|
|
|
return front_neighbors
|
2023-04-28 19:10:33 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def front_neighbor(self):
|
|
|
|
"""
|
|
|
|
returns neighbor of current pos
|
|
|
|
which is towards the front of the ant
|
|
|
|
"""
|
2023-05-18 16:00:43 +02:00
|
|
|
neighbors__prev_pos = self.neighbors(self._prev_pos)
|
2023-04-28 19:10:33 +02:00
|
|
|
for candidate in self.front_neighbors:
|
2023-05-18 16:00:43 +02:00
|
|
|
# neighbor in front direction only shares current pos as neighborhood with _prev_pos
|
2023-04-28 19:10:33 +02:00
|
|
|
candidate_neighbors = self.model.grid.get_neighborhood(candidate)
|
2023-05-18 16:00:43 +02:00
|
|
|
overlap = [x for x in candidate_neighbors if x in neighbors__prev_pos]
|
2023-04-28 19:10:33 +02:00
|
|
|
if len(overlap) == 1:
|
|
|
|
return candidate
|
|
|
|
|
2023-04-26 23:45:14 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3.
|
|
|
|
|
|
|
|
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 <https://www.gnu.org/licenses/>
|
|
|
|
"""
|