Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2871 was 2871, checked in by abeham, 14 years ago

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