Ejemplo n.º 1
0
        public void SinglePointCrossoverCrossTest()
        {
            SinglePointCrossover_Accessor target = new SinglePointCrossover_Accessor(new PrivateObject(typeof(SinglePointCrossover)));
            ItemArray <BinaryVector>      parents;
            TestRandom random = new TestRandom();
            bool       exceptionFired;

            // The following test checks if there is an exception when there are more than 2 parents
            random.Reset();
            parents        = new ItemArray <BinaryVector>(new BinaryVector[] { new BinaryVector(5), new BinaryVector(6), new BinaryVector(4) });
            exceptionFired = false;
            try {
                BinaryVector actual;
                actual = target.Cross(random, parents);
            } catch (ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
            // The following test checks if there is an exception when there are less than 2 parents
            random.Reset();
            parents        = new ItemArray <BinaryVector>(new BinaryVector[] { new BinaryVector(4) });
            exceptionFired = false;
            try {
                BinaryVector actual;
                actual = target.Cross(random, parents);
            } catch (ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
Ejemplo n.º 2
0
        public void SinglePointCrossoverCrossTest()
        {
            var target = new PrivateObject(typeof(SinglePointCrossover));
            ItemArray <IntegerVector> parents;
            TestRandom random = new TestRandom();
            bool       exceptionFired;

            // The following test checks if there is an exception when there are more than 2 parents
            random.Reset();
            parents        = new ItemArray <IntegerVector>(new IntegerVector[] { new IntegerVector(5), new IntegerVector(6), new IntegerVector(4) });
            exceptionFired = false;
            try {
                IntegerVector actual;
                actual = (IntegerVector)target.Invoke("Cross", random, parents);
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
            // The following test checks if there is an exception when there are less than 2 parents
            random.Reset();
            parents        = new ItemArray <IntegerVector>(new IntegerVector[] { new IntegerVector(4) });
            exceptionFired = false;
            try {
                IntegerVector actual;
                actual = (IntegerVector)target.Invoke("Cross", random, parents);
            }
            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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
        public void DiscreteCrossoverApplyTest()
        {
            TestRandom    random = new TestRandom();
            IntegerVector parent1, parent2, expected, actual;
            bool          exceptionFired;

            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 0, 0, 1, 0, 1 };
            parent1           = new IntegerVector(new int[] { 2, 2, 3, 5, 1 });
            parent2           = new IntegerVector(new int[] { 4, 1, 3, 2, 8 });
            expected          = new IntegerVector(new int[] { 2, 2, 3, 5, 8 });
            actual            = DiscreteCrossover.Apply(random, new ItemArray <IntegerVector>(new IntegerVector[] { parent1, parent2 }));
            Assert.IsTrue(Auxiliary.IntegerVectorIsEqualByPosition(actual, expected));

            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 0, 0, 1, 0, 1 };
            parent1           = new IntegerVector(new int[] { 2, 2, 3, 5, 1, 9 }); // this parent is longer
            parent2           = new IntegerVector(new int[] { 4, 1, 3, 2, 8 });
            exceptionFired    = false;
            try {
                actual = DiscreteCrossover.Apply(random, new ItemArray <IntegerVector>(new IntegerVector[] { 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 PositionBasedCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent1, parent2, expected, actual;

            // The following test is based on an example from Larranaga, 1999. Genetic Algorithms for the Traveling Salesman Problem.
            random.Reset();
            random.IntNumbers = new int[] { 3, 1, 2, 5 };
            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, 3, 5, 1, 2, 4, 6, 7 });
            Assert.IsTrue(expected.Validate());
            actual = PositionBasedCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));

            // perform a test when two permutations are of unequal length
            random.Reset();
            bool exceptionFired = false;

            try {
                PositionBasedCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
Ejemplo n.º 10
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);
        }
        public void EdgeRecombinationCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent1, parent2, expected, actual;

            // The following test is based on an example from Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, pp. 54-55
            random.Reset();
            random.IntNumbers    = new int[] { 0 };
            random.DoubleNumbers = new double[] { 0.5, 0, 0, 0 };
            parent1 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 8, 2, 6, 7, 1, 5, 4, 0, 3 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 4, 5, 1, 7, 6, 2, 8, 3 });
            Assert.IsTrue(expected.Validate());
            actual = EdgeRecombinationCrossover.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 {
                EdgeRecombinationCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
            }
            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);
        }
