private QAPIndividual CXCrossover(QAPIndividual parent1, QAPIndividual parent2) { int problemSize = parent1.problem.ProblemSize; int[] newPermutation = new int[problemSize]; bool[] isUsed = new bool[problemSize]; // First, all items assigned to the same position in both parents are copied to this position in the child. for (int i = 0; i < problemSize; i++) { newPermutation[i] = -1;// no element if (parent1.permutation[i] == parent2.permutation[i]) { newPermutation[i] = parent1.permutation[i]; isUsed[newPermutation[i]] = true; } } for (int i = 0; i < newPermutation.Length; i++) { if (newPermutation[i] == -1) { int from = random.Next(2); QAPIndividual parent; QAPIndividual parentOther; if (from == 0) { parent = parent1; parentOther = parent2; } else { parent = parent2; parentOther = parent1; } int pos = i; do { var val = parentOther.permutation[pos]; newPermutation[pos] = parent.permutation[pos]; for (var j = 0; j < problemSize; j++) { if (parent.permutation[j] != val) { continue; } pos = j; break; } } while (pos != i); } } return(new QAPIndividual(problem, newPermutation)); }
// results in "qap/results/testName" public void test(string testName) { QAPProblem problem = problemFromFile(testName); //configuration GlobalConfiguration globalConfig = new GlobalConfiguration() { maximize = false }; globalConfig.configurations = new List <IslandConfiguration>(); globalConfig.configurations.Add(new IslandConfiguration(algorithm.enums.EvolutionStrategy.Tournament, 0.01, 0.1, 20, 10, 1)); globalConfig.configurations.Add(new IslandConfiguration(algorithm.enums.EvolutionStrategy.Tournament, 0.02, 0.05, 30, 10, 1)); globalConfig.configurations.Add(new IslandConfiguration(algorithm.enums.EvolutionStrategy.Tournament, 0.05, 0.02, 20, 10, 1)); globalConfig.configurations.Add(new IslandConfiguration(algorithm.enums.EvolutionStrategy.Tournament, 0.1, 0.01, 30, 10, 1)); globalConfig.connections = new List <int> [globalConfig.configurations.Count]; for (int i = 0; i < globalConfig.configurations.Count; i++) { globalConfig.connections[i] = new List <int>(); } //globalConfig.connections[0].Add(3); //globalConfig.connections[1].Add(3); //globalConfig.connections[2].Add(3); globalConfig.evolutionTimeInSeconds = 25; globalConfig.generator = new QAPGenerator(problem); IslandSupervisor supervisor = new IslandSupervisor(globalConfig); QAPIndividual result = (QAPIndividual)supervisor.getResult(); foreach (int i in result.permutation) { Console.WriteLine(i.ToString() + " "); } //int result = 5; saveResult(result.value(), testName); }
private QAPIndividual CXCrossover(QAPIndividual parent1, QAPIndividual parent2) { int problemSize = parent1.problem.ProblemSize; int[] newPermutation = new int[problemSize]; bool[] isUsed = new bool[problemSize]; // First, all items assigned to the same position in both parents are copied to this position in the child. for (int i = 0; i < problemSize; i++) { newPermutation[i] = -1;// no element if (parent1.permutation[i] == parent2.permutation[i]) { newPermutation[i] = parent1.permutation[i]; isUsed[newPermutation[i]] = true; } } for (int i = 0; i < newPermutation.Length; i++) { if (newPermutation[i] == -1) { int from = random.Next(2); QAPIndividual parent; QAPIndividual parentOther; if (from == 0) { parent = parent1; parentOther = parent2; } else { parent = parent2; parentOther = parent1; } int pos = i; do { var val = parentOther.permutation[pos]; newPermutation[pos] = parent.permutation[pos]; for (var j = 0; j < problemSize; j++) { if (parent.permutation[j] != val) continue; pos = j; break; } } while (pos != i); } } return new QAPIndividual(problem, newPermutation); }
public Tuple <algorithm.interfaces.IIndividual, algorithm.interfaces.IIndividual> crossover(algorithm.interfaces.IIndividual partner) { QAPIndividual qapPartner = (QAPIndividual)partner; // ugly :/ //QAPIndividual firstChild = makeChild(this, qapPartner); //QAPIndividual secondChild = makeChild(this, qapPartner); //return new Tuple<algorithm.interfaces.IIndividual, algorithm.interfaces.IIndividual>(firstChild, secondChild); int n = this.problem.ProblemSize; int[] firstPermutation = this.permutation; int[] secondPermutation = qapPartner.permutation; int[][] permutations = new int[2][]; permutations[0] = firstPermutation; permutations[1] = secondPermutation; int[] offspring1 = new int[n]; int[] offspring2 = new int[n]; for (int i = 0; i < n; i++) { offspring1[i] = -1; offspring2[i] = -1; } //printArray(offspring1); for (int i = 0; i < n; i++) { //Console.WriteLine(i); if (offspring1[i] == -1) { //Console.WriteLine(i); int par = random.Next(2); int start1 = permutations[par][i]; int start2 = permutations[1 - par][i]; int el1 = start1; int el2 = start2; offspring1[i] = el1; offspring2[i] = el2; int pos1 = pos(permutations[1 - par], el1); int pos2 = pos(permutations[par], el2); //Console.WriteLine("" + pos1 + " " + pos2 + " " + el1 + " " + el2 + " " + par + " " + i); //printArray(offspring1); //printArray(offspring2); //Console.WriteLine("new pos1 = " + pos1 + " new pos2 = " + pos2); while (i != pos1) { el1 = permutations[par][pos1]; el2 = permutations[1 - par][pos2]; offspring1[pos1] = el1; offspring2[pos2] = el2; //Console.WriteLine("" + pos1 + " " + pos2 + " " + el1 + " " + el2); //printArray(offspring1); //printArray(offspring2); pos1 = pos(permutations[1 - par], el1); pos2 = pos(permutations[par], el2); //Console.ReadKey(); } } } return(new Tuple <algorithm.interfaces.IIndividual, algorithm.interfaces.IIndividual>(new QAPIndividual(problem, offspring1), new QAPIndividual(problem, offspring2))); }