Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/13/09 15:38:26 (15 years ago)
Author:
gkronber
Message:

fixed calculation of range of a variable (to ignore NaNs) and fixed calculation of maxPunishment in the BakedTreeEvaluator. #615 (Evaluation of HL3 function trees should be equivalent to evaluation in HL2)

Location:
trunk/sources/HeuristicLab.DataAnalysis/3.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAnalysis/3.2/Dataset.cs

    r1784 r1786  
    5252    public int Columns {
    5353      get { return columns; }
    54       set { 
     54      set {
    5555        columns = value;
    5656        if (variableNames == null || variableNames.Length != columns) {
     
    7272
    7373    public void SetValue(int i, int j, double v) {
    74       if(v != samples[columns * i + j]) {
     74      if (v != samples[columns * i + j]) {
    7575        samples[columns * i + j] = v;
    7676        CreateDictionaries();
     
    8484        scalingFactor = new double[columns];
    8585        scalingOffset = new double[columns];
    86         for(int i = 0; i < scalingFactor.Length; i++) {
     86        for (int i = 0; i < scalingFactor.Length; i++) {
    8787          scalingFactor[i] = 1.0;
    8888          scalingOffset[i] = 0.0;
     
    110110      cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
    111111      cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
    112       for(int i = 0; i < columns; i++) {
     112      for (int i = 0; i < columns; i++) {
    113113        cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
    114114        cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>();
     
    176176
    177177      variableNames = ParseVariableNamesString(node.Attributes["VariableNames"].Value);
    178       if(node.Attributes["ScalingFactors"] != null)
     178      if (node.Attributes["ScalingFactors"] != null)
    179179        scalingFactor = ParseDoubleString(node.Attributes["ScalingFactors"].Value);
    180180      else {
    181181        scalingFactor = new double[columns]; // compatibility with old serialization format
    182         for(int i = 0; i < scalingFactor.Length; i++) scalingFactor[i] = 1.0;
    183       }
    184       if(node.Attributes["ScalingOffsets"] != null)
     182        for (int i = 0; i < scalingFactor.Length; i++) scalingFactor[i] = 1.0;
     183      }
     184      if (node.Attributes["ScalingOffsets"] != null)
    185185        scalingOffset = ParseDoubleString(node.Attributes["ScalingOffsets"].Value);
    186186      else {
    187187        scalingOffset = new double[columns]; // compatibility with old serialization format
    188         for(int i = 0; i < scalingOffset.Length; i++) scalingOffset[i] = 0.0;
     188        for (int i = 0; i < scalingOffset.Length; i++) scalingOffset[i] = 0.0;
    189189      }
    190190
    191191      string[] tokens = node.InnerText.Split(';');
    192       if(tokens.Length != rows * columns) throw new FormatException();
     192      if (tokens.Length != rows * columns) throw new FormatException();
    193193      samples = new double[rows * columns];
    194       for(int row = 0; row < rows; row++) {
    195         for(int column = 0; column < columns; column++) {
    196           if(double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) {
     194      for (int row = 0; row < rows; row++) {
     195        for (int column = 0; column < columns; column++) {
     196          if (double.TryParse(tokens[row * columns + column], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out samples[row * columns + column]) == false) {
    197197            throw new FormatException("Can't parse " + tokens[row * columns + column] + " as double value.");
    198198          }
     
    208208    private string ToString(NumberFormatInfo format) {
    209209      StringBuilder builder = new StringBuilder();
    210       for(int row = 0; row < rows; row++) {
    211         for(int column = 0; column < columns; column++) {
     210      for (int row = 0; row < rows; row++) {
     211        for (int column = 0; column < columns; column++) {
    212212          builder.Append(";");
    213213          builder.Append(samples[row * columns + column].ToString("r", format));
    214214        }
    215215      }
    216       if(builder.Length > 0) builder.Remove(0, 1);
     216      if (builder.Length > 0) builder.Remove(0, 1);
    217217      return builder.ToString();
    218218    }
     
    220220    private string GetVariableNamesString() {
    221221      string s = "";
    222       for(int i = 0; i < variableNames.Length; i++) {
     222      for (int i = 0; i < variableNames.Length; i++) {
    223223        s += variableNames[i] + "; ";
    224224      }
    225225
    226       if(variableNames.Length > 0) {
     226      if (variableNames.Length > 0) {
    227227        s = s.TrimEnd(';', ' ');
    228228      }
     
    231231    private string GetString(double[] xs) {
    232232      string s = "";
    233       for(int i = 0; i < xs.Length; i++) {
     233      for (int i = 0; i < xs.Length; i++) {
    234234        s += xs[i].ToString("r", CultureInfo.InvariantCulture) + "; ";
    235235      }
    236236
    237       if(xs.Length > 0) {
     237      if (xs.Length > 0) {
    238238        s = s.TrimEnd(';', ' ');
    239239      }
     
    244244      p = p.Trim();
    245245      string[] tokens = p.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    246       for(int i = 0; i < tokens.Length; i++) tokens[i] = tokens[i].Trim();
     246      for (int i = 0; i < tokens.Length; i++) tokens[i] = tokens[i].Trim();
    247247      return tokens;
    248248    }
     
    251251      string[] ss = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    252252      double[] xs = new double[ss.Length];
    253       for(int i = 0; i < xs.Length; i++) {
     253      for (int i = 0; i < xs.Length; i++) {
    254254        xs[i] = double.Parse(ss[i], CultureInfo.InvariantCulture);
    255255      }
     
    262262
    263263    public double GetMean(int column, int from, int to) {
    264       if(!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) {
     264      if (!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) {
    265265        double[] values = new double[to - from];
    266         for(int sample = from; sample < to; sample++) {
     266        for (int sample = from; sample < to; sample++) {
    267267          values[sample - from] = GetValue(sample, column);
    268268        }
    269269        double mean = Statistics.Mean(values);
    270         if(!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>();
     270        if (!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>();
    271271        cachedMeans[column][from][to] = mean;
    272272        return mean;
     
    281281
    282282    public double GetRange(int column, int from, int to) {
    283       if(!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) {
     283      if (!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) {
    284284        double[] values = new double[to - from];
    285         for(int sample = from; sample < to; sample++) {
     285        for (int sample = from; sample < to; sample++) {
    286286          values[sample - from] = GetValue(sample, column);
    287287        }
    288288        double range = Statistics.Range(values);
    289         if(!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>();
     289        if (!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>();
    290290        cachedRanges[column][from][to] = range;
    291291        return range;
     
    297297    public double GetMaximum(int column) {
    298298      double max = Double.NegativeInfinity;
    299       for(int i = 0; i < Rows; i++) {
     299      for (int i = 0; i < Rows; i++) {
    300300        double val = GetValue(i, column);
    301         if(val > max) max = val;
     301        if (!double.IsNaN(val) && val > max) max = val;
    302302      }
    303303      return max;
     
    306306    public double GetMinimum(int column) {
    307307      double min = Double.PositiveInfinity;
    308       for(int i = 0; i < Rows; i++) {
     308      for (int i = 0; i < Rows; i++) {
    309309        double val = GetValue(i, column);
    310         if(val < min) min = val;
     310        if (!double.IsNaN(val) && val < min) min = val;
    311311      }
    312312      return min;
     
    314314
    315315    internal void ScaleVariable(int column) {
    316       if(scalingFactor[column] == 1.0 && scalingOffset[column] == 0.0) {
     316      if (scalingFactor[column] == 1.0 && scalingOffset[column] == 0.0) {
    317317        double min = GetMinimum(column);
    318318        double max = GetMaximum(column);
    319319        double range = max - min;
    320         if(range == 0) ScaleVariable(column, 1.0, -min);
     320        if (range == 0) ScaleVariable(column, 1.0, -min);
    321321        else ScaleVariable(column, 1.0 / range, -min);
    322322      }
     
    328328      scalingFactor[column] = factor;
    329329      scalingOffset[column] = offset;
    330       for(int i = 0; i < Rows; i++) {
     330      for (int i = 0; i < Rows; i++) {
    331331        double origValue = samples[i * columns + column];
    332332        samples[i * columns + column] = (origValue + offset) * factor;
     
    337337
    338338    internal void UnscaleVariable(int column) {
    339       if(scalingFactor[column] != 1.0 || scalingOffset[column]!=0.0) {
    340         for(int i = 0; i < rows; i++) {
     339      if (scalingFactor[column] != 1.0 || scalingOffset[column] != 0.0) {
     340        for (int i = 0; i < rows; i++) {
    341341          double scaledValue = samples[i * columns + column];
    342342          samples[i * columns + column] = scaledValue / scalingFactor[column] - scalingOffset[column];
  • trunk/sources/HeuristicLab.DataAnalysis/3.2/Statistics.cs

    r1529 r1786  
    4141      T minimum = enumerator.Current;
    4242
    43       while(enumerator.MoveNext()) {
     43      while (enumerator.MoveNext()) {
    4444        T current = enumerator.Current;
    45         if(current.CompareTo(minimum) < 0) {
     45        if (current.CompareTo(minimum) < 0) {
    4646          minimum = current;
    4747        }
     
    6565      T maximum = enumerator.Current;
    6666
    67       while(enumerator.MoveNext()) {
     67      while (enumerator.MoveNext()) {
    6868        T current = enumerator.Current;
    69         if(current.CompareTo(maximum) > 0) {
     69        if (current.CompareTo(maximum) > 0) {
    7070          maximum = current;
    7171        }
     
    9999    /// <returns></returns>
    100100    public static double Range(double[] values, int start, int end) {
    101       if(start < 0 || start > values.Length || end < 0 || end > values.Length || start > end) {
     101      if (start < 0 || start > values.Length || end < 0 || end > values.Length || start > end) {
    102102        throw new InvalidOperationException();
    103103      }
     
    105105      double minimum = values[start];
    106106      double maximum = minimum;
    107       for(int i = start; i < end; i++) {
    108         if(values[i] > maximum) {
    109           maximum = values[i];
    110         }
    111         if(values[i] < minimum) {
    112           minimum = values[i];
     107      for (int i = start; i < end; i++) {
     108        if (!double.IsNaN(values[i])) {
     109          if (values[i] > maximum) {
     110            maximum = values[i];
     111          }
     112          if (values[i] < minimum) {
     113            minimum = values[i];
     114          }
    113115        }
    114116      }
     
    124126      int n = values.Length;
    125127      double sum = 0.0;
    126       for(int i = 0; i < n; i++) {
    127         if(double.IsNaN(values[i])) {
     128      for (int i = 0; i < n; i++) {
     129        if (double.IsNaN(values[i])) {
    128130          throw new NotFiniteNumberException();
    129131        } else {
     
    156158    /// <returns></returns>
    157159    public static double Mean(double[] values, int start, int end) {
    158       if(values.Length == 0) throw new InvalidOperationException();
     160      if (values.Length == 0) throw new InvalidOperationException();
    159161      double sum = 0.0;
    160162      int n = 0;
    161       for(int i = start; i < end; i++) {
    162         if(!double.IsNaN(values[i])) {
     163      for (int i = start; i < end; i++) {
     164        if (!double.IsNaN(values[i])) {
    163165          sum += values[i];
    164166          n++;
     
    175177    /// <returns></returns>
    176178    public static double Median(double[] values) {
    177       if(values.Length == 0) throw new InvalidOperationException();
     179      if (values.Length == 0) throw new InvalidOperationException();
    178180      int n = values.Length;
    179       if(n == 0)
     181      if (n == 0)
    180182        return 0;
    181183
     
    186188
    187189      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
    188       if(n % 2 == 1) {
     190      if (n % 2 == 1) {
    189191        return sortedValues[n / 2];
    190192      } else {
Note: See TracChangeset for help on using the changeset viewer.