1
+ # -*- coding: utf-8 -*-
1
2
from __future__ import print_function
2
3
import math
3
4
import random
@@ -28,6 +29,8 @@ def move(self):
28
29
a = random .randint (0 , len (self .state ) - 1 )
29
30
b = random .randint (0 , len (self .state ) - 1 )
30
31
self .state [a ], self .state [b ] = self .state [b ], self .state [a ]
32
+ # no efficiency gain, just proof of concept
33
+ return self .energy ()
31
34
32
35
def energy (self ):
33
36
"""Calculates the length of the route."""
@@ -37,7 +40,6 @@ def energy(self):
37
40
return e
38
41
39
42
40
-
41
43
if __name__ == '__main__' :
42
44
43
45
# latitude and longitude for the twenty largest U.S. cities
@@ -79,7 +81,7 @@ def energy(self):
79
81
distance_matrix [ka ][kb ] = distance (va , vb )
80
82
81
83
tsp = TravellingSalesmanProblem (init_state , distance_matrix )
82
- tsp .steps = 100000
84
+ tsp .set_schedule ( tsp . auto ( minutes = 0.2 ))
83
85
# since our state is just a list, slice is the fastest way to copy
84
86
tsp .copy_strategy = "slice"
85
87
state , e = tsp .anneal ()
@@ -89,5 +91,4 @@ def energy(self):
89
91
90
92
print ()
91
93
print ("%i mile route:" % e )
92
- for city in state :
93
- print ("\t " , city )
94
+ print (" ➞ " .join (state ))
0 commit comments