Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/Swap3Manipulator.cs @ 15943

Last change on this file since 15943 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
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      permutation.Swap(index1, index2);
68      permutation.Swap(index2, index3);
69    }
70
71    /// <summary>
72    /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
73    /// </summary>
74    /// <remarks>Calls <see cref="Apply"/>.</remarks>
75    /// <param name="random">A random number generator.</param>
76    /// <param name="permutation">The permutation to manipulate.</param>
77    protected override void Manipulate(IRandom random, Permutation permutation) {
78      Apply(random, permutation);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.