Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3231 was 3231, checked in by abeham, 14 years ago

Added a permutation type property specifying whether it's a relative (directed or undirected) or absolute permutation #889

File size: 3.6 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  [Item("Permutation", "Represents a permutation of integer values.")]
28  [StorableClass]
29  public class Permutation : IntArray {
30    [Storable]
31    private PermutationTypes permutationType;
32    /// <summary>
33    /// Gets the type of the permutation (see <see cref="PermutationType"/>).
34    /// </summary>
35    public PermutationTypes PermutationType {
36      get { return permutationType; }
37    }
38
39    public Permutation() : base() {
40      permutationType = PermutationTypes.RelativeUndirected;
41    }
42    public Permutation(PermutationTypes type) : base() {
43      permutationType = type;
44    }
45
46    public Permutation(PermutationTypes type, int length)
47      : base(length) {
48      for (int i = 0; i < length; i++)
49        this[i] = i;
50      permutationType = type;
51    }
52    public Permutation(PermutationTypes type, int length, IRandom random)
53      : this(type, length) {
54      Randomize(random);
55    }
56    public Permutation(PermutationTypes type, int[] elements) : base(elements) {
57      permutationType = type;
58    }
59    public Permutation(PermutationTypes type, IntArray elements) : this(type, elements.Length) {
60      for (int i = 0; i < array.Length; i++)
61        array[i] = elements[i];
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      Permutation clone = new Permutation(permutationType, array);
66      cloner.RegisterClonedObject(this, clone);
67      return clone;
68    }
69
70    public virtual bool Validate() {
71      bool[] values = new bool[Length];
72      int value;
73
74      for (int i = 0; i < values.Length; i++)
75        values[i] = false;
76      for (int i = 0; i < Length; i++) {
77        value = this[i];
78        if ((value < 0) || (value >= values.Length)) return false;
79        if (values[value]) return false;
80        values[value] = true;
81      }
82      return true;
83    }
84
85    public virtual void Randomize(IRandom random, int startIndex, int length) {
86      if (length > 1) {
87        // Knuth shuffle
88        int index1, index2;
89        int val;
90        for (int i = length - 1; i > 0; i--) {
91          index1 = startIndex + i;
92          index2 = startIndex + random.Next(i + 1);
93          if (index1 != index2) {
94            val = array[index1];
95            array[index1] = array[index2];
96            array[index2] = val;
97          }
98        }
99        OnReset();
100      }
101    }
102    public void Randomize(IRandom random) {
103      Randomize(random, 0, Length);
104    }
105
106    public virtual int GetCircular(int position) {
107      if (position >= Length) position = position % Length;
108      while (position < 0) position += Length;
109      return this[position];
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.