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