Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Permutation/3.3/Permutation.cs @ 2861

Last change on this file since 2861 was 2830, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 4.4 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.Permutation {
30  [EmptyStorableClass]
31  [Item("Permutation", "Represents a permutation of integer values.")]
32  [Creatable("Test")]
33  public sealed class Permutation : ValueTypeArrayData<int>, IStringConvertibleMatrixData {
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    #region IStringConvertibleMatrixData Members
88    StringConvertibleArrayDataDimensions IStringConvertibleMatrixData.Dimensions {
89      get { return StringConvertibleArrayDataDimensions.Rows; }
90    }
91    int IStringConvertibleMatrixData.Rows {
92      get { return Length; }
93      set { Length = value; }
94    }
95    int IStringConvertibleMatrixData.Columns {
96      get { return 1; }
97      set { throw new NotSupportedException("Columns cannot be changed."); }
98    }
99
100    bool IStringConvertibleMatrixData.Validate(string value, out string errorMessage) {
101      int val;
102      bool valid = int.TryParse(value, out val);
103      errorMessage = string.Empty;
104      if (!valid) {
105        StringBuilder sb = new StringBuilder();
106        sb.Append("Invalid Value (Valid Value Format: \"");
107        sb.Append(FormatPatterns.GetIntFormatPattern());
108        sb.Append("\")");
109        errorMessage = sb.ToString();
110      }
111      return valid;
112    }
113    string IStringConvertibleMatrixData.GetValue(int rowIndex, int columIndex) {
114      return this[rowIndex].ToString();
115    }
116    bool IStringConvertibleMatrixData.SetValue(string value, int rowIndex, int columnIndex) {
117      int val;
118      if (int.TryParse(value, out val)) {
119        this[rowIndex] = val;
120        return true;
121      } else {
122        return false;
123      }
124    }
125    event EventHandler<EventArgs<int, int>> IStringConvertibleMatrixData.ItemChanged {
126      add { base.ItemChanged += value; }
127      remove { base.ItemChanged -= value; }
128    }
129    event EventHandler IStringConvertibleMatrixData.Reset {
130      add { base.Reset += value; }
131      remove { base.Reset -= value; }
132    }
133    #endregion
134  }
135}
Note: See TracBrowser for help on using the repository browser.