I am trying to develop the genetic algorithm to solve Traveling Saleman problem and part of my challenge is to generate random tours long enough and write a fitness function to evaluate the cost incurred for each of the random tour. I have attempted to write the following. My objective is to generate random tours from the city to city distance matrix without any repetitions in the tour. The random function should return a tour without any cities repeating twice. Please can any one help me with the logic for this problem:
class ran
{
public static void ranper(int n, Random newrand, int perm[])
{
int i,j,k;
for(i=1;i<=n;i++)
{
perm[i] = i;
}
for (i=1; i<=n; i++)
{
j = (int)(i + ran.nextDouble() * (n + 1 - i));
k = perm[i];
perm[i] = perm[j];
perm[j] = k;
}
System.out.println("A random permuation of " + n + " elements:");
for (i=1; i<=n; i++)
{
System.out.print(" " + perm[i]);
}
}
public static void main(String args[])
{
int n = 9,j,k,i;
long seed = 1;
int perm[] = new int[n+1];
Random newrand = new Random(seed);
randper(n, newrand, perm);
System.out.println();
}
}