Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Permutation/3.3/Swap2Manipulator.cs @ 2498

Last change on this file since 2498 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 2.6 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 swapping to randomly chosen elements.
30  /// </summary>
31  public class Swap2Manipulator : PermutationManipulatorBase {
32    /// <inheritdoc select="summary"/>
33    public override string Description {
34      get { return @"TODO\r\nOperator description still missing ..."; }
35    }
36
37    /// <summary>
38    /// Swaps two randomly chosen elements in the given <paramref name="permutation"/> array.
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 index1, index2, temp;
46
47      index1 = random.Next(result.Length);
48      index2 = random.Next(result.Length);
49
50      temp = result[index1];
51      result[index1] = result[index2];
52      result[index2] = temp;
53
54      return result;
55    }
56
57    /// <summary>
58    /// Swaps two randomly chosen elements in the given <paramref name="permutation"/> array.
59    /// </summary>
60    /// <remarks>Calls <see cref="Apply"/>.</remarks>
61    /// <param name="scope">The current scope.</param>
62    /// <param name="random">The random number generator.</param>
63    /// <param name="permutation">The permutation array to manipulate.</param>
64    /// <returns>The new permuation array with the manipulated data.</returns>
65    protected override int[] Manipulate(IScope scope, IRandom random, int[] permutation) {
66      return Apply(random, permutation);
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.