Home / Computer science / Traveling salesman problem (TSP) / Computation in Python
Use traveling_salesman.tour_random
import numpy as np
import traveling_salesman as ts
instance = ts.read_instance('pr76.tsp')
distances = ts.distances(instance)
random = np.random.default_rng(0)
tour, distance = ts.tour_random(instance, distances, random)
solution = ts.solution(tour, instance)
return f"Solution: \n {solution}" "\n\n" f"Distance: {distance:.2E}"
Solution: [ 6 65 62 17 59 40 36 72 46 9 48 31 73 63 5 71 44 20 21 51 28 43 24 35 11 54 3 75 61 76 14 12 18 53 29 37 19 45 4 2 67 25 22 1 7 23 27 38 74 66 56 41 13 16 50 33 26 47 15 10 39 68 58 52 32 69 49 8 64 70 60 55 30 42 57 34] Distance: 5.43E+05