Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/CyclicCrossover2.cs @ 13337

Last change on this file since 13337 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 variant of the cyclic crossover on two permutations.
30  /// </summary>
31  /// <remarks>It is implemented as described in Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. p. 136.<br />
32  /// The operator first selects a random position in the permutation and from that position copies the first cycle to the offspring.
33  /// Then all empty positions are filled from the second parent.
34  /// </remarks>
35  [Item("CyclicCrossover2", "An operator which performs the cyclic crossover on 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. 136.")]
36  [StorableClass]
37  public class CyclicCrossover2 : PermutationCrossover {
38    [StorableConstructor]
39    protected CyclicCrossover2(bool deserializing) : base(deserializing) { }
40    protected CyclicCrossover2(CyclicCrossover2 original, Cloner cloner) : base(original, cloner) { }
41    public CyclicCrossover2() : base() { }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new CyclicCrossover2(this, cloner);
45    }
46
47    /// <summary>
48    /// Performs a variant of the cyclic crossover on <paramref name="parent1"/> and <paramref name="parent2"/>.
49    /// </summary>
50    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
51    /// <exception cref="InvalidOperationException">Thrown if the numbers in the permutation elements are not in the range [0;N) with N = length of the permutation.</exception>
52    /// <remarks>
53    /// Start at a randomly chosen position x in parent1 and transfer it to the child at the same position.
54    /// Now this position x is no longer available for the node on position x in parent2, so
55    /// the value of the node at position x in parent2 is searched in parent1 and is then transferred
56    /// to the child preserving the position. Now this new position y is no longer available for the node in parent2 and so on.<br/>
57    /// This procedure is repeated until it is again at position x, then the cycle is over.<br/>
58    /// All further positions are copied from the second permutation.
59    /// </remarks>
60    /// <exception cref="ArgumentException">Thrown if the two parents are not of equal length.</exception>
61    /// <param name="random">The random number generator.</param>
62    /// <param name="parent1">The parent scope 1 to cross over.</param>
63    /// <param name="parent2">The parent scope 2 to cross over.</param>
64    /// <returns>The created cross over permutation as int array.</returns>
65    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
66      if (parent1.Length != parent2.Length) throw new ArgumentException("CyclicCrossover2: The parent permutations are of unequal length.");
67      int length = parent1.Length;
68      int[] result = new int[length];
69      bool[] indexCopied = new bool[length];
70      int j, number;
71
72      j = random.Next(length);  // get number to start cycle
73      while (!indexCopied[j]) {  // copy whole cycle to new permutation
74        result[j] = parent1[j];
75        number = parent2[j];
76        indexCopied[j] = true;
77
78        j = 0;
79        while ((j < length) && (parent1[j] != number)) {  // search copied number in second permutation
80          j++;
81        }
82      }
83
84      for (int i = 0; i < length; i++) {  // copy rest of secound permutation to new permutation
85        if (!indexCopied[i]) {
86          result[i] = parent2[i];
87        }
88      }
89      return new Permutation(parent1.PermutationType, result);
90    }
91
92    /// <summary>
93    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
94    /// </summary>
95    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
96    /// <param name="random">A random number generator.</param>
97    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
98    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
99    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
100      if (parents.Length != 2) throw new InvalidOperationException("CyclicCrossover2: The number of parents is not equal to 2.");
101      return Apply(random, parents[0], parents[1]);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.