Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Adapted PermutationEncoding and TSP.

File size: 4.2 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
[6628]22using System;
[3376]23using HeuristicLab.Common;
[2526]24using HeuristicLab.Core;
[2]25using HeuristicLab.Data;
[13366]26using HeuristicLab.Optimization;
[2794]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]28
[3053]29namespace HeuristicLab.Encodings.PermutationEncoding {
[3231]30  [Item("Permutation", "Represents a permutation of integer values.")]
[3017]31  [StorableClass]
[13366]32  public class Permutation : IntArray, ISolution {
[3231]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; }
[6628]40      set {
41        bool changed = (permutationType != value);
42        permutationType = value;
43        if (changed) OnPermutationTypeChanged();
44      }
[3231]45    }
46
[4722]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;
[3231]52    }
[4722]53    public Permutation() : this(PermutationTypes.RelativeUndirected) { }
[6628]54    public Permutation(PermutationTypes type)
55      : base() {
[3231]56      permutationType = type;
57    }
58    public Permutation(PermutationTypes type, int length)
[2794]59      : base(length) {
60      for (int i = 0; i < length; i++)
61        this[i] = i;
[3231]62      permutationType = type;
[2]63    }
[3231]64    public Permutation(PermutationTypes type, int length, IRandom random)
65      : this(type, length) {
[2794]66      Randomize(random);
[2]67    }
[6628]68    public Permutation(PermutationTypes type, int[] elements)
69      : base(elements) {
[3231]70      permutationType = type;
71    }
[6628]72    public Permutation(PermutationTypes type, IntArray elements)
73      : this(type, elements.Length) {
[3054]74      for (int i = 0; i < array.Length; i++)
75        array[i] = elements[i];
[2794]76    }
[2]77
[2794]78    public override IDeepCloneable Clone(Cloner cloner) {
[4722]79      return new Permutation(this, cloner);
[2]80    }
[2794]81
[3054]82    public virtual bool Validate() {
[2794]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
[3054]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          }
[2794]110        }
[3054]111        OnReset();
[2794]112      }
113    }
114    public void Randomize(IRandom random) {
115      Randomize(random, 0, Length);
116    }
117
[3054]118    public virtual int GetCircular(int position) {
[2997]119      if (position >= Length) position = position % Length;
120      while (position < 0) position += Length;
121      return this[position];
122    }
[6628]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    }
[2]130  }
131}
Note: See TracBrowser for help on using the repository browser.