[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2829] | 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
[2829] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 25 |
|
---|
[3053] | 26 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[850] | 27 | /// <summary>
|
---|
[2829] | 28 | /// An operator which performs the cyclic crossover on two permutations.
|
---|
[850] | 29 | /// </summary>
|
---|
[2829] | 30 | /// <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 />
|
---|
| 31 | /// The operator first determines all cycles in the permutation and then composes the offspring by alternating between the cycles of the two parents.
|
---|
[850] | 32 | /// </remarks>
|
---|
[2871] | 33 | [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.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[2829] | 35 | public class CyclicCrossover : PermutationCrossover {
|
---|
[850] | 36 | /// <summary>
|
---|
[2829] | 37 | /// Performs the cyclic crossover on <paramref name="parent1"/> and <paramref name="parent2"/>.
|
---|
[850] | 38 | /// </summary>
|
---|
[2829] | 39 | /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
|
---|
[2835] | 40 | /// <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] | 41 | /// <remarks>
|
---|
| 42 | /// First this method randomly determines from which parent to start with equal probability.
|
---|
| 43 | /// Then it copies the first cycle from the chosen parent starting from index 0 in the permutation.
|
---|
| 44 | /// 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.
|
---|
| 45 | /// It continues to switch between parents for each completed cycle until no new cycle is found anymore.<br /><br />
|
---|
| 46 | /// The stochasticity of this operator is rather low. There are only two possible outcomes for a given pair of parents.
|
---|
| 47 | /// </remarks>
|
---|
[2854] | 48 | /// <exception cref="ArgumentException">Thrown if the two parents are not of equal length.</exception>
|
---|
[850] | 49 | /// <param name="random">The random number generator.</param>
|
---|
| 50 | /// <param name="parent1">The parent scope 1 to cross over.</param>
|
---|
| 51 | /// <param name="parent2">The parent scope 2 to cross over.</param>
|
---|
| 52 | /// <returns>The created cross over permutation as int array.</returns>
|
---|
[2829] | 53 | public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
|
---|
[2835] | 54 | if (parent1.Length != parent2.Length) throw new ArgumentException("CyclicCrossover: The parent permutations are of unequal length.");
|
---|
[2] | 55 | int length = parent1.Length;
|
---|
[2830] | 56 | int[] result = new int[length];
|
---|
[2] | 57 | bool[] indexCopied = new bool[length];
|
---|
[2829] | 58 | int[] invParent1 = new int[length];
|
---|
| 59 | int[] invParent2 = new int[length];
|
---|
[2] | 60 |
|
---|
[2829] | 61 | // calculate inverse mappings (number -> index) for parent1 and parent2
|
---|
| 62 | try {
|
---|
| 63 | for (int i = 0; i < length; i++) {
|
---|
| 64 | invParent1[parent1[i]] = i;
|
---|
| 65 | invParent2[parent2[i]] = i;
|
---|
[2] | 66 | }
|
---|
[4068] | 67 | }
|
---|
| 68 | catch (IndexOutOfRangeException) {
|
---|
[2835] | 69 | throw new InvalidOperationException("CyclicCrossover: The permutation must consist of numbers in the interval [0;N) with N = length of the permutation.");
|
---|
[2] | 70 | }
|
---|
| 71 |
|
---|
[2829] | 72 | // randomly choose whether to start copying from parent1 or parent2
|
---|
| 73 | bool copyFromParent1 = ((random.NextDouble() < 0.5) ? (false) : (true));
|
---|
| 74 |
|
---|
| 75 | int j = 0;
|
---|
| 76 | do {
|
---|
| 77 | do {
|
---|
| 78 | if (copyFromParent1) {
|
---|
| 79 | result[j] = parent1[j]; // copy number at position j from parent1
|
---|
[2854] | 80 | indexCopied[j] = true;
|
---|
[2829] | 81 | j = invParent2[result[j]]; // set position j to the position of the copied number in parent2
|
---|
| 82 | } else {
|
---|
| 83 | result[j] = parent2[j]; // copy number at position j from parent2
|
---|
[2854] | 84 | indexCopied[j] = true;
|
---|
[2829] | 85 | j = invParent1[result[j]]; // set position j to the position of the copied number in parent1
|
---|
| 86 | }
|
---|
| 87 | } while (!indexCopied[j]);
|
---|
| 88 | copyFromParent1 = !copyFromParent1;
|
---|
| 89 | j = 0;
|
---|
| 90 | while (j < length && indexCopied[j]) j++;
|
---|
| 91 | } while (j < length);
|
---|
| 92 |
|
---|
[3231] | 93 | return new Permutation(parent1.PermutationType, result);
|
---|
[2] | 94 | }
|
---|
| 95 |
|
---|
[850] | 96 | /// <summary>
|
---|
[2829] | 97 | /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
|
---|
[850] | 98 | /// </summary>
|
---|
[1218] | 99 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
| 100 | /// <param name="random">A random number generator.</param>
|
---|
| 101 | /// <param name="parents">An array containing the two permutations that should be crossed.</param>
|
---|
| 102 | /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
|
---|
[2829] | 103 | protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
|
---|
[2835] | 104 | if (parents.Length != 2) throw new InvalidOperationException("CyclicCrossover: The number of parents is not equal to 2.");
|
---|
[1218] | 105 | return Apply(random, parents[0], parents[1]);
|
---|
[2] | 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|