Free cookie consent management tool by TermsFeed Policy Generator

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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    }
Note: See TracChangeset for help on using the changeset viewer.