Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.3/InversionManipulator.cs @ 2794

Last change on this file since 2794 was 2794, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • worked on operators
File size: 2.7 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.Core;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24
25namespace HeuristicLab.Permutation {
26  /// <summary>
27  /// An operator which inverts a randomly chosen part of a permutation.
28  /// </summary>
29  [Item("InversionManipulator", "An operator which inverts a randomly chosen part of a permutation.")]
30  [EmptyStorableClass]
31  [Creatable("Test")]
32  public class InversionManipulator : PermutationManipulator {
33
34    /// <summary>
35    /// Inverts a randomly chosen part of a permutation.
36    /// </summary>
37    /// <param name="random">A random number generator.</param>
38    /// <param name="permutation">The permutation to manipulate.</param>
39    /// <returns>The new manipulated permutation.</returns>
40    public static Permutation Apply(IRandom random, Permutation permutation) {
41      Permutation result = (Permutation)permutation.Clone();
42      int breakPoint1, breakPoint2;
43
44      breakPoint1 = random.Next(result.Length - 1);
45      do {
46        breakPoint2 = random.Next(result.Length - 1);
47      } while (breakPoint2 == breakPoint1);
48      if (breakPoint2 < breakPoint1) { int h = breakPoint1; breakPoint1 = breakPoint2; breakPoint2 = h; }
49
50      for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // invert permutation between breakpoints
51        result[breakPoint1 + i] = permutation[breakPoint2 - i];
52      }
53      return result;
54    }
55
56    /// <summary>
57    /// Inverts a randomly chosen part of a permutation.
58    /// </summary>
59    /// <param name="random">A random number generator.</param>
60    /// <param name="permutation">The permutation to manipulate.</param>
61    /// <returns>The new manipulated permuation.</returns>
62    protected override Permutation Manipulate(IRandom random, Permutation permutation) {
63      return Apply(random, permutation);
64    }
65  }
66}
Note: See TracBrowser for help on using the repository browser.