Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4047 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 4.0 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.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    /// <summary>
37    /// Mixes the elements of the given <paramref name="permutation"/> randomly
38    /// in a randomly chosen interval.
39    /// </summary>
40    /// <param name="random">The random number generator.</param>
41    /// <param name="permutation">The permutation to manipulate.</param>
42    public static void Apply(IRandom random, Permutation permutation) {
43      Permutation original = (Permutation)permutation.Clone();
44
45      int breakPoint1, breakPoint2;
46      int[] scrambledIndices, remainingIndices, temp;
47      int selectedIndex, index;
48
49      breakPoint1 = random.Next(original.Length - 1);
50      breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
51
52      scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
53      remainingIndices = new int[breakPoint2 - breakPoint1 + 1];
54      for (int i = 0; i < remainingIndices.Length; i++) {  // initialise indices
55        remainingIndices[i] = i;
56      }
57      for (int i = 0; i < scrambledIndices.Length; i++) {  // generate permutation of indices
58        selectedIndex = random.Next(remainingIndices.Length);
59        scrambledIndices[i] = remainingIndices[selectedIndex];
60
61        temp = remainingIndices;
62        remainingIndices = new int[temp.Length - 1];
63        index = 0;
64        for (int j = 0; j < remainingIndices.Length; j++) {
65          if (index == selectedIndex) {
66            index++;
67          }
68          remainingIndices[j] = temp[index];
69          index++;
70        }
71      }
72
73      for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // scramble permutation between breakpoints
74        permutation[breakPoint1 + i] = original[breakPoint1 + scrambledIndices[i]];
75      }
76    }
77
78    /// <summary>
79    /// Mixes the elements of the given <paramref name="permutation"/> randomly
80    /// in a randomly chosen interval.
81    /// </summary>
82    /// <remarks>Calls <see cref="Apply"/>.</remarks>
83    /// <param name="random">A random number generator.</param>
84    /// <param name="permutation">The permutation to manipulate.</param>
85    protected override void Manipulate(IRandom random, Permutation permutation) {
86      Apply(random, permutation);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.