Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1218


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

Refactoring of crossover operators (#470)

Location:
trunk/sources
Files:
5 deleted
30 edited
1 moved

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

    r1176 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 bit 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 bit vectors that should be crossed.</param>
    6666    /// <returns>The newly created bit vector, resulting from the single point crossover.</returns>
    67     protected override bool[] Cross(IScope scope, IRandom random, bool[] parent1, bool[] parent2) {
    68       return Apply(random, parent1, parent2);
     67    protected override bool[] Cross(IScope scope, IRandom random, bool[][] 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  }
  • trunk/sources/HeuristicLab.Evolutionary/CrossoverBase.cs

    r1159 r1218  
    2828namespace HeuristicLab.Evolutionary {
    2929  /// <summary>
    30   /// Base class for crossing over operators.
     30  /// Base class for crossover operators.
    3131  /// </summary>
    3232  public abstract class CrossoverBase : OperatorBase {
     
    4040
    4141    /// <summary>
    42     /// Replaces the parents (the sub scopes of the current <paramref name="scope"/>) with created children
    43     /// by crossing over of two adjacent sub scopes.
     42    /// Retrieves the random number generator and calls <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom)"/>.
    4443    /// </summary>
    45     /// <exception cref="InvalidOperationException">Thrown when the size of the mating pool
    46     /// is not even.</exception>
    47     /// <param name="scope">The current scope whose sub scopes shall be parents.</param>
     44    /// <param name="scope">The current scope which represents a new child.</param>
    4845    /// <returns><c>null</c>.</returns>
    4946    public override IOperation Apply(IScope scope) {
    5047      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    51 
    52       if (scope.SubScopes.Count != 2)
    53         throw new InvalidOperationException("ERROR: Number of parents is != 2");
    54 
    55       IScope parent1 = scope.SubScopes[0];
    56       IScope parent2 = scope.SubScopes[1];
    57       IScope child = scope;
    58       Cross(scope, random, parent1, parent2, child);
    59 
     48      Cross(scope, random);
    6049      return null;
    6150    }
    6251
    6352    /// <summary>
    64     /// Performs a cross over of <paramref name="parent1"/> and <paramref name="parent2"/>
    65     /// to create a new <paramref name="child"/>.
     53    /// Performs a crossover of all parents (sub-scopes) of <paramref name="scope"/> to create a new child.
     54    /// Note that all children have to be prepared using the <see cref="HeuristicLab.Evolutionary.ChildrenInitializer"/>.
    6655    /// </summary>
    67     /// <param name="scope">The current scope.</param>
     56    /// <param name="scope">The current scope which represents a new child.</param>
    6857    /// <param name="random">A random number generator.</param>
    69     /// <param name="parent1">The parent scope 1 to cross over.</param>
    70     /// <param name="parent2">The parent scope 2 to cross over.</param>
    71     /// <param name="child">The resulting child of the cross over.</param>
    72     protected abstract void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child);
     58    protected abstract void Cross(IScope scope, IRandom random);
    7359  }
    7460}
  • trunk/sources/HeuristicLab.Evolutionary/HeuristicLab.Evolutionary.csproj

    r1159 r1218  
    6161  <ItemGroup>
    6262    <Compile Include="ChildrenInitializer.cs" />
    63     <Compile Include="MultiCrossoverBase.cs" />
    6463    <Compile Include="SASEGASAReunificator.cs" />
    6564    <Compile Include="CrossoverBase.cs" />
  • 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  }
  • trunk/sources/HeuristicLab.Permutation/AbsolutePositionTopologicalCrossover.cs

    r850 r1218  
    7575
    7676    /// <summary>
    77     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
    78     /// by taking the values from both parents one by one with the same index.
     77    /// Performs an absolute position topological crossover operation for two given parent permutations.
    7978    /// </summary>
    80     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     79    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    8180    /// <param name="scope">The current scope.</param>
    8281    /// <param name="random">A random number generator.</param>
    83     /// <param name="parent1">The parent scope 1 to cross over.</param>
    84     /// <param name="parent2">The parent scope 2 to cross over.</param>
    85     /// <returns>The created cross over permutation as int array.</returns>
    86     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    87       return Apply(random, parent1, parent2);
     82    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     83    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     84    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     85      if (parents.Length != 2) throw new InvalidOperationException("ERROR in AbsolutePositionTopologicalCrossover: The number of parents is not equal to 2");
     86      return Apply(random, parents[0], parents[1]);
    8887    }
    8988  }
  • trunk/sources/HeuristicLab.Permutation/CosaCrossover.cs

    r850 r1218  
    8686
    8787    /// <summary>
    88     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
    89     /// by taking first the reverse elements of a randomly chosen interval of parent1
    90     /// and inserting it in the result at a position specified by the permutation of parent2.
    91     /// The remaining elements to be inserted are taken again from parent1 in the forward direction.
     88    /// Performs a COSA crossover operation for two given parent permutations.
    9289    /// </summary>
    93     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     90    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    9491    /// <param name="scope">The current scope.</param>
    95     /// <param name="random">The random number generator.</param>
    96     /// <param name="parent1">The parent scope 1 to cross over.</param>
    97     /// <param name="parent2">The parent scope 2 to cross over.</param>
    98     /// <returns>The created cross over permutation as int array.</returns>
    99     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    100       return Apply(random, parent1, parent2);
     92    /// <param name="random">A random number generator.</param>
     93    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     94    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     95    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     96      if (parents.Length != 2) throw new InvalidOperationException("ERROR in CosaCrossover: The number of parents is not equal to 2");
     97      return Apply(random, parents[0], parents[1]);
    10198    }
    10299  }
  • trunk/sources/HeuristicLab.Permutation/CyclicCrossover.cs

    r850 r1218  
    8080
    8181    /// <summary>
    82     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
    83     /// by copying a whole cycle starting at a randomly chosen position in parent1 and taking the rest
    84     /// from parent2.
     82    /// Performs a cyclic crossover operation for two given parent permutations.
    8583    /// </summary>
    86     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     84    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    8785    /// <param name="scope">The current scope.</param>
    88     /// <param name="random">The random number generator.</param>
    89     /// <param name="parent1">The parent scope 1 to cross over.</param>
    90     /// <param name="parent2">The parent scope 2 to cross over.</param>
    91     /// <returns>The created cross over permutation as int array.</returns>
    92     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    93       return Apply(random, parent1, parent2);
     86    /// <param name="random">A random number generator.</param>
     87    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     88    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     89    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     90      if (parents.Length != 2) throw new InvalidOperationException("ERROR in CyclicCrossover: The number of parents is not equal to 2");
     91      return Apply(random, parents[0], parents[1]);
    9492    }
    9593  }
  • trunk/sources/HeuristicLab.Permutation/EdgeRecombinationCrossover.cs

    r850 r1218  
    139139
    140140    /// <summary>
    141     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="2"/>
    142     /// by calculating the edges of each element. Starts at a randomly chosen position,
    143     /// the next element is a neighbour with the least
    144     /// number of neighbours, the next again a neighbour and so on.
     141    /// Performs an edge recombination crossover operation for two given parent permutations.
    145142    /// </summary>
    146     /// <exception cref="InvalidOperationException">Thrown when the permutation lacks a number.
    147     /// </exception>
    148     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     143    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    149144    /// <param name="scope">The current scope.</param>
    150     /// <param name="random">The random number generator.</param>
    151     /// <param name="parent1">The parent scope 1 to cross over.</param>
    152     /// <param name="parent2">The parent scope 2 to cross over.</param>
    153     /// <returns>The created cross over permutation as int array.</returns>
    154     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    155       return Apply(random, parent1, parent2);
     145    /// <param name="random">A random number generator.</param>
     146    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     147    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     148    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     149      if (parents.Length != 2) throw new InvalidOperationException("ERROR in EdgeRecombinationCrossover: The number of parents is not equal to 2");
     150      return Apply(random, parents[0], parents[1]);
    156151    }
    157152  }
  • trunk/sources/HeuristicLab.Permutation/MaximalPreservativeCrossover.cs

    r850 r1218  
    7878
    7979    /// <summary>
    80     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
    81     /// by preserving a big randomly chosen region of one permutation and taking the missing ones from the other
    82     /// permuation array.
     80    /// Performs a maximal preservative crossover operation for two given parent permutations.
    8381    /// </summary>
    84     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     82    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    8583    /// <param name="scope">The current scope.</param>
    86     /// <param name="random">The random number generator.</param>
    87     /// <param name="parent1">The permutation array of parent 1.</param>
    88     /// <param name="parent2">The permutation array of parent 2.</param>
    89     /// <returns>The created cross over permutation as int array.</returns>
    90     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    91       return Apply(random, parent1, parent2);
     84    /// <param name="random">A random number generator.</param>
     85    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     86    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     87    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     88      if (parents.Length != 2) throw new InvalidOperationException("ERROR in MaximalPreservativeCrossover: The number of parents is not equal to 2");
     89      return Apply(random, parents[0], parents[1]);
    9290    }
    9391  }
  • trunk/sources/HeuristicLab.Permutation/OrderBasedCrossover.cs

    r850 r1218  
    9292
    9393    /// <summary>
    94     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> by
    95     /// randomly selecting some values from the first permutation that will be inserted one after each
    96     /// other; the missing ones are picked in the correct order from the second permutation.
     94    /// Performs an order-based crossover operation for two given parent permutations.
    9795    /// </summary>
    98     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     96    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    9997    /// <param name="scope">The current scope.</param>
    100     /// <param name="random">The random number generator.</param>
    101     /// <param name="parent1">The parent scope 1 to cross over.</param>
    102     /// <param name="parent2">The parent scope 2 to cross over.</param>
    103     /// <returns>The created cross over permutation as int array.</returns>
    104     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    105       return Apply(random, parent1, parent2);
     98    /// <param name="random">A random number generator.</param>
     99    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     100    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     101    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     102      if (parents.Length != 2) throw new InvalidOperationException("ERROR in OrderBasedCrossover: The number of parents is not equal to 2");
     103      return Apply(random, parents[0], parents[1]);
    106104    }
    107105  }
  • trunk/sources/HeuristicLab.Permutation/OrderCrossover.cs

    r850 r1218  
    7474
    7575    /// <summary>
    76     /// Performs a cross over permuation of <paramref name="parent1"/> and <paramref name="parent2"/>
    77     /// by taking a randomly chosen interval from <paramref name="parent1"/> and the rest from
    78     /// <paramref name="parent2"/>.
     76    /// Performs an order crossover operation for two given parent permutations.
    7977    /// </summary>
    80     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     78    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    8179    /// <param name="scope">The current scope.</param>
    82     /// <param name="random">The random number generator.</param>
    83     /// <param name="parent1">The parent scope 1 to cross over.</param>
    84     /// <param name="parent2">The parent scope 2 to cross over.</param>
    85     /// <returns>The created cross over permutation as int array.</returns>
    86     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    87       return Apply(random, parent1, parent2);
     80    /// <param name="random">A random number generator.</param>
     81    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     82    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     83    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     84      if (parents.Length != 2) throw new InvalidOperationException("ERROR in OrderCrossover: The number of parents is not equal to 2");
     85      return Apply(random, parents[0], parents[1]);
    8886    }
    8987  }
  • trunk/sources/HeuristicLab.Permutation/PartiallyMatchedCrossover.cs

    r850 r1218  
    8787
    8888    /// <summary>
    89     /// Performs a cross over permuation of <paramref name="parent1"/> and <paramref name="parent2"/>
    90     /// by taking a randomly chosen interval from <paramref name="parent1"/>, preserving the position,
    91     /// then all positions from <paramref name="parent2"/> which are still free in the child
    92     /// (the position is free and the value is "free")
    93     /// and then missing ones from <paramref name="parent2"/> in the order they occur
    94     /// in <paramref name="parent2"/>.
     89    /// Performs a partially matched crossover operation for two given parent permutations.
    9590    /// </summary>
    96     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     91    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    9792    /// <param name="scope">The current scope.</param>
    98     /// <param name="random">The random number generator.</param>
    99     /// <param name="parent1">The parent scope 1 to cross over.</param>
    100     /// <param name="parent2">The parent scope 2 to cross over.</param>
    101     /// <returns>The created cross over permutation as int array.</returns>
    102     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    103       return Apply(random, parent1, parent2);
     93    /// <param name="random">A random number generator.</param>
     94    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     95    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     96    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     97      if (parents.Length != 2) throw new InvalidOperationException("ERROR in PartiallyMatchedCrossover: The number of parents is not equal to 2");
     98      return Apply(random, parents[0], parents[1]);
    10499    }
    105100  }
  • trunk/sources/HeuristicLab.Permutation/PermutationCrossoverBase.cs

    r850 r1218  
    2828namespace HeuristicLab.Permutation {
    2929  /// <summary>
    30   /// Base class for cross over permutations.
     30  /// Base class for all permutation crossover operators.
    3131  /// </summary>
    3232  public abstract class PermutationCrossoverBase : CrossoverBase {
     
    4141
    4242    /// <summary>
    43     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> with
    44     /// the given random number generator (<paramref name="random"/>) to create a new
    45     /// <paramref name="child"/>.
     43    /// Performs a crossover by calling <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom, int[][]"/>
     44    /// and adds the created permutation to the current scope.
    4645    /// </summary>
    47     /// <exception cref="InvalidOperationException">Thrown when the two permutations have a different
    48     /// length.</exception>
    49     /// <remarks>Calls <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom, int[], int[])"/>.</remarks>
    50     /// <param name="scope">The scope where to get the actual child variable name.</param>
    51     /// <param name="random">The random number generator.</param>
    52     /// <param name="parent1">The parent scope 1 to cross over.</param>
    53     /// <param name="parent2">The parent scope 2 to cross over.</param>
    54     /// <param name="child">The child scope which to assign the permutated data.</param>
    55     protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
    56       Permutation perm1 = parent1.GetVariableValue<Permutation>("Permutation", false);
    57       Permutation perm2 = parent2.GetVariableValue<Permutation>("Permutation", false);
     46    /// <exception cref="InvalidOperationException">Thrown if the parents have different lengths.</exception>
     47    /// <param name="scope">The current scope which represents a new child.</param>
     48    /// <param name="random">A random number generator.</param>
     49    protected sealed override void Cross(IScope scope, IRandom random) {
     50      int[][] parents = new int[scope.SubScopes.Count][];
     51      int length = -1;
     52      for (int i = 0; i < scope.SubScopes.Count; i++) {
     53        parents[i] = scope.SubScopes[i].GetVariableValue<Permutation>("Permutation", false).Data;
     54        if (i == 0) length = parents[i].Length;
     55        else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in PermutationCrossoverBase: Cannot apply crossover to permutations of different length");
     56      }
    5857
    59       if (perm1.Data.Length != perm2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to permutations of different length.");
    60 
    61       int[] result = Cross(scope, random, perm1.Data, perm2.Data);
    62       child.AddVariable(new Variable(scope.TranslateName("Permutation"), new Permutation(result)));
     58      int[] result = Cross(scope, random, parents);
     59      scope.AddVariable(new Variable(scope.TranslateName("Permutation"), new Permutation(result)));
    6360    }
    6461
    6562    /// <summary>
    66     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> with
    67     /// the given random number generator (<paramref name="random"/>) .
     63    /// Performs a crossover of multiple permutations.
    6864    /// </summary>
    69     /// <param name="scope">The scope of the variables.</param>
    70     /// <param name="random">The random number generator.</param>
    71     /// <param name="parent1">The parent scope 1 to cross over.</param>
    72     /// <param name="parent2">The parent scope 2 to cross over.</param>
    73     /// <returns>The created cross over permutation as int array.</returns>
    74     protected abstract int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2);
     65    /// <param name="scope">The current scope.</param>
     66    /// <param name="random">A random number generator.</param>
     67    /// <param name="parents">An array containing all parent permutations.</param>
     68    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     69    protected abstract int[] Cross(IScope scope, IRandom random, int[][] parents);
    7570  }
    7671}
  • trunk/sources/HeuristicLab.Permutation/PositionBasedCrossover.cs

    r850 r1218  
    7878
    7979    /// <summary>
    80     /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
    81     /// based on randomly chosen positions to define which position to take from where.
     80    /// Performs a position-based crossover operation for two given parent permutations.
    8281    /// </summary>
    83     /// <remarks>Calls <see cref="Apply"/>.</remarks>
     82    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    8483    /// <param name="scope">The current scope.</param>
    85     /// <param name="random">The random number generator.</param>
    86     /// <param name="parent1">The permutation array of parent 1.</param>
    87     /// <param name="parent2">The permutation array of parent 2.</param>
    88     /// <returns>The created cross over permutation as int array.</returns>
    89     protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {
    90       return Apply(random, parent1, parent2);
     84    /// <param name="random">A random number generator.</param>
     85    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
     86    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
     87    protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
     88      if (parents.Length != 2) throw new InvalidOperationException("ERROR in PositionBasedCrossover: The number of parents is not equal to 2");
     89      return Apply(random, parents[0], parents[1]);
    9190    }
    9291  }
  • 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  }
  • trunk/sources/HeuristicLab.SimOpt/SimOptDiscreteMultiCrossover.cs

    r637 r1218  
    2929
    3030namespace HeuristicLab.SimOpt {
    31   public class SimOptDiscreteMultiCrossover : MultiCrossoverBase {
     31  public class SimOptDiscreteMultiCrossover : CrossoverBase {
    3232
    3333    public override string Description {
     
    4040    }
    4141
    42     protected override void Cross(IScope scope, IRandom random, IScope[] parents, IScope child) {
     42    protected override void Cross(IScope scope, IRandom random) {
    4343      ICollection<IConstraint> violated;
    4444
    45       ConstrainedItemList[] p = new ConstrainedItemList[parents.Length];
     45      ConstrainedItemList[] p = new ConstrainedItemList[scope.SubScopes.Count];
    4646      for (int i = 0; i < p.Length; i++) {
    47         p[i] = parents[i].GetVariableValue<ConstrainedItemList>("Item", false);
     47        p[i] = scope.SubScopes[i].GetVariableValue<ConstrainedItemList>("Item", false);
    4848        if (i > 0 && p[i].Count != p[i-1].Count) throw new InvalidOperationException("ERROR: the lists do not contain the same number of items");
    4949      }
     
    5555          childList.BeginCombinedOperation();
    5656          for (int i = 0; i < childList.Count; i++) {
    57             int nextParent = random.Next(0, parents.Length);
     57            int nextParent = random.Next(0, scope.SubScopes.Count);
    5858            if (nextParent > 0) childList.TrySetAt(i, (IItem)p[nextParent].Clone(), out violated);
    5959          }
    6060        } while (!childList.EndCombinedOperation(out violated) && ++iter < 100);
    6161        if (violated.Count == 0) {
    62           child.AddVariable(new Variable(parents[0].TranslateName("Item"), childList));
     62          scope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("Item"), childList));
    6363        }
    6464      }
  • trunk/sources/HeuristicLab.SimOpt/SimOptSinglePointCrossover.cs

    r637 r1218  
    4040    }
    4141
    42     protected override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
     42    protected override void Cross(IScope scope, IRandom random) {
    4343      ICollection<IConstraint> violated;
    44       ConstrainedItemList p1 = parent1.GetVariableValue<ConstrainedItemList>("Item", false);
    45       ConstrainedItemList p2 = parent2.GetVariableValue<ConstrainedItemList>("Item", false);
     44      ConstrainedItemList p1 = scope.SubScopes[0].GetVariableValue<ConstrainedItemList>("Item", false);
     45      ConstrainedItemList p2 = scope.SubScopes[1].GetVariableValue<ConstrainedItemList>("Item", false);
    4646
    4747      if (p1.Count != p2.Count) throw new InvalidOperationException("ERROR: the lists do not contain the same number of items");
     
    5858        } while (!childList.EndCombinedOperation(out violated) && ++iter < 100);
    5959        if (violated.Count == 0) {
    60           child.AddVariable(new Variable(parent1.TranslateName("Item"), childList));
     60          scope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("Item"), childList));
    6161        }
    6262      }
Note: See TracChangeset for help on using the changeset viewer.