[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 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 |
|
---|
[4722] | 22 | using HeuristicLab.Common;
|
---|
[2] | 23 | using HeuristicLab.Core;
|
---|
[2854] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 25 |
|
---|
[3053] | 26 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[850] | 27 | /// <summary>
|
---|
| 28 | /// Manipulates a permutation array by moving and reversing a randomly chosen interval of elements to another
|
---|
| 29 | /// (randomly chosen) position in the array.
|
---|
| 30 | /// </summary>
|
---|
[2854] | 31 | /// <remarks>
|
---|
[2871] | 32 | /// It is implemented as described in Fogel, D.B. 1993. Applying Evolutionary Programming to Selected TSP Problems, Cybernetics and Systems, 22, pp 27-36.
|
---|
[2854] | 33 | /// </remarks>
|
---|
[2871] | 34 | [Item("TranslocationInversionManipulator", "An operator which inverts a randomly chosen part of a permutation and inserts it at a random position. It is implemented as described in Fogel, D.B. 1993. Applying Evolutionary Programming to Selected TSP Problems, Cybernetics and Systems, 22, pp. 27-36.")]
|
---|
[3017] | 35 | [StorableClass]
|
---|
[2854] | 36 | public class TranslocationInversionManipulator : PermutationManipulator {
|
---|
[4722] | 37 | [StorableConstructor]
|
---|
| 38 | protected TranslocationInversionManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 39 | protected TranslocationInversionManipulator(TranslocationInversionManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 40 | public TranslocationInversionManipulator() : base() { }
|
---|
| 41 |
|
---|
| 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 43 | return new TranslocationInversionManipulator(this, cloner);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[850] | 46 | /// <summary>
|
---|
| 47 | /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
|
---|
| 48 | /// <paramref name="permutation"/> array and reverses it.
|
---|
| 49 | /// </summary>
|
---|
| 50 | /// <param name="random">The random number generator.</param>
|
---|
| 51 | /// <param name="permutation">The permutation array to manipulate.</param>
|
---|
[2854] | 52 | public static void Apply(IRandom random, Permutation permutation) {
|
---|
| 53 | Permutation original = (Permutation)permutation.Clone();
|
---|
[2] | 54 | int breakPoint1, breakPoint2, insertPoint, insertPointLimit;
|
---|
| 55 |
|
---|
[2854] | 56 | breakPoint1 = random.Next(original.Length - 1);
|
---|
| 57 | breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
|
---|
| 58 | insertPointLimit = original.Length - breakPoint2 + breakPoint1 - 1; // get insertion point in remaining part
|
---|
[2] | 59 | if (insertPointLimit > 0)
|
---|
| 60 | insertPoint = random.Next(insertPointLimit);
|
---|
| 61 | else
|
---|
| 62 | insertPoint = 0;
|
---|
| 63 |
|
---|
| 64 | int i = 0; // index in new permutation
|
---|
| 65 | int j = 0; // index in old permutation
|
---|
[2854] | 66 | while (i < original.Length) {
|
---|
[2] | 67 | if (i == insertPoint) { // copy translocated area
|
---|
| 68 | for (int k = breakPoint2; k >= breakPoint1; k--) {
|
---|
[2854] | 69 | permutation[i] = original[k];
|
---|
[2] | 70 | i++;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | if (j == breakPoint1) { // skip area between breakpoints
|
---|
| 74 | j = breakPoint2 + 1;
|
---|
| 75 | }
|
---|
[2854] | 76 | if ((i < original.Length) && (j < original.Length)) {
|
---|
| 77 | permutation[i] = original[j];
|
---|
[2] | 78 | i++;
|
---|
| 79 | j++;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[850] | 84 | /// <summary>
|
---|
| 85 | /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
|
---|
| 86 | /// <paramref name="permutation"/> array and reverses it.
|
---|
| 87 | /// </summary>
|
---|
| 88 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
[2854] | 89 | /// <param name="random">A random number generator.</param>
|
---|
| 90 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 91 | protected override void Manipulate(IRandom random, Permutation permutation) {
|
---|
| 92 | Apply(random, permutation);
|
---|
[2] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|