Changeset 3054 for trunk/sources/HeuristicLab.Encodings.PermutationEncoding
- Timestamp:
- 03/16/10 04:24:01 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Permutation.cs ¶
r3053 r3054 20 20 #endregion 21 21 22 using System;23 using System.Text;24 using HeuristicLab.Common;25 22 using HeuristicLab.Core; 26 23 using HeuristicLab.Data; … … 31 28 [Item("Permutation", "Represents a permutation of integer values.")] 32 29 [Creatable("Test")] 33 public sealed class Permutation : ValueTypeArray<int>, IStringConvertibleArray {30 public class Permutation : IntArray { 34 31 public Permutation() : base() { } 35 32 public Permutation(int length) … … 42 39 Randomize(random); 43 40 } 44 public Permutation(int[] elements) 45 : base(elements) { 41 public Permutation(int[] elements) : base(elements) { } 42 public Permutation(IntArray elements) 43 : this(elements.Length) { 44 for (int i = 0; i < array.Length; i++) 45 array[i] = elements[i]; 46 46 } 47 private Permutation(Permutation elements) : base(elements) { }48 47 49 48 public override IDeepCloneable Clone(Cloner cloner) { 50 Permutation clone = new Permutation( this);49 Permutation clone = new Permutation(array); 51 50 cloner.RegisterClonedObject(this, clone); 52 51 return clone; 53 52 } 54 53 55 public bool Validate() {54 public virtual bool Validate() { 56 55 bool[] values = new bool[Length]; 57 56 int value; … … 68 67 } 69 68 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; 69 public virtual void Randomize(IRandom random, int startIndex, int length) { 70 if (length > 1) { 71 // 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 = array[index1]; 79 array[index1] = array[index2]; 80 array[index2] = val; 81 } 80 82 } 83 OnReset(); 81 84 } 82 85 } … … 85 88 } 86 89 87 public int GetCircular(int position) {90 public virtual int GetCircular(int position) { 88 91 if (position >= Length) position = position % Length; 89 92 while (position < 0) position += Length; 90 93 return this[position]; 91 94 } 92 93 #region IStringConvertibleArrayData Members94 int IStringConvertibleArray.Length {95 get { return Length; }96 set { Length = value; }97 }98 99 bool IStringConvertibleArray.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 IStringConvertibleArray.GetValue(int index) {113 return this[index].ToString();114 }115 bool IStringConvertibleArray.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 #endregion125 95 } 126 96 }
Note: See TracChangeset
for help on using the changeset viewer.