Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/09 01:21:53 (16 years ago)
Author:
swagner
Message:

Refactoring of crossover operators (#470)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.RealVector/ContinuousCrossover.cs

    r1184 r1218  
    2828namespace HeuristicLab.RealVector {
    2929  /// <summary>
    30   /// Continuous crossover for real vectors: for each element randomly either the element of the first
    31   /// parent or the average of both parents is selected.
     30  /// Continuous crossover for real vectors: for each element either the element of the first
     31  /// parent or the average of all parents is selected randomly.
    3232  /// </summary>
    3333  public class ContinuousCrossover : RealVectorCrossoverBase {
     
    3838
    3939    /// <summary>
    40     /// Performs a continuous crossover of the two given real vectors.
     40    /// Performs a continuous crossover of multiple given real vectors.
    4141    /// </summary>
    4242    /// <param name="random">The random number generator.</param>
    43     /// <param name="parent1">The first parent for the crossover.</param>
    44     /// <param name="parent2">The second parent for the crossover.</param>
     43    /// <param name="parents">An array containing the parents that should be crossed.</param>
    4544    /// <returns>The newly created real vector, resulting from the continuous crossover.</returns>
    46     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    47       int length = parent1.Length;
     45    public static double[] Apply(IRandom random, double[][] parents) {
     46      int length = parents[0].Length;
    4847      double[] result = new double[length];
    4948
    5049      for (int i = 0; i < length; i++) {
    5150        if (random.NextDouble() < 0.5) {
    52           result[i] = (parent1[i] + parent2[i]) / 2;
     51          double sum = 0.0;
     52          for (int j = 0; j < parents.Length; j++)
     53            sum += parents[j][i];
     54          result[i] = sum / parents.Length;
    5355        } else {
    54           result[i] = parent1[i];
     56          result[i] = parents[0][i];
    5557        }
    5658      }
     
    5961
    6062    /// <summary>
    61     /// Performs a continuous crossover of the two given real vectors.
     63    /// Performs a continuous crossover operation for multiple given parent real vectors.
    6264    /// </summary>
    63     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     65    /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception>
    6466    /// <param name="scope">The current scope.</param>
    65     /// <param name="random">The random number generator.</param>
    66     /// <param name="parent1">The first parent for the crossover.</param>
    67     /// <param name="parent2">The second parent for the crossover.</param>
    68     /// <returns>The newly created real vector, resulting from the continuous crossover.</returns>
    69     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    70       return Apply(random, parent1, parent2);
     67    /// <param name="random">A random number generator.</param>
     68    /// <param name="parents">An array containing the real vectors that should be crossed.</param>
     69    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     70    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     71      if (parents.Length < 2) throw new InvalidOperationException("ERROR in ContinuousCrossover: The number of parents is less than 2");
     72      return Apply(random, parents);
    7173    }
    7274  }
Note: See TracChangeset for help on using the changeset viewer.