Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/CosaCrossover.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.6 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2854]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;
[2854]24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]25
[3053]26namespace HeuristicLab.Encodings.PermutationEncoding {
[850]27  /// <summary>
[2879]28  /// Performs the crossover described in the COSA optimization method.
[850]29  /// </summary>
[2879]30  /// <remarks>
31  /// It is implemented as described in Wendt, O. 1994. COSA: COoperative Simulated Annealing - Integration von Genetischen Algorithmen und Simulated Annealing am Beispiel der Tourenplanung. Dissertation Thesis. IWI Frankfurt.<br />
32  /// The operator actually performs a 2-opt mutation on the first parent, but it uses the second parent to determine which new edge should be inserted.
33  /// Thus the mutation is not random as the second breakpoint depends on the information that is encoded in other members of the population.
34  /// The idea is that the child should not sit right inbetween the two parents, but rather go a little bit from one parent in direction to the other.
[2856]35  /// </remarks>
[2879]36  [Item("CosaCrossover", "An operator which performs the crossover described in the COSA optimization method. It is implemented as described in Wendt, O. 1994. COSA: COoperative Simulated Annealing - Integration von Genetischen Algorithmen und Simulated Annealing am Beispiel der Tourenplanung. Dissertation Thesis. IWI Frankfurt.")]
[3017]37  [StorableClass]
[2854]38  public class CosaCrossover : PermutationCrossover {
[850]39    /// <summary>
[2879]40    /// The operator actually performs a 2-opt mutation on the first parent, but it uses the second parent to determine which new edge should be inserted.
41    /// Thus the mutation is not random as the second breakpoint depends on the information that is encoded in other members of the population.
42    /// The idea is that the child should not sit right inbetween the two parents, but rather go a little bit from one parent in direction to the other.
[850]43    /// </summary>
[2854]44    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
[850]45    /// <param name="random">The random number generator.</param>
46    /// <param name="parent1">The parent scope 1 to cross over.</param>
47    /// <param name="parent2">The parent scope 2 to cross over.</param>
48    /// <returns>The created cross over permutation as int array.</returns>
[2854]49    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
50      if (parent1.Length != parent2.Length) throw new ArgumentException("CosaCrossover: The parent permutations are of unequal length.");
[2]51      int length = parent1.Length;
52      int[] result = new int[length];
53      int crossPoint, startIndex, endIndex;
54
55      crossPoint = random.Next(length);
56      startIndex = (crossPoint + 1) % length;
57
58      int i = 0;
[2879]59      while ((i < parent2.Length) && (parent2[i] != parent1[crossPoint])) {  // find index of cross point in second permutation
[2]60        i++;
61      }
[2879]62      int newEdge = parent2[(i + 1) % length]; // the number that follows the cross point number in parent2 is the new edge that we want to insert
63      endIndex = 0;
64      while ((endIndex < parent1.Length) && (parent1[endIndex] != newEdge)) {  // find index of the new edge in the first permutation
65        endIndex++;
[2]66      }
67
[2879]68      if (startIndex <= endIndex) {
69        // copy parent1 to child and reverse the order in between startIndex and endIndex
70        for (i = 0; i < parent1.Length; i++) {
71          if (i >= startIndex && i <= endIndex) {
72            result[i] = parent1[endIndex - i + startIndex];
73          } else {
74            result[i] = parent1[i];
75          }
76        }
77      } else { // startIndex > endIndex
78        for (i = 0; i < parent1.Length; i++) {
79          if (i >= startIndex || i <= endIndex) {
80            result[i] = parent1[(endIndex - i + startIndex + length) % length]; // add length to wrap around when dropping below index 0
81          } else {
82            result[i] = parent1[i];
83          }
84        }
[2]85      }
[3231]86      return new Permutation(parent1.PermutationType, result);
[2]87    }
88
[850]89    /// <summary>
[2871]90    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
[850]91    /// </summary>
[1218]92    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
93    /// <param name="random">A random number generator.</param>
94    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
95    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
[2854]96    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
[2871]97      if (parents.Length != 2) throw new InvalidOperationException("CosaCrossover: The number of parents is not equal to 2");
[1218]98      return Apply(random, parents[0], parents[1]);
[2]99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.