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