Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Evaluation/ClassificationMeanSquaredErrorEvaluator.cs @ 478

Last change on this file since 478 was 478, checked in by gkronber, 16 years ago

fixed another small glitch in the classification evaluator

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Functions;
30using HeuristicLab.DataAnalysis;
31
32namespace HeuristicLab.StructureIdentification {
33  public class ClassificationMeanSquaredErrorEvaluator : GPEvaluatorBase {
34    protected double[] backupValues;
35    private const double EPSILON = 1.0E-6;
36    public override string Description {
37      get {
38        return @"Evaluates 'FunctionTree' for all samples of 'DataSet' and calculates the mean-squared-error
39for the estimated values vs. the real values of 'TargetVariable'.";
40      }
41    }
42
43    public ClassificationMeanSquaredErrorEvaluator()
44      : base() {
45      AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
46    }
47
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      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
52      double[] classesArr = new double[classes.Count];
53      for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
54      Array.Sort(classesArr);
55
56      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;
77        }
78      }
79
80      errorsSquaredSum /= (trainingEnd - trainingStart);
81      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
82        errorsSquaredSum = double.MaxValue;
83      }
84      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);
85      return errorsSquaredSum;
86    }
87
88    private bool IsEqual(double x, double y) {
89      return Math.Abs(x - y) < EPSILON;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.