Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/Swap3Manipulator.cs @ 13810

Last change on this file since 13810 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.PermutationEncoding {
28  /// <summary>
29  /// Manipulates a permutation array by swaping three randomly chosen elements.
30  /// </summary>
31  /// <remarks>
32  /// It is implemented such that first 3 positions are randomly chosen in the interval [0;N) with N = length of the permutation with all positions being distinct from each other.
33  /// Then position 1 is put in place of position 3, position 2 is put in place of position 1 and position 3 is put in place of position 2.
34  /// </remarks>
35  [Item("Swap3Manipulator", "An operator which manipulates a permutation array by swaping three randomly chosen elements. It is implemented such that first 3 positions are randomly chosen in the interval [0;N) with N = length of the permutation with all positions being distinct from each other. Then position 1 is put in place of position 3, position 2 is put in place of position 1 and position 3 is put in place of position 2.")]
36  [StorableClass]
37  public class Swap3Manipulator : PermutationManipulator {
38    [StorableConstructor]
39    protected Swap3Manipulator(bool deserializing) : base(deserializing) { }
40    protected Swap3Manipulator(Swap3Manipulator original, Cloner cloner) : base(original, cloner) { }
41    public Swap3Manipulator() : base() { }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new Swap3Manipulator(this, cloner);
45    }
46
47    /// <summary>
48    /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
49    /// </summary>
50    /// <exception cref="ArgumentException">Thrown when the <paramref name="permutation"/> contains less than 3 elements.</exception>
51    /// <remarks>
52    /// It is implemented such that first 3 positions are randomly chosen in the interval [0;N) with N = length of the permutation with all positions being distinct from each other.
53    /// Then position 1 is put in place of position 3, position 2 is put in place of position 1 and position 3 is put in place of position 2.
54    /// </remarks>
55    /// <param name="random">The random number generator.</param>
56    /// <param name="permutation">The permutation to manipulate.</param>
57    public static void Apply(IRandom random, Permutation permutation) {
58      if (permutation.Length < 3) throw new ArgumentException("Swap3Manipulator: The permutation must be at least of size 3.", "permutation");
59      int index1, index2, index3, temp;
60
61      do {
62        index1 = random.Next(permutation.Length);
63        index2 = random.Next(permutation.Length);
64        index3 = random.Next(permutation.Length);
65      } while (index1 == index2 || index2 == index3 || index1 == index3);
66
67      // swap positions 1 and 2
68      temp = permutation[index1];
69      permutation[index1] = permutation[index2];
70      permutation[index2] = temp;
71      // swap positions 2 and 3
72      temp = permutation[index2];
73      permutation[index2] = permutation[index3];
74      permutation[index3] = temp;
75    }
76
77    /// <summary>
78    /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
79    /// </summary>
80    /// <remarks>Calls <see cref="Apply"/>.</remarks>
81    /// <param name="random">A random number generator.</param>
82    /// <param name="permutation">The permutation to manipulate.</param>
83    protected override void Manipulate(IRandom random, Permutation permutation) {
84      Apply(random, permutation);
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.