1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
29 | [Item("Permutation", "Represents a permutation of integer values.")]
|
---|
30 | [StorableClass]
|
---|
31 | public class Permutation : IntArray {
|
---|
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; }
|
---|
39 | set {
|
---|
40 | bool changed = (permutationType != value);
|
---|
41 | permutationType = value;
|
---|
42 | if (changed) OnPermutationTypeChanged();
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
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;
|
---|
51 | }
|
---|
52 | public Permutation() : this(PermutationTypes.RelativeUndirected) { }
|
---|
53 | public Permutation(PermutationTypes type)
|
---|
54 | : base() {
|
---|
55 | permutationType = type;
|
---|
56 | }
|
---|
57 | public Permutation(PermutationTypes type, int length)
|
---|
58 | : base(length) {
|
---|
59 | for (int i = 0; i < length; i++)
|
---|
60 | this[i] = i;
|
---|
61 | permutationType = type;
|
---|
62 | }
|
---|
63 | public Permutation(PermutationTypes type, int length, IRandom random)
|
---|
64 | : this(type, length) {
|
---|
65 | Randomize(random);
|
---|
66 | }
|
---|
67 | public Permutation(PermutationTypes type, int[] elements)
|
---|
68 | : base(elements) {
|
---|
69 | permutationType = type;
|
---|
70 | }
|
---|
71 | public Permutation(PermutationTypes type, IntArray elements)
|
---|
72 | : this(type, elements.Length) {
|
---|
73 | for (int i = 0; i < array.Length; i++)
|
---|
74 | array[i] = elements[i];
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
78 | return new Permutation(this, cloner);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public virtual bool Validate() {
|
---|
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 |
|
---|
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 | }
|
---|
109 | }
|
---|
110 | OnReset();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | public void Randomize(IRandom random) {
|
---|
114 | Randomize(random, 0, Length);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public virtual int GetCircular(int position) {
|
---|
118 | if (position >= Length) position = position % Length;
|
---|
119 | while (position < 0) position += Length;
|
---|
120 | return this[position];
|
---|
121 | }
|
---|
122 |
|
---|
123 | public virtual void Swap(int i, int j) {
|
---|
124 | var h = array[i];
|
---|
125 | array[i] = array[j];
|
---|
126 | array[j] = h;
|
---|
127 | OnReset();
|
---|
128 | }
|
---|
129 |
|
---|
130 | public virtual void Reverse(int startIndex, int length) {
|
---|
131 | Array.Reverse(array, startIndex, length);
|
---|
132 | if (length > 1) OnReset();
|
---|
133 | }
|
---|
134 |
|
---|
135 | public virtual void Move(int startIndex, int endIndex, int insertIndex) {
|
---|
136 | if (insertIndex == startIndex) return;
|
---|
137 | if (insertIndex > startIndex && insertIndex <= endIndex) {
|
---|
138 | var start = endIndex + 1;
|
---|
139 | var end = endIndex + insertIndex - startIndex;
|
---|
140 | insertIndex = startIndex;
|
---|
141 | startIndex = start;
|
---|
142 | endIndex = end;
|
---|
143 | }
|
---|
144 | var original = (int[])array.Clone();
|
---|
145 | Array.Copy(original, startIndex, array, insertIndex, endIndex - startIndex + 1);
|
---|
146 | if (insertIndex > endIndex)
|
---|
147 | Array.Copy(original, endIndex + 1, array, startIndex, insertIndex - startIndex);
|
---|
148 | else Array.Copy(original, insertIndex, array, insertIndex + endIndex - startIndex + 1, startIndex - insertIndex);
|
---|
149 | OnReset();
|
---|
150 | }
|
---|
151 |
|
---|
152 | public virtual void Replace(int startIndex, int[] replacement) {
|
---|
153 | Array.Copy(replacement, 0, array, startIndex, replacement.Length);
|
---|
154 | OnReset();
|
---|
155 | }
|
---|
156 |
|
---|
157 | public event EventHandler PermutationTypeChanged;
|
---|
158 |
|
---|
159 | protected virtual void OnPermutationTypeChanged() {
|
---|
160 | var handler = PermutationTypeChanged;
|
---|
161 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|