Skip to content

Commit ec07d89

Browse files
authored
aoc_tsp: Replace scheduler with AgentSet functionality (#191)
Also use the new, standardized order of count-do-collect.
1 parent c4b4c75 commit ec07d89

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

examples/aco_tsp/aco_tsp/model.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ class AcoTspModel(mesa.Model):
158158
159159
There is only one model-level parameter: how many agents the model contains. When a new model
160160
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.
163161
"""
164162

165163
def __init__(
@@ -176,12 +174,10 @@ def __init__(
176174
self.num_cities = tsp_graph.num_cities
177175
self.all_cities = set(range(self.num_cities))
178176
self.max_steps = max_steps
179-
self.schedule = mesa.time.RandomActivation(self)
180177
self.grid = mesa.space.NetworkGrid(tsp_graph.g)
181178

182179
for i in range(self.num_agents):
183180
agent = AntTSP(unique_id=i, model=self, alpha=ant_alpha, beta=ant_beta)
184-
self.schedule.add(agent)
185181

186182
city = tsp_graph.cities[self.random.randrange(self.num_cities)]
187183
self.grid.place_agent(agent, city)
@@ -206,14 +202,15 @@ def __init__(
206202
"tsp_solution": "tsp_solution",
207203
},
208204
)
205+
self.datacollector.collect(self) # Collect initial state at steps=0
209206

210207
self.running = True
211208

212209
def update_pheromone(self, q: float = 100, ro: float = 0.5):
213210
# tau_ij(t+1) = (1-ro)*tau_ij(t) + delta_tau_ij(t)
214211
# delta_tau_ij(t) = sum_k^M {Q/L^k} * I[i,j \in T^k]
215212
delta_tau_ij = {}
216-
for k, agent in enumerate(self.schedule.agents):
213+
for k, agent in enumerate(self.agents):
217214
delta_tau_ij[k] = agent.calculate_pheromone_delta(q)
218215

219216
for i, j in self.grid.G.edges():
@@ -227,16 +224,14 @@ def update_pheromone(self, q: float = 100, ro: float = 0.5):
227224

228225
def step(self):
229226
"""
230-
A model step. Used for collecting data and advancing the schedule
227+
A model step. Used for activating the agents and collecting data.
231228
"""
232-
self.datacollector.collect(self)
233-
self.schedule.step()
234-
self.num_steps += 1
229+
self.agents.shuffle().do("step")
235230
self.update_pheromone()
236231

237232
# Check len of cities visited by an agent
238233
best_instance_iter = float("inf")
239-
for agent in self.schedule.agents:
234+
for agent in self.agents:
240235
# Check for best path
241236
if agent.tsp_distance < self.best_distance:
242237
self.best_distance = agent.tsp_distance
@@ -249,3 +244,5 @@ def step(self):
249244

250245
if self.num_steps >= self.max_steps:
251246
self.running = False
247+
248+
self.datacollector.collect(self)

0 commit comments

Comments
 (0)