Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.3/ScrambleManipulator.cs @ 2861

Last change on this file since 2861 was 2854, checked in by svonolfe, 15 years ago

Ported ported permutation operators to HL 3.3 and added test cases (#889)

File size: 3.8 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 System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Permutation {
29  /// <summary>
30  /// Manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.
31  /// </summary>
32  /// <remarks>
33  /// Implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms,
34  /// in Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.
35  /// </remarks>
36  [Item("ScrambleManipulator", "An operator which manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.")]
37  [EmptyStorableClass]
38  [Creatable("Test")]
39  public class ScrambleManipulator : PermutationManipulator {
40    /// <summary>
41    /// Mixes the elements of the given <paramref name="permutation"/> randomly
42    /// in a randomly chosen interval.
43    /// </summary>
44    /// <param name="random">The random number generator.</param>
45    /// <param name="permutation">The permutation to manipulate.</param>
46    public static void Apply(IRandom random, Permutation permutation) {
47      Permutation original = (Permutation)permutation.Clone();
48
49      int breakPoint1, breakPoint2;
50      int[] scrambledIndices, remainingIndices, temp;
51      int selectedIndex, index;
52
53      breakPoint1 = random.Next(original.Length - 1);
54      breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
55
56      scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
57      remainingIndices = new int[breakPoint2 - breakPoint1 + 1];
58      for (int i = 0; i < remainingIndices.Length; i++) {  // initialise indices
59        remainingIndices[i] = i;
60      }
61      for (int i = 0; i < scrambledIndices.Length; i++) {  // generate permutation of indices
62        selectedIndex = random.Next(remainingIndices.Length);
63        scrambledIndices[i] = remainingIndices[selectedIndex];
64
65        temp = remainingIndices;
66        remainingIndices = new int[temp.Length - 1];
67        index = 0;
68        for (int j = 0; j < remainingIndices.Length; j++) {
69          if (index == selectedIndex) {
70            index++;
71          }
72          remainingIndices[j] = temp[index];
73          index++;
74        }
75      }
76
77      for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // scramble permutation between breakpoints
78        permutation[breakPoint1 + i] = original[breakPoint1 + scrambledIndices[i]];
79      }
80    }
81
82    /// <summary>
83    /// Mixes the elements of the given <paramref name="permutation"/> randomly
84    /// in a randomly chosen interval.
85    /// </summary>
86    /// <remarks>Calls <see cref="Apply"/>.</remarks>
87    /// <param name="random">A random number generator.</param>
88    /// <param name="permutation">The permutation to manipulate.</param>
89    protected override void Manipulate(IRandom random, Permutation permutation) {
90      Apply(random, permutation);
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.