Skip to content Skip to sidebar Skip to footer

Generating A List Of Random Permutations Of Another List

So, I'm trying to tackle the TSP with a Genetic Algorithm. To do that I need to create a population pool. What I wan't to accomplish is to create a list of random permutations that

Solution 1:

shuffle modifies the list in-place. you need to add a copy of the list to your routes; otherwise a reference to the same list is added which will be in its last shuffled state.

for x in range(n):
    random.shuffle(cities)
    routes.append(cities.copy())

Solution 2:

import random
print [random.sample(cities,n) for i in xrange(n)]

You can try this.


Post a Comment for "Generating A List Of Random Permutations Of Another List"