Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/Swap3Manipulator.cs @ 15367

Last change on this file since 15367 was 14662, checked in by abeham, 8 years ago

#2731: Added better performing manipulation methods to Permutation and adapted the manipulators to use them

  • Added unit tests
File size: 4.1 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[4722]23using HeuristicLab.Common;
[2]24using HeuristicLab.Core;
[2854]25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]26
[3053]27namespace HeuristicLab.Encodings.PermutationEncoding {
[850]28  /// <summary>
29  /// Manipulates a permutation array by swaping three randomly chosen elements.
30  /// </summary>
[2871]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.")]
[3017]36  [StorableClass]
[2854]37  public class Swap3Manipulator : PermutationManipulator {
[4722]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
[850]47    /// <summary>
48    /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
49    /// </summary>
[2871]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>
[850]55    /// <param name="random">The random number generator.</param>
[2854]56    /// <param name="permutation">The permutation to manipulate.</param>
57    public static void Apply(IRandom random, Permutation permutation) {
[2871]58      if (permutation.Length < 3) throw new ArgumentException("Swap3Manipulator: The permutation must be at least of size 3.", "permutation");
[14662]59      int index1, index2, index3;
[2]60
[2871]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);
[2]66
[14662]67      permutation.Swap(index1, index2);
68      permutation.Swap(index2, index3);
[2]69    }
70
[850]71    /// <summary>
72    /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
73    /// </summary>
74    /// <remarks>Calls <see cref="Apply"/>.</remarks>
[2854]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);
[2]79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.