public void OrderCrossover2ApplyTest() {
      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. p. 135.
      random.Reset();
      random.IntNumbers = new int[] { 5, 7 };
      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, 9, 1, 3, 8, 4, 7 });
      Assert.IsTrue(parent2.Validate());
      expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 0, 9, 1, 3, 5, 6, 7, 8, 4 });
      Assert.IsTrue(expected.Validate());
      actual = OrderCrossover2.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);
    }
 public void MichalewiczNonUniformAllPositionsManipulatorApplyTest() {
   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.DoubleNumbers = new double[] { 0.2, 0.5, 0.7, 0.8, 0.9, 0.5, 0.2, 0.5, 0.7, 0.8 };
   parent = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 });
   expected = new RealVector(new double[] { 0.45, 0.22, 0.3, 0.6, 0.14 });
   bounds = new DoubleMatrix(new double[,] { { 0.3, 0.7 } });
   generationsDependency = new DoubleValue(0.1);
   currentGeneration = new IntValue(1);
   maximumGenerations = new IntValue(4);
   MichalewiczNonUniformAllPositionsManipulator.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.DoubleNumbers = new double[] { 0.2, 0.5, 0.7, 0.8, 0.9, 0.5, 0.2, 0.5, 0.7, 0.8 };
   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 {
     MichalewiczNonUniformAllPositionsManipulator.Apply(random, parent, bounds, currentGeneration, maximumGenerations, generationsDependency);
   } catch (System.ArgumentException) {
     exceptionFired = true;
   }
   Assert.IsTrue(exceptionFired);
 }
 public void SinglePointCrossoverApplyTest() {
   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[] { 3 };
   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, 2, 8 });
   actual = SinglePointCrossover.Apply(random, parent1, parent2);
   Assert.IsTrue(Auxiliary.IntegerVectorIsEqualByPosition(actual, expected));
   // The following test is not based on published examples
   random.Reset();
   random.IntNumbers = new int[] { 2 };
   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 = SinglePointCrossover.Apply(random, parent1, parent2);
   }
   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 MaximalPreservativeCrossoverApplyTest() {
      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, 2 };
      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[] { 1, 0, 2, 3, 4, 5, 7, 6 });
      Assert.IsTrue(expected.Validate());
      actual = MaximalPreservativeCrossover.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 {
        MaximalPreservativeCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
      }
      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
 }
 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 HeuristicCrossoverApplyTest() {
   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 };
   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.14, 0.23, 0.3, 0.59, -0.11 });
   actual = HeuristicCrossover.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 };
   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 = HeuristicCrossover.Apply(random, parent1, parent2);
   }
   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 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);
 }
Example #11
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 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 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 ApplyTest() {
   IRandom random = new TestRandom(new int[] { 1, 1, 0, 0, 1, 1, 0, 0, 1 }, null);
   PWREncoding parent1 = TestUtils.CreateTestPWR1();
   PWREncoding parent2 = TestUtils.CreateTestPWR2();
   PWREncoding expected = new PWREncoding();
   expected.PermutationWithRepetition = new IntegerVector(new int[] { 1, 0, 1, 0, 1, 2, 0, 2, 2 });
   PWREncoding actual;
   actual = PWRPPXCrossover.Apply(random, parent1, parent2);
   Assert.IsTrue(TestUtils.PRWEncodingEquals(expected, actual));
 }
 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);
 }
 public void ApplyTest() {
   IRandom random = new TestRandom(new int[] { 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1 }, new double[] { 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9 });
   Schedule parent1 = TestUtils.CreateTestSchedule1();
   Schedule parent2 = TestUtils.CreateTestSchedule2();
   ItemList<Job> jobData = TestUtils.CreateJobData();
   double mutProp = 0.05;
   Schedule actual;
   actual = DirectScheduleGTCrossover.Apply(random, parent1, parent2, jobData, mutProp);
   Schedule expected = DirectScheduleRandomCreator.Apply(3, 3, new PWREncoding(3, 3, new TestRandom(new int[] { 0, 2, 1, 1, 0, 2, 1, 2, 0 }, null)), TestUtils.CreateJobData());
   Assert.IsTrue(TestUtils.ScheduleEquals(actual, expected));
 }
 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));
 }
    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 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));
 }
    public void ApplyTest() {
      IRandom random = new TestRandom(new int[] { 2, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 }, null);
      JSMEncoding individual = TestUtils.CreateTestJSM1();
      JSMShiftChangeManipulator.Apply(random, individual);
      JSMEncoding expected = new JSMEncoding();
      ItemList<Permutation> jsm = new ItemList<Permutation>();
      for (int i = 0; i < 3; i++) {
        jsm.Add(new Permutation(PermutationTypes.Absolute, new int[] { 0, 1, 3, 2, 4, 5 }));
        jsm.Add(new Permutation(PermutationTypes.Absolute, new int[] { 0, 1, 3, 4, 2, 5 }));
      }
      expected.JobSequenceMatrix = jsm;

      Assert.IsTrue(TestUtils.JSMEncodingEquals(expected, individual));
    }
 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));
 }
