Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3063 was 3054, checked in by swagner, 15 years ago

Refactored classes of HeuristicLab.Data and implemented encoding specific data classes (#909)

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Encodings.PermutationEncoding {
27  [StorableClass]
28  [Item("Permutation", "Represents a permutation of integer values.")]
29  [Creatable("Test")]
30  public class Permutation : IntArray {
31    public Permutation() : base() { }
32    public Permutation(int length)
33      : base(length) {
34      for (int i = 0; i < length; i++)
35        this[i] = i;
36    }
37    public Permutation(int length, IRandom random)
38      : this(length) {
39      Randomize(random);
40    }
41    public Permutation(int[] elements) : base(elements) { }
42    public Permutation(IntArray elements)
43      : this(elements.Length) {
44      for (int i = 0; i < array.Length; i++)
45        array[i] = elements[i];
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      Permutation clone = new Permutation(array);
50      cloner.RegisterClonedObject(this, clone);
51      return clone;
52    }
53
54    public virtual bool Validate() {
55      bool[] values = new bool[Length];
56      int value;
57
58      for (int i = 0; i < values.Length; i++)
59        values[i] = false;
60      for (int i = 0; i < Length; i++) {
61        value = this[i];
62        if ((value < 0) || (value >= values.Length)) return false;
63        if (values[value]) return false;
64        values[value] = true;
65      }
66      return true;
67    }
68
69    public virtual void Randomize(IRandom random, int startIndex, int length) {
70      if (length > 1) {
71        // Knuth shuffle
72        int index1, index2;
73        int val;
74        for (int i = length - 1; i > 0; i--) {
75          index1 = startIndex + i;
76          index2 = startIndex + random.Next(i + 1);
77          if (index1 != index2) {
78            val = array[index1];
79            array[index1] = array[index2];
80            array[index2] = val;
81          }
82        }
83        OnReset();
84      }
85    }
86    public void Randomize(IRandom random) {
87      Randomize(random, 0, Length);
88    }
89
90    public virtual int GetCircular(int position) {
91      if (position >= Length) position = position % Length;
92      while (position < 0) position += Length;
93      return this[position];
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.