Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/PartiallyMatchedCrossover.cs @ 4501

Last change on this file since 4501 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 6.1 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2820]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
22using System;
23using HeuristicLab.Core;
[2820]24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]25
[3053]26namespace HeuristicLab.Encodings.PermutationEncoding {
[850]27  /// <summary>
[2820]28  /// An operator which performs the partially matched crossover on two permutations.
29  /// </summar>
30  /// <remarks>
[2871]31  /// It is implemented as described in Fogel, D.B. 1988. An Evolutionary Approach to the Traveling Salesman Problem. Biological Cybernetics, 60, pp. 139-144, Springer-Verlag.
[2820]32  /// which references Goldberg, D.E., and Lingle, R. 1985. Alleles, loci, and the traveling salesman problem. Proceedings of an International Conference on Genetic Algorithms and their Applications. Carnegie-Mellon University, pp. 154-159.
33  /// as the original source of the operator.
34  /// </remarks>
[2871]35  [Item("PartiallyMatchedCrossover", "An operator which performs the partially matched crossover on two permutations. It is implemented as described in Fogel, D.B. 1988. An Evolutionary Approach to the Traveling Salesman Problem. Biological Cybernetics, 60, pp. 139-144, Springer-Verlag.")]
[3017]36  [StorableClass]
[2820]37  public class PartiallyMatchedCrossover : PermutationCrossover {
[850]38    /// <summary>
[2820]39    /// Performs the partially matched crossover on <paramref name="parent1"/> and <paramref name="parent2"/>.
[850]40    /// </summary>
[2823]41    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length or when the permutations are shorter than 4 elements.</exception>
[2829]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>
[2820]43    /// <remarks>
[2829]44    /// Initially the new offspring is a clone of <paramref name="parent2"/>.
45    /// Then a segment is extracted from <paramref name="parent1"/> and copied to the offspring position by position.
46    /// Whenever a position is copied, the number at that position currently in the offspring is transfered to the position where the copied number has been.
47    /// E.g.: Position 15 is selected to be copied from <paramref name="parent1"/> to <paramref name="parent2"/>. At position 15 there is a '3' in <paramref name="parent1"/> and a '5' in the new offspring.
48    /// The '5' in the offspring is then moved to replace the '3' in the offspring and '3' is written at position 15.
[2820]49    /// </remarks>
50    /// <param name="random">A random number generator.</param>
51    /// <param name="parent1">The first parent permutation to cross.</param>
52    /// <param name="parent2">The second parent permutation to cross.</param>
53    /// <returns>The new permutation resulting from the crossover.</returns>
54    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
[2835]55      if (parent1.Length != parent2.Length) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutations are of unequal length.");
56      if (parent1.Length < 4) throw new ArgumentException("PartiallyMatchedCrossover: The parent permutation must be at least of size 4.");
[2]57      int length = parent1.Length;
[2830]58      int[] result = new int[length];
[2823]59      int[] invResult = new int[length];
[2]60
[2823]61      int breakPoint1, breakPoint2;
62      do {
63        breakPoint1 = random.Next(length - 1);
64        breakPoint2 = random.Next(breakPoint1 + 1, length);
65      } while (breakPoint2 - breakPoint1 >= length - 2); // prevent the case [0,length-1) -> clone of parent1
[2]66
[2823]67      // clone parent2 and calculate inverse permutation (number -> index)
[2829]68      try {
69        for (int j = 0; j < length; j++) {
70          result[j] = parent2[j];
71          invResult[result[j]] = j;
72        }
[4068]73      }
74      catch (IndexOutOfRangeException) {
[2835]75        throw new InvalidOperationException("PartiallyMatchedCrossover: The permutation must consist of consecutive numbers from 0 to N-1 with N = length of the permutation.");
[2]76      }
77
[2823]78      for (int j = breakPoint1; j <= breakPoint2; j++) {
79        int orig = result[j]; // save the former value
80        result[j] = parent1[j]; // overwrite the former value with the new value
[2829]81        int index = invResult[result[j]]; // look where the new value is in the offspring
[2823]82        result[index] = orig; // write the former value to this position
83        invResult[orig] = index; // update the inverse mapping
84        // it's not necessary to do 'invResult[result[j]] = j' as this will not be needed again
[2820]85      }
86
[3231]87      return new Permutation(parent1.PermutationType, result);
[2]88    }
89
[850]90    /// <summary>
[2829]91    /// Checks number of parents and calls <see cref="Apply(Apply(IRandom, Permutation, Permutation)"/>.
[850]92    /// </summary>
[2820]93    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two permutations in <paramref name="parents"/>.</exception>
[1218]94    /// <param name="random">A random number generator.</param>
95    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
96    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
[2820]97    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
[2835]98      if (parents.Length != 2) throw new InvalidOperationException("PartiallyMatchedCrossover: The number of parents is not equal to 2.");
[1218]99      return Apply(random, parents[0], parents[1]);
[2]100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.