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 |
|
---|
22 | using System;
|
---|
23 | using System.Text;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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 | if (!Validate()) throw new ArgumentException("Elements do not represent a valid permutation.");
|
---|
47 | }
|
---|
48 | private Permutation(Permutation elements) : base(elements) { }
|
---|
49 |
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | Permutation clone = new Permutation(this);
|
---|
52 | cloner.RegisterClonedObject(this, clone);
|
---|
53 | return clone;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public bool Validate() {
|
---|
57 | bool[] values = new bool[Length];
|
---|
58 | int value;
|
---|
59 |
|
---|
60 | for (int i = 0; i < values.Length; i++)
|
---|
61 | values[i] = false;
|
---|
62 | for (int i = 0; i < Length; i++) {
|
---|
63 | value = this[i];
|
---|
64 | if ((value < 0) || (value >= values.Length)) return false;
|
---|
65 | if (values[value]) return false;
|
---|
66 | values[value] = true;
|
---|
67 | }
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void Randomize(IRandom random, int startIndex, int length) { // Knuth shuffle
|
---|
72 | int index1, index2;
|
---|
73 | int val;
|
---|
74 | for (int i = length - 1; i > 0; i--) {
|
---|
75 | index1 = startIndex + i;
|
---|
76 | index2 = startIndex + random.Next(i + 1);
|
---|
77 | if (index1 != index2) {
|
---|
78 | val = this[index1];
|
---|
79 | this[index1] = this[index2];
|
---|
80 | this[index2] = val;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | public void Randomize(IRandom random) {
|
---|
85 | Randomize(random, 0, Length);
|
---|
86 | }
|
---|
87 |
|
---|
88 | #region IStringConvertibleMatrixData Members
|
---|
89 | StringConvertibleArrayDataDimensions IStringConvertibleMatrixData.Dimensions {
|
---|
90 | get { return StringConvertibleArrayDataDimensions.Rows; }
|
---|
91 | }
|
---|
92 | int IStringConvertibleMatrixData.Rows {
|
---|
93 | get { return Length; }
|
---|
94 | set { Length = value; }
|
---|
95 | }
|
---|
96 | int IStringConvertibleMatrixData.Columns {
|
---|
97 | get { return 1; }
|
---|
98 | set { throw new NotSupportedException("Columns cannot be changed."); }
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool IStringConvertibleMatrixData.Validate(string value, out string errorMessage) {
|
---|
102 | int val;
|
---|
103 | bool valid = int.TryParse(value, out val);
|
---|
104 | errorMessage = string.Empty;
|
---|
105 | if (!valid) {
|
---|
106 | StringBuilder sb = new StringBuilder();
|
---|
107 | sb.Append("Invalid Value (Valid Value Format: \"");
|
---|
108 | sb.Append(FormatPatterns.GetIntFormatPattern());
|
---|
109 | sb.Append("\")");
|
---|
110 | errorMessage = sb.ToString();
|
---|
111 | }
|
---|
112 | return valid;
|
---|
113 | }
|
---|
114 | string IStringConvertibleMatrixData.GetValue(int rowIndex, int columIndex) {
|
---|
115 | return this[rowIndex].ToString();
|
---|
116 | }
|
---|
117 | bool IStringConvertibleMatrixData.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
118 | int val;
|
---|
119 | if (int.TryParse(value, out val)) {
|
---|
120 | this[rowIndex] = val;
|
---|
121 | return true;
|
---|
122 | } else {
|
---|
123 | return false;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | event EventHandler<EventArgs<int, int>> IStringConvertibleMatrixData.ItemChanged {
|
---|
127 | add { base.ItemChanged += value; }
|
---|
128 | remove { base.ItemChanged -= value; }
|
---|
129 | }
|
---|
130 | event EventHandler IStringConvertibleMatrixData.Reset {
|
---|
131 | add { base.Reset += value; }
|
---|
132 | remove { base.Reset -= value; }
|
---|
133 | }
|
---|
134 | #endregion
|
---|
135 | }
|
---|
136 | }
|
---|