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