Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/ScrambleManipulator.cs @ 5867

Last change on this file since 5867 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.3 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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]22using HeuristicLab.Common;
[2]23using HeuristicLab.Core;
[2854]24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]25
[3053]26namespace 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) {
52      Permutation original = (Permutation)permutation.Clone();
53
[2]54      int breakPoint1, breakPoint2;
55      int[] scrambledIndices, remainingIndices, temp;
56      int selectedIndex, index;
57
[2854]58      breakPoint1 = random.Next(original.Length - 1);
59      breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
[2]60
61      scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
62      remainingIndices = new int[breakPoint2 - breakPoint1 + 1];
63      for (int i = 0; i < remainingIndices.Length; i++) {  // initialise indices
64        remainingIndices[i] = i;
65      }
66      for (int i = 0; i < scrambledIndices.Length; i++) {  // generate permutation of indices
67        selectedIndex = random.Next(remainingIndices.Length);
68        scrambledIndices[i] = remainingIndices[selectedIndex];
69
70        temp = remainingIndices;
71        remainingIndices = new int[temp.Length - 1];
72        index = 0;
73        for (int j = 0; j < remainingIndices.Length; j++) {
74          if (index == selectedIndex) {
75            index++;
76          }
77          remainingIndices[j] = temp[index];
78          index++;
79        }
80      }
81
82      for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // scramble permutation between breakpoints
[2854]83        permutation[breakPoint1 + i] = original[breakPoint1 + scrambledIndices[i]];
[2]84      }
85    }
86
[850]87    /// <summary>
88    /// Mixes the elements of the given <paramref name="permutation"/> randomly
89    /// in a randomly chosen interval.
90    /// </summary>
91    /// <remarks>Calls <see cref="Apply"/>.</remarks>
[2854]92    /// <param name="random">A random number generator.</param>
93    /// <param name="permutation">The permutation to manipulate.</param>
94    protected override void Manipulate(IRandom random, Permutation permutation) {
95      Apply(random, permutation);
[2]96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.