[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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 randomly scrambling the elements in a randomly chosen interval.
|
---|
| 29 | /// </summary>
|
---|
[2854] | 30 | /// <remarks>
|
---|
[2871] | 31 | /// It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.
|
---|
[2854] | 32 | /// </remarks>
|
---|
[2871] | 33 | [Item("ScrambleManipulator", "An operator which manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval. It is implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms. In Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[2854] | 35 | public class ScrambleManipulator : PermutationManipulator {
|
---|
[4722] | 36 | [StorableConstructor]
|
---|
| 37 | protected ScrambleManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 38 | protected ScrambleManipulator(ScrambleManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 39 | public ScrambleManipulator() : base() { }
|
---|
| 40 |
|
---|
| 41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 42 | return new ScrambleManipulator(this, cloner);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[850] | 45 | /// <summary>
|
---|
| 46 | /// Mixes the elements of the given <paramref name="permutation"/> randomly
|
---|
| 47 | /// in a randomly chosen interval.
|
---|
| 48 | /// </summary>
|
---|
| 49 | /// <param name="random">The random number generator.</param>
|
---|
[2854] | 50 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 51 | public static void Apply(IRandom random, Permutation permutation) {
|
---|
[2] | 52 | int breakPoint1, breakPoint2;
|
---|
| 53 | int[] scrambledIndices, remainingIndices, temp;
|
---|
| 54 | int selectedIndex, index;
|
---|
| 55 |
|
---|
[6648] | 56 | breakPoint1 = random.Next(permutation.Length - 1);
|
---|
| 57 | breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
|
---|
[2] | 58 |
|
---|
| 59 | scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
|
---|
| 60 | remainingIndices = new int[breakPoint2 - breakPoint1 + 1];
|
---|
| 61 | for (int i = 0; i < remainingIndices.Length; i++) { // initialise indices
|
---|
| 62 | remainingIndices[i] = i;
|
---|
| 63 | }
|
---|
| 64 | for (int i = 0; i < scrambledIndices.Length; i++) { // generate permutation of indices
|
---|
| 65 | selectedIndex = random.Next(remainingIndices.Length);
|
---|
| 66 | scrambledIndices[i] = remainingIndices[selectedIndex];
|
---|
| 67 |
|
---|
| 68 | temp = remainingIndices;
|
---|
| 69 | remainingIndices = new int[temp.Length - 1];
|
---|
| 70 | index = 0;
|
---|
| 71 | for (int j = 0; j < remainingIndices.Length; j++) {
|
---|
| 72 | if (index == selectedIndex) {
|
---|
| 73 | index++;
|
---|
| 74 | }
|
---|
| 75 | remainingIndices[j] = temp[index];
|
---|
| 76 | index++;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[6648] | 80 | Apply(permutation, breakPoint1, scrambledIndices);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public static void Apply(Permutation permutation, int startIndex, int[] scrambleArray) {
|
---|
| 84 | Permutation original = (Permutation)permutation.Clone();
|
---|
| 85 | for (int i = 0; i < scrambleArray.Length; i++) { // scramble permutation between breakpoints
|
---|
| 86 | permutation[startIndex + i] = original[startIndex + scrambleArray[i]];
|
---|
[2] | 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[850] | 90 | /// <summary>
|
---|
| 91 | /// Mixes the elements of the given <paramref name="permutation"/> randomly
|
---|
| 92 | /// in a randomly chosen interval.
|
---|
| 93 | /// </summary>
|
---|
| 94 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
[2854] | 95 | /// <param name="random">A random number generator.</param>
|
---|
| 96 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 97 | protected override void Manipulate(IRandom random, Permutation permutation) {
|
---|
| 98 | Apply(random, permutation);
|
---|
[2] | 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|