Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/OrderCrossover2.cs @ 3065

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

Renamed solution encoding plugins (#909)

File size: 4.6 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  /// An operator which performs a slight variant of the order crossover on two permutations.
29  /// </summary>
30  /// <remarks>
31  /// It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. p. 135.<br />
32  /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
33  /// the positions. But then, instead of starting from the end of the copied interval, the missing values are copied from the start of the permutation in the order they occur.
34  /// </remarks>
35  [Item("OrderCrossover2", "An operator which performs an order crossover of two permutations. It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. p. 135.")]
36  [StorableClass]
37  [Creatable("Test")]
38  public class OrderCrossover2 : PermutationCrossover {
39    /// <summary>
40    /// Performs a slight variation of the order crossover of two permutations.
41    /// </summary>
42    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
43    /// <remarks>
44    /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
45    /// the positions. Then, from the beginning of the permutation, copies the missing values from the second permutation
46    /// in the order they occur.
47    /// </remarks>
48    /// <param name="random">A random number generator.</param>
49    /// <param name="parent1">The first parent permutation to cross.</param>
50    /// <param name="parent2">The second parent permutation to cross.</param>
51    /// <returns>The new permutation resulting from the crossover.</returns>
52    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
53      if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover2: The parent permutations are of unequal length.");
54      int[] result = new int[parent1.Length];
55      bool[] copied = new bool[result.Length];
56
57      int breakPoint1 = random.Next(result.Length - 1);
58      int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);
59
60      for (int i = breakPoint1; i <= breakPoint2; i++) {  // copy part of first permutation
61        result[i] = parent1[i];
62        copied[parent1[i]] = true;
63      }
64
65      int index = 0;
66      for (int i = 0; i < parent2.Length; i++) {  // copy remaining part of second permutation
67        if (index == breakPoint1) {  // skip already copied part
68          index = breakPoint2 + 1;
69        }
70        if (!copied[parent2[i]]) {
71          result[index] = parent2[i];
72          index++;
73        }
74      }
75      return new Permutation(result);
76    }
77
78    /// <summary>
79    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
80    /// </summary>
81    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
82    /// <param name="random">A random number generator.</param>
83    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
84    /// <returns>The new permutation resulting from the crossover.</returns>
85    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
86      if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover2: Number of parents is not equal to 2.");
87      return Apply(random, parents[0], parents[1]);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.