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)

Location:
trunk/sources/HeuristicLab.IntVector
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.IntVector/DiscreteCrossover.cs

    r1157 r1218  
    5656
    5757    /// <summary>
    58     /// Performs a discrete crossover operation of the two given parents.
     58    /// Performs a discrete crossover operation for two given parent integer vectors.
    5959    /// </summary>
     60    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    6061    /// <param name="scope">The current scope.</param>
    6162    /// <param name="random">A random number generator.</param>
    62     /// <param name="parent1">The first parent for the crossover operation.</param>
    63     /// <param name="parent2">The second parent for the crossover operation.</param>
     63    /// <param name="parents">An array containing the two integer vectors that should be crossed.</param>
    6464    /// <returns>The newly created integer vector, resulting from the crossover operation.</returns>
    65     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    66       return Apply(random, parent1, parent2);
     65    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     66      if (parents.Length != 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is not equal to 2");
     67      return Apply(random, parents[0], parents[1]);
    6768    }
    6869  }
  • trunk/sources/HeuristicLab.IntVector/IntVectorCrossoverBase.cs

    r1157 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, int[][]"/>
     45    /// and adds the created integer vector to the current scope.
     46    /// </summary>
     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>
     49    /// <param name="random">A random number generator.</param>
     50    protected sealed override void Cross(IScope scope, IRandom random) {
     51      int[][] parents = new int[scope.SubScopes.Count][];
     52      int length = -1;
     53      for (int i = 0; i < scope.SubScopes.Count; i++) {
     54        parents[i] = scope.SubScopes[i].GetVariableValue<IntArrayData>("IntVector", false).Data;
     55        if (i == 0) length = parents[i].Length;
     56        else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in IntVectorCrossoverBase: Cannot apply crossover to integer vectors of different length");
     57      }
     58
     59      int[] result = Cross(scope, random, parents);
     60      scope.AddVariable(new Variable(scope.TranslateName("IntVector"), new IntArrayData(result)));
     61    }
     62
     63    /// <summary>
     64    /// Performs a crossover of multiple integer vectors.
    4565    /// </summary>
    4666    /// <param name="scope">The current scope.</param>
    4767    /// <param name="random">A random number generator.</param>
    48     /// <param name="parent1">The first parent for crossover.</param>
    49     /// <param name="parent2">The second parent for crossover.</param>
    50     /// <param name="child">The resulting child scope.</param>
    51     protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
    52       IVariableInfo intVectorInfo = GetVariableInfo("IntVector");
    53       IntArrayData vector1 = parent1.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false);
    54       IntArrayData vector2 = parent2.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false);
    55 
    56       if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to integer vectors of different length.");
    57 
    58       int[] result = Cross(scope, random, vector1.Data, vector2.Data);
    59       child.AddVariable(new Variable(child.TranslateName(intVectorInfo.FormalName), new IntArrayData(result)));
    60     }
    61 
    62     /// <summary>
    63     /// Performs a crossover of two given parents.
    64     /// </summary>
    65     /// <param name="scope">The current scope.</param>
    66     /// <param name="random">A random number generator.</param>
    67     /// <param name="parent1">The first parent for crossover.</param>
    68     /// <param name="parent2">The second parent for crossover.</param>
     68    /// <param name="parents">An array containing all parent integer vectors.</param>
    6969    /// <returns>The newly created integer vector, resulting from the crossover operation.</returns>
    70     protected abstract int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2);
     70    protected abstract int[] Cross(IScope scope, IRandom random, int[][] parents);
    7171  }
    7272}
  • trunk/sources/HeuristicLab.IntVector/SinglePointCrossover.cs

    r1157 r1218  
    5757
    5858    /// <summary>
    59     /// Performs a single point crossover at a randomly chosen position of the two
     59    /// Performs a single point crossover at a randomly chosen position of two
    6060    /// given parent integer vectors.
    6161    /// </summary>
     62    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    6263    /// <param name="scope">The current scope.</param>
    6364    /// <param name="random">A random number generator.</param>
    64     /// <param name="parent1">The first parent for crossover.</param>
    65     /// <param name="parent2">The second parent for crossover.</param>
     65    /// <param name="parents">An array containing the two integer vectors that should be crossed.</param>
    6666    /// <returns>The newly created integer vector, resulting from the single point crossover.</returns>
    67     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    68       return Apply(random, parent1, parent2);
     67    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     68      if (parents.Length != 2) throw new InvalidOperationException("ERROR in SinglePointCrossover: The number of parents is not equal to 2");
     69      return Apply(random, parents[0], parents[1]);
    6970    }
    7071  }
Note: See TracChangeset for help on using the changeset viewer.