Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/Permutation.cs @ 16723

Last change on this file since 16723 was 16723, checked in by abeham, 5 years ago

#2521: merged changes from r15684 to trunk HEAD (r16716) and resolved all merge conflicts

  • it doesn't build
File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HEAL.Attic;
28
29namespace HeuristicLab.Encodings.PermutationEncoding {
30  [Item("Permutation", "Represents a permutation of integer values.")]
31  [StorableType("FBBCFA53-C1AE-4069-907B-99C720F5AC51")]
32  public class Permutation : IntArray, ISolution {
33    [Storable]
34    private PermutationTypes permutationType;
35    /// <summary>
36    /// Gets the type of the permutation (see <see cref="PermutationType"/>).
37    /// </summary>
38    public PermutationTypes PermutationType {
39      get { return permutationType; }
40      set {
41        bool changed = (permutationType != value);
42        permutationType = value;
43        if (changed) OnPermutationTypeChanged();
44      }
45    }
46
47    [StorableConstructor]
48    protected Permutation(StorableConstructorFlag _) : base(_) { }
49    protected Permutation(Permutation original, Cloner cloner)
50      : base(original, cloner) {
51      this.permutationType = original.permutationType;
52    }
53    public Permutation() : this(PermutationTypes.RelativeUndirected) { }
54    public Permutation(PermutationTypes type)
55      : base() {
56      permutationType = type;
57    }
58    public Permutation(PermutationTypes type, int length)
59      : base(length) {
60      for (int i = 0; i < length; i++)
61        this[i] = i;
62      permutationType = type;
63    }
64    public Permutation(PermutationTypes type, int length, IRandom random)
65      : this(type, length) {
66      Randomize(random);
67    }
68    public Permutation(PermutationTypes type, int[] elements)
69      : base(elements) {
70      permutationType = type;
71    }
72    public Permutation(PermutationTypes type, IntArray elements)
73      : this(type, elements.Length) {
74      for (int i = 0; i < array.Length; i++)
75        array[i] = elements[i];
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new Permutation(this, cloner);
80    }
81
82    public virtual bool Validate() {
83      bool[] values = new bool[Length];
84      int value;
85
86      for (int i = 0; i < values.Length; i++)
87        values[i] = false;
88      for (int i = 0; i < Length; i++) {
89        value = this[i];
90        if ((value < 0) || (value >= values.Length)) return false;
91        if (values[value]) return false;
92        values[value] = true;
93      }
94      return true;
95    }
96
97    public virtual void Randomize(IRandom random, int startIndex, int length) {
98      if (length > 1) {
99        // Knuth shuffle
100        int index1, index2;
101        int val;
102        for (int i = length - 1; i > 0; i--) {
103          index1 = startIndex + i;
104          index2 = startIndex + random.Next(i + 1);
105          if (index1 != index2) {
106            val = array[index1];
107            array[index1] = array[index2];
108            array[index2] = val;
109          }
110        }
111        OnReset();
112      }
113    }
114    public void Randomize(IRandom random) {
115      Randomize(random, 0, Length);
116    }
117
118    public virtual int GetCircular(int position) {
119      if (position >= Length) position = position % Length;
120      while (position < 0) position += Length;
121      return this[position];
122    }
123
124    public virtual void Swap(int i, int j) {
125      var h = array[i];
126      array[i] = array[j];
127      array[j] = h;
128      OnReset();
129    }
130
131    public virtual void Reverse(int startIndex, int length) {
132      Array.Reverse(array, startIndex, length);
133      if (length > 1) OnReset();
134    }
135
136    public virtual void Move(int startIndex, int endIndex, int insertIndex) {
137      if (insertIndex == startIndex) return;
138      if (insertIndex > startIndex && insertIndex <= endIndex) {
139        var start = endIndex + 1;
140        var end = endIndex + insertIndex - startIndex;
141        insertIndex = startIndex;
142        startIndex = start;
143        endIndex = end;
144      }
145      var original = (int[])array.Clone();
146      Array.Copy(original, startIndex, array, insertIndex, endIndex - startIndex + 1);
147      if (insertIndex > endIndex)
148        Array.Copy(original, endIndex + 1, array, startIndex, insertIndex - startIndex);
149      else Array.Copy(original, insertIndex, array, insertIndex + endIndex - startIndex + 1, startIndex - insertIndex);
150      OnReset();
151    }
152
153    public virtual void Replace(int startIndex, int[] replacement) {
154      Array.Copy(replacement, 0, array, startIndex, replacement.Length);
155      OnReset();
156    }
157
158    public event EventHandler PermutationTypeChanged;
159
160    protected virtual void OnPermutationTypeChanged() {
161      var handler = PermutationTypeChanged;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.