Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 3.8 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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
[3376]22using HeuristicLab.Common;
[2526]23using HeuristicLab.Core;
[2]24using HeuristicLab.Data;
[2794]25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]26
[3053]27namespace HeuristicLab.Encodings.PermutationEncoding {
[3231]28  [Item("Permutation", "Represents a permutation of integer values.")]
[3017]29  [StorableClass]
[3054]30  public class Permutation : IntArray {
[3231]31    [Storable]
32    private PermutationTypes permutationType;
33    /// <summary>
34    /// Gets the type of the permutation (see <see cref="PermutationType"/>).
35    /// </summary>
36    public PermutationTypes PermutationType {
37      get { return permutationType; }
38    }
39
[4722]40    [StorableConstructor]
41    protected Permutation(bool deserializing) : base(deserializing) { }
42    protected Permutation(Permutation original, Cloner cloner)
43      : base(original, cloner) {
44      this.permutationType = original.permutationType;
[3231]45    }
[4722]46    public Permutation() : this(PermutationTypes.RelativeUndirected) { }
[3231]47    public Permutation(PermutationTypes type) : base() {
48      permutationType = type;
49    }
50    public Permutation(PermutationTypes type, int length)
[2794]51      : base(length) {
52      for (int i = 0; i < length; i++)
53        this[i] = i;
[3231]54      permutationType = type;
[2]55    }
[3231]56    public Permutation(PermutationTypes type, int length, IRandom random)
57      : this(type, length) {
[2794]58      Randomize(random);
[2]59    }
[3231]60    public Permutation(PermutationTypes type, int[] elements) : base(elements) {
61      permutationType = type;
62    }
63    public Permutation(PermutationTypes type, IntArray elements) : this(type, elements.Length) {
[3054]64      for (int i = 0; i < array.Length; i++)
65        array[i] = elements[i];
[2794]66    }
[2]67
[2794]68    public override IDeepCloneable Clone(Cloner cloner) {
[4722]69      return new Permutation(this, cloner);
[2]70    }
[2794]71
[3054]72    public virtual bool Validate() {
[2794]73      bool[] values = new bool[Length];
74      int value;
75
76      for (int i = 0; i < values.Length; i++)
77        values[i] = false;
78      for (int i = 0; i < Length; i++) {
79        value = this[i];
80        if ((value < 0) || (value >= values.Length)) return false;
81        if (values[value]) return false;
82        values[value] = true;
83      }
84      return true;
85    }
86
[3054]87    public virtual void Randomize(IRandom random, int startIndex, int length) {
88      if (length > 1) {
89        // Knuth shuffle
90        int index1, index2;
91        int val;
92        for (int i = length - 1; i > 0; i--) {
93          index1 = startIndex + i;
94          index2 = startIndex + random.Next(i + 1);
95          if (index1 != index2) {
96            val = array[index1];
97            array[index1] = array[index2];
98            array[index2] = val;
99          }
[2794]100        }
[3054]101        OnReset();
[2794]102      }
103    }
104    public void Randomize(IRandom random) {
105      Randomize(random, 0, Length);
106    }
107
[3054]108    public virtual int GetCircular(int position) {
[2997]109      if (position >= Length) position = position % Length;
110      while (position < 0) position += Length;
111      return this[position];
112    }
[2]113  }
114}
Note: See TracBrowser for help on using the repository browser.