Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ClassificationMeanSquaredErrorEvaluator.cs @ 696

Last change on this file since 696 was 668, checked in by mkommend, 16 years ago

namespaces changed to HeuristicLab.GP.StructureIdentification.Classification
(ticket #177)

File size: 3.5 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.GP.StructureIdentification;
29
30namespace HeuristicLab.GP.StructureIdentification.Classification {
31  public class ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
32    private const double EPSILON = 1.0E-6;
33    private double[] classesArr;
34    public override string Description {
35      get {
36        return @"Evaluates 'FunctionTree' for all samples of 'DataSet' and calculates the mean-squared-error
37for the estimated values vs. the real values of 'TargetVariable'.";
38      }
39    }
40
41    public ClassificationMeanSquaredErrorEvaluator()
42      : 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));
44    }
45
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) {
55      double errorsSquaredSum = 0;
56      for(int sample = start; sample < end; sample++) {
57        double estimated = GetEstimatedValue(sample);
58        double original = GetOriginalValue(sample);
59        SetOriginalValue(sample, estimated);
60        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
61          double error = estimated - original;
62          // between classes use squared error
63          // on the lower end and upper end only add linear error if the absolute error is larger than 1
64          // 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)) {
67            errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class
68          } else {
69            errorsSquaredSum += error * error;
70          }
71        }
72      }
73
74      errorsSquaredSum /= (end - start);
75      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
76        errorsSquaredSum = double.MaxValue;
77      }
78      mse.Data = errorsSquaredSum;
79    }
80
81    private bool IsEqual(double x, double y) {
82      return Math.Abs(x - y) < EPSILON;
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.