public void EdgeRecombinationCrossoverApplyTest() { TestRandom random = new TestRandom(); Permutation parent1, parent2, expected, actual; // The following test is based on an example from Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, pp. 54-55 random.Reset(); random.IntNumbers = new int[] { 0 }; random.DoubleNumbers = new double[] { 0.5, 0, 0, 0 }; parent1 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }); Assert.IsTrue(parent1.Validate()); parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 8, 2, 6, 7, 1, 5, 4, 0, 3 }); Assert.IsTrue(parent2.Validate()); expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 4, 5, 1, 7, 6, 2, 8, 3 }); Assert.IsTrue(expected.Validate()); actual = EdgeRecombinationCrossover.Apply(random, parent1, parent2); Assert.IsTrue(actual.Validate()); Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual)); // perform a test when the two permutations are of unequal length random.Reset(); bool exceptionFired = false; try { EdgeRecombinationCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6)); } catch (System.ArgumentException) { exceptionFired = true; } Assert.IsTrue(exceptionFired); }
public void EdgeRecombinationCrossover_NextNodeIsNeighborsOfPreviousNode() { var elements = new List <string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null); var parentChromosomes = generator.GeneratePopulation(2); var child = new EdgeRecombinationCrossover <string>(null, null).Crossover(parentChromosomes.ElementAt(0), parentChromosomes.ElementAt(1)).ToArray <string>(); for (int i = 0; i < child.Length - 1; i++) { var neigbors = GetNeighbors(parentChromosomes.ElementAt(0).ToArray <string>(), parentChromosomes.ElementAt(1).ToArray <string>(), child[i]); RemoveAlreadyVisitedNeigbors(neigbors, child, i); if (neigbors.Count == 0) { continue; } Assert.IsTrue(neigbors.Contains(child[i + 1]), "Didn't jump to neighbor"); } }