Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3337 was 3317, checked in by swagner, 15 years ago

Implemented ReadOnlyView property for items (#969).

File size: 3.7 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      clone.ReadOnlyView = ReadOnlyView;
68      return clone;
69    }
70
71    public virtual bool Validate() {
72      bool[] values = new bool[Length];
73      int value;
74
75      for (int i = 0; i < values.Length; i++)
76        values[i] = false;
77      for (int i = 0; i < Length; i++) {
78        value = this[i];
79        if ((value < 0) || (value >= values.Length)) return false;
80        if (values[value]) return false;
81        values[value] = true;
82      }
83      return true;
84    }
85
86    public virtual void Randomize(IRandom random, int startIndex, int length) {
87      if (length > 1) {
88        // Knuth shuffle
89        int index1, index2;
90        int val;
91        for (int i = length - 1; i > 0; i--) {
92          index1 = startIndex + i;
93          index2 = startIndex + random.Next(i + 1);
94          if (index1 != index2) {
95            val = array[index1];
96            array[index1] = array[index2];
97            array[index2] = val;
98          }
99        }
100        OnReset();
101      }
102    }
103    public void Randomize(IRandom random) {
104      Randomize(random, 0, Length);
105    }
106
107    public virtual int GetCircular(int position) {
108      if (position >= Length) position = position % Length;
109      while (position < 0) position += Length;
110      return this[position];
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.