[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2794] | 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 |
|
---|
[2526] | 22 | using HeuristicLab.Core;
|
---|
[2] | 23 | using HeuristicLab.Data;
|
---|
[2794] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 25 |
|
---|
[3053] | 26 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[3231] | 27 | [Item("Permutation", "Represents a permutation of integer values.")]
|
---|
[3017] | 28 | [StorableClass]
|
---|
[3054] | 29 | public class Permutation : IntArray {
|
---|
[3231] | 30 | [Storable]
|
---|
| 31 | private PermutationTypes permutationType;
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Gets the type of the permutation (see <see cref="PermutationType"/>).
|
---|
| 34 | /// </summary>
|
---|
| 35 | public PermutationTypes PermutationType {
|
---|
| 36 | get { return permutationType; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public Permutation() : base() {
|
---|
| 40 | permutationType = PermutationTypes.RelativeUndirected;
|
---|
| 41 | }
|
---|
| 42 | public Permutation(PermutationTypes type) : base() {
|
---|
| 43 | permutationType = type;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public Permutation(PermutationTypes type, int length)
|
---|
[2794] | 47 | : base(length) {
|
---|
| 48 | for (int i = 0; i < length; i++)
|
---|
| 49 | this[i] = i;
|
---|
[3231] | 50 | permutationType = type;
|
---|
[2] | 51 | }
|
---|
[3231] | 52 | public Permutation(PermutationTypes type, int length, IRandom random)
|
---|
| 53 | : this(type, length) {
|
---|
[2794] | 54 | Randomize(random);
|
---|
[2] | 55 | }
|
---|
[3231] | 56 | public Permutation(PermutationTypes type, int[] elements) : base(elements) {
|
---|
| 57 | permutationType = type;
|
---|
| 58 | }
|
---|
| 59 | public Permutation(PermutationTypes type, IntArray elements) : this(type, elements.Length) {
|
---|
[3054] | 60 | for (int i = 0; i < array.Length; i++)
|
---|
| 61 | array[i] = elements[i];
|
---|
[2794] | 62 | }
|
---|
[2] | 63 |
|
---|
[2794] | 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3231] | 65 | Permutation clone = new Permutation(permutationType, array);
|
---|
[2526] | 66 | cloner.RegisterClonedObject(this, clone);
|
---|
[2] | 67 | return clone;
|
---|
| 68 | }
|
---|
[2794] | 69 |
|
---|
[3054] | 70 | public virtual bool Validate() {
|
---|
[2794] | 71 | bool[] values = new bool[Length];
|
---|
| 72 | int value;
|
---|
| 73 |
|
---|
| 74 | for (int i = 0; i < values.Length; i++)
|
---|
| 75 | values[i] = false;
|
---|
| 76 | for (int i = 0; i < Length; i++) {
|
---|
| 77 | value = this[i];
|
---|
| 78 | if ((value < 0) || (value >= values.Length)) return false;
|
---|
| 79 | if (values[value]) return false;
|
---|
| 80 | values[value] = true;
|
---|
| 81 | }
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[3054] | 85 | public virtual void Randomize(IRandom random, int startIndex, int length) {
|
---|
| 86 | if (length > 1) {
|
---|
| 87 | // Knuth shuffle
|
---|
| 88 | int index1, index2;
|
---|
| 89 | int val;
|
---|
| 90 | for (int i = length - 1; i > 0; i--) {
|
---|
| 91 | index1 = startIndex + i;
|
---|
| 92 | index2 = startIndex + random.Next(i + 1);
|
---|
| 93 | if (index1 != index2) {
|
---|
| 94 | val = array[index1];
|
---|
| 95 | array[index1] = array[index2];
|
---|
| 96 | array[index2] = val;
|
---|
| 97 | }
|
---|
[2794] | 98 | }
|
---|
[3054] | 99 | OnReset();
|
---|
[2794] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | public void Randomize(IRandom random) {
|
---|
| 103 | Randomize(random, 0, Length);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[3054] | 106 | public virtual int GetCircular(int position) {
|
---|
[2997] | 107 | if (position >= Length) position = position % Length;
|
---|
| 108 | while (position < 0) position += Length;
|
---|
| 109 | return this[position];
|
---|
| 110 | }
|
---|
[2] | 111 | }
|
---|
| 112 | }
|
---|