Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/EdgeRecombinationCrossover.cs @ 4656

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

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

File size: 7.4 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 a cross over permutation between two permutation arrays by calculating the edges (neighbours)
29  /// of each element. Starts at a randomly chosen position, the next element is a neighbour with the least
30  /// number of neighbours, the next again a neighbour and so on.
31  /// </summary>
32  /// <remarks>
33  /// It is implemented as described in Whitley et.al. 1991, The Traveling Salesman and Sequence Scheduling, in Davis, L. (Ed.), Handbook of Genetic Algorithms, New York, pp. 350-372.<br />
34  /// The operator first determines all cycles in the permutation and then composes the offspring by alternating between the cycles of the two parents.
35  /// </remarks>
36  [Item("EdgeRecombinationCrossover", "An operator which performs the edge recombination crossover on two permutations. It is implemented as described in Whitley et.al. 1991, The Traveling Salesman and Sequence Scheduling, in Davis, L. (Ed.), Handbook of Genetic Algorithms, New York, pp. 350-372.")]
37  [StorableClass]
38  public class EdgeRecombinationCrossover : PermutationCrossover {
39    /// <summary>
40    /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="2"/>
41    /// by calculating the edges of each element. Starts at a randomly chosen position,
42    /// the next element is a neighbour with the least
43    /// number of neighbours, the next again a neighbour and so on.
44    /// </summary>
45    /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
46    /// <exception cref="InvalidOperationException">Thrown when the permutation lacks a number.
47    /// </exception>
48    /// <param name="random">The random number generator.</param>
49    /// <param name="parent1">The parent scope 1 to cross over.</param>
50    /// <param name="parent2">The parent scope 2 to cross over.</param>
51    /// <returns>The created cross over permutation as int array.</returns>
52    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
53      if (parent1.Length != parent2.Length) throw new ArgumentException("EdgeRecombinationCrossover: The parent permutations are of unequal length.");
54      int length = parent1.Length;
55      int[] result = new int[length];
56      int[,] edgeList = new int[length, 4];
57      bool[] remainingNumbers = new bool[length];
58      int index, currentEdge, currentNumber, nextNumber, currentEdgeCount, minEdgeCount;
59
60      for (int i = 0; i < length; i++) {  // generate edge list for every number
61        remainingNumbers[i] = true;
62
63        index = 0;
64        while ((index < length) && (parent1[index] != i)) {  // search edges in parent1
65          index++;
66        }
67        if (index == length) {
68          throw (new InvalidOperationException("Permutation doesn't contain number " + i + "."));
69        } else {
70          edgeList[i, 0] = parent1[(index - 1 + length) % length];
71          edgeList[i, 1] = parent1[(index + 1) % length];
72        }
73        index = 0;
74        while ((index < length) && (parent2[index] != i)) {  // search edges in parent2
75          index++;
76        }
77        if (index == length) {
78          throw (new InvalidOperationException("Permutation doesn't contain number " + i + "."));
79        } else {
80          currentEdge = parent2[(index - 1 + length) % length];
81          if ((edgeList[i, 0] != currentEdge) && (edgeList[i, 1] != currentEdge)) {  // new edge found ?
82            edgeList[i, 2] = currentEdge;
83          } else {
84            edgeList[i, 2] = -1;
85          }
86          currentEdge = parent2[(index + 1) % length];
87          if ((edgeList[i, 0] != currentEdge) && (edgeList[i, 1] != currentEdge)) {  // new edge found ?
88            edgeList[i, 3] = currentEdge;
89          } else {
90            edgeList[i, 3] = -1;
91          }
92        }
93      }
94
95      currentNumber = random.Next(length);  // get number to start
96      for (int i = 0; i < length; i++) {
97        result[i] = currentNumber;
98        remainingNumbers[currentNumber] = false;
99
100        for (int j = 0; j < 4; j++) {  // remove all edges to / from currentNumber
101          if (edgeList[currentNumber, j] != -1) {
102            for (int k = 0; k < 4; k++) {
103              if (edgeList[edgeList[currentNumber, j], k] == currentNumber) {
104                edgeList[edgeList[currentNumber, j], k] = -1;
105              }
106            }
107          }
108        }
109
110        minEdgeCount = 5;  // every number hasn't more than 4 edges
111        nextNumber = -1;
112        for (int j = 0; j < 4; j++) {  // find next number with least edges
113          if (edgeList[currentNumber, j] != -1) {  // next number found
114            currentEdgeCount = 0;
115            for (int k = 0; k < 4; k++) {  // count edges of next number
116              if (edgeList[edgeList[currentNumber, j], k] != -1) {
117                currentEdgeCount++;
118              }
119            }
120            if ((currentEdgeCount < minEdgeCount) ||
121              ((currentEdgeCount == minEdgeCount) && (random.NextDouble() < 0.5))) {
122              nextNumber = edgeList[currentNumber, j];
123              minEdgeCount = currentEdgeCount;
124            }
125          }
126        }
127        currentNumber = nextNumber;
128        if (currentNumber == -1) {  // current number has no more edge
129          index = 0;
130          while ((index < length) && (!remainingNumbers[index])) {  // choose next remaining number
131            index++;
132          }
133          if (index < length) {
134            currentNumber = index;
135          }
136        }
137      }
138      return new Permutation(parent1.PermutationType, result);
139    }
140
141    /// <summary>
142    /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
143    /// </summary>
144    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
145    /// <param name="random">A random number generator.</param>
146    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
147    /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
148    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
149      if (parents.Length != 2) throw new InvalidOperationException("ERROR in EdgeRecombinationCrossover: The number of parents is not equal to 2");
150      return Apply(random, parents[0], parents[1]);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.