Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/UncertainMeanSquaredErrorEvaluator.cs @ 2212

Last change on this file since 2212 was 2212, checked in by gkronber, 15 years ago

GP Refactoring: #713

  • added project GP.Operators
  • moved operators from plugin GP to plugin GP.Operators
  • deleted unused constraints
  • removed dependency of GP plugins on Constraints plugin
  • moved StructID functions into directory Symbols
  • deleted unused class FunView
  • implemented add and remove functionality for the FunctionLibraryView
File size: 5.2 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Random;
26
27namespace HeuristicLab.GP.StructureIdentification {
28  public class UncertainMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
29    public override string Description {
30      get {
31        return @"Evaluates 'FunctionTree' for all samples of the dataset and calculates the mean-squared-error
32for the estimated values vs. the real values of 'TargetVariable'.
33This operator stops the computation as soon as an upper limit for the mean-squared-error is reached.";
34      }
35    }
36
37    public UncertainMeanSquaredErrorEvaluator()
38      : base() {
39      AddVariableInfo(new VariableInfo("Random", "", typeof(MersenneTwister), VariableKind.In));
40      AddVariableInfo(new VariableInfo("MinEvaluatedSamples", "", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("QualityLimit", "The upper limit of the MSE which is used as early stopping criterion.", typeof(DoubleData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("ConfidenceBounds", "Confidence bounds of the calculated MSE", typeof(DoubleData), VariableKind.New | VariableKind.Out));
43      AddVariableInfo(new VariableInfo("ActuallyEvaluatedSamples", "", typeof(IntData), VariableKind.New | VariableKind.Out));
44    }
45
46    // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE
47    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
48      double qualityLimit = GetVariableValue<DoubleData>("QualityLimit", scope, true).Data;
49      int minSamples = GetVariableValue<IntData>("MinEvaluatedSamples", scope, true).Data;
50      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
51      DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false);
52      if (mse == null) {
53        mse = new DoubleData();
54        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse));
55      }
56      DoubleData confidenceBounds = GetVariableValue<DoubleData>("ConfidenceBounds", scope, false, false);
57      if (confidenceBounds == null) {
58        confidenceBounds = new DoubleData();
59        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfidenceBounds"), confidenceBounds));
60      }
61      IntData evaluatedSamples = GetVariableValue<IntData>("ActuallyEvaluatedSamples", scope, false, false);
62      if (evaluatedSamples == null) {
63        evaluatedSamples = new IntData();
64        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ActuallyEvaluatedSamples"), evaluatedSamples));
65      }
66
67      int rows = end - start;
68      double mean = 0;
69      double stdDev = 0;
70      double confidenceInterval = 0;
71      double m2 = 0;
72      int[] indexes = InitIndexes(mt, start, end);
73      int n = 0;
74      for (int sample = 0; sample < rows; sample++) {
75        double estimated = evaluator.Evaluate(indexes[sample]);
76        double original = dataset.GetValue(indexes[sample], targetVariable);
77        if (!double.IsNaN(original) && !double.IsInfinity(original)) {
78          n++;
79          double error = estimated - original;
80          double squaredError = error * error;
81          double delta = squaredError - mean;
82          mean = mean + delta / n;
83          m2 = m2 + delta * (squaredError - mean);
84
85          if (n > minSamples && n % minSamples == 0) {
86            stdDev = Math.Sqrt(Math.Sqrt(m2 / (n - 1)));
87            confidenceInterval = 2.364 * stdDev / Math.Sqrt(n);
88            if (qualityLimit < mean - confidenceInterval || qualityLimit > mean + confidenceInterval) {
89              break;
90            }
91          }
92        }
93      }
94
95      evaluatedSamples.Data = n;
96      mse.Data = mean;
97      stdDev = Math.Sqrt(Math.Sqrt(m2 / (n - 1)));
98      confidenceBounds.Data = 2.364 * stdDev / Math.Sqrt(n);
99    }
100
101    private int[] InitIndexes(MersenneTwister mt, int start, int end) {
102      int n = end - start;
103      int[] indexes = new int[n];
104      for (int i = 0; i < n; i++) indexes[i] = i + start;
105      for (int i = 0; i < n - 1; i++) {
106        int j = mt.Next(i, n);
107        int tmp = indexes[j];
108        indexes[j] = indexes[i];
109        indexes[i] = tmp;
110      }
111      return indexes;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.