source: branches/ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/Permutation.cs @ 13366

Last change on this file since 13366 was 13366, checked in by mkommend, 7 years ago

#2521: Adapted PermutationEncoding and TSP.

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