0

I followed a tutorial on how to use mlrose for TSP. The tutorial didn't cover on how can I define the start point (and also end point of the optimized route). How can I do it (if I can do it). I'll link what I got so far.

This is the code I used:

import six
import sys
sys.modules['sklearn.externals.six'] = six
import mlrose

# Create list of city coordinates
coords_list = [(supplier.iat[0,0],supplier.iat[0,1])]


for i in range(len(retailers)):
    
    entry = [(retailers.iat[i,0], retailers.iat[i,1])]
    coords_list += entry

# Initialize fitness function object using coords_list
fitness_coords = mlrose.TravellingSales(coords = coords_list)

#define problem_fit
problem_fit = mlrose.TSPOpt(length = len(coords_list), fitness_fn = fitness_coords,
                            maximize=False)
# Solve problem using the genetic algorithm
best_state, best_fitness = mlrose.genetic_alg(problem_fit, mutation_prob = 0.2,
                                              max_attempts = 100, random_state = 2)

print('The best route found is: ', best_state)

print('The lenght at the best route is: ', best_fitness)


The coord_list can be also:

coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]

I just want, for example, that (1,1) is the first and last point of the route. The output I wished for would be -> "The best route found is: 0 2 5 3 4 7 1 6 0"

JM Aido
  • 13
  • 2

0 Answers0