[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2854] | 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using HeuristicLab.Core;
|
---|
[2854] | 23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 24 |
|
---|
[3053] | 25 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[850] | 26 | /// <summary>
|
---|
| 27 | /// Manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.
|
---|
| 28 | /// </summary>
|
---|
[2854] | 29 | /// <remarks>
|
---|
[2871] | 30 | /// 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] | 31 | /// </remarks>
|
---|
[2871] | 32 | [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] | 33 | [StorableClass]
|
---|
[2854] | 34 | public class ScrambleManipulator : PermutationManipulator {
|
---|
[850] | 35 | /// <summary>
|
---|
| 36 | /// Mixes the elements of the given <paramref name="permutation"/> randomly
|
---|
| 37 | /// in a randomly chosen interval.
|
---|
| 38 | /// </summary>
|
---|
| 39 | /// <param name="random">The random number generator.</param>
|
---|
[2854] | 40 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 41 | public static void Apply(IRandom random, Permutation permutation) {
|
---|
| 42 | Permutation original = (Permutation)permutation.Clone();
|
---|
| 43 |
|
---|
[2] | 44 | int breakPoint1, breakPoint2;
|
---|
| 45 | int[] scrambledIndices, remainingIndices, temp;
|
---|
| 46 | int selectedIndex, index;
|
---|
| 47 |
|
---|
[2854] | 48 | breakPoint1 = random.Next(original.Length - 1);
|
---|
| 49 | breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
|
---|
[2] | 50 |
|
---|
| 51 | scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
|
---|
| 52 | remainingIndices = new int[breakPoint2 - breakPoint1 + 1];
|
---|
| 53 | for (int i = 0; i < remainingIndices.Length; i++) { // initialise indices
|
---|
| 54 | remainingIndices[i] = i;
|
---|
| 55 | }
|
---|
| 56 | for (int i = 0; i < scrambledIndices.Length; i++) { // generate permutation of indices
|
---|
| 57 | selectedIndex = random.Next(remainingIndices.Length);
|
---|
| 58 | scrambledIndices[i] = remainingIndices[selectedIndex];
|
---|
| 59 |
|
---|
| 60 | temp = remainingIndices;
|
---|
| 61 | remainingIndices = new int[temp.Length - 1];
|
---|
| 62 | index = 0;
|
---|
| 63 | for (int j = 0; j < remainingIndices.Length; j++) {
|
---|
| 64 | if (index == selectedIndex) {
|
---|
| 65 | index++;
|
---|
| 66 | }
|
---|
| 67 | remainingIndices[j] = temp[index];
|
---|
| 68 | index++;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) { // scramble permutation between breakpoints
|
---|
[2854] | 73 | permutation[breakPoint1 + i] = original[breakPoint1 + scrambledIndices[i]];
|
---|
[2] | 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[850] | 77 | /// <summary>
|
---|
| 78 | /// Mixes the elements of the given <paramref name="permutation"/> randomly
|
---|
| 79 | /// in a randomly chosen interval.
|
---|
| 80 | /// </summary>
|
---|
| 81 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
[2854] | 82 | /// <param name="random">A random number generator.</param>
|
---|
| 83 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 84 | protected override void Manipulate(IRandom random, Permutation permutation) {
|
---|
| 85 | Apply(random, permutation);
|
---|
[2] | 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|