コード例 #1
0
        public void NPointCrossoverApplyTest()
        {
            TestRandom   random = new TestRandom();
            BinaryVector parent1, parent2, expected, actual;
            IntValue     n;
            bool         exceptionFired;

            // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 48
            random.Reset();
            n = new IntValue(1);
            random.IntNumbers = new int[] { 4 };
            parent1           = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
            parent2           = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
            expected          = new BinaryVector(new bool[] { false, false, false, false, false, false, false, false, true });
            actual            = NPointCrossover.Apply(random, parent1, parent2, n);
            Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));

            // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 48
            random.Reset();
            n = new IntValue(2);
            random.IntNumbers = new int[] { 4, 5 };
            parent1           = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
            parent2           = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
            expected          = new BinaryVector(new bool[] { false, false, false, false, false, false, false, false, false });
            actual            = NPointCrossover.Apply(random, parent1, parent2, n);
            Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));

            // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 48
            random.Reset();
            n = new IntValue(2);
            random.IntNumbers = new int[] { 4, 5 };
            parent2           = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
            parent1           = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
            expected          = new BinaryVector(new bool[] { true, true, false, true, true, false, false, false, true });
            actual            = NPointCrossover.Apply(random, parent1, parent2, n);
            Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));

            // The following test is not based on any published examples
            random.Reset();
            random.IntNumbers = new int[] { 2 };
            parent1           = new BinaryVector(new bool[] { false, true, true, false, false }); // this parent is longer
            parent2           = new BinaryVector(new bool[] { false, true, true, false });
            exceptionFired    = false;
            try {
                actual = NPointCrossover.Apply(random, parent1, parent2, n);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
        public void SinglePositionBitflipManipulatorApplyTest()
        {
            TestRandom   random = new TestRandom();
            BinaryVector parent, expected;

            // The following test is based on Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg, p. 21.
            random.Reset();
            random.IntNumbers = new int[] { 4 };
            parent            = new BinaryVector(new bool[] { true, true, true, false, false, false, false, false, false,
                                                              false, true, true, true, true, true, true, false, false, false, true, false, true });
            expected = new BinaryVector(new bool[] { true, true, true, false, true, false, false, false, false,
                                                     false, true, true, true, true, true, true, false, false, false, true, false, true });
            SinglePositionBitflipManipulator.Apply(random, parent);
            Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(expected, parent));
        }
コード例 #3
0
        public void SinglePointCrossoverApplyTest()
        {
            TestRandom   random = new TestRandom();
            BinaryVector parent1, parent2, expected, actual;
            bool         exceptionFired;

            // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 49
            random.Reset();
            random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
            parent1  = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
            parent2  = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
            expected = new BinaryVector(new bool[] { false, true, false, false, false, false, false, false, false });
            actual   = UniformCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));

            // The following test is based on Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, p. 49
            random.Reset();
            random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
            parent2  = new BinaryVector(new bool[] { false, false, false, false, true, false, false, false, false });
            parent1  = new BinaryVector(new bool[] { true, true, false, true, false, false, false, false, true });
            expected = new BinaryVector(new bool[] { true, false, false, true, true, false, false, false, true });
            actual   = UniformCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(Auxiliary.BinaryVectorIsEqualByPosition(actual, expected));

            // The following test is not based on any published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.35, 0.62, 0.18, 0.42, 0.83, 0.76, 0.39, 0.51, 0.36 };
            parent1        = new BinaryVector(new bool[] { false, true, true, false, false }); // this parent is longer
            parent2        = new BinaryVector(new bool[] { false, true, true, false });
            exceptionFired = false;
            try {
                actual = UniformCrossover.Apply(random, parent1, parent2);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }