Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/CyclicCrossover2.cs @ 4047

Last change on this file since 4047 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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.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    /// <summary>
39    /// Performs a variant of the cyclic crossover on <paramref name="parent1"/> and <paramref name="parent2"/>.
40    /// </summary>
41    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
42    /// <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>
43    /// <remarks>
44    /// Start at a randomly chosen position x in parent1 and transfer it to the child at the same position.
45    /// Now this position x is no longer available for the node on position x in parent2, so
46    /// the value of the node at position x in parent2 is searched in parent1 and is then transferred
47    /// to the child preserving the position. Now this new position y is no longer available for the node in parent2 and so on.<br/>
48    /// This procedure is repeated until it is again at position x, then the cycle is over.<br/>
49    /// All further positions are copied from the second permutation.
50    /// </remarks>
51    /// <exception cref="ArgumentException">Thrown if the two parents are not of equal length.</exception>
52    /// <param name="random">The random number generator.</param>
53    /// <param name="parent1">The parent scope 1 to cross over.</param>
54    /// <param name="parent2">The parent scope 2 to cross over.</param>
55    /// <returns>The created cross over permutation as int array.</returns>
56    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
57      if (parent1.Length != parent2.Length) throw new ArgumentException("CyclicCrossover2: The parent permutations are of unequal length.");
58      int length = parent1.Length;
59      int[] result = new int[length];
60      bool[] indexCopied = new bool[length];
61      int j, number;
62
63      j = random.Next(length);  // get number to start cycle
64      while (!indexCopied[j]) {  // copy whole cycle to new permutation
65        result[j] = parent1[j];
66        number = parent2[j];
67        indexCopied[j] = true;
68
69        j = 0;
70        while ((j < length) && (parent1[j] != number)) {  // search copied number in second permutation
71          j++;
72        }
73      }
74
75      for (int i = 0; i < length; i++) {  // copy rest of secound permutation to new permutation
76        if (!indexCopied[i]) {
77          result[i] = parent2[i];
78        }
79      }
80      return new Permutation(parent1.PermutationType, result);
81    }
82
83    /// <summary>
84    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
85    /// </summary>
86    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
87    /// <param name="random">A random number generator.</param>
88    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
89    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
90    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
91      if (parents.Length != 2) throw new InvalidOperationException("CyclicCrossover2: The number of parents is not equal to 2.");
92      return Apply(random, parents[0], parents[1]);
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.