Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4501 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24
25namespace HeuristicLab.Encodings.PermutationEncoding {
26  /// <summary>
27  /// Manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.
28  /// </summary>
29  /// <remarks>
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.
31  /// </remarks>
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.")]
33  [StorableClass]
34  public class ScrambleManipulator : PermutationManipulator {
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>
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
44      int breakPoint1, breakPoint2;
45      int[] scrambledIndices, remainingIndices, temp;
46      int selectedIndex, index;
47
48      breakPoint1 = random.Next(original.Length - 1);
49      breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
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
73        permutation[breakPoint1 + i] = original[breakPoint1 + scrambledIndices[i]];
74      }
75    }
76
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>
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);
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.