[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
[14662] | 22 | using System.Linq;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2] | 24 | using HeuristicLab.Core;
|
---|
[2854] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 26 |
|
---|
[3053] | 27 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[850] | 28 | /// <summary>
|
---|
| 29 | /// Manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.
|
---|
| 30 | /// </summary>
|
---|
[2854] | 31 | /// <remarks>
|
---|
[2871] | 32 | /// 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] | 33 | /// </remarks>
|
---|
[2871] | 34 | [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] | 35 | [StorableClass]
|
---|
[2854] | 36 | public class ScrambleManipulator : PermutationManipulator {
|
---|
[4722] | 37 | [StorableConstructor]
|
---|
| 38 | protected ScrambleManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 39 | protected ScrambleManipulator(ScrambleManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 40 | public ScrambleManipulator() : base() { }
|
---|
| 41 |
|
---|
| 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 43 | return new ScrambleManipulator(this, cloner);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[850] | 46 | /// <summary>
|
---|
| 47 | /// Mixes the elements of the given <paramref name="permutation"/> randomly
|
---|
| 48 | /// in a randomly chosen interval.
|
---|
| 49 | /// </summary>
|
---|
| 50 | /// <param name="random">The random number generator.</param>
|
---|
[2854] | 51 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 52 | public static void Apply(IRandom random, Permutation permutation) {
|
---|
[2] | 53 | int breakPoint1, breakPoint2;
|
---|
| 54 | int[] scrambledIndices, remainingIndices, temp;
|
---|
| 55 | int selectedIndex, index;
|
---|
| 56 |
|
---|
[6648] | 57 | breakPoint1 = random.Next(permutation.Length - 1);
|
---|
| 58 | breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
|
---|
[2] | 59 |
|
---|
[14662] | 60 | // TODO: Use Fisher-Yates-Shuffle rather than complicated code below
|
---|
| 61 | // scrambledIndices = Enumerable.Range(0, breakPoint2 - breakPoint1 + 1).Shuffle(random).ToArray();
|
---|
| 62 | // Also, it would be more memory-efficient to change here and Apply(Permutation, int, int[]) below to interpret scrambleArray as values, not indices
|
---|
| 63 | // Don't forget the move generator
|
---|
| 64 | // BackwardsCompatibility3.3
|
---|
| 65 | #region This whole code should be replaced by above line when going for 3.4
|
---|
[2] | 66 | scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
|
---|
| 67 | remainingIndices = new int[breakPoint2 - breakPoint1 + 1];
|
---|
| 68 | for (int i = 0; i < remainingIndices.Length; i++) { // initialise indices
|
---|
| 69 | remainingIndices[i] = i;
|
---|
| 70 | }
|
---|
| 71 | for (int i = 0; i < scrambledIndices.Length; i++) { // generate permutation of indices
|
---|
| 72 | selectedIndex = random.Next(remainingIndices.Length);
|
---|
| 73 | scrambledIndices[i] = remainingIndices[selectedIndex];
|
---|
| 74 |
|
---|
| 75 | temp = remainingIndices;
|
---|
| 76 | remainingIndices = new int[temp.Length - 1];
|
---|
| 77 | index = 0;
|
---|
| 78 | for (int j = 0; j < remainingIndices.Length; j++) {
|
---|
| 79 | if (index == selectedIndex) {
|
---|
| 80 | index++;
|
---|
| 81 | }
|
---|
| 82 | remainingIndices[j] = temp[index];
|
---|
| 83 | index++;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
[14662] | 86 | #endregion
|
---|
[2] | 87 |
|
---|
[6648] | 88 | Apply(permutation, breakPoint1, scrambledIndices);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public static void Apply(Permutation permutation, int startIndex, int[] scrambleArray) {
|
---|
[14662] | 92 | permutation.Replace(startIndex, scrambleArray.Select(x => permutation[startIndex + x]).ToArray());
|
---|
[2] | 93 | }
|
---|
| 94 |
|
---|
[850] | 95 | /// <summary>
|
---|
| 96 | /// Mixes the elements of the given <paramref name="permutation"/> randomly
|
---|
| 97 | /// in a randomly chosen interval.
|
---|
| 98 | /// </summary>
|
---|
| 99 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
[2854] | 100 | /// <param name="random">A random number generator.</param>
|
---|
| 101 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 102 | protected override void Manipulate(IRandom random, Permutation permutation) {
|
---|
| 103 | Apply(random, permutation);
|
---|
[2] | 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|