Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/InversionManipulator.cs @ 1159

Last change on this file since 1159 was 850, checked in by vdorfer, 15 years ago

Created API documentation for HeuristicLab.Permutation namespace (#331)

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
26
27namespace HeuristicLab.Permutation {
28  /// <summary>
29  /// Manipulates a permutation array by reversing a randomly chosen interval.
30  /// </summary>
31  public class InversionManipulator : PermutationManipulatorBase {
32    /// <inheritdoc select="summary"/>
33    public override string Description {
34      get { return @"TODO\r\nOperator description still missing ..."; }
35    }
36
37    /// <summary>
38    /// Reverses the specified <paramref name="permutation"/> between two randomly generated positions.
39    /// </summary>
40    /// <param name="random">The random number generator.</param>
41    /// <param name="permutation">The permutation array to manipulate.</param>
42    /// <returns>The new permuation array with the manipulated data.</returns>
43    public static int[] Apply(IRandom random, int[] permutation) {
44      int[] result = (int[])permutation.Clone();
45      int breakPoint1, breakPoint2;
46
47      breakPoint1 = random.Next(result.Length - 1);
48      do {
49        breakPoint2 = random.Next(result.Length - 1);
50      } while (breakPoint2 == breakPoint1);
51      if (breakPoint2 < breakPoint1) { int h = breakPoint1; breakPoint1 = breakPoint2; breakPoint2 = h; }
52
53      for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // reverse permutation between breakpoints
54        result[breakPoint1 + i] = permutation[breakPoint2 - i];
55      }
56      return result;
57    }
58
59    /// <summary>
60    /// Reverses the specified <paramref name="permutation"/> between two randomly generated positions.
61    /// </summary>
62    /// <remarks>Calls <see cref="Apply"/>.</remarks>
63    /// <param name="scope">The current scope.</param>
64    /// <param name="random">The random number generator.</param>
65    /// <param name="permutation">The permutation array to manipulate.</param>
66    /// <returns>The new permuation array with the manipulated data.</returns>
67    protected override int[] Manipulate(IScope scope, IRandom random, int[] permutation) {
68      return Apply(random, permutation);
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.