Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs @ 2

Last change on this file since 2 was 2, checked in by swagner, 16 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 5.6 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 MeanSquaredErrorEvaluator : OperatorBase {
34    public override string Description {
35      get { return @"Evaluates 'OperatorTree' for samples 'FirstSampleIndex' - 'LastSampleIndex' (inclusive) and calculates the mean-squared-error
36for the estimated values vs. the real values of 'TargetVariable'."; }
37    }
38
39    public MeanSquaredErrorEvaluator()
40      : base() {
41      AddVariableInfo(new VariableInfo("OperatorTree", "The function tree that should be evaluated", typeof(IFunction), 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("FirstSampleIndex", "Index of the first row of the dataset on which the function should be evaluated", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("LastSampleIndex", "Index of the last row of the dataset on which the function should be evaluated (inclusive)", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("UseEstimatedTargetValues", "When the function tree contains the target variable this variable determines " +
48      "if we should use the estimated or the original values of the target variable in the evaluation", typeof(BoolData), VariableKind.In));
49      AddVariableInfo(new VariableInfo("Quality", "The mean squared error of the model", typeof(DoubleData), VariableKind.New));
50    }
51
52    private double[] savedTargetVariable = new double[0];
53
54    public override IOperation Apply(IScope scope) {
55
56      int firstSampleIndex = GetVariableValue<IntData>("FirstSampleIndex", scope, true).Data;
57      int lastSampleIndex = GetVariableValue<IntData>("LastSampleIndex", scope, true).Data;
58
59      if(firstSampleIndex >= lastSampleIndex) {
60        throw new InvalidProgramException();
61      }
62
63
64      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
65      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
66
67      IFunction function = GetVariableValue<IFunction>("OperatorTree", scope, true);
68
69      double errorsSquaredSum = 0;
70
71      double maximumPunishment = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data * dataset.GetRange(targetVariable, firstSampleIndex, lastSampleIndex);
72      double targetMean = dataset.GetMean(targetVariable, firstSampleIndex, lastSampleIndex);
73      bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValues", scope, true).Data;
74
75      if(useEstimatedValues && savedTargetVariable.Length != lastSampleIndex - firstSampleIndex + 1) {
76        savedTargetVariable = new double[lastSampleIndex - firstSampleIndex + 1];
77      }
78      for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
79
80        double estimated = function.Evaluate(dataset, sample);
81        double original = dataset.GetValue(sample, targetVariable);
82
83        if(useEstimatedValues) {
84          savedTargetVariable[sample - firstSampleIndex] = original;
85          dataset.SetValue(sample, targetVariable, estimated);
86        }
87
88        if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
89          estimated = targetMean + maximumPunishment;
90        } else if(estimated > targetMean + maximumPunishment) {
91          estimated = targetMean + maximumPunishment;
92        } else if(estimated < targetMean - maximumPunishment) {
93          estimated = targetMean - maximumPunishment;
94        }
95
96        double error = estimated - original;
97        errorsSquaredSum += error * error;
98      }
99
100      errorsSquaredSum /= (lastSampleIndex - firstSampleIndex);
101      if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
102        errorsSquaredSum = double.MaxValue;
103      }
104
105      if(useEstimatedValues) {
106        // restore original values of the target variable
107        for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) {
108          dataset.SetValue(sample, targetVariable, savedTargetVariable[sample - firstSampleIndex]);
109        }
110      }
111
112      scope.AddVariable(new HeuristicLab.Core.Variable("Quality", new DoubleData(errorsSquaredSum)));
113      return null;
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.