Skip to content

Commit

Permalink
update salesman.py example to use auto schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Aug 3, 2019
1 parent 5405975 commit 08fe2f1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions examples/salesman.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
import math
import random
Expand Down Expand Up @@ -28,6 +29,8 @@ def move(self):
a = random.randint(0, len(self.state) - 1)
b = random.randint(0, len(self.state) - 1)
self.state[a], self.state[b] = self.state[b], self.state[a]
# no efficiency gain, just proof of concept
return self.energy()

def energy(self):
"""Calculates the length of the route."""
Expand All @@ -37,7 +40,6 @@ def energy(self):
return e



if __name__ == '__main__':

# latitude and longitude for the twenty largest U.S. cities
Expand Down Expand Up @@ -79,7 +81,7 @@ def energy(self):
distance_matrix[ka][kb] = distance(va, vb)

tsp = TravellingSalesmanProblem(init_state, distance_matrix)
tsp.steps = 100000
tsp.set_schedule(tsp.auto(minutes=0.2))
# since our state is just a list, slice is the fastest way to copy
tsp.copy_strategy = "slice"
state, e = tsp.anneal()
Expand All @@ -89,5 +91,4 @@ def energy(self):

print()
print("%i mile route:" % e)
for city in state:
print("\t", city)
print(" ➞ ".join(state))

0 comments on commit 08fe2f1

Please sign in to comment.