Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3337 was 3231, checked in by abeham, 15 years ago

Added a permutation type property specifying whether it's a relative (directed or undirected) or absolute permutation #889

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