@@ -158,8 +158,6 @@ class AcoTspModel(mesa.Model):
158
158
159
159
There is only one model-level parameter: how many agents the model contains. When a new model
160
160
is started, we want it to populate itself with the given number of agents.
161
-
162
- The scheduler is a special model component which controls the order in which agents are activated.
163
161
"""
164
162
165
163
def __init__ (
@@ -176,12 +174,10 @@ def __init__(
176
174
self .num_cities = tsp_graph .num_cities
177
175
self .all_cities = set (range (self .num_cities ))
178
176
self .max_steps = max_steps
179
- self .schedule = mesa .time .RandomActivation (self )
180
177
self .grid = mesa .space .NetworkGrid (tsp_graph .g )
181
178
182
179
for i in range (self .num_agents ):
183
180
agent = AntTSP (unique_id = i , model = self , alpha = ant_alpha , beta = ant_beta )
184
- self .schedule .add (agent )
185
181
186
182
city = tsp_graph .cities [self .random .randrange (self .num_cities )]
187
183
self .grid .place_agent (agent , city )
@@ -206,14 +202,15 @@ def __init__(
206
202
"tsp_solution" : "tsp_solution" ,
207
203
},
208
204
)
205
+ self .datacollector .collect (self ) # Collect initial state at steps=0
209
206
210
207
self .running = True
211
208
212
209
def update_pheromone (self , q : float = 100 , ro : float = 0.5 ):
213
210
# tau_ij(t+1) = (1-ro)*tau_ij(t) + delta_tau_ij(t)
214
211
# delta_tau_ij(t) = sum_k^M {Q/L^k} * I[i,j \in T^k]
215
212
delta_tau_ij = {}
216
- for k , agent in enumerate (self .schedule . agents ):
213
+ for k , agent in enumerate (self .agents ):
217
214
delta_tau_ij [k ] = agent .calculate_pheromone_delta (q )
218
215
219
216
for i , j in self .grid .G .edges ():
@@ -227,16 +224,14 @@ def update_pheromone(self, q: float = 100, ro: float = 0.5):
227
224
228
225
def step (self ):
229
226
"""
230
- A model step. Used for collecting data and advancing the schedule
227
+ A model step. Used for activating the agents and collecting data.
231
228
"""
232
- self .datacollector .collect (self )
233
- self .schedule .step ()
234
- self .num_steps += 1
229
+ self .agents .shuffle ().do ("step" )
235
230
self .update_pheromone ()
236
231
237
232
# Check len of cities visited by an agent
238
233
best_instance_iter = float ("inf" )
239
- for agent in self .schedule . agents :
234
+ for agent in self .agents :
240
235
# Check for best path
241
236
if agent .tsp_distance < self .best_distance :
242
237
self .best_distance = agent .tsp_distance
@@ -249,3 +244,5 @@ def step(self):
249
244
250
245
if self .num_steps >= self .max_steps :
251
246
self .running = False
247
+
248
+ self .datacollector .collect (self )
0 commit comments