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 HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Encodings.Permutation {
|
---|
26 | /// <summary>
|
---|
27 | /// Manipulates a permutation array by moving a randomly chosen interval of elements to another
|
---|
28 | /// (randomly chosen) position in the array.
|
---|
29 | /// </summary>
|
---|
30 | /// <remarks>
|
---|
31 | /// It is implemented as described in Michalewicz, Z. 1992. Genetic Algorithms + Data Structures = Evolution Programs, Springer Verlag, Berlin Heidelberg.
|
---|
32 | /// </remarks>
|
---|
33 | [Item("TranslocationManipulator", "An operator which Manipulates a permutation array by moving a randomly chosen interval of elements to another (randomly chosen) position in the array. It is implemented as described in Michalewicz, Z. 1992. Genetic Algorithms + Data Structures = Evolution Programs, Springer Verlag, Berlin Heidelberg.")]
|
---|
34 | [StorableClass]
|
---|
35 | [Creatable("Test")]
|
---|
36 | public class TranslocationManipulator : PermutationManipulator {
|
---|
37 | /// <summary>
|
---|
38 | /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
|
---|
39 | /// <paramref name="permutation"/> array.
|
---|
40 | /// </summary>
|
---|
41 | /// <param name="random">The random number generator.</param>
|
---|
42 | /// <param name="permutation">The permutation array to manipulate.</param>
|
---|
43 | public static void Apply(IRandom random, Permutation permutation) {
|
---|
44 | Permutation original = (Permutation)permutation.Clone();
|
---|
45 | int breakPoint1, breakPoint2, insertPoint, insertPointLimit;
|
---|
46 |
|
---|
47 | breakPoint1 = random.Next(permutation.Length - 1);
|
---|
48 | breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
|
---|
49 | insertPointLimit = permutation.Length - breakPoint2 + breakPoint1 - 1; // get insertion point in remaining part
|
---|
50 | if (insertPointLimit > 0)
|
---|
51 | insertPoint = random.Next(insertPointLimit);
|
---|
52 | else
|
---|
53 | insertPoint = 0;
|
---|
54 |
|
---|
55 | int i = 0; // index in new permutation
|
---|
56 | int j = 0; // index in old permutation
|
---|
57 | while (i < original.Length) {
|
---|
58 | if (i == insertPoint) { // copy translocated area
|
---|
59 | for (int k = breakPoint1; k <= breakPoint2; k++) {
|
---|
60 | permutation[i] = original[k];
|
---|
61 | i++;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | if (j == breakPoint1) { // skip area between breakpoints
|
---|
65 | j = breakPoint2 + 1;
|
---|
66 | }
|
---|
67 | if ((i < original.Length) && (j < original.Length)) {
|
---|
68 | permutation[i] = original[j];
|
---|
69 | i++;
|
---|
70 | j++;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
|
---|
77 | /// <paramref name="permutation"/> array.
|
---|
78 | /// </summary>
|
---|
79 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
80 | /// <param name="random">A random number generator.</param>
|
---|
81 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
82 | protected override void Manipulate(IRandom random, Permutation permutation) {
|
---|
83 | Apply(random, permutation);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|