Exemple #1
0
        public void DiscreteCrossoverApplyTest()
        {
            TestRandom             random = new TestRandom();
            RealVector             parent1, parent2, expected, actual;
            ItemArray <RealVector> parents;
            bool exceptionFired;

            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 0, 0, 1, 0, 1 };
            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 });
            parents           = new ItemArray <RealVector>(new RealVector[] { parent1, parent2 });
            expected          = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.8 });
            actual            = DiscreteCrossover.Apply(random, parents);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 0, 0, 1, 0, 1, 0 };
            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 });
            parents           = new ItemArray <RealVector>(new RealVector[] { parent1, parent2 });
            exceptionFired    = false;
            try {
                actual = DiscreteCrossover.Apply(random, parents);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
Exemple #2
0
        public void LocalCrossoverApplyTest()
        {
            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, 0.1, 0.2, 0.4, 0.23 };
            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.34, 0.11, 0.3, 0.32, 0.639 });
            actual   = LocalCrossover.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, 0.1, 0.2, 0.4, 0.23, 0.5 };
            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 = LocalCrossover.Apply(random, parent1, parent2);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
        public void PolynomialAllPositionManipulatorApplyTest()
        {
            TestRandom  random = new TestRandom();
            RealVector  parent, expected;
            DoubleValue contiguity, maxManipulation;
            bool        exceptionFired;

            // The following test is not based on published examples
            random.Reset();
            random.DoubleNumbers = new double[] { 0.2, 0.7, 0.8, 0.01, 0.1 };
            parent          = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
            expected        = new RealVector(new double[] { 0.120213215256006, 0.249415354697564, 0.379786784743994, 0.322759240811056, -0.0182075293954083 });
            contiguity      = new DoubleValue(0.8);
            maxManipulation = new DoubleValue(0.2);
            PolynomialAllPositionManipulator.Apply(random, parent, contiguity, maxManipulation);
            Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(expected, parent));
            // The following test is not based on published examples
            exceptionFired = false;
            random.Reset();
            random.DoubleNumbers = new double[] { 0.2, 0.7, 0.8, 0.01, 0.1 };
            parent          = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
            contiguity      = new DoubleValue(-1); //Contiguity value < 0
            maxManipulation = new DoubleValue(0.2);
            try {
                PolynomialAllPositionManipulator.Apply(random, parent, contiguity, maxManipulation);
            } catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
        public void PolynomialOnePositionManipulatorApplyTest()
        {
            TestRandom  random = new TestRandom();
            RealVector  parent, expected;
            DoubleValue contiguity, maxManipulation;
            bool        exceptionFired;

            // 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.1261980542102, 0.1 });
            contiguity      = new DoubleValue(0.2);
            maxManipulation = new DoubleValue(0.7);
            PolynomialOnePositionManipulator.Apply(random, parent, contiguity, maxManipulation);
            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 };
            parent          = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
            contiguity      = new DoubleValue(-1); //Contiguity value < 0
            maxManipulation = new DoubleValue(0.2);
            try {
                PolynomialOnePositionManipulator.Apply(random, parent, contiguity, maxManipulation);
            } catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
        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);
        }
Exemple #6
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));
        }
Exemple #8
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);
        }