[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7270] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2] | 24 | using HeuristicLab.Core;
|
---|
[2854] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 26 |
|
---|
[3053] | 27 | namespace 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");
|
---|
[2] | 59 | int index1, index2, index3, temp;
|
---|
| 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 |
|
---|
[2871] | 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;
|
---|
[2] | 75 | }
|
---|
| 76 |
|
---|
[850] | 77 | /// <summary>
|
---|
| 78 | /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
[2854] | 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);
|
---|
[2] | 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|