Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/InsertionManipulator.cs @ 1020

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

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

File size: 3.1 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 randomly one element to another position in the array.
30  /// </summary>
31  public class InsertionManipulator : PermutationManipulatorBase {
32    /// <inheritdoc select="summary"/>
33    public override string Description {
34      get { return @"TODO\r\nOperator description still missing ..."; }
35    }
36
37    /// <summary>
38    /// Moves an randomly chosen element in the specified <paramref name="permutation"/> array
39    /// to another randomly generated position.
40    /// </summary>
41    /// <param name="random">The random number generator.</param>
42    /// <param name="permutation">The array to manipulate.</param>
43    /// <returns>The new permuation array with the manipulated data.</returns>
44    public static int[] Apply(IRandom random, int[] permutation) {
45      int[] result = (int[])permutation.Clone();
46      int cutIndex, insertIndex, number;
47
48      cutIndex = random.Next(permutation.Length);
49      insertIndex = random.Next(permutation.Length);
50      number = permutation[cutIndex];
51
52      int i = 0;  // index in new permutation
53      int j = 0;  // index in old permutation
54      while (i < permutation.Length) {
55        if (j == cutIndex) {
56          j++;
57        }
58        if (i == insertIndex) {
59          result[i] = number;
60          i++;
61        }
62        if ((i < permutation.Length) && (j < permutation.Length)) {
63          result[i] = permutation[j];
64          i++;
65          j++;
66        }
67      }
68      return result;
69    }
70
71    /// <summary>
72    /// Moves an randomly chosen element in the specified <paramref name="permutation"/> array
73    /// to another randomly generated position.
74    /// </summary>
75    /// <remarks>Calls <see cref="Apply"/>.</remarks>
76    /// <param name="scope">The current scope.</param>
77    /// <param name="random">The random number generator.</param>
78    /// <param name="permutation">The array to manipulate.</param>
79    /// <returns>The new permuation array with the manipulated data.</returns>
80    protected override int[] Manipulate(IScope scope, IRandom random, int[] permutation) {
81      return Apply(random, permutation);
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.