Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/TranslocationManipulator.cs @ 10604

Last change on this file since 10604 was 9456, checked in by swagner, 12 years ago

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

File size: 4.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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 moving a randomly chosen interval of elements to another
29  /// (randomly chosen) position in the array.
30  /// </summary>
[2854]31  /// <remarks>
[2871]32  /// It is implemented as described in Michalewicz, Z. 1992. Genetic Algorithms + Data Structures = Evolution Programs, Springer Verlag, Berlin Heidelberg.
[2854]33  /// </remarks>
[2871]34  [Item("TranslocationManipulator", "An operator which Manipulates a permutation array by moving a randomly chosen interval of elements to another (randomly chosen) position in the array. It is implemented as described in Michalewicz, Z. 1992. Genetic Algorithms + Data Structures = Evolution Programs, Springer Verlag, Berlin Heidelberg.")]
[3017]35  [StorableClass]
[2854]36  public class TranslocationManipulator : PermutationManipulator {
[4722]37    [StorableConstructor]
38    protected TranslocationManipulator(bool deserializing) : base(deserializing) { }
39    protected TranslocationManipulator(TranslocationManipulator original, Cloner cloner) : base(original, cloner) { }
40    public TranslocationManipulator() : base() { }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new TranslocationManipulator(this, cloner);
44    }
45
[850]46    /// <summary>
47    /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
48    /// <paramref name="permutation"/> array.
49    /// </summary>
50    /// <param name="random">The random number generator.</param>
51    /// <param name="permutation">The permutation array to manipulate.</param>
[2854]52    public static void Apply(IRandom random, Permutation permutation) {
[2]53      int breakPoint1, breakPoint2, insertPoint, insertPointLimit;
54
55      breakPoint1 = random.Next(permutation.Length - 1);
56      breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
57      insertPointLimit = permutation.Length - breakPoint2 + breakPoint1 - 1;  // get insertion point in remaining part
58      if (insertPointLimit > 0)
59        insertPoint = random.Next(insertPointLimit);
60      else
61        insertPoint = 0;
62
[3200]63      Apply(permutation, breakPoint1, breakPoint2, insertPoint);
64    }
65
66    public static void Apply(Permutation permutation, int breakPoint1, int breakPoint2, int insertPoint) {
67      Permutation original = (Permutation)permutation.Clone();
68
[2]69      int i = 0;  // index in new permutation
70      int j = 0;  // index in old permutation
[2854]71      while (i < original.Length) {
[2]72        if (i == insertPoint) {  // copy translocated area
73          for (int k = breakPoint1; k <= breakPoint2; k++) {
[2854]74            permutation[i] = original[k];
[2]75            i++;
76          }
77        }
78        if (j == breakPoint1) {  // skip area between breakpoints
79          j = breakPoint2 + 1;
80        }
[2854]81        if ((i < original.Length) && (j < original.Length)) {
82          permutation[i] = original[j];
[2]83          i++;
84          j++;
85        }
86      }
87    }
88
[850]89    /// <summary>
90    /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
91    /// <paramref name="permutation"/> array.
92    /// </summary>
93    /// <remarks>Calls <see cref="Apply"/>.</remarks>
[2854]94    /// <param name="random">A random number generator.</param>
95    /// <param name="permutation">The permutation to manipulate.</param>
96    protected override void Manipulate(IRandom random, Permutation permutation) {
97      Apply(random, permutation);
[2]98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.