Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/TranslocationInversionManipulator.cs @ 1022

Last change on this file since 1022 was 850, checked in by vdorfer, 16 years ago

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

File size: 3.7 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 moving and reversing a randomly chosen interval of elements to another
30  /// (randomly chosen) position in the array.
31  /// </summary>
32  public class TranslocationInversionManipulator : PermutationManipulatorBase {
33    /// <inheritdoc select="summary"/>
34    public override string Description {
35      get { return @"TODO\r\nOperator description still missing ..."; }
36    }
37
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    /// <returns>The new permuation array with the manipulated data.</returns>
45    public static int[] Apply(IRandom random, int[] permutation) {
46      int[] result = (int[])permutation.Clone();
47      int breakPoint1, breakPoint2, insertPoint, insertPointLimit;
48
49      breakPoint1 = random.Next(permutation.Length - 1);
50      breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
51      insertPointLimit = permutation.Length - breakPoint2 + breakPoint1 - 1;  // get insertion point in remaining part
52      if (insertPointLimit > 0)
53        insertPoint = random.Next(insertPointLimit);
54      else
55        insertPoint = 0;
56
57      int i = 0;  // index in new permutation
58      int j = 0;  // index in old permutation
59      while (i < permutation.Length) {
60        if (i == insertPoint) {  // copy translocated area
61          for (int k = breakPoint2; k >= breakPoint1; k--) {
62            result[i] = permutation[k];
63            i++;
64          }
65        }
66        if (j == breakPoint1) {  // skip area between breakpoints
67          j = breakPoint2 + 1;
68        }
69        if ((i < permutation.Length) && (j < permutation.Length)) {
70          result[i] = permutation[j];
71          i++;
72          j++;
73        }
74      }
75      return result;
76    }
77
78    /// <summary>
79    /// Moves a randomly chosen interval of elements to another (randomly chosen) position in the given
80    /// <paramref name="permutation"/> array and reverses it.
81    /// </summary>
82    /// <remarks>Calls <see cref="Apply"/>.</remarks>
83    /// <param name="scope">The current scope.</param>
84    /// <param name="random">The random number generator.</param>
85    /// <param name="permutation">The permutation array to manipulate.</param>
86    /// <returns>The new permuation array with the manipulated data.</returns>
87    protected override int[] Manipulate(IScope scope, IRandom random, int[] permutation) {
88      return Apply(random, permutation);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.