Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11063 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Encodings.PermutationEncoding {
27  /// <summary>
28  /// Manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.
29  /// </summary>
30  /// <remarks>
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.
32  /// </remarks>
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.")]
34  [StorableClass]
35  public class ScrambleManipulator : PermutationManipulator {
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
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>
50    /// <param name="permutation">The permutation to manipulate.</param>
51    public static void Apply(IRandom random, Permutation permutation) {
52      int breakPoint1, breakPoint2;
53      int[] scrambledIndices, remainingIndices, temp;
54      int selectedIndex, index;
55
56      breakPoint1 = random.Next(permutation.Length - 1);
57      breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
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
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]];
87      }
88    }
89
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>
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);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.