public void BlendAlphaCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            RealVector  parent1, parent2, expected, actual;
            DoubleValue alpha;
            bool        exceptionFired;

            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.5, 0.5, 0.5, 0.5, 0.5 };
            alpha    = new DoubleValue(0.5);
            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.3, 0.15, 0.3, 0.35, 0.45 });
            actual   = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25 };
            alpha    = new DoubleValue(0.25);
            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.225, 0.1875, 0.3, 0.4625, 0.1875 });
            actual   = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25 };
            alpha          = new DoubleValue(-0.25); // negative values for alpha are not allowed
            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.225, 0.1875, 0.3, 0.4625, 0.1875 });
            exceptionFired = false;
            try {
                actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.25, 0.75, 0.25, 0.75, 0.25, .75 };
            alpha          = new DoubleValue(0.25);
            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 });
            expected       = new RealVector(new double[] { 0.225, 0.1875, 0.3, 0.4625, 0.1875 });
            exceptionFired = false;
            try {
                actual = BlendAlphaCrossover.Apply(random, parent1, parent2, alpha);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
예제 #2
0
        public void SimulatedBinaryCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            RealVector  parent1, parent2, expected, actual;
            DoubleValue contiguity;
            bool        exceptionFired;

            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.3, 0.9, 0.7, 0.2, 0.8, 0.1, 0.2, 0.3, 0.4, 0.8, 0.7 };
            contiguity           = new DoubleValue(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.644880972204315, 0.0488239539275703, 0.3, 0.5, 0.1 });
            actual   = SimulatedBinaryCrossover.Apply(random, parent1, parent2, contiguity);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.3, 0.9, 0.7, 0.2, 0.8, 0.1, 0.2, 0.3, 0.4, 0.8, 0.7 };
            contiguity           = new DoubleValue(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 = SimulatedBinaryCrossover.Apply(random, parent1, parent2, contiguity);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.3, 0.9, 0.7, 0.2, 0.8, 0.1, 0.2, 0.3, 0.4, 0.8, 0.7 };
            contiguity           = new DoubleValue(-0.3); //  contiguity < 0
            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 });
            exceptionFired = false;
            try {
                actual = SimulatedBinaryCrossover.Apply(random, parent1, parent2, contiguity);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
        public void UniformOnePositionManipulatorApplyTest()
        {
            TestRandom   random = new TestRandom();
            RealVector   parent, expected;
            DoubleMatrix bounds;

            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers    = new int[] { 3 };
            random.DoubleNumbers = new double[] { 0.2 };
            parent   = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
            expected = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.3, 0.1 });
            bounds   = new DoubleMatrix(new double[, ] {
                { 0.2, 0.7 }
            });
            UniformOnePositionManipulator.Apply(random, parent, bounds);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(expected, parent));
        }
예제 #4
0
        public void MichalewiczNonUniformOnePositionManipulatorApplyTest()
        {
            TestRandom   random = new TestRandom();
            RealVector   parent, expected;
            DoubleValue  generationsDependency;
            DoubleMatrix bounds;
            IntValue     currentGeneration, maximumGenerations;
            bool         exceptionFired;

            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers    = new int[] { 3 };
            random.DoubleNumbers = new double[] { 0.2, 0.7 };
            parent   = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
            expected = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.34, 0.1 });
            bounds   = new DoubleMatrix(new double[, ] {
                { 0.3, 0.7 }
            });
            generationsDependency = new DoubleValue(0.1);
            currentGeneration     = new IntValue(1);
            maximumGenerations    = new IntValue(4);
            MichalewiczNonUniformOnePositionManipulator.Apply(random, parent, bounds, currentGeneration, maximumGenerations, generationsDependency);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(expected, parent));
            // The following test is not based on published examples
            exceptionFired = false;
            random.Reset();
            random.IntNumbers    = new int[] { 3 };
            random.DoubleNumbers = new double[] { 0.2, 0.7 };
            parent = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
            bounds = new DoubleMatrix(new double[, ] {
                { 0.3, 0.7 }
            });
            generationsDependency = new DoubleValue(0.1);
            currentGeneration     = new IntValue(5); //current generation > max generation
            maximumGenerations    = new IntValue(4);
            try {
                MichalewiczNonUniformOnePositionManipulator.Apply(random, parent, bounds, currentGeneration, maximumGenerations, generationsDependency);
            } catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
        public void SinglePointCrossoverApplyTest()
        {
            TestRandom random = new TestRandom();
            RealVector parent1, parent2, expected, actual;
            bool       exceptionFired;

            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 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.2, 0.2, 0.3, 0.2, 0.8 });
            actual            = SinglePointCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 2 };
            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 = SinglePointCrossover.Apply(random, parent1, parent2);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 5 }; // should not have an effect
            parent1           = new RealVector(new double[] { 0.2, 0.4 });
            parent2           = new RealVector(new double[] { 0.6, 0.1 });
            expected          = new RealVector(new double[] { 0.2, 0.1 });
            actual            = SinglePointCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
            // The following test is not based on published examples
        }