Ejemplo n.º 13
0
        public void CosaCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent1, parent2, expected, actual;

            // The following test is based on an example from Wendt, O. 1994. COSA: COoperative Simulated Annealing - Integration von Genetischen Algorithmen und Simulated Annealing am Beispiel der Tourenplanung. Dissertation Thesis. IWI Frankfurt.
            random.Reset();
            random.IntNumbers = new int[] { 1 };
            parent1           = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 5, 2, 4, 3 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 3, 0, 2, 1, 4, 5 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 4, 2, 5, 3 });
            Assert.IsTrue(expected.Validate());
            actual = CosaCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 4 };
            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[] { 7, 6, 5, 3, 4, 2, 1, 0 });
            Assert.IsTrue(expected.Validate());
            actual = CosaCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 5 };
            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[] { 4, 3, 5, 1, 0, 9, 7, 2, 8, 6 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 7, 6, 2, 3, 4, 5, 1, 0, 9, 8 });
            Assert.IsTrue(expected.Validate());
            actual = CosaCrossover.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 {
                CosaCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
        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);
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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);
        }
        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));
        }
Ejemplo n.º 19
0
        public void UniformOnePositionManipulatorApplyTest()
        {
            TestRandom    random = new TestRandom();
            IntegerVector parent, expected;
            IntMatrix     bounds = new IntMatrix(1, 2);

            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 3, 3 };
            parent            = new IntegerVector(new int[] { 2, 2, 3, 5, 1 });
            expected          = new IntegerVector(new int[] { 2, 2, 3, 3, 1 });
            bounds[0, 0]      = 2;
            bounds[0, 1]      = 7;
            UniformOnePositionManipulator.Apply(random, parent, bounds);
            Assert.IsTrue(Auxiliary.IntegerVectorIsEqualByPosition(expected, parent));
        }
Ejemplo n.º 20
0
        public void ScrambleManipulatorApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent, expected;

            // 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.IntNumbers = new int[] { 3, 6, 1, 1, 1, 0 };
            parent            = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
            Assert.IsTrue(parent.Validate());

            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 4, 5, 6, 3, 7 });
            Assert.IsTrue(expected.Validate());
            ScrambleManipulator.Apply(random, parent);
            Assert.IsTrue(parent.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, parent));
        }
Ejemplo n.º 21
0
        public void InversionManipulatorApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent, expected;

            // The following test is based on an example from Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, pp. 46-47
            random.Reset();
            random.IntNumbers = new int[] { 1, 4 };
            parent            = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
            Assert.IsTrue(parent.Validate());

            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 4, 3, 2, 1, 5, 6, 7, 8 });
            Assert.IsTrue(expected.Validate());
            InversionManipulator.Apply(random, parent);
            Assert.IsTrue(parent.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, parent));
        }
Ejemplo n.º 22
0
        public void Swap3ManipulatorApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent, expected;

            // Test manipulator
            random.Reset();
            random.IntNumbers    = new int[] { 1, 3, 6 };
            random.DoubleNumbers = new double[] { 0 };
            parent = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
            Assert.IsTrue(parent.Validate());

            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 3, 2, 6, 4, 5, 1, 7, 8 });
            Assert.IsTrue(expected.Validate());
            Swap3Manipulator.Apply(random, parent);
            Assert.IsTrue(parent.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, parent));
        }
        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));
        }
Ejemplo n.º 24
0
        public void OrderCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent1, parent2, expected, actual;

            // The following test is based on an example from Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg, pp. 55-56
            random.Reset();
            random.IntNumbers = new int[] { 3, 6 };
            parent1           = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 8, 2, 6, 7, 1, 5, 4, 0, 3 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 7, 1, 3, 4, 5, 6, 0, 8 });
            Assert.IsTrue(expected.Validate());
            actual = OrderCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(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, pp. 129-170.
            random.Reset();
            random.IntNumbers = new int[] { 2, 4 };
            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[] { 7, 6, 2, 3, 4, 0, 1, 5 });
            actual   = OrderCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
            // The following test is based on an example from Talbi, E.G. 2009. Metaheuristics - From Design to Implementation. Wiley, p. 218.
            random.Reset();
            random.IntNumbers = new int[] { 2, 5 };
            parent1           = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 7, 3, 0, 4, 8, 2, 5, 1, 6 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 8, 2, 3, 4, 5, 1, 6, 7 });
            Assert.IsTrue(expected.Validate());
            actual = OrderCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
            // The following test is not based on published examples
            random.Reset();
            random.IntNumbers = new int[] { 0, 5 };
            parent1           = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 5, 3, 4, 0, 9, 8, 2, 7, 1, 6 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 1, 4, 3, 7, 8, 6, 5, 0, 9 });
            Assert.IsTrue(expected.Validate());
            actual = OrderCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
            // based on the previous with changed breakpoints
            random.Reset();
            random.IntNumbers = new int[] { 6, 9 };
            expected          = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 3, 4, 8, 2, 7, 1, 6, 0, 5, 9 });
            Assert.IsTrue(expected.Validate());
            actual = OrderCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
            // another one based on the previous with changed breakpoints
            random.Reset();
            random.IntNumbers = new int[] { 0, 9 };
            expected          = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 1, 4, 3, 7, 8, 6, 0, 5, 9 });
            Assert.IsTrue(expected.Validate());
            actual = OrderCrossover.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 {
                OrderCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }