Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Permutation.cs @ 15428

Last change on this file since 15428 was 14662, checked in by abeham, 8 years ago

#2731: Added better performing manipulation methods to Permutation and adapted the manipulators to use them

  • Added unit tests
File size: 5.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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]22using System;
[3376]23using HeuristicLab.Common;
[2526]24using HeuristicLab.Core;
[2]25using HeuristicLab.Data;
[2794]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]27
[3053]28namespace 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
[14662]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
[6628]157    public event EventHandler PermutationTypeChanged;
158
159    protected virtual void OnPermutationTypeChanged() {
160      var handler = PermutationTypeChanged;
161      if (handler != null) handler(this, EventArgs.Empty);
162    }
[2]163  }
164}
Note: See TracBrowser for help on using the repository browser.