Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/OrderCrossover2.cs @ 4656

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

Sorted usings and removed unused usings in entire solution (#1094)

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  public class OrderCrossover2 : PermutationCrossover {
38    /// <summary>
39    /// Performs a slight variation of the order crossover of two permutations.
40    /// </summary>
41    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
42    /// <remarks>
43    /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
44    /// the positions. Then, from the beginning of the permutation, copies the missing values from the second permutation
45    /// in the order they occur.
46    /// </remarks>
47    /// <param name="random">A random number generator.</param>
48    /// <param name="parent1">The first parent permutation to cross.</param>
49    /// <param name="parent2">The second parent permutation to cross.</param>
50    /// <returns>The new permutation resulting from the crossover.</returns>
51    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
52      if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover2: The parent permutations are of unequal length.");
53      int[] result = new int[parent1.Length];
54      bool[] copied = new bool[result.Length];
55
56      int breakPoint1 = random.Next(result.Length - 1);
57      int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);
58
59      for (int i = breakPoint1; i <= breakPoint2; i++) {  // copy part of first permutation
60        result[i] = parent1[i];
61        copied[parent1[i]] = true;
62      }
63
64      int index = 0;
65      for (int i = 0; i < parent2.Length; i++) {  // copy remaining part of second permutation
66        if (index == breakPoint1) {  // skip already copied part
67          index = breakPoint2 + 1;
68        }
69        if (!copied[parent2[i]]) {
70          result[index] = parent2[i];
71          index++;
72        }
73      }
74      return new Permutation(parent1.PermutationType, result);
75    }
76
77    /// <summary>
78    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
79    /// </summary>
80    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
81    /// <param name="random">A random number generator.</param>
82    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
83    /// <returns>The new permutation resulting from the crossover.</returns>
84    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
85      if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover2: Number of parents is not equal to 2.");
86      return Apply(random, parents[0], parents[1]);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.