Changeset 3675


Ignore:
Timestamp:
05/06/10 16:41:51 (12 years ago)
Author:
abeham
Message:

#890

  • changed SinglePointCrossover to work with vectors of length 2
  • updated unit tests
Location:
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Crossovers/SinglePointCrossover.cs

    r3376 r3675  
    4949    public static RealVector Apply(IRandom random, RealVector parent1, RealVector parent2) {
    5050      if (parent1.Length != parent2.Length) throw new ArgumentException("SinglePointCrossover: Parents are of unequal length");
     51      if (parent1.Length < 2) throw new ArgumentException("SinglePointCrossover: Cannot be applied to vectors with just one dimension.");
    5152      int length = parent1.Length;
    5253      RealVector result = new RealVector(length);
    53       int breakPoint = random.Next(1, length - 1);
     54      int breakPoint;
     55      if (length == 2) breakPoint = 1;
     56      else breakPoint = random.Next(1, length - 1);
    5457
    5558      for (int i = 0; i < breakPoint; i++)
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Tests/SinglePointCrossoverTest.cs

    r3376 r3675  
    9595      }
    9696      Assert.IsTrue(exceptionFired);
     97      // The following test checks if there is an exception when the vector has just one dimension
     98      random.Reset();
     99      parents = new ItemArray<RealVector>(new RealVector[] { new RealVector(1) });
     100      exceptionFired = false;
     101      try {
     102        RealVector actual;
     103        actual = target.Cross(random, parents);
     104      } catch (System.ArgumentException) {
     105        exceptionFired = true;
     106      }
     107      Assert.IsTrue(exceptionFired);
    97108    }
    98109
     
    125136      }
    126137      Assert.IsTrue(exceptionFired);
     138      // The following test is not based on published examples
     139      random.Reset();
     140      random.IntNumbers = new int[] { 5 }; // should not have an effect
     141      parent1 = new RealVector(new double[] { 0.2, 0.4 });
     142      parent2 = new RealVector(new double[] { 0.6, 0.1 });
     143      expected = new RealVector(new double[] { 0.2, 0.1 });
     144      actual = SinglePointCrossover.Apply(random, parent1, parent2);
     145      Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(actual, expected));
     146      // The following test is not based on published examples
    127147    }
    128148
Note: See TracChangeset for help on using the changeset viewer.