Example #22
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 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));
    }
    public void TranslocationManipulatorApplyTest() {
      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, p. 24
      random.Reset();
      random.IntNumbers = new int[] { 2, 4, 4 };
      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, 5, 6, 2, 3, 4, 7 });
      Assert.IsTrue(expected.Validate());
      TranslocationManipulator.Apply(random, parent);
      Assert.IsTrue(parent.Validate());
      Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, parent));
    }
    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 ApplyTest() {
      IRandom random = new TestRandom(new int[] { 3 }, null);
      JSMEncoding p1 = TestUtils.CreateTestJSM1();
      JSMEncoding p2 = TestUtils.CreateTestJSM2();
      JSMEncoding expected = new JSMEncoding();
      ItemList<Permutation> jsm = new ItemList<Permutation>();
      for (int i = 0; i < 6; i++) {
        jsm.Add(new Permutation(PermutationTypes.Absolute, new int[] { 2, 1, 0, 3, 4, 5 }));
      }
      expected.JobSequenceMatrix = jsm;

      JSMEncoding actual;
      actual = JSMSXXCrossover.Apply(random, p1, p2);

      Assert.IsTrue(TestUtils.JSMEncodingEquals(expected, actual));
    }
 public void UniformLikeCrossoverApplyTest() {
   // test from the paper
   IRandom random = new TestRandom(new int[] { 0 }, new double[] { 0.2, 0.7, 0.2, 0.2 }); // TODO: Initialize to an appropriate value
   Permutation parent1 = new Permutation(PermutationTypes.Absolute,
     new int[] { 3, 2, 0, 7, 5, 4, 1, 6 });
   Assert.IsTrue(parent1.Validate());
   Permutation parent2 = new Permutation(PermutationTypes.Absolute,
     new int[] { 5, 0, 4, 7, 1, 3, 2, 6 });
   Assert.IsTrue(parent2.Validate());
   Permutation expected = new Permutation(PermutationTypes.Absolute,
     new int[] { 3, 0, 4, 7, 5, 2, 1, 6 });
   Assert.IsTrue(expected.Validate());
   Permutation actual;
   actual = UniformLikeCrossover.Apply(random, parent1, parent2);
   Assert.IsTrue(actual.Validate());
   Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
 }
 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 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);
    }
Example #30
0
 protected TestRandom(TestRandom original, Cloner cloner)
 {
     this.intNumbers    = original.intNumbers.ToArray();
     this.doubleNumbers = original.doubleNumbers.ToArray();
 }
Example #31
0
 protected TestRandom(TestRandom original, Cloner cloner) {
   this.intNumbers = original.intNumbers.ToArray();
   this.doubleNumbers = original.doubleNumbers.ToArray();
 }