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 System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Permutation {
|
---|
30 | /// <summary>
|
---|
31 | /// Performs a crossover operation between two permutation arrays by taking randomly chosen
|
---|
32 | /// reverse and forward intervals from the first permutation array inserting
|
---|
33 | /// it in the child on different positions depending on the second permutation array.
|
---|
34 | /// </summary>
|
---|
35 | /// <remarks>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.<br />
|
---|
36 | /// </remarks>
|
---|
37 | [Item("CosaCrossover", "An operator which performs a crossover operation between two permutation arrays by taking randomly chosen reverse and forward intervals from the first permutation array inserting it in the child on different positions depending on the second permutation array.")]
|
---|
38 | [EmptyStorableClass]
|
---|
39 | [Creatable("Test")]
|
---|
40 | public class CosaCrossover : PermutationCrossover {
|
---|
41 | /// <summary>
|
---|
42 | /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/>
|
---|
43 | /// by taking first the reverse elements of a randomly chosen interval of parent1
|
---|
44 | /// and inserting it in the result at a position specified by the permutation of parent2.
|
---|
45 | /// The remaining elements to be inserted are taken again from parent1 in the forward direction.
|
---|
46 | /// </summary>
|
---|
47 | /// <exception cref="ArgumentException">Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are not of equal length.</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("CosaCrossover: The parent permutations are of unequal length.");
|
---|
54 | int length = parent1.Length;
|
---|
55 | int[] result = new int[length];
|
---|
56 | int crossPoint, startIndex, endIndex;
|
---|
57 |
|
---|
58 | crossPoint = random.Next(length);
|
---|
59 | startIndex = (crossPoint + 1) % length;
|
---|
60 |
|
---|
61 | int i = 0;
|
---|
62 | while ((i < parent2.Length) && (parent2[i] != parent1[startIndex])) { // find index of start value in second permutation
|
---|
63 | i++;
|
---|
64 | }
|
---|
65 | i = (i + 1) % length;
|
---|
66 | int j = 0;
|
---|
67 | while ((j < parent1.Length) && (parent1[j] != parent2[i])) { // find index of parent2[i] in first permutation
|
---|
68 | j++;
|
---|
69 | }
|
---|
70 | endIndex = (j - 1 + length) % length;
|
---|
71 |
|
---|
72 | i = endIndex;
|
---|
73 | j = 0;
|
---|
74 | do { // permutation from end to crosspoint (backwards)
|
---|
75 | result[j] = parent1[i];
|
---|
76 | i = (i - 1 + length) % length;
|
---|
77 | j++;
|
---|
78 | } while (i != crossPoint);
|
---|
79 |
|
---|
80 | i = (endIndex + 1) % length;
|
---|
81 | while (i != crossPoint) { // permutation from end to crosspoint (forwards)
|
---|
82 | result[j] = parent1[i];
|
---|
83 | i = (i + 1) % length;
|
---|
84 | j++;
|
---|
85 | }
|
---|
86 | result[j] = parent1[crossPoint]; // last station: crosspoint
|
---|
87 | return new Permutation(result);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /// <summary>
|
---|
91 | /// Performs a COSA crossover operation for two given parent permutations.
|
---|
92 | /// </summary>
|
---|
93 | /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
|
---|
94 | /// <param name="random">A random number generator.</param>
|
---|
95 | /// <param name="parents">An array containing the two permutations that should be crossed.</param>
|
---|
96 | /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
|
---|
97 | protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
|
---|
98 | if (parents.Length != 2) throw new InvalidOperationException("ERROR in CosaCrossover: The number of parents is not equal to 2");
|
---|
99 | return Apply(random, parents[0], parents[1]);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|