示例#1
0
        public void CyclicCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent1, parent2, expected, actual;

            // The following test is based on an example from Larranaga, P. et al. 1999. Genetic Algorithms for the Travelling Salesman Problem: A Review of Representations and Operators. Artificial Intelligence Review, 13
            random.Reset();
            random.DoubleNumbers = new double[] { 0.9 };
            parent1 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 1, 3, 5, 7, 6, 4, 2, 0 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 5, 3, 6, 4, 2, 7 });
            Assert.IsTrue(expected.Validate());
            actual = CyclicCrossover.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 {
                CyclicCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
示例#2
0
        public void CyclicCrossover2ApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent1, parent2, expected, actual;

            // The following test is based on an example from Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. pp. 134.
            random.Reset();
            random.IntNumbers = new int[] { 0 };
            parent1           = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 5, 6, 0, 7, 1, 3, 8, 4, 9 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 5, 2, 3, 7, 1, 6, 8, 4, 9 });
            Assert.IsTrue(expected.Validate());
            actual = CyclicCrossover2.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 {
                CyclicCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }