Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/16/10 04:24:01 (15 years ago)
Author:
swagner
Message:

Refactored classes of HeuristicLab.Data and implemented encoding specific data classes (#909)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Permutation.cs

    r3053 r3054  
    2020#endregion
    2121
    22 using System;
    23 using System.Text;
    24 using HeuristicLab.Common;
    2522using HeuristicLab.Core;
    2623using HeuristicLab.Data;
     
    3128  [Item("Permutation", "Represents a permutation of integer values.")]
    3229  [Creatable("Test")]
    33   public sealed class Permutation : ValueTypeArray<int>, IStringConvertibleArray {
     30  public class Permutation : IntArray {
    3431    public Permutation() : base() { }
    3532    public Permutation(int length)
     
    4239      Randomize(random);
    4340    }
    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];
    4646    }
    47     private Permutation(Permutation elements) : base(elements) { }
    4847
    4948    public override IDeepCloneable Clone(Cloner cloner) {
    50       Permutation clone = new Permutation(this);
     49      Permutation clone = new Permutation(array);
    5150      cloner.RegisterClonedObject(this, clone);
    5251      return clone;
    5352    }
    5453
    55     public bool Validate() {
     54    public virtual bool Validate() {
    5655      bool[] values = new bool[Length];
    5756      int value;
     
    6867    }
    6968
    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          }
    8082        }
     83        OnReset();
    8184      }
    8285    }
     
    8588    }
    8689
    87     public int GetCircular(int position) {
     90    public virtual int GetCircular(int position) {
    8891      if (position >= Length) position = position % Length;
    8992      while (position < 0) position += Length;
    9093      return this[position];
    9194    }
    92 
    93     #region IStringConvertibleArrayData Members
    94     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     #endregion
    12595  }
    12696}
Note: See TracChangeset for help on using the changeset viewer.