I'm working on a script which creates random solutions to the traveling salesman problem. I have a set of cities, as well as a set of distances (which I not yet need since I'm creating random solutions).
I am attempting to use Itertools.permutations on the list of cities in order to create unique routes. My questions are:
Thanks in advance!
This Python code will generate the permutations based on a random starting point and will stop after 6 permutations:
from itertools import permutations
from random import shuffle
A=range(26)
shuffle(A)
for i,perm in enumerate(permutations(A)):
print perm
if i>=5:
break
Note that the permutations still have a lot of structure so the second permutation will be a lot like the first.
You may do better simply using shuffle(A) each time to try a different permutation. (It may regenerate a permutation but with very low probability for 29 cities.) For example,
for i in range(10):
shuffle(A)
print A