Free cookie consent management tool by TermsFeed Policy Generator

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

Refactoring of crossover operators (#470)

File:
1 edited

Legend:

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

    r1184 r1218  
    2727namespace HeuristicLab.RealVector {
    2828  /// <summary>
    29   /// Discrete crossover for real vectors: Selects randomly either the value of the first or the
    30   /// second parent.
     29  /// Discrete crossover for real vectors: For each position in the new vector an allele
     30  /// of one of the parents is randomly selected.
    3131  /// </summary>
    3232  public class DiscreteCrossover : RealVectorCrossoverBase {
    3333    /// <inheritdoc select="summary"/>
    3434    public override string Description {
    35       get { return "Discrete crossover for real vectors."; }
     35      get { return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent."; }
    3636    }
    3737
    3838    /// <summary>
    39     /// Performs a discrete crossover operation of the two given parents.
     39    /// Performs a discrete crossover operation on multiple given parents.
    4040    /// </summary>
    4141    /// <param name="random">A random number generator.</param>
    42     /// <param name="parent1">The first parent for the crossover operation.</param>
    43     /// <param name="parent2">The second parent for the crossover operation.</param>
     42    /// <param name="parents">An array containing the parents that should be crossed.</param>
    4443    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    45     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    46       int length = parent1.Length;
     44    public static double[] Apply(IRandom random, double[][] parents) {
     45      int length = parents[0].Length;
    4746      double[] result = new double[length];
    48 
    4947      for (int i = 0; i < length; i++) {
    50         if (random.NextDouble() < 0.5)
    51           result[i] = parent1[i];
    52         else
    53           result[i] = parent2[i];
     48        result[i] = parents[random.Next(parents.Length)][i];
    5449      }
    5550      return result;
     
    5752
    5853    /// <summary>
    59     /// Performs a discrete crossover operation of the two given parents.
     54    /// Performs a discrete crossover operation for multiple given parent real vectors.
    6055    /// </summary>
     56    /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception>
    6157    /// <param name="scope">The current scope.</param>
    6258    /// <param name="random">A random number generator.</param>
    63     /// <param name="parent1">The first parent for the crossover operation.</param>
    64     /// <param name="parent2">The second parent for the crossover operation.</param>
     59    /// <param name="parents">An array containing the real vectors that should be crossed.</param>
    6560    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    66     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    67       return Apply(random, parent1, parent2);
     61    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     62      if (parents.Length < 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is less than 2");
     63      return Apply(random, parents);
    6864    }
    6965  }
Note: See TracChangeset for help on using the changeset viewer.