Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/08 11:21:04 (16 years ago)
Author:
gkronber
Message:

fixed #328 by restructuring evaluation operators to remove state in evaluation operators.

Location:
trunk/sources/HeuristicLab.GP.StructureIdentification
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/BakedTreeEvaluator.cs

    r699 r702  
    3030
    3131namespace HeuristicLab.GP.StructureIdentification {
     32  /// <summary>
     33  /// Evaluates FunctionTrees recursively by interpretation of the function symbols in each node.
     34  /// Not thread-safe!
     35  /// </summary>
    3236  public class BakedTreeEvaluator {
    3337    private const double EPSILON = 1.0e-7;
     38    private double estimatedValueMax;
     39    private double estimatedValueMin;
    3440
    3541    private class Instr {
     
    4349
    4450    private List<Instr> code;
     51    private Instr[] codeArr;
    4552    private int PC;
    4653    private Dataset dataset;
     
    5259    }
    5360
    54     public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset) {
     61    public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
    5562      this.dataset = dataset;
     63      double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable);
     64
     65      // get the mean of the values of the target variable to determin the max and min bounds of the estimated value
     66      double targetMean = dataset.GetMean(targetVariable, start, end - 1);
     67      estimatedValueMin = targetMean - maximumPunishment;
     68      estimatedValueMax = targetMean + maximumPunishment;
     69
    5670      List<LightWeightFunction> linearRepresentation = functionTree.LinearRepresentation;
    5771      code.Clear();
     
    6175        code.Add(curInstr);
    6276      }
     77
     78      codeArr = code.ToArray<Instr>();
    6379    }
    6480
     
    88104      PC = 0;
    89105      this.sampleIndex = sampleIndex;
    90       return EvaluateBakedCode();
     106
     107      double estimated = EvaluateBakedCode();
     108      if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
     109        estimated = estimatedValueMax;
     110      } else if(estimated > estimatedValueMax) {
     111        estimated = estimatedValueMax;
     112      } else if(estimated < estimatedValueMin) {
     113        estimated = estimatedValueMin;
     114      }
     115      return estimated;
    91116    }
    92117
     
    101126
    102127    private double EvaluateBakedCode() {
    103       Instr currInstr = code[PC++];
     128      Instr currInstr = codeArr[PC++];
    104129      switch(currInstr.symbol) {
    105130        case EvaluatorSymbolTable.VARIABLE: {
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/CoefficientOfDeterminationEvaluator.cs

    r656 r702  
    3030namespace HeuristicLab.GP.StructureIdentification {
    3131  public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase {
    32     private DoubleData r2;
    3332    public override string Description {
    3433      get {
     
    4342    }
    4443
    45     public override IOperation Apply(IScope scope) {
    46       r2 = GetVariableValue<DoubleData>("R2", scope, false, false);
    47       if(r2 == null) {
    48         r2 = new DoubleData();
    49         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2));
    50       }
    51 
    52       return base.Apply(scope);
    53     }
    54 
    55     public override void Evaluate(int start, int end) {
     44    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5645      double errorsSquaredSum = 0.0;
    5746      double originalDeviationTotalSumOfSquares = 0.0;
     47      double targetMean = dataset.GetMean(targetVariable, start, end);
    5848      for(int sample = start; sample < end; sample++) {
    59         double estimated = GetEstimatedValue(sample);
    60         double original = GetOriginalValue(sample);
    61         SetOriginalValue(sample, estimated);
     49        double estimated = evaluator.Evaluate(sample);
     50        double original = dataset.GetValue(targetVariable, sample);
     51        if(updateTargetValues) {
     52          dataset.SetValue(targetVariable, sample, estimated);
     53        }
    6254        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6355          double error = estimated - original;
    6456          errorsSquaredSum += error * error;
    6557
    66           double origDeviation = original - TargetMean;
     58          double origDeviation = original - targetMean;
    6759          originalDeviationTotalSumOfSquares += origDeviation * origDeviation;
    6860        }
     
    7466      if(double.IsNaN(quality) || double.IsInfinity(quality))
    7567        quality = double.MaxValue;
     68
     69      DoubleData r2 = GetVariableValue<DoubleData>("R2", scope, false, false);
     70      if(r2 == null) {
     71        r2 = new DoubleData();
     72        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2));
     73      }
     74
    7675      r2.Data = quality;
    7776    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/EarlyStoppingMeanSquaredErrorEvaluator.cs

    r656 r702  
    3030namespace HeuristicLab.GP.StructureIdentification {
    3131  public class EarlyStoppingMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
    32     private double qualityLimit;
    3332    public override string Description {
    3433      get {
     
    4443    }
    4544
    46     public override IOperation Apply(IScope scope) {
    47       qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
    48       return base.Apply(scope);
    49     }
     45    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
     46    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     47      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
     48      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     49      if(mse == null) {
     50        mse = new DoubleData();
     51        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
     52      }
    5053
    51     // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    52     public override void Evaluate(int start, int end) {
    5354      double errorsSquaredSum = 0;
    5455      int rows = end - start;
    5556      for(int sample = start; sample < end; sample++) {
    56         double estimated = GetEstimatedValue(sample);
    57         double original = GetOriginalValue(sample);
    58         SetOriginalValue(sample, estimated);
     57        double estimated = evaluator.Evaluate(sample);
     58        double original = dataset.GetValue(targetVariable, sample);
     59        if(updateTargetValues) {
     60          dataset.SetValue(targetVariable, sample, estimated);
     61        }
    5962        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6063          double error = estimated - original;
     
    7174        errorsSquaredSum = double.MaxValue;
    7275      }
     76
    7377      mse.Data = errorsSquaredSum;
    7478    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/GPEvaluatorBase.cs

    r656 r702  
    3131namespace HeuristicLab.GP.StructureIdentification {
    3232  public abstract class GPEvaluatorBase : OperatorBase {
    33     private int targetVariable;
    34     private int start;
    35     private int end;
    36     private bool useEstimatedValues;
    37     private double[] backupValues;
    38     private int evaluatedSamples;
    39     private double estimatedValueMax;
    40     private double estimatedValueMin;
    41     private int treeSize;
    42     private double totalEvaluatedNodes;
    43     protected Dataset dataset;
    44     private double targetMean;
    45     private BakedTreeEvaluator evaluator;
    46     protected double TargetMean { get { return targetMean; } }
    47 
    4833    public GPEvaluatorBase()
    4934      : base() {
     
    6146    public override IOperation Apply(IScope scope) {
    6247      // get all variable values
    63       targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    64       dataset = GetVariableValue<Dataset>("Dataset", scope, true);
     48      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
     49      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    6550      BakedFunctionTree functionTree = GetVariableValue<BakedFunctionTree>("FunctionTree", scope, true);
    66       double maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable);
    67       treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
    68       totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
     51      double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
     52      int treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
     53      double totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
    6954      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
    7055      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
    71       useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
     56      bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
     57      double[] backupValues = null;
    7258      // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array
    7359      if(useEstimatedValues &&
    74         (backupValues == null || start != this.start || end != this.end)) {
    75         this.start = start;
    76         this.end = end;
     60        (backupValues == null || backupValues.Length!=end-start)) {
    7761        backupValues = new double[end - start];
    7862        for(int i = start; i < end; i++) {
     
    8064        }
    8165      }
    82       // get the mean of the values of the target variable to determin the max and min bounds of the estimated value
    83       targetMean = dataset.GetMean(targetVariable, start, end - 1);
    84       estimatedValueMin = targetMean - maximumPunishment;
    85       estimatedValueMax = targetMean + maximumPunishment;
    8666
    8767      // initialize and reset the evaluator
    88       if(evaluator == null) evaluator = new BakedTreeEvaluator();
    89       evaluator.ResetEvaluator(functionTree, dataset);
    90       evaluatedSamples = 0;
     68      BakedTreeEvaluator evaluator = new BakedTreeEvaluator();
     69      evaluator.ResetEvaluator(functionTree, dataset, targetVariable, start, end, punishmentFactor);
    9170
    92       Evaluate(start, end);
     71      Evaluate(scope, evaluator, dataset, targetVariable, start, end, useEstimatedValues);
    9372
    9473      // restore the values of the target variable from the backup array if necessary
    95       if(useEstimatedValues) RestoreDataset(dataset, targetVariable, start, end);
     74      if(useEstimatedValues) {
     75        for(int i = start; i < end; i++) {
     76          dataset.SetValue(i, targetVariable, backupValues[i - start]);
     77        }
     78      }
     79
    9680      // update the value of total evaluated nodes
    97       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * evaluatedSamples;
     81      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (end-start);
    9882      return null;
    9983    }
    10084
    101     private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {
    102       for(int i = from; i < to; i++) {
    103         dataset.SetValue(i, targetVariable, backupValues[i - from]);
    104       }
    105     }
    106 
    107     public abstract void Evaluate(int start, int end);
    108 
    109     public void SetOriginalValue(int sample, double value) {
    110       if(useEstimatedValues) {
    111         dataset.SetValue(sample, targetVariable, value);
    112       }
    113     }
    114 
    115     public double GetOriginalValue(int sample) {
    116       return dataset.GetValue(sample, targetVariable);
    117     }
    118 
    119     public double GetEstimatedValue(int sample) {
    120       evaluatedSamples++;
    121       double estimated = evaluator.Evaluate(sample);
    122       if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    123         estimated = estimatedValueMax;
    124       } else if(estimated > estimatedValueMax) {
    125         estimated = estimatedValueMax;
    126       } else if(estimated < estimatedValueMin) {
    127         estimated = estimatedValueMin;
    128       }
    129       return estimated;
    130     }
     85    public abstract void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues);
    13186  }
    13287}
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs

    r656 r702  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification {
    3132  public class MeanAbsolutePercentageErrorEvaluator : GPEvaluatorBase {
    32     private DoubleData mape;
    3333    public override string Description {
    3434      get {
     
    4343    }
    4444
    45     public override IOperation Apply(IScope scope) {
    46       mape = GetVariableValue<DoubleData>("MAPE", scope, false, false);
    47       if(mape == null) {
    48         mape = new DoubleData();
    49         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPE"), mape));
    50       }
    51 
    52       return base.Apply(scope);
    53     }
    54 
    55     public override void Evaluate(int start, int end) {
     45    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5646      double errorsSum = 0.0;
    5747      int n = 0;
    5848      for(int sample = start; sample < end; sample++) {
    59         double estimated = GetEstimatedValue(sample);
    60         double original = GetOriginalValue(sample);
    61         SetOriginalValue(sample, estimated);
    62         if(!double.IsNaN(original) && !double.IsInfinity(original) && original!=0.0) {
     49        double estimated = evaluator.Evaluate(sample);
     50        double original = dataset.GetValue(targetVariable, sample);
     51
     52        if(updateTargetValues) {
     53          dataset.SetValue(targetVariable, sample, estimated);
     54        }
     55       
     56        if(!double.IsNaN(original) && !double.IsInfinity(original) && original != 0.0) {
    6357          double percent_error = Math.Abs((estimated - original) / original);
    6458          errorsSum += percent_error;
     
    6963      if(double.IsNaN(quality) || double.IsInfinity(quality))
    7064        quality = double.MaxValue;
     65
     66      // create a variable for the MAPE
     67      DoubleData mape = GetVariableValue<DoubleData>("MAPE", scope, false, false);
     68      if(mape == null) {
     69        mape = new DoubleData();
     70        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPE"), mape));
     71      }
     72
    7173      mape.Data = quality;
    7274    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/MeanSquaredErrorEvaluator.cs

    r656 r702  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification {
    3132  public class MeanSquaredErrorEvaluator : GPEvaluatorBase {
    32     protected DoubleData mse;
    3333    public override string Description {
    3434      get {
     
    4343    }
    4444
    45     public override IOperation Apply(IScope scope) {
    46       mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
    47       if(mse == null) {
    48         mse = new DoubleData();
    49         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
    50       }
    51 
    52       return base.Apply(scope);
    53     }
    54 
    55     public override void Evaluate(int start, int end) {
     45    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    5646      double errorsSquaredSum = 0;
    5747      for(int sample = start; sample < end; sample++) {
    58         double original = GetOriginalValue(sample);
    59         double estimated = GetEstimatedValue(sample);
    60         SetOriginalValue(sample, estimated);
     48        double original = dataset.GetValue(targetVariable, sample);
     49        double estimated = evaluator.Evaluate(sample);
     50        if(updateTargetValues) {
     51          dataset.SetValue(targetVariable, sample, estimated);
     52        }
    6153        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6254          double error = estimated - original;
     
    6961        errorsSquaredSum = double.MaxValue;
    7062      }
     63
     64      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     65      if(mse == null) {
     66        mse = new DoubleData();
     67        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
     68      }
     69
    7170      mse.Data = errorsSquaredSum;
    7271    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/SimpleEvaluator.cs

    r656 r702  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Operators;
     29using HeuristicLab.DataAnalysis;
    2930
    3031namespace HeuristicLab.GP.StructureIdentification {
    3132  public class SimpleEvaluator : GPEvaluatorBase {
    32     private ItemList values;
    3333    public SimpleEvaluator()
    3434      : base() {
     
    3636    }
    3737
    38     public override IOperation Apply(IScope scope) {
    39       values = GetVariableValue<ItemList>("Values", scope, false, false);
     38    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     39      ItemList values = GetVariableValue<ItemList>("Values", scope, false, false);
    4040      if(values == null) {
    4141        values = new ItemList();
     
    4747      }
    4848      values.Clear();
    49       return base.Apply(scope);
    50     }
    5149
    52     public override void Evaluate(int start, int end) {
    5350      for(int sample = start; sample < end; sample++) {
    5451        ItemList row = new ItemList();
    55         double estimated = GetEstimatedValue(sample);
    56         double original = GetOriginalValue(sample);
    57         SetOriginalValue(sample, estimated);
     52        double estimated = evaluator.Evaluate(sample);
     53        double original = dataset.GetValue(targetVariable, sample);
     54        if(updateTargetValues) {
     55          dataset.SetValue(targetVariable, sample, estimated);
     56        }
    5857        row.Add(new DoubleData(estimated));
    5958        row.Add(new DoubleData(original));
  • trunk/sources/HeuristicLab.GP.StructureIdentification/Evaluators/VarianceAccountedForEvaluator.cs

    r656 r702  
    3131namespace HeuristicLab.GP.StructureIdentification {
    3232  public class VarianceAccountedForEvaluator : GPEvaluatorBase {
    33     private DoubleData vaf;
    3433    public override string Description {
    3534      get {
     
    5453    }
    5554
    56     public override IOperation Apply(IScope scope) {
    57       vaf = GetVariableValue<DoubleData>("VAF", scope, false, false);
    58       if(vaf == null) {
    59         vaf = new DoubleData();
    60         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("VAF"), vaf));
    61       }
    62 
    63       return base.Apply(scope);
    64     }
    65 
    66     public override void Evaluate(int start, int end) {
     55    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
    6756      int nSamples = end - start;
    6857      double[] errors = new double[nSamples];
    6958      double[] originalTargetVariableValues = new double[nSamples];
    7059      for(int sample = start; sample < end; sample++) {
    71         double estimated = GetEstimatedValue(sample);
    72         double original = GetOriginalValue(sample);
    73         SetOriginalValue(sample, estimated);
     60        double estimated = evaluator.Evaluate(sample);
     61        double original = dataset.GetValue(targetVariable, sample);
     62        if(updateTargetValues) {
     63          dataset.SetValue(targetVariable, sample, estimated);
     64        }
    7465        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    7566          errors[sample - start] = original - estimated;
     
    8475        quality = double.MaxValue;
    8576      }
     77      DoubleData vaf = GetVariableValue<DoubleData>("VAF", scope, false, false);
     78      if(vaf == null) {
     79        vaf = new DoubleData();
     80        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("VAF"), vaf));
     81      }
     82
    8683      vaf.Data = quality;
    8784    }
Note: See TracChangeset for help on using the changeset viewer.