Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/05/10 13:14:26 (14 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/HeuristicCrossover.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;
    27 using HeuristicLab.Evolutionary;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Parameters;
    2829
    2930namespace HeuristicLab.Encodings.RealVector {
     
    3233  /// of the two parents times a randomly chosen factor.
    3334  /// </summary>
    34   public class HeuristicCrossover : RealVectorCrossoverBase {
     35  /// <remarks>
     36  /// It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218.
     37  /// </remarks>
     38  [Item("HeuristicCrossover", "Heuristic crossover for real vectors: Takes for each position the better parent and adds the difference. It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218.")]
     39  [EmptyStorableClass]
     40  public class HeuristicCrossover : RealVectorCrossover {
     41    /// <summary>
     42    /// Whether the problem is a maximization or minimization problem.
     43    /// </summary>
     44    public ValueLookupParameter<BoolData> MaximizationParameter {
     45      get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
     46    }
     47    /// <summary>
     48    /// The quality of the parents.
     49    /// </summary>
     50    public SubScopesLookupParameter<DoubleData> QualityParameter {
     51      get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; }
     52    }
     53
    3554    /// <summary>
    3655    /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
     
    3958    public HeuristicCrossover()
    4059      : base() {
    41       AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    42       AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
    43     }
    44 
    45     /// <inheritdoc select="summary"/>
    46     public override string Description {
    47       get { return "Heuristic crossover for real vectors."; }
     60      Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "Whether the problem is a maximization problem or not."));
     61      Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The quality values of the parents."));
    4862    }
    4963
     
    5165    /// Perfomrs a heuristic crossover on the two given parents.
    5266    /// </summary>
     67    /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
    5368    /// <param name="random">The random number generator.</param>
    54     /// <param name="maximization">Boolean flag whether it is a maximization problem.</param>
    55     /// <param name="parent1">The first parent for the crossover operation.</param>
    56     /// <param name="quality1">The quality of the first parent.</param>
    57     /// <param name="parent2">The second parent for the crossover operation.</param>
    58     /// <param name="quality2">The quality of the second parent.</param>
     69    /// <param name="betterParent">The first parent for the crossover operation.</param>
     70    /// <param name="worseParent">The second parent for the crossover operation.</param>
    5971    /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
    60     public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2) {
    61       int length = parent1.Length;
     72    public static DoubleArrayData Apply(IRandom random, DoubleArrayData betterParent, DoubleArrayData worseParent) {
     73      if (betterParent.Length != worseParent.Length)
     74        throw new ArgumentException("ERROR in HeuristicCrossover: the two parents are not of the same length");
     75     
     76      int length = betterParent.Length;
    6277      double[] result = new double[length];
    6378      double factor = random.NextDouble();
    6479
    6580      for (int i = 0; i < length; i++) {
    66         if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2)))
    67           result[i] = parent1[i] + factor * (parent1[i] - parent2[i]);
    68         else
    69           result[i] = parent2[i] + factor * (parent2[i] - parent1[i]);
     81        result[i] = betterParent[i] + factor * (betterParent[i] - worseParent[i]);
    7082      }
    71       return result;
     83      return new DoubleArrayData(result);
    7284    }
    7385
     
    7587    /// Performs a heuristic crossover operation for two given parent real vectors.
    7688    /// </summary>
    77     /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    78     /// <param name="scope">The current scope.</param>
     89    /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
     90    /// <exception cref="InvalidOperationException">
     91    /// Thrown when either:<br/>
     92    /// <list type="bullet">
     93    /// <item><description>Maximization parameter could not be found.</description></item>
     94    /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
     95    /// </list>
     96    /// </exception>
    7997    /// <param name="random">A random number generator.</param>
    8098    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
    8199    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    82     protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
    83       if (parents.Length != 2) throw new InvalidOperationException("ERROR in HeuristicCrossover: The number of parents is not equal to 2");
    84       bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
    85       double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
    86       double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data;
     100    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
     101      if (parents.Length != 2) throw new ArgumentException("ERROR in HeuristicCrossover: The number of parents is not equal to 2");
    87102
    88       return Apply(random, maximization, parents[0], quality1, parents[1], quality2);
     103      if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
     104      if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("HeuristicCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
     105
     106      ItemArray<DoubleData> qualities = QualityParameter.ActualValue;
     107      bool maximization = MaximizationParameter.ActualValue.Value;
     108
     109      if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
     110        return Apply(random, parents[0], parents[1]);
     111      else
     112        return Apply(random, parents[1], parents[0]);
    89113    }
    90114  }
Note: See TracChangeset for help on using the changeset viewer.