Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed Creatable test attribute (#935).

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  public class OrderBasedCrossover : PermutationCrossover {
39    /// <summary>
40    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> by
41    /// randomly selecting some values from the first permutation that will be inserted one after each
42    /// other; the missing ones are picked in the correct order from the second permutation.
43    /// </summary>
44    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
45    /// <param name="random">A random number generator.</param>
46    /// <param name="parent1">The first parent permutation to cross.</param>
47    /// <param name="parent2">The second parent permutation to cross.</param>
48    /// <returns>The new permutation resulting from the crossover.</returns>
49    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
50      if (parent1.Length != parent2.Length) throw new ArgumentException("OrderBasedCrossover: The parent permutations are of unequal length.");
51      int length = parent1.Length;
52      int[] result = new int[length];
53      int[] selectedNumbers = new int[random.Next(length + 1)];
54      bool[] numberSelected = new bool[length];
55      int index, selectedIndex, currentIndex;
56
57      for (int i = 0; i < selectedNumbers.Length; i++) {  // select numbers for array
58        index = 0;
59        while (numberSelected[parent1[index]]) {  // find first valid index
60          index++;
61        }
62
63        selectedIndex = random.Next(length - i);
64        currentIndex = 0;
65        while ((index < parent1.Length) && (currentIndex != selectedIndex)) {  // find selected number
66          index++;
67          if (!numberSelected[parent1[index]]) {
68            currentIndex++;
69          }
70        }
71        numberSelected[parent1[index]] = true;
72      }
73
74      index = 0;
75      for (int i = 0; i < parent1.Length; i++) {  // copy selected numbers in array
76        if (numberSelected[parent1[i]]) {
77          selectedNumbers[index] = parent1[i];
78          index++;
79        }
80      }
81
82      index = 0;
83      for (int i = 0; i < result.Length; i++) {  // copy rest of second permutation and selected numbers in order of first permutation
84        if (numberSelected[parent2[i]]) {
85          result[i] = selectedNumbers[index];
86          index++;
87        } else {
88          result[i] = parent2[i];
89        }
90      }
91      return new Permutation(result);
92    }
93
94    /// <summary>
95    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
96    /// </summary>
97    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
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 new permutation resulting from the crossover.</returns>
101    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
102      if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover: Number of parents is not equal to 2.");
103      return Apply(random, parents[0], parents[1]);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.