Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/05/10 13:14:26 (15 years ago)
Author:
svonolfe
Message:

Added heuristic, local, random convex crossover and added some initial unit tests for all RealVector operators (#890)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/LocalCrossover.cs

    r2900 r2936  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Data;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Encodings.RealVector {
     
    3132  /// always newly created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor).
    3233  /// </summary>
    33   public class LocalCrossover : RealVectorCrossoverBase {
    34     /// <inheritdoc select="summary"/>
    35     public override string Description {
    36       get { return "Local crossover for real vectors."; }
    37     }
    38 
     34  /// <remarks>
     35  /// It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 194.
     36  /// </remarks>
     37  [Item("LocalCrossover", "Local crossover for real vectors: Takes for each element the allele of the first parent times a always newly created randomly chosen factor and adds the allele of the second parent times (1 - the randomly chosen factor. " +
     38    "It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL., p. 194.")]
     39  [EmptyStorableClass]
     40  public class LocalCrossover : RealVectorCrossover {
    3941    /// <summary>
    4042    /// Performs a local crossover on the two given parent vectors.
    4143    /// </summary>
     44    /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
    4245    /// <param name="random">The random number generator.</param>
    4346    /// <param name="parent1">The first parent for the crossover operation.</param>
    4447    /// <param name="parent2">The second parent for the crossover operation.</param>
    4548    /// <returns>The newly created real vector, resulting from the local crossover.</returns>
    46     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
     49    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2) {
     50      if (parent1.Length != parent2.Length)
     51        throw new ArgumentException("ERROR in LocalCrossover: the two parents are not of the same length");
     52     
    4753      double factor;
    4854      int length = parent1.Length;
     
    5359        result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]);
    5460      }
    55       return result;
     61      return new DoubleArrayData(result);
    5662    }
    5763
     
    5965    /// Performs a local crossover operation for two given parent real vectors.
    6066    /// </summary>
    61     /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    62     /// <param name="scope">The current scope.</param>
     67    /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception>
    6368    /// <param name="random">A random number generator.</param>
    6469    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
    6570    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    66     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    67       if (parents.Length != 2) throw new InvalidOperationException("ERROR in LocalCrossover: The number of parents is not equal to 2");
     71    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     72      if (parents.Length != 2) throw new ArgumentException("ERROR in LocalCrossover: The number of parents is not equal to 2");
    6873      return Apply(random, parents[0], parents[1]);
    6974    }
Note: See TracChangeset for help on using the changeset viewer.