Skip to content

Commit 4ee29b8

Browse files
authored
Use utils.argmax_random_tie for best-individual selection in tsp.py (#1297)
np.argmax does not accept a key argument (TypeError) and np.argmax_random_tie does not exist (argmax_random_tie lives in utils). Use utils.argmax_random_tie in both the genetic algorithm and hill climbing loops of the TSP GUI. Fixes #1256.
1 parent efaf99e commit 4ee29b8

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

gui/tsp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def fitness_fn(state):
260260
while True:
261261
population = [mutate(recombine(*select(2, population, fitness_fn)), self.mutation_rate.get())
262262
for _ in range(len(population))]
263-
current_best = np.argmax(population, key=fitness_fn)
263+
current_best = utils.argmax_random_tie(population, key=fitness_fn)
264264
if fitness_fn(current_best) > fitness_fn(all_time_best):
265265
all_time_best = current_best
266266
self.cost.set("Cost = " + str('%0.3f' % (-1 * problem.value(all_time_best))))
@@ -294,7 +294,7 @@ def find_neighbors(state, number_of_neighbors=100):
294294
current = Node(problem.initial)
295295
while True:
296296
neighbors = find_neighbors(current.state, self.no_of_neighbors.get())
297-
neighbor = np.argmax_random_tie(neighbors, key=lambda node: problem.value(node.state))
297+
neighbor = utils.argmax_random_tie(neighbors, key=lambda node: problem.value(node.state))
298298
map_canvas.delete('poly')
299299
points = []
300300
for city in current.state:

0 commit comments

Comments
 (0)