Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3160 was 3160, checked in by swagner, 14 years ago

Removed Creatable test attribute (#935).

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  public class Permutation : IntArray {
30    public Permutation() : base() { }
31    public Permutation(int length)
32      : base(length) {
33      for (int i = 0; i < length; i++)
34        this[i] = i;
35    }
36    public Permutation(int length, IRandom random)
37      : this(length) {
38      Randomize(random);
39    }
40    public Permutation(int[] elements) : base(elements) { }
41    public Permutation(IntArray elements)
42      : this(elements.Length) {
43      for (int i = 0; i < array.Length; i++)
44        array[i] = elements[i];
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      Permutation clone = new Permutation(array);
49      cloner.RegisterClonedObject(this, clone);
50      return clone;
51    }
52
53    public virtual bool Validate() {
54      bool[] values = new bool[Length];
55      int value;
56
57      for (int i = 0; i < values.Length; i++)
58        values[i] = false;
59      for (int i = 0; i < Length; i++) {
60        value = this[i];
61        if ((value < 0) || (value >= values.Length)) return false;
62        if (values[value]) return false;
63        values[value] = true;
64      }
65      return true;
66    }
67
68    public virtual void Randomize(IRandom random, int startIndex, int length) {
69      if (length > 1) {
70        // Knuth shuffle
71        int index1, index2;
72        int val;
73        for (int i = length - 1; i > 0; i--) {
74          index1 = startIndex + i;
75          index2 = startIndex + random.Next(i + 1);
76          if (index1 != index2) {
77            val = array[index1];
78            array[index1] = array[index2];
79            array[index2] = val;
80          }
81        }
82        OnReset();
83      }
84    }
85    public void Randomize(IRandom random) {
86      Randomize(random, 0, Length);
87    }
88
89    public virtual int GetCircular(int position) {
90      if (position >= Length) position = position % Length;
91      while (position < 0) position += Length;
92      return this[position];
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.