Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/ScrambleManipulator.cs @ 17181

Last change on this file since 17181 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HEAL.Attic;
26
27namespace HeuristicLab.Encodings.PermutationEncoding {
28  /// <summary>
29  /// Manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.
30  /// </summary>
31  /// <remarks>
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.
33  /// </remarks>
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.")]
35  [StorableType("C324E297-C54E-4A18-BF41-3F0EF9595762")]
36  public class ScrambleManipulator : PermutationManipulator {
37    [StorableConstructor]
38    protected ScrambleManipulator(StorableConstructorFlag _) : base(_) { }
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
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>
51    /// <param name="permutation">The permutation to manipulate.</param>
52    public static void Apply(IRandom random, Permutation permutation) {
53      int breakPoint1, breakPoint2;
54      int[] scrambledIndices, remainingIndices, temp;
55      int selectedIndex, index;
56
57      breakPoint1 = random.Next(permutation.Length - 1);
58      breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
59
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
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      }
86      #endregion
87
88      Apply(permutation, breakPoint1, scrambledIndices);
89    }
90
91    public static void Apply(Permutation permutation, int startIndex, int[] scrambleArray) {
92      permutation.Replace(startIndex, scrambleArray.Select(x => permutation[startIndex + x]).ToArray());
93    }
94
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>
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);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.