Free cookie consent management tool by TermsFeed Policy Generator

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

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

File:
1 edited

Legend:

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

    r668 r702  
    2929
    3030namespace HeuristicLab.GP.StructureIdentification.Classification {
    31   public class ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
    32     private const double EPSILON = 1.0E-6;
    33     private double[] classesArr;
     31  public class ClassificationMeanSquaredErrorEvaluator : GPClassificationEvaluatorBase {
     32    private const double EPSILON = 1.0E-7;
    3433    public override string Description {
    3534      get {
     
    4140    public ClassificationMeanSquaredErrorEvaluator()
    4241      : base() {
    43       AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
     42      AddVariableInfo(new VariableInfo("MSE", "The mean squared error of the model", typeof(DoubleData), VariableKind.New));
    4443    }
    4544
    46     public override IOperation Apply(IScope scope) {
    47       ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
    48       classesArr = new double[classes.Count];
    49       for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
    50       Array.Sort(classesArr);
    51       return base.Apply(scope);
    52     }
    53 
    54     public override void Evaluate(int start, int end) {
     45    public override void  Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end)
     46{
    5547      double errorsSquaredSum = 0;
    5648      for(int sample = start; sample < end; sample++) {
    57         double estimated = GetEstimatedValue(sample);
    58         double original = GetOriginalValue(sample);
    59         SetOriginalValue(sample, estimated);
     49        double estimated = evaluator.Evaluate(sample);
     50        double original = dataset.GetValue(targetVariable, sample);
    6051        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
    6152          double error = estimated - original;
     
    6354          // on the lower end and upper end only add linear error if the absolute error is larger than 1
    6455          // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error
    65           if((IsEqual(original, classesArr[0]) && error < -1.0) ||
    66             (IsEqual(original, classesArr[classesArr.Length - 1]) && error > 1.0)) {
     56          if((IsEqual(original, classes[0]) && error < -1.0) ||
     57            (IsEqual(original, classes[classes.Length - 1]) && error > 1.0)) {
    6758            errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class
    6859          } else {
     
    7667        errorsSquaredSum = double.MaxValue;
    7768      }
     69
     70      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
     71      if(mse == null) {
     72        mse = new DoubleData();
     73        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
     74      }
     75
    7876      mse.Data = errorsSquaredSum;
    7977    }
Note: See TracChangeset for help on using the changeset viewer.