Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/UncertainMeanSquaredErrorEvaluator.cs @ 18242

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

Implemented #824 (Refactor: ITreeEvaluator interface to provide a method that evaluates a tree on a range of samples.)

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