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