[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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>
|
---|
[2879] | 29 | /// Performs the crossover described in the COSA optimization method.
|
---|
[850] | 30 | /// </summary>
|
---|
[2879] | 31 | /// <remarks>
|
---|
| 32 | /// 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 />
|
---|
| 33 | /// 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.
|
---|
| 34 | /// Thus the mutation is not random as the second breakpoint depends on the information that is encoded in other members of the population.
|
---|
| 35 | /// 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] | 36 | /// </remarks>
|
---|
[2879] | 37 | [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] | 38 | [StorableClass]
|
---|
[2854] | 39 | public class CosaCrossover : PermutationCrossover {
|
---|
[4722] | 40 | [StorableConstructor]
|
---|
| 41 | protected CosaCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 42 | protected CosaCrossover(CosaCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 43 | public CosaCrossover() : base() { }
|
---|
| 44 |
|
---|
| 45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 46 | return new CosaCrossover(this, cloner);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[850] | 49 | /// <summary>
|
---|
[2879] | 50 | /// 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.
|
---|
| 51 | /// Thus the mutation is not random as the second breakpoint depends on the information that is encoded in other members of the population.
|
---|
| 52 | /// 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] | 53 | /// </summary>
|
---|
[2854] | 54 | /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
|
---|
[850] | 55 | /// <param name="random">The random number generator.</param>
|
---|
| 56 | /// <param name="parent1">The parent scope 1 to cross over.</param>
|
---|
| 57 | /// <param name="parent2">The parent scope 2 to cross over.</param>
|
---|
| 58 | /// <returns>The created cross over permutation as int array.</returns>
|
---|
[2854] | 59 | public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
|
---|
| 60 | if (parent1.Length != parent2.Length) throw new ArgumentException("CosaCrossover: The parent permutations are of unequal length.");
|
---|
[2] | 61 | int length = parent1.Length;
|
---|
| 62 | int[] result = new int[length];
|
---|
| 63 | int crossPoint, startIndex, endIndex;
|
---|
| 64 |
|
---|
| 65 | crossPoint = random.Next(length);
|
---|
| 66 | startIndex = (crossPoint + 1) % length;
|
---|
| 67 |
|
---|
| 68 | int i = 0;
|
---|
[2879] | 69 | while ((i < parent2.Length) && (parent2[i] != parent1[crossPoint])) { // find index of cross point in second permutation
|
---|
[2] | 70 | i++;
|
---|
| 71 | }
|
---|
[2879] | 72 | 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
|
---|
| 73 | endIndex = 0;
|
---|
| 74 | while ((endIndex < parent1.Length) && (parent1[endIndex] != newEdge)) { // find index of the new edge in the first permutation
|
---|
| 75 | endIndex++;
|
---|
[2] | 76 | }
|
---|
| 77 |
|
---|
[2879] | 78 | if (startIndex <= endIndex) {
|
---|
| 79 | // copy parent1 to child and reverse the order in between startIndex and endIndex
|
---|
| 80 | for (i = 0; i < parent1.Length; i++) {
|
---|
| 81 | if (i >= startIndex && i <= endIndex) {
|
---|
| 82 | result[i] = parent1[endIndex - i + startIndex];
|
---|
| 83 | } else {
|
---|
| 84 | result[i] = parent1[i];
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | } else { // startIndex > endIndex
|
---|
| 88 | for (i = 0; i < parent1.Length; i++) {
|
---|
| 89 | if (i >= startIndex || i <= endIndex) {
|
---|
| 90 | result[i] = parent1[(endIndex - i + startIndex + length) % length]; // add length to wrap around when dropping below index 0
|
---|
| 91 | } else {
|
---|
| 92 | result[i] = parent1[i];
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[2] | 95 | }
|
---|
[3231] | 96 | return new Permutation(parent1.PermutationType, result);
|
---|
[2] | 97 | }
|
---|
| 98 |
|
---|
[850] | 99 | /// <summary>
|
---|
[2871] | 100 | /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
|
---|
[850] | 101 | /// </summary>
|
---|
[1218] | 102 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
| 103 | /// <param name="random">A random number generator.</param>
|
---|
| 104 | /// <param name="parents">An array containing the two permutations that should be crossed.</param>
|
---|
| 105 | /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
|
---|
[2854] | 106 | protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
|
---|
[2871] | 107 | if (parents.Length != 2) throw new InvalidOperationException("CosaCrossover: The number of parents is not equal to 2");
|
---|
[1218] | 108 | return Apply(random, parents[0], parents[1]);
|
---|
[2] | 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|