Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12012 was 12012, checked in by ascheibe, 9 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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