Free cookie consent management tool by TermsFeed Policy Generator

Changeset 479 for trunk/sources


Ignore:
Timestamp:
08/10/08 11:08:07 (16 years ago)
Author:
gkronber
Message:

implemented #242 (All GP evaluators should support the 'UseEstimatedTargetValues' switch for autoregressive modelling).
Also used the chance to remove a lot of the code duplication and thus improve the readability of all GP evaluators.

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

Legend:

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

    r476 r479  
    3333  public class AccuracyEvaluator : GPEvaluatorBase {
    3434    private const double EPSILON = 1.0E-6;
     35    private double[] classesArr;
     36    private double[] thresholds;
    3537    public override string Description {
    3638      get {
     
    4446    }
    4547
    46     private double[] original = new double[1];
    47     private double[] estimated = new double[1];
    48     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    49       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    50       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    51       int nSamples = trainingEnd-trainingStart;
     48    public override IOperation Apply(IScope scope) {
    5249      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
    53       double[] classesArr = new double[classes.Count];
    54       for(int i=0;i<classesArr.Length;i++) classesArr[i] = classes[i].Data;
     50      classesArr = new double[classes.Count];
     51      for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
    5552      Array.Sort(classesArr);
    56       double[] thresholds = new double[classes.Count - 1];
    57       for(int i=0;i<classesArr.Length-1;i++) {
    58         thresholds[i] = (classesArr[i]+classesArr[i+1]) / 2.0;
     53      thresholds = new double[classes.Count - 1];
     54      for(int i = 0; i < classesArr.Length - 1; i++) {
     55        thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
    5956      }
    6057
     58      return base.Apply(scope);
     59    }
     60
     61    public override double Evaluate(int start, int end) {
     62      int nSamples = end-start;
    6163      int nCorrect = 0;
    62       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    63         double est = evaluator.Evaluate(sample);
    64         double origClass = dataset.GetValue(sample, targetVariable);
     64      for(int sample = start; sample < end; sample++) {
     65        double est = GetEstimatedValue(sample);
     66        double origClass = GetOriginalValue(sample);
    6567        double estClass = double.NaN;
    6668        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
     
    7981        if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
    8082      }
    81       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples;
    8283      return  nCorrect / (double)nSamples;
    8384    }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/ClassificationMeanSquaredErrorEvaluator.cs

    r478 r479  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public class ClassificationMeanSquaredErrorEvaluator : GPEvaluatorBase {
    34     protected double[] backupValues;
    3534    private const double EPSILON = 1.0E-6;
     35    private double[] classesArr;
    3636    public override string Description {
    3737      get {
     
    4646    }
    4747
    48     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    49       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    50       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
     48    public override IOperation Apply(IScope scope) {
    5149      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
    52       double[] classesArr = new double[classes.Count];
     50      classesArr = new double[classes.Count];
    5351      for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
    5452      Array.Sort(classesArr);
     53      return base.Apply(scope);
     54    }
     55
     56    public override double Evaluate(int start, int end) {
    5557
    5658      double errorsSquaredSum = 0;
    57       double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    58       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    59         double estimated = evaluator.Evaluate(sample);
    60         double original = dataset.GetValue(sample, targetVariable);
    61         if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    62           estimated = targetMean + maximumPunishment;
    63         } else if(estimated > targetMean + maximumPunishment) {
    64           estimated = targetMean + maximumPunishment;
    65         } else if(estimated < targetMean - maximumPunishment) {
    66           estimated = targetMean - maximumPunishment;
    67         }
    68         double error = estimated - original;
    69         // between classes use squared error
    70         // on the lower end and upper end only add linear error if the absolute error is larger than 1
    71         // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error
    72         if(error < -1.0 && IsEqual(original, classesArr[0]) && estimated < classesArr[0] ||
    73           error > 1.0 && IsEqual(original, classesArr[classesArr.Length - 1]) && estimated > classesArr[classesArr.Length - 1]) {
    74           errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class
    75         } else {
    76           errorsSquaredSum += error * error;
     59      for(int sample = start; sample < end; sample++) {
     60        double estimated = GetEstimatedValue(sample);
     61        double original = GetOriginalValue(sample);
     62        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
     63          double error = estimated - original;
     64          // between classes use squared error
     65          // on the lower end and upper end only add linear error if the absolute error is larger than 1
     66          // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error
     67          if(error < -1.0 && IsEqual(original, classesArr[0]) && estimated < classesArr[0] ||
     68            error > 1.0 && IsEqual(original, classesArr[classesArr.Length - 1]) && estimated > classesArr[classesArr.Length - 1]) {
     69            errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class
     70          } else {
     71            errorsSquaredSum += error * error;
     72          }
    7773        }
    7874      }
    7975
    80       errorsSquaredSum /= (trainingEnd - trainingStart);
     76      errorsSquaredSum /= (end - start);
    8177      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
    8278        errorsSquaredSum = double.MaxValue;
    8379      }
    84       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);
    8580      return errorsSquaredSum;
    8681    }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs

    r400 r479  
    4343    }
    4444
    45     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    46       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    47       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
     45    public override double Evaluate(int start, int end) {
    4846      double errorsSquaredSum = 0.0;
    4947      double originalDeviationTotalSumOfSquares = 0.0;
    50       double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    51       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    52         double estimated = evaluator.Evaluate(sample);
    53         double original = dataset.GetValue(sample, targetVariable);
     48      for(int sample = start; sample < end; sample++) {
     49        double estimated = GetEstimatedValue(sample);
     50        double original = GetOriginalValue(sample);
    5451        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    55           if(double.IsNaN(estimated) || double.IsInfinity(estimated))
    56             estimated = targetMean + maximumPunishment;
    57           else if(estimated > (targetMean + maximumPunishment))
    58             estimated = targetMean + maximumPunishment;
    59           else if(estimated < (targetMean - maximumPunishment))
    60             estimated = targetMean - maximumPunishment;
    61 
    6252          double error = estimated - original;
    6353          errorsSquaredSum += error * error;
    6454
    65           double origDeviation = original - targetMean;
     55          double origDeviation = original - TargetMean;
    6656          originalDeviationTotalSumOfSquares += origDeviation * origDeviation;
    6757        }
    6858      }
    69 
    70       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);
    7159
    7260      double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares;
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/EarlyStoppingMeanSquaredErrorEvaluator.cs

    r396 r479  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public class EarlyStoppingMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
     34    private double qualityLimit;
    3435    public override string Description {
    3536      get {
     
    4546    }
    4647
     48    public override IOperation Apply(IScope scope) {
     49      qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
     50      return base.Apply(scope);
     51    }
     52
    4753    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
    48     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    49       double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, false).Data;
    50       bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, false).Data;
    51       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    52       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    53       int rows = trainingEnd-trainingStart;
    54       if(useEstimatedValues && backupValues == null) {
    55         backupValues = new double[rows];
    56         for(int i = trainingStart; i < trainingEnd; i++) {
    57           backupValues[i-trainingStart] = dataset.GetValue(i, targetVariable);
     54    public override double Evaluate(int start, int end) {
     55      double errorsSquaredSum = 0;
     56      int rows = end - start;
     57      for(int sample = start; sample < end; sample++) {
     58        double estimated = GetEstimatedValue(sample);
     59        double original = GetOriginalValue(sample);
     60        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
     61          double error = estimated - original;
     62          errorsSquaredSum += error * error;
     63        }
     64        // check the limit and stop as soon as we hit the limit
     65        if(errorsSquaredSum / rows >= qualityLimit) {
     66          return errorsSquaredSum / (sample - start + 1); // return estimated MSE (when the remaining errors are on average the same)
    5867        }
    5968      }
    60       double errorsSquaredSum = 0;
    61       double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    62       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    63         double estimated = evaluator.Evaluate(sample);
    64         double original = dataset.GetValue(sample, targetVariable);
    65         if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    66           estimated = targetMean + maximumPunishment;
    67         } else if(estimated > targetMean + maximumPunishment) {
    68           estimated = targetMean + maximumPunishment;
    69         } else if(estimated < targetMean - maximumPunishment) {
    70           estimated = targetMean - maximumPunishment;
    71         }
    72 
    73         double error = estimated - original;
    74         errorsSquaredSum += error * error;
    75 
    76         // check the limit and stop as soon as we hit the limit
    77         if(errorsSquaredSum / rows >= qualityLimit) {
    78           scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (sample-trainingStart + 1);
    79           if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, sample);
    80           return errorsSquaredSum / (sample-trainingStart + 1); // return estimated MSE (when the remaining errors are on average the same)
    81         }
    82         if(useEstimatedValues) {
    83           dataset.SetValue(sample, targetVariable, estimated);
    84         }
    85       }
    86       if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, trainingEnd);
    8769      errorsSquaredSum /= rows;
    8870      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
    8971        errorsSquaredSum = double.MaxValue;
    9072      }
    91       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * rows;
    9273      return errorsSquaredSum;
    93     }
    94 
    95     private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {
    96       for(int i = from; i < to; i++) {
    97         dataset.SetValue(i, targetVariable, backupValues[i-from]);
    98       }
    9974    }
    10075  }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/GPEvaluatorBase.cs

    r396 r479  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public abstract class GPEvaluatorBase : OperatorBase {
    34     protected double maximumPunishment;
    35     protected int treeSize;
    36     protected double totalEvaluatedNodes;
    37     protected IEvaluator evaluator;
     34    private IEvaluator evaluator;
     35    private int targetVariable;
     36    private int trainingStart;
     37    private int trainingEnd;
     38    private bool useEstimatedValues;
     39    private double[] backupValues;
     40    private int evaluatedSamples;
     41    private double estimatedValueMax;
     42    private double estimatedValueMin;
     43    private int treeSize;
     44    private double totalEvaluatedNodes;
     45    private Dataset dataset;
     46    private double targetMean;
     47    protected double TargetMean { get { return targetMean; } }
    3848
    3949    public GPEvaluatorBase()
     
    4757      AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of training samples in dataset", typeof(IntData), VariableKind.In));
    4858      AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "End index of training samples in dataset", typeof(IntData), VariableKind.In));
     59      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));
    4960      AddVariableInfo(new VariableInfo("Quality", "The evaluated quality of the model", typeof(DoubleData), VariableKind.New));
    5061    }
    5162
    5263    public override IOperation Apply(IScope scope) {
    53       int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    54       Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
     64      // get all variable values
     65      targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
     66      dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    5567      IFunctionTree functionTree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
    56       this.maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable);
    57       this.treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
    58       this.totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
     68      double maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable);
     69      treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
     70      totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
     71      trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
     72      trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
     73      useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
     74      // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array
     75      if(useEstimatedValues && backupValues == null) {
     76        backupValues = new double[trainingEnd - trainingStart];
     77        for(int i = trainingStart; i < trainingEnd; i++) {
     78          backupValues[i - trainingStart] = dataset.GetValue(i, targetVariable);
     79        }
     80      }
     81      // get the mean of the values of the target variable to determin the max and min bounds of the estimated value
     82      targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
     83      estimatedValueMin = targetMean - maximumPunishment;
     84      estimatedValueMax = targetMean + maximumPunishment;
     85
     86      // initialize and reset the evaluator
    5987      if(evaluator == null) evaluator = functionTree.CreateEvaluator(dataset);
    6088      evaluator.ResetEvaluator(functionTree);
    61       double result = Evaluate(scope, functionTree, targetVariable, dataset);
     89      evaluatedSamples = 0;
    6290
     91      // calculate the quality measure
     92      double result = Evaluate(trainingStart, trainingEnd);
     93
     94      // restore the values of the target variable from the backup array if necessary
     95      if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, trainingEnd);
     96      // update the value of total evaluated nodes
     97      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * evaluatedSamples;
     98      // write the calculate quality value
    6399      DoubleData quality = GetVariableValue<DoubleData>("Quality", scope, false, false);
    64100      if(quality == null) {
     
    67103        quality.Data = result;
    68104      }
    69 
    70105      return null;
    71106    }
    72107
    73     public abstract double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset);
     108    private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {
     109      for(int i = from; i < to; i++) {
     110        dataset.SetValue(i, targetVariable, backupValues[i - from]);
     111      }
     112    }
     113
     114    public abstract double Evaluate(int start, int end);
     115
     116    public double GetOriginalValue(int sample) {
     117      if(useEstimatedValues)
     118        return backupValues[sample - trainingStart];
     119      else
     120        return dataset.GetValue(sample, targetVariable);
     121    }
     122
     123    public double GetEstimatedValue(int sample) {
     124      evaluatedSamples++;
     125      double estimated = evaluator.Evaluate(sample);
     126      if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
     127        estimated = estimatedValueMax;
     128      } else if(estimated > estimatedValueMax) {
     129        estimated = estimatedValueMax;
     130      } else if(estimated < estimatedValueMin) {
     131        estimated = estimatedValueMin;
     132      }
     133
     134      if(useEstimatedValues) {
     135        dataset.SetValue(sample, targetVariable, estimated);
     136      }
     137
     138      return estimated;
     139    }
    74140  }
    75141}
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MCCEvaluator.cs

    r453 r479  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public class MCCEvaluator : GPEvaluatorBase {
     34    private double limit;
     35    private double[] original = new double[1];
     36    private double[] estimated = new double[1];
    3437    public override string Description {
    3538      get {
     
    3740      }
    3841    }
    39 
    4042    public MCCEvaluator()
    4143      : base() {
     
    4345    }
    4446
    45     private double[] original = new double[1];
    46     private double[] estimated = new double[1];
    47     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    48       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    49       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    50       int nSamples = trainingEnd-trainingStart;
    51       double limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data;
     47    public override IOperation Apply(IScope scope) {
     48      limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data;
     49      return base.Apply(scope);
     50    }
     51
     52    public override double Evaluate(int start, int end) {
     53      int nSamples = end - start;
    5254      if(estimated.Length != nSamples) {
    5355        estimated = new double[nSamples];
     
    5759      double positive = 0;
    5860      double negative = 0;
    59       double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    60       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    61         double est = evaluator.Evaluate(sample);
    62         double orig = dataset.GetValue(sample, targetVariable);
    63         if(double.IsNaN(est) || double.IsInfinity(est)) {
    64           est = targetMean + maximumPunishment;
    65         } else if(est > targetMean + maximumPunishment) {
    66           est = targetMean + maximumPunishment;
    67         } else if(est < targetMean - maximumPunishment) {
    68           est = targetMean - maximumPunishment;
    69         }
    70         estimated[sample-trainingStart] = est;
    71         original[sample-trainingStart] = orig;
     61      for(int sample = start; sample < end; sample++) {
     62        double est = GetEstimatedValue(sample);
     63        double orig = GetOriginalValue(sample);
     64        estimated[sample - start] = est;
     65        original[sample - start] = orig;
    7266        if(orig >= limit) positive++;
    7367        else negative++;
     
    7973      double tn = negative;
    8074      double fp = 0;
    81       for(int i = original.Length-1; i >= 0 ; i--) {
     75      for(int i = original.Length - 1; i >= 0; i--) {
    8276        if(original[i] >= limit) {
    8377          tp++; fn--;
     
    9084        }
    9185      }
    92       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);
    9386      return best_mcc;
    9487    }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanAbsolutePercentageErrorEvaluator.cs

    r400 r479  
    4343    }
    4444
    45     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    46       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    47       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
     45    public override double Evaluate(int start, int end) {
    4846      double errorsSum = 0.0;
    49       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    50         double estimated = evaluator.Evaluate(sample);
    51         double original = dataset.GetValue(sample, targetVariable);
     47      for(int sample = start; sample < end; sample++) {
     48        double estimated = GetEstimatedValue(sample);
     49        double original = GetOriginalValue(sample);
    5250        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    53           if(double.IsNaN(estimated) || double.IsInfinity(estimated))
    54             estimated = maximumPunishment;
    55           else if(estimated > maximumPunishment)
    56             estimated = maximumPunishment;
    57           else if(estimated < -maximumPunishment)
    58             estimated = -maximumPunishment;
    59 
    6051          double percent_error = Math.Abs((estimated - original) / original);
    6152          errorsSum += percent_error;
    6253        }
    6354      }
    64       int nSamples = trainingEnd - trainingStart;
    65       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples;
    66       double quality = errorsSum / nSamples;
     55      double quality = errorsSum / (end - start);
    6756      if(double.IsNaN(quality) || double.IsInfinity(quality))
    6857        quality = double.MaxValue;
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs

    r396 r479  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public class MeanSquaredErrorEvaluator : GPEvaluatorBase {
    34     protected double[] backupValues;
    3534    public override string Description {
    3635      get {
     
    4241    public MeanSquaredErrorEvaluator()
    4342      : 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)));
    4743    }
    4844
    49     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    50       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    51       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
     45    public override double Evaluate(int start, int end) {
    5246      double errorsSquaredSum = 0;
    53       double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    54       bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, false).Data;
    55       if(useEstimatedValues && backupValues == null) {
    56         backupValues = new double[trainingEnd - trainingStart];
    57         for(int i = trainingStart; i < trainingEnd; i++) {
    58           backupValues[i-trainingStart] = dataset.GetValue(i, targetVariable);
    59         }
    60       }
    61       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    62         double estimated = evaluator.Evaluate(sample);
    63         double original = dataset.GetValue(sample, targetVariable);
    64         if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    65           estimated = targetMean + maximumPunishment;
    66         } else if(estimated > targetMean + maximumPunishment) {
    67           estimated = targetMean + maximumPunishment;
    68         } else if(estimated < targetMean - maximumPunishment) {
    69           estimated = targetMean - maximumPunishment;
    70         }
    71         double error = estimated - original;
    72         errorsSquaredSum += error * error;
    73         if(useEstimatedValues) {
    74           dataset.SetValue(sample, targetVariable, estimated);
     47      for(int sample = start; sample < end; sample++) {
     48        double original = GetOriginalValue(sample);
     49        double estimated = GetEstimatedValue(sample);
     50        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
     51          double error = estimated - original;
     52          errorsSquaredSum += error * error;
    7553        }
    7654      }
    7755
    78       if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, trainingEnd);
    79       errorsSquaredSum /= (trainingEnd-trainingStart);
     56      errorsSquaredSum /= (end - start);
    8057      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
    8158        errorsSquaredSum = double.MaxValue;
    8259      }
    83       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd-trainingStart);
    8460      return errorsSquaredSum;
    85     }
    86 
    87     private void RestoreDataset(Dataset dataset, int targetVariable, int from, int to) {
    88       for(int i = from; i < to; i++) {
    89         dataset.SetValue(i, targetVariable, backupValues[i-from]);
    90       }
    9161    }
    9262  }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/SimpleEvaluator.cs

    r396 r479  
    3131
    3232namespace HeuristicLab.StructureIdentification {
    33   public class SimpleEvaluator : OperatorBase {
    34     protected int treeSize;
    35     protected double totalEvaluatedNodes;
    36     private IEvaluator evaluator;
    37 
     33  public class SimpleEvaluator : GPEvaluatorBase {
     34    private ItemList values;
    3835    public SimpleEvaluator()
    3936      : base() {
    40       AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IFunctionTree), VariableKind.In));
    41       AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), VariableKind.In));
    42       AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
    43       AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
    44       AddVariableInfo(new VariableInfo("TotalEvaluatedNodes", "Number of evaluated nodes", typeof(DoubleData), VariableKind.In | VariableKind.Out));
    45       AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of training samples in dataset", typeof(IntData), VariableKind.In));
    46       AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "End index of training samples in dataset", typeof(IntData), VariableKind.In));
    4737      AddVariableInfo(new VariableInfo("Values", "The values of the target variable as predicted by the model and the original value of the target variable", typeof(ItemList), VariableKind.New | VariableKind.Out));
    4838    }
    4939
    5040    public override IOperation Apply(IScope scope) {
    51       int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
    52       Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    53       IFunctionTree functionTree = GetVariableValue<IFunctionTree>("FunctionTree", scope, true);
    54       this.treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
    55       this.totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
    56       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    57       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    58 
    59       ItemList values = GetVariableValue<ItemList>("Values", scope, false, false);
     41      values = GetVariableValue<ItemList>("Values", scope, false, false);
    6042      if(values == null) {
    6143        values = new ItemList();
     
    6749      }
    6850      values.Clear();
    69       if(evaluator == null) evaluator = functionTree.CreateEvaluator(dataset);
    70       evaluator.ResetEvaluator(functionTree);
    71       for(int sample = trainingStart; sample < trainingEnd; sample++) {
     51      return base.Apply(scope);
     52    }
     53
     54    public override double Evaluate(int start, int end) {
     55      for(int sample = start; sample < end; sample++) {
    7256        ItemList row = new ItemList();
    73         row.Add(new DoubleData(evaluator.Evaluate(sample)));
    74         row.Add(new DoubleData(dataset.GetValue(sample, targetVariable)));
     57        row.Add(new DoubleData(GetEstimatedValue(sample)));
     58        row.Add(new DoubleData(GetOriginalValue(sample)));
    7559        values.Add(row);
    7660      }
    77       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);
    78       return null;
     61      return double.NaN;
    7962    }
    8063  }
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/TheilInequalityCoefficientEvaluator.cs

    r400 r479  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase {
     34    private bool differential;
    3435    public override string Description {
    3536      get {
     
    4445    }
    4546
    46     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    47       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    48       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    49       bool difference = GetVariableValue<BoolData>("Differential", scope, true).Data;
     47    public override IOperation Apply(IScope scope) {
     48      differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
     49      return base.Apply(scope);
     50    }
     51
     52    public override double Evaluate(int start, int end) {
    5053      double errorsSquaredSum = 0.0;
    5154      double estimatedSquaredSum = 0.0;
    5255      double originalSquaredSum = 0.0;
    53       for(int sample = trainingStart; sample < trainingEnd; sample++) {
     56      for(int sample = start; sample < end; sample++) {
    5457        double prevValue = 0.0;
    55         if(difference) prevValue = dataset.GetValue(sample - 1, targetVariable);
    56         double estimatedChange = evaluator.Evaluate(sample) - prevValue;
    57         double originalChange = dataset.GetValue(sample, targetVariable) - prevValue;
     58        if(differential) prevValue = GetOriginalValue(sample - 1);
     59        double estimatedChange = GetEstimatedValue(sample) - prevValue;
     60        double originalChange = GetOriginalValue(sample) - prevValue;
    5861        if(!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) {
    59           if(double.IsNaN(estimatedChange) || double.IsInfinity(estimatedChange))
    60             estimatedChange = maximumPunishment;
    61           else if(estimatedChange > maximumPunishment)
    62             estimatedChange = maximumPunishment;
    63           else if(estimatedChange < -maximumPunishment)
    64             estimatedChange = - maximumPunishment;
    65 
    6662          double error = estimatedChange - originalChange;
    6763          errorsSquaredSum += error * error;
     
    7066        }
    7167      }
    72       int nSamples = trainingEnd - trainingStart;
    73       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples;
     68      int nSamples = end - start;
    7469      double quality = Math.Sqrt(errorsSquaredSum / nSamples) / (Math.Sqrt(estimatedSquaredSum / nSamples) + Math.Sqrt(originalSquaredSum / nSamples));
    75       if(double.IsNaN(quality) || double.IsInfinity(quality)) 
     70      if(double.IsNaN(quality) || double.IsInfinity(quality))
    7671        quality = double.MaxValue;
    7772      return quality;
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/VarianceAccountedForEvaluator.cs

    r400 r479  
    5353
    5454
    55     public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
    56       int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    57       int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    58       double[] errors = new double[trainingEnd-trainingStart];
    59       double[] originalTargetVariableValues = new double[trainingEnd-trainingStart];
    60       double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    61       for(int sample = trainingStart; sample < trainingEnd; sample++) {
    62         double estimated = evaluator.Evaluate(sample);
    63         double original = dataset.GetValue(sample, targetVariable);
     55    public override double Evaluate(int start, int end) {
     56      int nSamples = end - start;
     57      double[] errors = new double[nSamples];
     58      double[] originalTargetVariableValues = new double[nSamples];
     59      for(int sample = start; sample < end; sample++) {
     60        double estimated = GetEstimatedValue(sample);
     61        double original = GetOriginalValue(sample);
    6462        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    65           if(double.IsNaN(estimated) || double.IsInfinity(estimated))
    66             estimated = targetMean + maximumPunishment;
    67           else if(estimated > (targetMean + maximumPunishment))
    68             estimated = targetMean + maximumPunishment;
    69           else if(estimated < (targetMean - maximumPunishment))
    70             estimated = targetMean - maximumPunishment;
     63          errors[sample - start] = original - estimated;
     64          originalTargetVariableValues[sample - start] = original;
    7165        }
    72 
    73         errors[sample-trainingStart] = original - estimated;
    74         originalTargetVariableValues[sample-trainingStart] = original;
    7566      }
    76       scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd-trainingStart);
    77 
    7867      double errorsVariance = Statistics.Variance(errors);
    7968      double originalsVariance = Statistics.Variance(originalTargetVariableValues);
Note: See TracChangeset for help on using the changeset viewer.