Free cookie consent management tool by TermsFeed Policy Generator

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

Refactoring of crossover operators (#470)

Location:
trunk/sources/HeuristicLab.Permutation
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • 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  }
Note: See TracChangeset for help on using the changeset viewer.