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/HeuristicCrossover.cs

    r1184 r1218  
    3232  /// of the two parents times a randomly chosen factor.
    3333  /// </summary>
    34   public class HeuristicCrossover : CrossoverBase {
     34  public class HeuristicCrossover : RealVectorCrossoverBase {
    3535    /// <summary>
    36     /// Initializes a new instance of <see cref="HeuristicCrossover"/> with three variable infos
    37     /// (<c>Maximization</c>, <c>Quality</c> and <c>RealVector</c>).
     36    /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
     37    /// (<c>Maximization</c> and <c>Quality</c>).
    3838    /// </summary>
    3939    public HeuristicCrossover()
     
    4141      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    4242      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
    43       AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
    4443    }
    4544
     
    7473
    7574    /// <summary>
    76     /// Perfomrs a heuristic crossover on the two given parents.
     75    /// Performs a heuristic crossover operation for two given parent real vectors.
    7776    /// </summary>
    78     /// <exception cref="InvalidOperationException">Thrown when the parent vectors have different lengths.</exception>
     77    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    7978    /// <param name="scope">The current scope.</param>
    80     /// <param name="random">The random number generator.</param>
    81     /// <param name="parent1">The first parent for the crossover operation.</param>
    82     /// <param name="parent2">The second parent for the crossover operation.</param>
    83     /// <param name="child">The newly created real vector, resulting from the heuristic crossover.</param>
    84     protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
     79    /// <param name="random">A random number generator.</param>
     80    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
     81    /// <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");
    8584      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
    86       DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false);
    87       DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false);
    88       DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false);
    89       DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false);
     85      double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
     86      double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data;
    9087
    91       if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
    92 
    93       double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data);
    94       child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result)));
     88      return Apply(random, maximization, parents[0], quality1, parents[1], quality2);
    9589    }
    9690  }
Note: See TracChangeset for help on using the changeset viewer.