Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.3/TranslocationInversionManipulator.cs @ 2854

Last change on this file since 2854 was 2854, checked in by svonolfe, 14 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.Operators;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Permutation {
30  /// <summary>
31  /// Manipulates a permutation array by moving and reversing a randomly chosen interval of elements to another
32  /// (randomly chosen) position in the array.
33  /// </summary>
34  /// <remarks>
35  /// It is implemented as described in Fogel, D.B. (1993). Applying Evolutionary Programming to Selected TSP Problems,
36  /// Cybernetics and Systems, 22, pp 27-36.<br />
37  /// </remarks>
38  [Item("TranslocationInversionManipulator", "An operator which inverts a randomly chosen part of a permutation and inserts it at a random position.")]
39  [EmptyStorableClass]
40  [Creatable("Test")]
41  public class TranslocationInversionManipulator : PermutationManipulator {
42    /// <summary>
43    /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
44    /// <paramref name="permutation"/> array and reverses it.
45    /// </summary>
46    /// <param name="random">The random number generator.</param>
47    /// <param name="permutation">The permutation array to manipulate.</param>
48    public static void Apply(IRandom random, Permutation permutation) {
49      Permutation original = (Permutation)permutation.Clone();
50      int breakPoint1, breakPoint2, insertPoint, insertPointLimit;
51
52      breakPoint1 = random.Next(original.Length - 1);
53      breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
54      insertPointLimit = original.Length - breakPoint2 + breakPoint1 - 1;  // get insertion point in remaining part
55      if (insertPointLimit > 0)
56        insertPoint = random.Next(insertPointLimit);
57      else
58        insertPoint = 0;
59
60      int i = 0;  // index in new permutation
61      int j = 0;  // index in old permutation
62      while (i < original.Length) {
63        if (i == insertPoint) {  // copy translocated area
64          for (int k = breakPoint2; k >= breakPoint1; k--) {
65            permutation[i] = original[k];
66            i++;
67          }
68        }
69        if (j == breakPoint1) {  // skip area between breakpoints
70          j = breakPoint2 + 1;
71        }
72        if ((i < original.Length) && (j < original.Length)) {
73          permutation[i] = original[j];
74          i++;
75          j++;
76        }
77      }
78    }
79
80    /// <summary>
81    /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
82    /// <paramref name="permutation"/> array and reverses it.
83    /// </summary>
84    /// <remarks>Calls <see cref="Apply"/>.</remarks>
85    /// <param name="random">A random number generator.</param>
86    /// <param name="permutation">The permutation to manipulate.</param>
87    protected override void Manipulate(IRandom random, Permutation permutation) {
88      Apply(random, permutation);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.