Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
280 lines (227 loc) · 9.36 KB
/
Copy pathmodel.py
File metadata and controls
280 lines (227 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from mesa import Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from colony import Colony
from obstacle import Obstacle
from food import FoodGrid
from mesa.datacollection import DataCollector
import metrics
import numpy as np
import random
from scipy.ndimage import gaussian_filter
from scipy.spatial import distance
from ant import Ant
from copy import copy
from numba import jit
class Environment(Model):
""" A model which contains a number of ant colonies. """
def __init__(self, width, height, n_colonies, n_ants, n_obstacles, decay=0.2, sigma=0.1, moore=False, birth=True, death=True):
"""
:param width: int, width of the system
:param height: int, height of the system
:param n_colonies: int, number of colonies
:param n_ants: int, number of ants per colony
:param decay: float, the rate in which the pheromone decays
:param sigma: float, sigma of the Gaussian convolution
:param moore: boolean, True/False whether Moore/vonNeumann is used
"""
super().__init__()
# Agent variables
self.birth = birth
self.death = death
self.pheromone_level = 1
# Environment variables
self.width = width
self.height = height
self.grid = MultiGrid(width, height, False)
self.moore = moore
self.sigma = sigma
self.decay = decay
# Environment attributes
self.schedule = RandomActivation(self)
self.colonies = [Colony(self, i, (width // 2, height // 2), n_ants, birth=self.birth, death=self.death) for i in range(n_colonies)]
self.pheromones = np.zeros((width, height), dtype=np.float)
self.pheromone_updates = []
self.food = FoodGrid(self)
self.food.add_food()
self.obstacles = []
for _ in range(n_obstacles):
self.obstacles.append(Obstacle(self))
# Metric + data collection
self.min_distance = distance.cityblock(self.colonies[0].pos, self.food.get_food_pos())
self.datacollector = DataCollector(
model_reporters={"Minimum path length": metrics.min_path_length,
"Mean minimum path length": metrics.mean_min_path_length},
agent_reporters={"Agent minimum path length": lambda x: min(x.path_lengths),
"Encounters": Ant.count_encounters})
# Animation attributes
self.pheromone_im = None
self.ax = None
def step(self):
"""
Do a single time-step using freeze-dry states, colonies are updated each time-step in random orders, and ants
are updated per colony in random order.
"""
self.food.step()
self.datacollector.collect(self)
# Update all colonies
for col in random.sample(self.colonies, len(self.colonies)):
col.step()
self.schedule.step()
self.update_pheromones()
def move_agent(self, ant, pos):
"""
Move an agent across the map.
:param ant: class Ant
:param pos: tuple (x, y)
"""
if self.moore:
assert np.sum(np.subtract(pos, ant.pos) ** 2) in [1, 2], \
"the ant can't move from its original position {} to the new position {}, because the distance " \
"is too large".format(ant.pos, pos)
else:
assert np.sum(np.subtract(pos, ant.pos) ** 2) == 1, \
"the ant can't move from its original position {} to the new position {}, because the distance " \
"is too large, loc_food {}".format(ant.pos, pos, self.food.get_food_pos())
self.grid.move_agent(ant, pos)
def get_random_position(self):
return (np.random.randint(0, self.width), np.random.randint(0, self.height))
def position_taken(self, pos):
if pos in self.food.get_food_pos():
return True
for colony in self.colonies:
if colony.on_colony(pos):
return True
for obstacle in self.obstacles:
if obstacle.on_obstacle(pos):
return True
return False
def add_food(self):
"""
Add food somewhere on the map, which is not occupied by a colony yet
"""
self.food.add_food()
def place_pheromones(self, pos):
"""
Add pheromone somewhere on the map
:param pos: tuple (x, y)
"""
self.pheromone_updates.append((pos, self.pheromone_level))
def get_neighbor_pheromones(self, pos, id):
"""
Get the passable neighboring positions and their respective pheromone levels for the pheromone id
:param pos:
:param id:
:return:
"""
indices = self.grid.get_neighborhood(pos, self.moore)
indices = [x for x in indices if not any([isinstance(x, Obstacle) for x in self.grid[x[0]][x[1]]])]
pheromones = [self.pheromones[x, y] for x, y in indices]
return indices, pheromones
def update_pheromones(self):
"""
Place the pheromones at the end of a timestep on the grid. This is necessary for freeze-dry time-steps
"""
for (pos, level) in self.pheromone_updates:
# self.pheromones[pos] += level
self.pheromones[pos] += 1
self.pheromone_updates = []
# gaussian convolution using self.sigma
self.pheromones = gaussian_filter(self.pheromones, self.sigma) * self.decay
def animate(self, ax):
"""
:param ax:
:return:
"""
self.ax = ax
self.animate_pheromones()
self.animate_colonies()
self.animate_ants()
self.animate_food()
self.animate_obstacles()
def animate_pheromones(self):
"""
Update the visualization part of the Pheromones.
:param ax:
"""
pheromones = np.rot90(self.pheromones.astype(np.float64).reshape(self.width, self.height))
if not self.pheromone_im:
self.pheromone_im = self.ax.imshow(pheromones,
vmin=0, vmax=50,
interpolation='None', cmap="Purples")
else:
self.pheromone_im.set_array(pheromones)
def animate_colonies(self):
"""
Update the visualization part of the Colonies.
:return:
"""
for colony in self.colonies:
colony.update_vis()
def animate_food(self):
"""
Update the visualization part of the FoodGrid.
:return:
"""
self.food.update_vis()
def animate_ants(self):
"""
Update the visualization part of the Ants.
"""
for ant in self.schedule.agents:
ant.update_vis()
def animate_obstacles(self):
"""
Update the visualization part of the Obstacles.
:return:
"""
for obstacle in self.obstacles:
obstacle.update_vis()
def grid_to_array(self, pos):
"""
Convert the position/indices on self.grid to imshow array.
:param pos: tuple (int: x, int: y)
:return: tuple (float: x, float: y)
"""
return pos[0] - 0.5, self.height - pos[1] - 1.5
def pheromone_threshold(self, threshold):
""" Returns an array of the positions in the grid in which
the pheromone levels are above the given threshold"""
pher_above_thres = np.where(self.pheromones >= threshold)
return list(zip(pher_above_thres[0],pher_above_thres[1]))
def find_path(self, pher_above_thres):
""" Returns the shortest paths from all the colonies to all the food sources.
A path can only use the positions in the given array. Therefore, this function
checks whether there is a possible path for a certain pheremone level.
Essentially a breadth first search"""
space_searched = False
all_paths = []
food_sources = self.food.get_food_pos()
# Search the paths for a colony to all food sources
for colony in self.colonies:
colony_paths = []
pos_list = {colony.pos} # Prooning
possible_paths = [[colony.pos]]
# Continue expanding search area until all food sources found
# or until the entire space is searched
while food_sources != [] and not space_searched:
space_searched = True
temp = []
for path in possible_paths:
for neighbor in self.grid.get_neighborhood(include_center=False, radius=1, pos=path[-1], moore=self.moore):
if neighbor in food_sources:
food_path = copy(path)
food_path.append(neighbor)
colony_paths.append(food_path)
food_sources.remove(neighbor)
# Add epanded paths to the possible paths
if neighbor in pher_above_thres and neighbor not in pos_list:
space_searched = False
temp_path = copy(path)
temp_path.append(neighbor)
temp.append(temp_path)
pos_list.add(neighbor)
possible_paths.remove(path)
possible_paths += temp
all_paths.append(colony_paths)
return all_paths