public void HeuristicCrossoverApplyTest() { TestRandom random = new TestRandom(); RealVector parent1, parent2, expected, actual; bool exceptionFired; // The following test is not based on published examples random.Reset(); random.DoubleNumbers = new double[] { 0.3 }; parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 }); parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 }); expected = new RealVector(new double[] { 0.14, 0.23, 0.3, 0.59, -0.11 }); actual = HeuristicCrossover.Apply(random, parent1, parent2); Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected)); // The following test is not based on published examples random.Reset(); random.DoubleNumbers = new double[] { 0.3 }; parent1 = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1, 0.9 }); // this parent is longer parent2 = new RealVector(new double[] { 0.4, 0.1, 0.3, 0.2, 0.8 }); exceptionFired = false; try { actual = HeuristicCrossover.Apply(random, parent1, parent2); } catch (System.ArgumentException) { exceptionFired = true; } Assert.IsTrue(exceptionFired); }
public void HeuristicCrossover_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 HeuristicCrossover <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"); } }