Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/CosaCrossover.cs @ 3053

Last change on this file since 3053 was 3053, checked in by swagner, 14 years ago

Renamed solution encoding plugins (#909)

File size: 5.6 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  /// Performs the crossover described in the COSA optimization method.
29  /// </summary>
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.
35  /// </remarks>
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.")]
37  [StorableClass]
38  [Creatable("Test")]
39  public class CosaCrossover : PermutationCrossover {
40    /// <summary>
41    /// 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.
42    /// Thus the mutation is not random as the second breakpoint depends on the information that is encoded in other members of the population.
43    /// 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.
44    /// </summary>
45    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
46    /// <param name="random">The random number generator.</param>
47    /// <param name="parent1">The parent scope 1 to cross over.</param>
48    /// <param name="parent2">The parent scope 2 to cross over.</param>
49    /// <returns>The created cross over permutation as int array.</returns>
50    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
51      if (parent1.Length != parent2.Length) throw new ArgumentException("CosaCrossover: The parent permutations are of unequal length.");
52      int length = parent1.Length;
53      int[] result = new int[length];
54      int crossPoint, startIndex, endIndex;
55
56      crossPoint = random.Next(length);
57      startIndex = (crossPoint + 1) % length;
58
59      int i = 0;
60      while ((i < parent2.Length) && (parent2[i] != parent1[crossPoint])) {  // find index of cross point in second permutation
61        i++;
62      }
63      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
64      endIndex = 0;
65      while ((endIndex < parent1.Length) && (parent1[endIndex] != newEdge)) {  // find index of the new edge in the first permutation
66        endIndex++;
67      }
68
69      if (startIndex <= endIndex) {
70        // copy parent1 to child and reverse the order in between startIndex and endIndex
71        for (i = 0; i < parent1.Length; i++) {
72          if (i >= startIndex && i <= endIndex) {
73            result[i] = parent1[endIndex - i + startIndex];
74          } else {
75            result[i] = parent1[i];
76          }
77        }
78      } else { // startIndex > endIndex
79        for (i = 0; i < parent1.Length; i++) {
80          if (i >= startIndex || i <= endIndex) {
81            result[i] = parent1[(endIndex - i + startIndex + length) % length]; // add length to wrap around when dropping below index 0
82          } else {
83            result[i] = parent1[i];
84          }
85        }
86      }
87      return new Permutation(result);
88    }
89
90    /// <summary>
91    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
92    /// </summary>
93    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
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>
97    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
98      if (parents.Length != 2) throw new InvalidOperationException("CosaCrossover: The number of parents is not equal to 2");
99      return Apply(random, parents[0], parents[1]);
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.