1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 the crossover described in the COSA optimization method.
|
---|
30 | /// </summary>
|
---|
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.
|
---|
36 | /// </remarks>
|
---|
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.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class CosaCrossover : PermutationCrossover {
|
---|
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 |
|
---|
49 | /// <summary>
|
---|
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.
|
---|
53 | /// </summary>
|
---|
54 | /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</exception>
|
---|
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>
|
---|
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.");
|
---|
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;
|
---|
69 | while ((i < parent2.Length) && (parent2[i] != parent1[crossPoint])) { // find index of cross point in second permutation
|
---|
70 | i++;
|
---|
71 | }
|
---|
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++;
|
---|
76 | }
|
---|
77 |
|
---|
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 | }
|
---|
95 | }
|
---|
96 | return new Permutation(parent1.PermutationType, result);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /// <summary>
|
---|
100 | /// Checks number of parents and calls <see cref="Apply(IRandom, Permutation, Permutation)"/>.
|
---|
101 | /// </summary>
|
---|
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>
|
---|
106 | protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
|
---|
107 | if (parents.Length != 2) throw new InvalidOperationException("CosaCrossover: The number of parents is not equal to 2");
|
---|
108 | return Apply(random, parents[0], parents[1]);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|