Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3021 was 3017, checked in by epitzer, 15 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

File size: 3.9 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 System;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.Permutation {
30  [StorableClass]
31  [Item("Permutation", "Represents a permutation of integer values.")]
32  [Creatable("Test")]
33  public sealed class Permutation : ValueTypeArrayData<int>, IStringConvertibleArrayData {
34    public Permutation() : base() { }
35    public Permutation(int length)
36      : base(length) {
37      for (int i = 0; i < length; i++)
38        this[i] = i;
39    }
40    public Permutation(int length, IRandom random)
41      : this(length) {
42      Randomize(random);
43    }
44    public Permutation(int[] elements)
45      : base(elements) {
46    }
47    private Permutation(Permutation elements) : base(elements) { }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      Permutation clone = new Permutation(this);
51      cloner.RegisterClonedObject(this, clone);
52      return clone;
53    }
54
55    public bool Validate() {
56      bool[] values = new bool[Length];
57      int value;
58
59      for (int i = 0; i < values.Length; i++)
60        values[i] = false;
61      for (int i = 0; i < Length; i++) {
62        value = this[i];
63        if ((value < 0) || (value >= values.Length)) return false;
64        if (values[value]) return false;
65        values[value] = true;
66      }
67      return true;
68    }
69
70    public void Randomize(IRandom random, int startIndex, int length) {  // Knuth shuffle
71      int index1, index2;
72      int val;
73      for (int i = length - 1; i > 0; i--) {
74        index1 = startIndex + i;
75        index2 = startIndex + random.Next(i + 1);
76        if (index1 != index2) {
77          val = this[index1];
78          this[index1] = this[index2];
79          this[index2] = val;
80        }
81      }
82    }
83    public void Randomize(IRandom random) {
84      Randomize(random, 0, Length);
85    }
86
87    public int GetCircular(int position) {
88      if (position >= Length) position = position % Length;
89      while (position < 0) position += Length;
90      return this[position];
91    }
92
93    #region IStringConvertibleArrayData Members
94    int IStringConvertibleArrayData.Length {
95      get { return Length; }
96      set { Length = value; }
97    }
98
99    bool IStringConvertibleArrayData.Validate(string value, out string errorMessage) {
100      int val;
101      bool valid = int.TryParse(value, out val);
102      errorMessage = string.Empty;
103      if (!valid) {
104        StringBuilder sb = new StringBuilder();
105        sb.Append("Invalid Value (Valid Value Format: \"");
106        sb.Append(FormatPatterns.GetIntFormatPattern());
107        sb.Append("\")");
108        errorMessage = sb.ToString();
109      }
110      return valid;
111    }
112    string IStringConvertibleArrayData.GetValue(int index) {
113      return this[index].ToString();
114    }
115    bool IStringConvertibleArrayData.SetValue(string value, int index) {
116      int val;
117      if (int.TryParse(value, out val)) {
118        this[index] = val;
119        return true;
120      } else {
121        return false;
122      }
123    }
124    #endregion
125  }
126}
Note: See TracBrowser for help on using the repository browser.