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.BitVector/BitVectorCrossoverBase.cs

    r1176 r1218  
    4242
    4343    /// <summary>
    44     /// Performs a crossover of two given parents.
     44    /// Performs a crossover by calling <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom, bool[][]"/>
     45    /// and adds the created bit vector to the current scope.
    4546    /// </summary>
    46     /// <exception cref="InvalidOperationException">Thrown when the parents have different lengths.</exception>
    47     /// <param name="scope">The current scope.</param>
     47    /// <exception cref="InvalidOperationException">Thrown if the parents have different lengths.</exception>
     48    /// <param name="scope">The current scope which represents a new child.</param>
    4849    /// <param name="random">A random number generator.</param>
    49     /// <param name="parent1">The first parent for crossover.</param>
    50     /// <param name="parent2">The second parent for crossover.</param>
    51     /// <param name="child">The resulting child scope.</param>
    52     protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
    53       IVariableInfo bitVectorInfo = GetVariableInfo("BitVector");
    54       BoolArrayData vector1 = parent1.GetVariableValue<BoolArrayData>(bitVectorInfo.FormalName, false);
    55       BoolArrayData vector2 = parent2.GetVariableValue<BoolArrayData>(bitVectorInfo.FormalName, false);
     50    protected sealed override void Cross(IScope scope, IRandom random) {
     51      bool[][] parents = new bool[scope.SubScopes.Count][];
     52      int length = -1;
     53      for (int i = 0; i < scope.SubScopes.Count; i++) {
     54        parents[i] = scope.SubScopes[i].GetVariableValue<BoolArrayData>("BitVector", false).Data;
     55        if (i == 0) length = parents[i].Length;
     56        else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in BitVectorCrossoverBase: Cannot apply crossover to bit vectors of different length");
     57      }
    5658
    57       if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to bit vectors of different length.");
    58 
    59       bool[] result = Cross(scope, random, vector1.Data, vector2.Data);
    60       child.AddVariable(new Variable(child.TranslateName(bitVectorInfo.FormalName), new BoolArrayData(result)));
     59      bool[] result = Cross(scope, random, parents);
     60      scope.AddVariable(new Variable(scope.TranslateName("BitVector"), new BoolArrayData(result)));
    6161    }
    6262
    6363    /// <summary>
    64     /// Performs a crossover of two given parents.
     64    /// Performs a crossover of multiple bit vectors.
    6565    /// </summary>
    6666    /// <param name="scope">The current scope.</param>
    6767    /// <param name="random">A random number generator.</param>
    68     /// <param name="parent1">The first parent for crossover.</param>
    69     /// <param name="parent2">The second parent for crossover.</param>
     68    /// <param name="parents">An array containing all parent bit vectors.</param>
    7069    /// <returns>The newly created bit vector, resulting from the crossover operation.</returns>
    71     protected abstract bool[] Cross(IScope scope, IRandom random, bool[] parent1, bool[] parent2);
     70    protected abstract bool[] Cross(IScope scope, IRandom random, bool[][] parents);
    7271  }
    7372}
Note: See TracChangeset for help on using the changeset viewer.