Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/OrderCrossover2.cs @ 14186

Last change on this file since 14186 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.PermutationEncoding {
28  /// <summary>
29  /// An operator which performs a slight variant of the order crossover on two permutations.
30  /// </summary>
31  /// <remarks>
32  /// 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 />
33  /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
34  /// 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.
35  /// </remarks>
36  [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.")]
37  [StorableClass]
38  public class OrderCrossover2 : PermutationCrossover {
39    [StorableConstructor]
40    protected OrderCrossover2(bool deserializing) : base(deserializing) { }
41    protected OrderCrossover2(OrderCrossover2 original, Cloner cloner) : base(original, cloner) { }
42    public OrderCrossover2() : base() { }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new OrderCrossover2(this, cloner);
46    }
47
48    /// <summary>
49    /// Performs a slight variation of the order crossover of two permutations.
50    /// </summary>
51    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
52    /// <remarks>
53    /// Crosses two permutations by copying a randomly chosen interval from the first permutation, preserving
54    /// the positions. Then, from the beginning of the permutation, copies the missing values from the second permutation
55    /// in the order they occur.
56    /// </remarks>
57    /// <param name="random">A random number generator.</param>
58    /// <param name="parent1">The first parent permutation to cross.</param>
59    /// <param name="parent2">The second parent permutation to cross.</param>
60    /// <returns>The new permutation resulting from the crossover.</returns>
61    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
62      if (parent1.Length != parent2.Length) throw new ArgumentException("OrderCrossover2: The parent permutations are of unequal length.");
63      int[] result = new int[parent1.Length];
64      bool[] copied = new bool[result.Length];
65
66      int breakPoint1 = random.Next(result.Length - 1);
67      int breakPoint2 = random.Next(breakPoint1 + 1, result.Length);
68
69      for (int i = breakPoint1; i <= breakPoint2; i++) {  // copy part of first permutation
70        result[i] = parent1[i];
71        copied[parent1[i]] = true;
72      }
73
74      int index = 0;
75      for (int i = 0; i < parent2.Length; i++) {  // copy remaining part of second permutation
76        if (index == breakPoint1) {  // skip already copied part
77          index = breakPoint2 + 1;
78        }
79        if (!copied[parent2[i]]) {
80          result[index] = parent2[i];
81          index++;
82        }
83      }
84      return new Permutation(parent1.PermutationType, result);
85    }
86
87    /// <summary>
88    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
89    /// </summary>
90    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
91    /// <param name="random">A random number generator.</param>
92    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
93    /// <returns>The new permutation resulting from the crossover.</returns>
94    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
95      if (parents.Length != 2) throw new InvalidOperationException("OrderCrossover2: Number of parents is not equal to 2.");
96      return Apply(random, parents[0], parents[1]);
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.