I am trying to implement a function which generates the two-opt neighbourhood for a given tour in the tsp problem but currently it does not generate values as expected it keeps duplicating the intial value, I think I know where the code is going wrong but I'm not sure how to correct it.
public void twoOptNeighbourhoodGenerationFunction() {
int[] givenRoute = new int[] {1,2,3,4};
for(int i = 1; i < (givenRoute .length); i ++) {
for(int j = i; j < givenRoute .length; j ++) {
int[] currentRoute = Arrays.copyOf(givenRoute, givenRoute.length);
int temp = currentRoute [i];
currentRoute [i] = currentRoute [j];
currentRoute [j] = temp;
System.out.println(Arrays.toString());
}
}
}
I think the following line below is the one causing the issue
int[] currentRoute = Arrays.copyOf(givenRoute, givenRoute.length);
This is because everytime I go through the loop it recreates the original array and prints it out, I need a way to stop this original array from printing
This the current output I get
[1, 2, 3, 4]
[1, 3, 2, 4]
[1, 4, 3, 2]
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 2, 3, 4]
This is the output I would like
[1, 2, 3, 4]
[1, 3, 2, 4]
[1, 4, 3, 2]
[1, 2, 4, 3]
I have though about using an if statement to compare the currentRoute to the givenList and only print if it is different but I was wondering if there is a better way of doing things.
Aucun commentaire:
Enregistrer un commentaire