Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/CyclicCrossover.cs @ 15762

Last change on this file since 15762 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 6.3 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[4722]23using HeuristicLab.Common;
[2]24using HeuristicLab.Core;
[14927]25using HeuristicLab.Persistence;
[2]26
[3053]27namespace HeuristicLab.Encodings.PermutationEncoding {
[850]28  /// <summary>
[2829]29  /// An operator which performs the cyclic crossover on two permutations.
[850]30  /// </summary>
[2829]31  /// <remarks>It is implemented as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg.<br />
32  /// The operator first determines all cycles in the permutation and then composes the offspring by alternating between the cycles of the two parents.
[850]33  /// </remarks>
[2871]34  [Item("CyclicCrossover", "An operator which performs the cyclic crossover on two permutations. It is implemented as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg.")]
[14927]35  [StorableType("921f2ddb-2dec-46d8-a2ed-e2133f2d31a4")]
[2829]36  public class CyclicCrossover : PermutationCrossover {
[4722]37    [StorableConstructor]
[15018]38    protected CyclicCrossover(StorableConstructorFlag deserializing) : base(deserializing) { }
[4722]39    protected CyclicCrossover(CyclicCrossover original, Cloner cloner) : base(original, cloner) { }
40    public CyclicCrossover() : base() { }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new CyclicCrossover(this, cloner);
44    }
45
[850]46    /// <summary>
[2829]47    /// Performs the cyclic crossover on <paramref name="parent1"/> and <paramref name="parent2"/>.
[850]48    /// </summary>
[2829]49    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
[2835]50    /// <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>
[2829]51    /// <remarks>
52    /// First this method randomly determines from which parent to start with equal probability.
53    /// Then it copies the first cycle from the chosen parent starting from index 0 in the permutation.
54    /// After the cycle is complete it copies the next cycle starting from the index closest to 0 which was not yet copied from the other parent.
55    /// It continues to switch between parents for each completed cycle until no new cycle is found anymore.<br /><br />
56    /// The stochasticity of this operator is rather low. There are only two possible outcomes for a given pair of parents.
57    /// </remarks>
[2854]58    /// <exception cref="ArgumentException">Thrown if the two parents are not of equal length.</exception>
[850]59    /// <param name="random">The random number generator.</param>
60    /// <param name="parent1">The parent scope 1 to cross over.</param>
61    /// <param name="parent2">The parent scope 2 to cross over.</param>
62    /// <returns>The created cross over permutation as int array.</returns>
[2829]63    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
[2835]64      if (parent1.Length != parent2.Length) throw new ArgumentException("CyclicCrossover: The parent permutations are of unequal length.");
[2]65      int length = parent1.Length;
[2830]66      int[] result = new int[length];
[2]67      bool[] indexCopied = new bool[length];
[2829]68      int[] invParent1 = new int[length];
69      int[] invParent2 = new int[length];
[2]70
[2829]71      // calculate inverse mappings (number -> index) for parent1 and parent2
72      try {
73        for (int i = 0; i < length; i++) {
74          invParent1[parent1[i]] = i;
75          invParent2[parent2[i]] = i;
[2]76        }
[14927]77      } catch (IndexOutOfRangeException) {
[2835]78        throw new InvalidOperationException("CyclicCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation.");
[2]79      }
80
[2829]81      // randomly choose whether to start copying from parent1 or parent2
82      bool copyFromParent1 = ((random.NextDouble() < 0.5) ? (false) : (true));
83
84      int j = 0;
85      do {
86        do {
87          if (copyFromParent1) {
88            result[j] = parent1[j]; // copy number at position j from parent1
[2854]89            indexCopied[j] = true;
[2829]90            j = invParent2[result[j]]; // set position j to the position of the copied number in parent2
91          } else {
92            result[j] = parent2[j]; // copy number at position j from parent2
[2854]93            indexCopied[j] = true;
[2829]94            j = invParent1[result[j]]; // set position j to the position of the copied number in parent1
95          }
96        } while (!indexCopied[j]);
97        copyFromParent1 = !copyFromParent1;
98        j = 0;
99        while (j < length && indexCopied[j]) j++;
100      } while (j < length);
101
[3231]102      return new Permutation(parent1.PermutationType, result);
[2]103    }
104
[850]105    /// <summary>
[2829]106    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
[850]107    /// </summary>
[1218]108    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
109    /// <param name="random">A random number generator.</param>
110    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
111    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
[2829]112    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
[2835]113      if (parents.Length != 2) throw new InvalidOperationException("CyclicCrossover: The number of parents is not equal to 2.");
[1218]114      return Apply(random, parents[0], parents[1]);
[2]115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.