Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/OrderBasedCrossover.cs @ 3053

Last change on this file since 3053 was 3053, checked in by swagner, 14 years ago

Renamed solution encoding plugins (#909)

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Encodings.PermutationEncoding {
27  /// <summary>
28  /// Performs a cross over permutation of two permutation arrays by taking randomly a selection of values
29  /// (not an interval!) from the first permutation keeping the correct order and filling
30  /// the missing entries with the elements from the second permutation, also in the right order.
31  /// </summary>
32  /// <remarks>
33  /// This is in some papers also called Order Crossover #2.<br />
34  /// 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. 332-349.
35  /// </remarks>
36  [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. 332-349.")]
37  [StorableClass]
38  [Creatable("Test")]
39  public class OrderBasedCrossover : PermutationCrossover {
40    /// <summary>
41    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> by
42    /// randomly selecting some values from the first permutation that will be inserted one after each
43    /// other; the missing ones are picked in the correct order from the second permutation.
44    /// </summary>
45    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
46    /// <param name="random">A random number generator.</param>
47    /// <param name="parent1">The first parent permutation to cross.</param>
48    /// <param name="parent2">The second parent permutation to cross.</param>
49    /// <returns>The new permutation resulting from the crossover.</returns>
50    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
51      if (parent1.Length != parent2.Length) throw new ArgumentException("OrderBasedCrossover: The parent permutations are of unequal length.");
52      int length = parent1.Length;
53      int[] result = new int[length];
54      int[] selectedNumbers = new int[random.Next(length + 1)];
55      bool[] numberSelected = new bool[length];
56      int index, selectedIndex, currentIndex;
57
58      for (int i = 0; i < selectedNumbers.Length; i++) {  // select numbers for array
59        index = 0;
60        while (numberSelected[parent1[index]]) {  // find first valid index
61          index++;
62        }
63
64        selectedIndex = random.Next(length - i);
65        currentIndex = 0;
66        while ((index < parent1.Length) && (currentIndex != selectedIndex)) {  // find selected number
67          index++;
68          if (!numberSelected[parent1[index]]) {
69            currentIndex++;
70          }
71        }
72        numberSelected[parent1[index]] = true;
73      }
74
75      index = 0;
76      for (int i = 0; i < parent1.Length; i++) {  // copy selected numbers in array
77        if (numberSelected[parent1[i]]) {
78          selectedNumbers[index] = parent1[i];
79          index++;
80        }
81      }
82
83      index = 0;
84      for (int i = 0; i < result.Length; i++) {  // copy rest of second permutation and selected numbers in order of first permutation
85        if (numberSelected[parent2[i]]) {
86          result[i] = selectedNumbers[index];
87          index++;
88        } else {
89          result[i] = parent2[i];
90        }
91      }
92      return new Permutation(result);
93    }
94
95    /// <summary>
96    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
97    /// </summary>
98    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
99    /// <param name="random">A random number generator.</param>
100    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
101    /// <returns>The new permutation resulting from the crossover.</returns>
102    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
103      if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover: Number of parents is not equal to 2.");
104      return Apply(random, parents[0], parents[1]);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.