1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)


4  *


5  * This file is part of HeuristicLab.


6  *


7  * HeuristicLab is free software: you can redistribute it and/or modify


8  * it under the terms of the GNU General Public License as published by


9  * the Free Software Foundation, either version 3 of the License, or


10  * (at your option) any later version.


11  *


12  * HeuristicLab is distributed in the hope that it will be useful,


13  * but WITHOUT ANY WARRANTY; without even the implied warranty of


14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


15  * GNU General Public License for more details.


16  *


17  * You should have received a copy of the GNU General Public License


18  * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.


19  */


20  #endregion


21 


22  using System;


23  using HeuristicLab.Common;


24  using HeuristicLab.Core;


25  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


26 


27  namespace HeuristicLab.Encodings.PermutationEncoding {


28  /// <summary>


29  /// Performs a cross over permutation of two permutation arrays by taking randomly a selection of values


30  /// (not an interval!) from the first permutation keeping the correct order and filling


31  /// the missing entries with the elements from the second permutation, also in the right order.


32  /// </summary>


33  /// <remarks>


34  /// This is in some papers also called Order Crossover #2.<br />


35  /// It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp. 332349.


36  /// </remarks>


37  [Item("OrderBasedCrossover", "An operator which performs an order based crossover of two permutations. It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp. 332349.")]


38  [StorableClass]


39  public class OrderBasedCrossover : PermutationCrossover {


40  [StorableConstructor]


41  protected OrderBasedCrossover(bool deserializing) : base(deserializing) { }


42  protected OrderBasedCrossover(OrderBasedCrossover original, Cloner cloner) : base(original, cloner) { }


43  public OrderBasedCrossover() : base() { }


44 


45  public override IDeepCloneable Clone(Cloner cloner) {


46  return new OrderBasedCrossover(this, cloner);


47  }


48 


49  /// <summary>


50  /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> by


51  /// randomly selecting some values from the first permutation that will be inserted one after each


52  /// other; the missing ones are picked in the correct order from the second permutation.


53  /// </summary>


54  /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>


55  /// <param name="random">A random number generator.</param>


56  /// <param name="parent1">The first parent permutation to cross.</param>


57  /// <param name="parent2">The second parent permutation to cross.</param>


58  /// <returns>The new permutation resulting from the crossover.</returns>


59  public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {


60  if (parent1.Length != parent2.Length) throw new ArgumentException("OrderBasedCrossover: The parent permutations are of unequal length.");


61  int length = parent1.Length;


62  int[] result = new int[length];


63  int[] selectedNumbers = new int[random.Next(length + 1)];


64  bool[] numberSelected = new bool[length];


65  int index, selectedIndex, currentIndex;


66 


67  for (int i = 0; i < selectedNumbers.Length; i++) { // select numbers for array


68  index = 0;


69  while (numberSelected[parent1[index]]) { // find first valid index


70  index++;


71  }


72 


73  selectedIndex = random.Next(length  i);


74  currentIndex = 0;


75  while ((index < parent1.Length) && (currentIndex != selectedIndex)) { // find selected number


76  index++;


77  if (!numberSelected[parent1[index]]) {


78  currentIndex++;


79  }


80  }


81  numberSelected[parent1[index]] = true;


82  }


83 


84  index = 0;


85  for (int i = 0; i < parent1.Length; i++) { // copy selected numbers in array


86  if (numberSelected[parent1[i]]) {


87  selectedNumbers[index] = parent1[i];


88  index++;


89  }


90  }


91 


92  index = 0;


93  for (int i = 0; i < result.Length; i++) { // copy rest of second permutation and selected numbers in order of first permutation


94  if (numberSelected[parent2[i]]) {


95  result[i] = selectedNumbers[index];


96  index++;


97  } else {


98  result[i] = parent2[i];


99  }


100  }


101  return new Permutation(parent1.PermutationType, result);


102  }


103 


104  /// <summary>


105  /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.


106  /// </summary>


107  /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>


108  /// <param name="random">A random number generator.</param>


109  /// <param name="parents">An array containing the two permutations that should be crossed.</param>


110  /// <returns>The new permutation resulting from the crossover.</returns>


111  protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {


112  if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover: Number of parents is not equal to 2.");


113  return Apply(random, parents[0], parents[1]);


114  }


115  }


116  }

