我得到的输出是:
Inside loop : [5, 6, 3, 1, 4, 2]
Inside loop : [3, 1, 5, 6, 4, 2]
Inside loop : [1, 2, 4, 5, 6, 3]
Inside loop : [5, 2, 6, 4, 3, 1]
Outside loop : [5, 2, 6, 4, 3, 1]
Outside loop : [5, 2, 6, 4, 3, 1]
Outside loop : [5, 2, 6, 4, 3, 1]
Outside loop : [5, 2, 6, 4, 3, 1]
代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PossibleSolution {
// the indices of where to place the cuts to delimit routes (different
// vehicles)
int[] indicesCut;
// the set of ordered Customers for each route. Routes delimited by cuts
ArrayList<Integer> OrderedCustomers;
// length of array
int size;
// Constructor
public PossibleSolution(int[] indices, ArrayList<Integer> Customers) {
this.indicesCut = indices;
this.OrderedCustomers = Customers;
this.size = Customers.size();
}
// method to generate the neighborhood for one possible solution. We need a
// parameter
// to specify the number of neighbors to generate
public PossibleSolution[] generateNeighborhood(int number) {
PossibleSolution[] sol = new PossibleSolution[number];
for (int i = 0; i < number; i++) {
java.util.Collections.shuffle(this.OrderedCustomers);
sol[i] = new PossibleSolution(this.indicesCut, this.OrderedCustomers);
System.out.println("Inside loop : " + sol[i].OrderedCustomers);
}
for (int i = 0; i < number; i++) {
System.out.println("Outside loop : " + sol[i].OrderedCustomers);
}
return sol;
}
public static void main(String[] args) {
ArrayList<Integer> Customers = new ArrayList();
Customers.add(2);
Customers.add(4);
Customers.add(5);
Customers.add(1);
Customers.add(6);
Customers.add(3);
int[] ind = { 2, 3 };
PossibleSolution initialSol = new PossibleSolution(ind, Customers);
PossibleSolution[] table = initialSol.generateNeighborhood(4);
}
}