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

    r1184 r1218  
    3434  /// solution by the factor alpha, into the direction of the 'worse' solution by the factor beta.
    3535  /// </summary>
    36   public class BlendAlphaBetaCrossover : CrossoverBase {
     36  public class BlendAlphaBetaCrossover : RealVectorCrossoverBase {
    3737    /// <inheritdoc select="summary"/>
    3838    public override string Description {
     
    4545    /// <summary>
    4646    /// Initializes a new instance of <see cref="BlendAlphaBetaCrossover"/> with five variable infos
    47     /// (<c>Maximization</c>, <c>Quality</c>, <c>RealVector</c>, <c>Alpha</c> and <c>Beta</c>).
     47    /// (<c>Maximization</c>, <c>Quality</c>, <c>Alpha</c> and <c>Beta</c>).
    4848    /// </summary>
    4949    public BlendAlphaBetaCrossover()
     
    5151      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    5252      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
    53       AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
    5453      VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
    5554      alphaVarInfo.Local = true;
     
    110109
    111110    /// <summary>
    112     /// Performs a blend alpha beta crossover of two real vectors.
     111    /// Performs a blend alpha beta crossover operation for two given parent real vectors.
    113112    /// </summary>
     113    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    114114    /// <param name="scope">The current scope.</param>
    115     /// <param name="random">The random number generator.</param>
    116     /// <param name="parent1">The first parent for the crossover.</param>
    117     /// <param name="parent2">The second parent for the crossover.</param>
    118     /// <param name="child">The created child generated through the blend alpha beta crossover.</param>
    119     protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
     115    /// <param name="random">A random number generator.</param>
     116    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
     117    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     118    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     119      if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaBetaCrossover: The number of parents is not equal to 2");
    120120      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
    121       DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false);
    122       DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false);
    123       DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false);
    124       DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false);
     121      double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data;
     122      double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data;
    125123      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
    126124      double beta = GetVariableValue<DoubleData>("Beta", scope, true).Data;
    127125
    128       if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
    129 
    130       double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data, alpha, beta);
    131       child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result)));
     126      return Apply(random, maximization, parents[0], quality1, parents[1], quality2, alpha, beta);
    132127    }
    133128  }
Note: See TracChangeset for help on using the changeset viewer.