Free cookie consent management tool by TermsFeed Policy Generator

Changeset 332


Ignore:
Timestamp:
06/23/08 19:13:17 (16 years ago)
Author:
gkronber
Message:

implemented #172

Location:
trunk/sources/HeuristicLab.StructureIdentification/Evaluation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/EarlyStoppingMeanSquaredErrorEvaluator.cs

    r200 r332  
    4545    }
    4646
     47    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    4748    public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    4849      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
     50      bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, false).Data;
     51      if(useEstimatedValues && backupValues == null) {
     52        backupValues = new double[dataset.Rows];
     53        for(int i = 0; i < dataset.Rows; i++) {
     54          backupValues[i] = dataset.GetValue(i, targetVariable);
     55        }
     56      }
    4957      double errorsSquaredSum = 0;
    5058      double targetMean = dataset.GetMean(targetVariable);
     
    5967          estimated = targetMean - maximumPunishment;
    6068        }
     69
    6170        double error = estimated - original;
    6271        errorsSquaredSum += error * error;
     
    6574        if(errorsSquaredSum / dataset.Rows >= qualityLimit) {
    6675          scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * sample+1;
     76          if(useEstimatedValues) RestoreDataset(dataset, targetVariable, 0, sample);
    6777          return errorsSquaredSum / (sample + 1); // return estimated MSE (when the remaining errors are on average the same)
    6878        }
     79        if(useEstimatedValues) {
     80          dataset.SetValue(sample, targetVariable, estimated);
     81        }
    6982      }
     83      if(useEstimatedValues) RestoreDataset(dataset, targetVariable, 0, dataset.Rows);
    7084      errorsSquaredSum /= dataset.Rows;
    7185      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
     
    7589      return errorsSquaredSum;
    7690    }
     91
     92    private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {
     93      for(int i = from; i < to; i++) {
     94        dataset.SetValue(i, targetVariable, backupValues[i]);
     95      }
     96    }
    7797  }
    7898}
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs

    r200 r332  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public class MeanSquaredErrorEvaluator : GPEvaluatorBase {
     34    protected double[] backupValues;
    3435    public override string Description {
    3536      get {
     
    4142    public MeanSquaredErrorEvaluator()
    4243      : base() {
     44      AddVariableInfo(new VariableInfo("UseEstimatedTargetValue", "Wether to use the original (measured) or the estimated (calculated) value for the targat variable when doing autoregressive modelling", typeof(BoolData), VariableKind.In));
     45      GetVariableInfo("UseEstimatedTargetValue").Local = true;
     46      AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData(false)));
    4347    }
    4448
     
    4650      double errorsSquaredSum = 0;
    4751      double targetMean = dataset.GetMean(targetVariable);
     52      bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, false).Data;
     53      if(useEstimatedValues && backupValues == null) {
     54        backupValues = new double[dataset.Rows];
     55        for(int i = 0; i < dataset.Rows; i++) {
     56          backupValues[i] = dataset.GetValue(i, targetVariable);
     57        }
     58      }
     59
    4860      for(int sample = 0; sample < dataset.Rows; sample++) {
    49 
    5061        double estimated = functionTree.Evaluate(dataset, sample);
    5162        double original = dataset.GetValue(sample, targetVariable);
     
    5970        double error = estimated - original;
    6071        errorsSquaredSum += error * error;
     72        if(useEstimatedValues) {
     73          dataset.SetValue(sample, targetVariable, estimated);
     74        }
    6175      }
     76
     77      if(useEstimatedValues) RestoreDataset(dataset, targetVariable);
    6278      errorsSquaredSum /= dataset.Rows;
    6379      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
     
    6783      return errorsSquaredSum;
    6884    }
     85
     86    private void RestoreDataset(Dataset dataset, int targetVariable) {
     87      for(int i = 0; i < dataset.Rows; i++) {
     88        dataset.SetValue(i, targetVariable, backupValues[i]);
     89      }
     90    }
    6991  }
    7092}
Note: See TracChangeset for help on using the changeset viewer.