Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/09 01:21:53 (16 years ago)
Author:
swagner
Message:

Refactoring of crossover operators (#470)

Location:
trunk/sources/HeuristicLab.RealVector
Files:
4 deleted
11 edited
1 moved

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  }
  • trunk/sources/HeuristicLab.RealVector/BlendAlphaCrossover.cs

    r1184 r1218  
    7878
    7979    /// <summary>
    80     /// Performs a blend alpha crossover of two real vectors.
     80    /// Performs a blend alpha crossover operation for two given parent real vectors.
    8181    /// </summary>
    82     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     82    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    8383    /// <param name="scope">The current scope.</param>
    84     /// <param name="random">The random number generator.</param>
    85     /// <param name="parent1">The first parent for the crossover operation.</param>
    86     /// <param name="parent2">The second parent for the crossover operation.</param>
    87     /// <returns>The newly created real vector, resulting from the blend alpha crossover.</returns>
    88     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
     84    /// <param name="random">A random number generator.</param>
     85    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
     86    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     87    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     88      if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaCrossover: The number of parents is not equal to 2");
    8989      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
    90       return Apply(random, parent1, parent2, alpha);
     90      return Apply(random, parents[0], parents[1], alpha);
    9191    }
    9292  }
  • trunk/sources/HeuristicLab.RealVector/CompleteContinuousCrossover.cs

    r1184 r1218  
    2929  /// <summary>
    3030  /// Complete continuous crossover for real vectors: for each element of the new vector the average
    31   /// of both parents is calculated.
     31  /// of all parents is calculated.
    3232  /// </summary>
    3333  public class CompleteContinuousCrossover : RealVectorCrossoverBase {
     
    3838
    3939    /// <summary>
    40     /// Performs a complete continuous crossover of the two given real vectors.
     40    /// Performs a complete continuous crossover of multiple given real vectors.
    4141    /// </summary>
    4242    /// <param name="random">The random number generator.</param>
    43     /// <param name="parent1">The first parent for the crossover.</param>
    44     /// <param name="parent2">The second parent for the crossover.</param>
     43    /// <param name="parents">An array containing the parents that should be crossed.</param>
    4544    /// <returns>The newly created real vector, resulting from the complete continuous crossover.</returns>
    46     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    47       int length = parent1.Length;
     45    public static double[] Apply(IRandom random, double[][] parents) {
     46      int length = parents[0].Length;
    4847      double[] result = new double[length];
    49 
    50       for (int i = 0; i < length; i++)
    51         result[i] = (parent1[i] + parent2[i]) / 2;
     48      for (int i = 0; i < length; i++) {
     49        double sum = 0.0;
     50        for (int j = 0; j < parents.Length; j++)
     51          sum += parents[j][i];
     52        result[i] = sum / parents.Length;
     53      }
    5254      return result;
    5355    }
    5456
    5557    /// <summary>
    56     /// Performs a complete continuous crossover of the two given real vectors.
     58    /// Performs a complete continuous crossover operation for multiple given parent real vectors.
    5759    /// </summary>
    58     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     60    /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception>
    5961    /// <param name="scope">The current scope.</param>
    60     /// <param name="random">The random number generator.</param>
    61     /// <param name="parent1">The first parent for the crossover.</param>
    62     /// <param name="parent2">The second parent for the crossover.</param>
    63     /// <returns>The newly created real vector, resulting from the complete continuous crossover.</returns>
    64     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    65       return Apply(random, parent1, parent2);
     62    /// <param name="random">A random number generator.</param>
     63    /// <param name="parents">An array containing the real vectors that should be crossed.</param>
     64    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     65    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     66      if (parents.Length < 2) throw new InvalidOperationException("ERROR in CompleteContinuousCrossover: The number of parents is less than 2");
     67      return Apply(random, parents);
    6668    }
    6769  }
  • trunk/sources/HeuristicLab.RealVector/ContinuousCrossover.cs

    r1184 r1218  
    2828namespace HeuristicLab.RealVector {
    2929  /// <summary>
    30   /// Continuous crossover for real vectors: for each element randomly either the element of the first
    31   /// parent or the average of both parents is selected.
     30  /// Continuous crossover for real vectors: for each element either the element of the first
     31  /// parent or the average of all parents is selected randomly.
    3232  /// </summary>
    3333  public class ContinuousCrossover : RealVectorCrossoverBase {
     
    3838
    3939    /// <summary>
    40     /// Performs a continuous crossover of the two given real vectors.
     40    /// Performs a continuous crossover of multiple given real vectors.
    4141    /// </summary>
    4242    /// <param name="random">The random number generator.</param>
    43     /// <param name="parent1">The first parent for the crossover.</param>
    44     /// <param name="parent2">The second parent for the crossover.</param>
     43    /// <param name="parents">An array containing the parents that should be crossed.</param>
    4544    /// <returns>The newly created real vector, resulting from the continuous crossover.</returns>
    46     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    47       int length = parent1.Length;
     45    public static double[] Apply(IRandom random, double[][] parents) {
     46      int length = parents[0].Length;
    4847      double[] result = new double[length];
    4948
    5049      for (int i = 0; i < length; i++) {
    5150        if (random.NextDouble() < 0.5) {
    52           result[i] = (parent1[i] + parent2[i]) / 2;
     51          double sum = 0.0;
     52          for (int j = 0; j < parents.Length; j++)
     53            sum += parents[j][i];
     54          result[i] = sum / parents.Length;
    5355        } else {
    54           result[i] = parent1[i];
     56          result[i] = parents[0][i];
    5557        }
    5658      }
     
    5961
    6062    /// <summary>
    61     /// Performs a continuous crossover of the two given real vectors.
     63    /// Performs a continuous crossover operation for multiple given parent real vectors.
    6264    /// </summary>
    63     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     65    /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception>
    6466    /// <param name="scope">The current scope.</param>
    65     /// <param name="random">The random number generator.</param>
    66     /// <param name="parent1">The first parent for the crossover.</param>
    67     /// <param name="parent2">The second parent for the crossover.</param>
    68     /// <returns>The newly created real vector, resulting from the continuous crossover.</returns>
    69     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    70       return Apply(random, parent1, parent2);
     67    /// <param name="random">A random number generator.</param>
     68    /// <param name="parents">An array containing the real vectors that should be crossed.</param>
     69    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     70    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     71      if (parents.Length < 2) throw new InvalidOperationException("ERROR in ContinuousCrossover: The number of parents is less than 2");
     72      return Apply(random, parents);
    7173    }
    7274  }
  • trunk/sources/HeuristicLab.RealVector/DiscreteCrossover.cs

    r1184 r1218  
    2727namespace HeuristicLab.RealVector {
    2828  /// <summary>
    29   /// Discrete crossover for real vectors: Selects randomly either the value of the first or the
    30   /// second parent.
     29  /// Discrete crossover for real vectors: For each position in the new vector an allele
     30  /// of one of the parents is randomly selected.
    3131  /// </summary>
    3232  public class DiscreteCrossover : RealVectorCrossoverBase {
    3333    /// <inheritdoc select="summary"/>
    3434    public override string Description {
    35       get { return "Discrete crossover for real vectors."; }
     35      get { return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent."; }
    3636    }
    3737
    3838    /// <summary>
    39     /// Performs a discrete crossover operation of the two given parents.
     39    /// Performs a discrete crossover operation on multiple given parents.
    4040    /// </summary>
    4141    /// <param name="random">A random number generator.</param>
    42     /// <param name="parent1">The first parent for the crossover operation.</param>
    43     /// <param name="parent2">The second parent for the crossover operation.</param>
     42    /// <param name="parents">An array containing the parents that should be crossed.</param>
    4443    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    45     public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {
    46       int length = parent1.Length;
     44    public static double[] Apply(IRandom random, double[][] parents) {
     45      int length = parents[0].Length;
    4746      double[] result = new double[length];
    48 
    4947      for (int i = 0; i < length; i++) {
    50         if (random.NextDouble() < 0.5)
    51           result[i] = parent1[i];
    52         else
    53           result[i] = parent2[i];
     48        result[i] = parents[random.Next(parents.Length)][i];
    5449      }
    5550      return result;
     
    5752
    5853    /// <summary>
    59     /// Performs a discrete crossover operation of the two given parents.
     54    /// Performs a discrete crossover operation for multiple given parent real vectors.
    6055    /// </summary>
     56    /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception>
    6157    /// <param name="scope">The current scope.</param>
    6258    /// <param name="random">A random number generator.</param>
    63     /// <param name="parent1">The first parent for the crossover operation.</param>
    64     /// <param name="parent2">The second parent for the crossover operation.</param>
     59    /// <param name="parents">An array containing the real vectors that should be crossed.</param>
    6560    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    66     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    67       return Apply(random, parent1, parent2);
     61    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     62      if (parents.Length < 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is less than 2");
     63      return Apply(random, parents);
    6864    }
    6965  }
  • 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  }
  • trunk/sources/HeuristicLab.RealVector/HeuristicLab.RealVector.csproj

    r1159 r1218  
    6565    <Compile Include="ContinuousManipulator.cs" />
    6666    <Compile Include="DiscreteCrossover.cs" />
    67     <Compile Include="DiscreteMultiCrossover.cs" />
    6867    <Compile Include="HeuristicCrossover.cs" />
    69     <Compile Include="IntermediateMultiCrossover.cs" />
    7068    <Compile Include="MichalewiczNonUniformAllPositionsManipulator.cs" />
    7169    <Compile Include="MichalewiczNonUniformOnePositionManipulator.cs" />
    7270    <Compile Include="RandomConvexCrossover.cs" />
    7371    <Compile Include="LocalCrossover.cs" />
    74     <Compile Include="RealVectorMultiCrossoverBase.cs" />
    75     <Compile Include="RealVectorSelfAdaptiveMultiCrossoverBase.cs" />
    76     <Compile Include="SelfAdaptiveDiscreteMultiCrossover.cs" />
     72    <Compile Include="SelfAdaptiveDiscreteCrossover.cs" />
    7773    <Compile Include="SelfAdaptiveNormalAllPositionsManipulator.cs" />
    7874    <Compile Include="UniformAllPositionsManipulator.cs" />
  • trunk/sources/HeuristicLab.RealVector/LocalCrossover.cs

    r1184 r1218  
    5757
    5858    /// <summary>
    59     /// Performs a local crossover on the two given parent vectors.
     59    /// Performs a local crossover operation for two given parent real vectors.
    6060    /// </summary>
    61     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     61    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    6262    /// <param name="scope">The current scope.</param>
    63     /// <param name="random">The random number generator.</param>
    64     /// <param name="parent1">The first parent for the crossover operation.</param>
    65     /// <param name="parent2">The second parent for the crossover operation.</param>
    66     /// <returns>The newly created real vector, resulting from the local crossover.</returns>
    67     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    68       return Apply(random, parent1, parent2);
     63    /// <param name="random">A random number generator.</param>
     64    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
     65    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     66    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     67      if (parents.Length != 2) throw new InvalidOperationException("ERROR in LocalCrossover: The number of parents is not equal to 2");
     68      return Apply(random, parents[0], parents[1]);
    6969    }
    7070  }
  • trunk/sources/HeuristicLab.RealVector/RandomConvexCrossover.cs

    r1184 r1218  
    5656
    5757    /// <summary>
    58     /// Performs a random convex crossover on the two given parents.
     58    /// Performs a random convex crossover operation for two given parent real vectors.
    5959    /// </summary>
    60     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     60    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    6161    /// <param name="scope">The current scope.</param>
    62     /// <param name="random">The random number generator.</param>
    63     /// <param name="parent1">The first parent vector for the crossover.</param>
    64     /// <param name="parent2">The second parent vector for the crossover.</param>
    65     /// <returns>The newly created real vector, resulting from the random convex crossover.</returns>
    66     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    67       return Apply(random, parent1, parent2);
     62    /// <param name="random">A random number generator.</param>
     63    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
     64    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     65    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     66      if (parents.Length != 2) throw new InvalidOperationException("ERROR in RandomConvexCrossover: The number of parents is not equal to 2");
     67      return Apply(random, parents[0], parents[1]);
    6868    }
    6969  }
  • trunk/sources/HeuristicLab.RealVector/RealVectorCrossoverBase.cs

    r1184 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, double[][]"/>
     45    /// and adds the created real 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      double[][] parents = new double[scope.SubScopes.Count][];
     52      int length = -1;
     53      for (int i = 0; i < scope.SubScopes.Count; i++) {
     54        parents[i] = scope.SubScopes[i].GetVariableValue<DoubleArrayData>("RealVector", false).Data;
     55        if (i == 0) length = parents[i].Length;
     56        else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in RealVectorCrossoverBase: Cannot apply crossover to real vectors of different length");
     57      }
     58
     59      double[] result = Cross(scope, random, parents);
     60      scope.AddVariable(new Variable(scope.TranslateName("RealVector"), new DoubleArrayData(result)));
     61    }
     62
     63    /// <summary>
     64    /// Performs a crossover of multiple real 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       DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false);
    53       DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false);
    54 
    55       if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
    56 
    57       double[] result = Cross(scope, random, vector1.Data, vector2.Data);
    58       child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result)));
    59     }
    60 
    61     /// <summary>
    62     /// Performs a crossover of two given parents.
    63     /// </summary>
    64     /// <param name="scope">The current scope.</param>
    65     /// <param name="random">A random number generator.</param>
    66     /// <param name="parent1">The first parent for crossover.</param>
    67     /// <param name="parent2">The second parent for crossover.</param>
     68    /// <param name="parents">An array containing all parent real vectors.</param>
    6869    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
    69     protected abstract double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2);
     70    protected abstract double[] Cross(IScope scope, IRandom random, double[][] parents);
    7071  }
    7172}
  • trunk/sources/HeuristicLab.RealVector/SelfAdaptiveDiscreteCrossover.cs

    r1217 r1218  
    2828namespace HeuristicLab.RealVector {
    2929  /// <summary>
    30   /// Creates a new offspring by combining the alleles in the parents such that each allele is
     30  /// Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is
    3131  /// randomly selected from one parent. It will also use the same strategy to combine the endogenous
    32   /// strategy parameter vector. 
     32  /// strategy parameter vector.
    3333  /// </summary>
    34   public class SelfAdaptiveDiscreteMultiCrossover : RealVectorSelfAdaptiveMultiCrossoverBase {
     34  public class SelfAdaptiveDiscreteCrossover : RealVectorCrossoverBase {
    3535    /// <inheritdoc select="summary"/>
    3636    public override string Description {
    3737      get {
    38         return @"This creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector.";
     38        return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector.";
    3939      }
    4040    }
    4141
    4242    /// <summary>
    43     /// Performs a self adaptive discrete multiple crossover on the given list of <paramref name="parents"/>.
     43    /// Initializes a new instance of <see cref="SelfAdaptiveDiscreteCrossover"/> with one additional
     44    /// variable infos (<c>StrategyVector</c>).
    4445    /// </summary>
    45     /// <exception cref="InvalidOperationException">Thrown when the parent vectors have different lengths.</exception>
    46     /// <param name="random">The random number generator.</param>
    47     /// <param name="parents">The list of parents to crossover.</param>
    48     /// <param name="strategyParametersList">The strategy parameter list.</param>
    49     /// <param name="childIndividual">Output parameter; the created child.</param>
    50     /// <param name="strategyParameters">Output parameter; endogenous strategy parameters.</param>
    51     public static void Apply(IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) {
    52       childIndividual = new double[parents[0].Length];
    53       strategyParameters = new double[strategyParametersList[0].Length];
    54       try {
    55         for (int i = 0; i < childIndividual.Length; i++) {
    56           int nextParent = random.Next(parents.Count);
    57           childIndividual[i] = parents[nextParent][i];
    58           strategyParameters[i] = strategyParametersList[nextParent][i];
    59         }
    60       } catch (IndexOutOfRangeException) {
    61         throw new InvalidOperationException("Cannot apply self adaptive multicrossover to real vectors of different length.");
     46    public SelfAdaptiveDiscreteCrossover()
     47      : base() {
     48      AddVariableInfo(new VariableInfo("StrategyVector", "Endogenous strategy parameter vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
     49    }
     50
     51    /// <summary>
     52    /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
     53    /// </summary>
     54    /// <param name="random">A random number generator.</param>
     55    /// <param name="parents">An array containing the parents that should be crossed.</param>
     56    /// <param name="strategies">An array containing the strategy vectors of the parents.</param>
     57    /// <param name="child">The newly created real vector, resulting from the crossover operation (output parameter).</param>
     58    /// <param name="strategy">The newly created strategy vector, resulting from the crossover operation (output parameter).</param>
     59    public static void Apply(IRandom random, double[][] parents, double[][] strategies, out double[] child, out double[] strategy) {
     60      child = new double[parents[0].Length];
     61      strategy = new double[strategies[0].Length];
     62      for (int i = 0; i < child.Length; i++) {
     63        int nextParent = random.Next(parents.Length);
     64        child[i] = parents[nextParent][i];
     65        strategy[i] = strategies[nextParent][i];
    6266      }
    6367    }
    6468
    6569    /// <summary>
    66     /// Performs a self adaptive discrete multiple crossover on the given list of <paramref name="parents"/>.
     70    /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
    6771    /// </summary>
    68     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     72    /// <exception cref="InvalidOperationException">Thrown if there are less than two parents or if the length of the strategy vectors is not the same as the length of the real vectors.</exception>
    6973    /// <param name="scope">The current scope.</param>
    70     /// <param name="random">The random number generator.</param>
    71     /// <param name="parents">The list of parents to crossover.</param>
    72     /// <param name="strategyParametersList">The strategy parameter list.</param>
    73     /// <param name="childIndividual">Output parameter; the created child.</param>
    74     /// <param name="strategyParameters">Output parameter; endogenous strategy parameters.</param>
    75     protected override void Cross(IScope scope, IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) {
    76       Apply(random, parents, strategyParametersList, out childIndividual, out strategyParameters);
     74    /// <param name="random">A random number generator.</param>
     75    /// <param name="parents">An array containing the real vectors that should be crossed.</param>
     76    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     77    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     78      if (parents.Length < 2) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: The number of parents is less than 2");
     79      double[][] strategies = new double[scope.SubScopes.Count][];
     80      int length = parents[0].Length;
     81      for (int i = 0; i < scope.SubScopes.Count; i++) {
     82        strategies[i] = scope.SubScopes[i].GetVariableValue<DoubleArrayData>("StrategyVector", false).Data;
     83        if (strategies[i].Length != length) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: Strategy vectors do not have the same length as the real vectors");
     84      }
     85
     86      double[] child, strategy;
     87      Apply(random, parents, strategies, out child, out strategy);
     88      scope.AddVariable(new Variable(scope.TranslateName("StrategyVector"), new DoubleArrayData(strategy)));
     89      return child;
    7790    }
    7891  }
  • trunk/sources/HeuristicLab.RealVector/SinglePointCrossover.cs

    r1184 r1218  
    5757
    5858    /// <summary>
    59     /// Performs a single point crossover at a randomly chosen position of the two
    60     /// given parent real vectors.
     59    /// Performs a single point crossover operation for two given parent real vectors.
    6160    /// </summary>
     61    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    6262    /// <param name="scope">The current scope.</param>
    6363    /// <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>
    66     /// <returns>The newly created real vector, resulting from the single point crossover.</returns>
    67     protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
    68       return Apply(random, parent1, parent2);
     64    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
     65    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
     66    protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
     67      if (parents.Length != 2) throw new InvalidOperationException("ERROR in SinglePointCrossover: The number of parents is not equal to 2");
     68      return Apply(random, parents[0], parents[1]);
    6969    }
    7070  }
Note: See TracChangeset for help on using the changeset viewer.