Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.Permutation/3.3/Swap3Manipulator.cs @ 3494

Last change on this file since 3494 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: 3.2 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 swaping three randomly chosen elements.
30  /// </summary>
31  public class Swap3Manipulator : 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 three randomly chosen elements of 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, index3, temp;
46
47      index1 = random.Next(result.Length);
48      index2 = random.Next(result.Length);
49      index3 = random.Next(result.Length);
50
51      if (random.NextDouble() < 0.5) {
52        // swap edges 1 and 2
53        temp = result[index1];
54        result[index1] = result[index2];
55        result[index2] = temp;
56        // swap edges 2 and 3
57        temp = result[index2];
58        result[index2] = result[index3];
59        result[index3] = temp;
60      } else {
61        // swap edges 1 and 3
62        temp = result[index1];
63        result[index1] = result[index3];
64        result[index3] = temp;
65        // swap edges 2 and 3
66        temp = result[index2];
67        result[index2] = result[index3];
68        result[index3] = temp;
69      }
70      return result;
71    }
72
73    /// <summary>
74    /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
75    /// </summary>
76    /// <remarks>Calls <see cref="Apply"/>.</remarks>
77    /// <param name="scope">The current scope.</param>
78    /// <param name="random">The random number generator.</param>
79    /// <param name="permutation">The permutation array to manipulate.</param>
80    /// <returns>The new permuation array with the manipulated data.</returns>
81    protected override int[] Manipulate(IScope scope, IRandom random, int[] permutation) {
82      return Apply(random, permutation);
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.