[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2854] | 3 | * Copyright (C) 2002-2010 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;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
[2854] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 25 |
|
---|
[3053] | 26 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[850] | 27 | /// <summary>
|
---|
| 28 | /// Manipulates a permutation array by swaping three randomly chosen elements.
|
---|
| 29 | /// </summary>
|
---|
[2871] | 30 | /// <remarks>
|
---|
| 31 | /// 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.
|
---|
| 32 | /// 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.
|
---|
| 33 | /// </remarks>
|
---|
| 34 | [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] | 35 | [StorableClass]
|
---|
[2854] | 36 | public class Swap3Manipulator : PermutationManipulator {
|
---|
[850] | 37 | /// <summary>
|
---|
| 38 | /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
|
---|
| 39 | /// </summary>
|
---|
[2871] | 40 | /// <exception cref="ArgumentException">Thrown when the <paramref name="permutation"/> contains less than 3 elements.</exception>
|
---|
| 41 | /// <remarks>
|
---|
| 42 | /// 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.
|
---|
| 43 | /// 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.
|
---|
| 44 | /// </remarks>
|
---|
[850] | 45 | /// <param name="random">The random number generator.</param>
|
---|
[2854] | 46 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 47 | public static void Apply(IRandom random, Permutation permutation) {
|
---|
[2871] | 48 | if (permutation.Length < 3) throw new ArgumentException("Swap3Manipulator: The permutation must be at least of size 3.", "permutation");
|
---|
[2] | 49 | int index1, index2, index3, temp;
|
---|
| 50 |
|
---|
[2871] | 51 | do {
|
---|
| 52 | index1 = random.Next(permutation.Length);
|
---|
| 53 | index2 = random.Next(permutation.Length);
|
---|
| 54 | index3 = random.Next(permutation.Length);
|
---|
| 55 | } while (index1 == index2 || index2 == index3 || index1 == index3);
|
---|
[2] | 56 |
|
---|
[2871] | 57 | // swap positions 1 and 2
|
---|
| 58 | temp = permutation[index1];
|
---|
| 59 | permutation[index1] = permutation[index2];
|
---|
| 60 | permutation[index2] = temp;
|
---|
| 61 | // swap positions 2 and 3
|
---|
| 62 | temp = permutation[index2];
|
---|
| 63 | permutation[index2] = permutation[index3];
|
---|
| 64 | permutation[index3] = temp;
|
---|
[2] | 65 | }
|
---|
| 66 |
|
---|
[850] | 67 | /// <summary>
|
---|
| 68 | /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array.
|
---|
| 69 | /// </summary>
|
---|
| 70 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
[2854] | 71 | /// <param name="random">A random number generator.</param>
|
---|
| 72 | /// <param name="permutation">The permutation to manipulate.</param>
|
---|
| 73 | protected override void Manipulate(IRandom random, Permutation permutation) {
|
---|
| 74 | Apply(random, permutation);
|
---|
[2] | 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|