Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Classification/3.3/Symbolic/Analyzer/TrainingBestSymbolicClassificationSolutionAnalyzer.cs @ 7214

Last change on this file since 7214 was 7214, checked in by ascheibe, 12 years ago

#1706 adapted outdated plugins to changes in IAnalyzer

File size: 27.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Problems.DataAnalysis.Evaluators;
34using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
35using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
36using HeuristicLab.Problems.DataAnalysis.Symbolic;
37
38namespace HeuristicLab.Problems.DataAnalysis.Classification.Symbolic.Analyzers {
39  /// <summary>
40  /// An operator that analyzes the training best scaled symbolic classification solution.
41  /// </summary>
42  [Item("TrainingBestSymbolicClassificationSolutionAnalyzer", "An operator that analyzes the training best scaled symbolic classification solution.")]
43  [StorableClass]
44  [NonDiscoverableType]
45  public sealed class TrainingBestSymbolicClassificationSolutionAnalyzer : SingleSuccessorOperator, ISymbolicClassificationAnalyzer {
46    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
47    private const string QualityParameterName = "Quality";
48    private const string MaximizationParameterName = "Maximization";
49    private const string CalculateSolutionComplexityParameterName = "CalculateSolutionComplexity";
50    private const string CalculateSolutionAccuracyParameterName = "CalculateSolutionAccuracy";
51    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
52    private const string ProblemDataParameterName = "ClassificationProblemData";
53    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
54    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
55    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
56    private const string BestSolutionParameterName = "Best training solution";
57    private const string BestSolutionQualityParameterName = "Best training solution quality";
58    private const string BestSolutionLengthParameterName = "Best training solution length";
59    private const string BestSolutionHeightParameterName = "Best training solution height";
60    private const string BestSolutionVariablesParameterName = "Best training solution variables";
61    private const string BestSolutionTrainingRSquaredParameterName = "Best training solution R² (training)";
62    private const string BestSolutionTestRSquaredParameterName = "Best training solution R² (test)";
63    private const string BestSolutionTrainingMseParameterName = "Best training solution mean squared error (training)";
64    private const string BestSolutionTestMseParameterName = "Best training solution mean squared error (test)";
65    private const string BestSolutionTrainingRelativeErrorParameterName = "Best training solution relative error (training)";
66    private const string BestSolutionTestRelativeErrorParameterName = "Best training solution relative error (test)";
67    private const string BestSolutionAccuracyTrainingParameterName = "Best training solution accuracy (training)";
68    private const string BestSolutionAccuracyTestParameterName = "Best training solution accuracy (test)";
69    private const string ResultsParameterName = "Results";
70
71    public bool EnabledByDefault {
72      get { return true; }
73    }
74
75    #region parameter properties
76    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
77      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
78    }
79    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
80      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
81    }
82    public ILookupParameter<BoolValue> MaximizationParameter {
83      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
84    }
85    public IValueParameter<BoolValue> CalculateSolutionComplexityParameter {
86      get { return (IValueParameter<BoolValue>)Parameters[CalculateSolutionComplexityParameterName]; }
87    }
88    public IValueParameter<BoolValue> CalculateSolutionAccuracyParameter {
89      get { return (IValueParameter<BoolValue>)Parameters[CalculateSolutionAccuracyParameterName]; }
90    }
91    public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
92      get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
93    }
94    public IValueLookupParameter<ClassificationProblemData> ProblemDataParameter {
95      get { return (IValueLookupParameter<ClassificationProblemData>)Parameters[ProblemDataParameterName]; }
96    }
97    public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
98      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
99    }
100    public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
101      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
102    }
103    public IValueLookupParameter<BoolValue> ApplyLinearScalingParameter {
104      get { return (IValueLookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
105    }
106
107    public ILookupParameter<SymbolicClassificationSolution> BestSolutionParameter {
108      get { return (ILookupParameter<SymbolicClassificationSolution>)Parameters[BestSolutionParameterName]; }
109    }
110    public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
111      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
112    }
113    public ILookupParameter<IntValue> BestSolutionLengthParameter {
114      get { return (ILookupParameter<IntValue>)Parameters[BestSolutionLengthParameterName]; }
115    }
116    public ILookupParameter<IntValue> BestSolutionHeightParameter {
117      get { return (ILookupParameter<IntValue>)Parameters[BestSolutionHeightParameterName]; }
118    }
119    public ILookupParameter<IntValue> BestSolutionVariablesParameter {
120      get { return (ILookupParameter<IntValue>)Parameters[BestSolutionVariablesParameterName]; }
121    }
122    public ILookupParameter<DoubleValue> BestSolutionTrainingRSquaredParameter {
123      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionTrainingRSquaredParameterName]; }
124    }
125    public ILookupParameter<DoubleValue> BestSolutionTestRSquaredParameter {
126      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionTestRSquaredParameterName]; }
127    }
128    public ILookupParameter<DoubleValue> BestSolutionTrainingMseParameter {
129      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionTrainingMseParameterName]; }
130    }
131    public ILookupParameter<DoubleValue> BestSolutionTestMseParameter {
132      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionTestMseParameterName]; }
133    }
134    public ILookupParameter<DoubleValue> BestSolutionTrainingRelativeErrorParameter {
135      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionTrainingRelativeErrorParameterName]; }
136    }
137    public ILookupParameter<DoubleValue> BestSolutionTestRelativeErrorParameter {
138      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionTestRelativeErrorParameterName]; }
139    }
140    public ILookupParameter<DoubleValue> BestSolutionTrainingAccuracyParameter {
141      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionAccuracyTrainingParameterName]; }
142    }
143    public ILookupParameter<DoubleValue> BestSolutionTestAccuracyParameter {
144      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionAccuracyTestParameterName]; }
145    }
146    public ILookupParameter<ResultCollection> ResultsParameter {
147      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
148    }
149    #endregion
150    #region properties
151    public ItemArray<SymbolicExpressionTree> SymbolicExpressionTree {
152      get { return SymbolicExpressionTreeParameter.ActualValue; }
153    }
154    public ItemArray<DoubleValue> Quality {
155      get { return QualityParameter.ActualValue; }
156    }
157    public BoolValue Maximization {
158      get { return MaximizationParameter.ActualValue; }
159    }
160    public BoolValue CalculateSolutionComplexity {
161      get { return CalculateSolutionComplexityParameter.Value; }
162      set { CalculateSolutionComplexityParameter.Value = value; }
163    }
164    public BoolValue CalculateSolutionAccuracy {
165      get { return CalculateSolutionAccuracyParameter.Value; }
166      set { CalculateSolutionAccuracyParameter.Value = value; }
167    }
168    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
169      get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
170    }
171    public ClassificationProblemData ProblemData {
172      get { return ProblemDataParameter.ActualValue; }
173    }
174    public DoubleValue UpperEstimationLimit {
175      get { return UpperEstimationLimitParameter.ActualValue; }
176    }
177    public DoubleValue LowerEstimationLimit {
178      get { return LowerEstimationLimitParameter.ActualValue; }
179    }
180    public BoolValue ApplyLinearScaling {
181      get { return ApplyLinearScalingParameter.ActualValue; }
182      set { ApplyLinearScalingParameter.ActualValue = value; }
183    }
184
185    public ResultCollection Results {
186      get { return ResultsParameter.ActualValue; }
187    }
188    public SymbolicClassificationSolution BestSolution {
189      get { return BestSolutionParameter.ActualValue; }
190      set { BestSolutionParameter.ActualValue = value; }
191    }
192    public DoubleValue BestSolutionQuality {
193      get { return BestSolutionQualityParameter.ActualValue; }
194      set { BestSolutionQualityParameter.ActualValue = value; }
195    }
196    public IntValue BestSolutionLength {
197      get { return BestSolutionLengthParameter.ActualValue; }
198      set { BestSolutionLengthParameter.ActualValue = value; }
199    }
200    public IntValue BestSolutionHeight {
201      get { return BestSolutionHeightParameter.ActualValue; }
202      set { BestSolutionHeightParameter.ActualValue = value; }
203    }
204    public IntValue BestSolutionVariables {
205      get { return BestSolutionVariablesParameter.ActualValue; }
206      set { BestSolutionVariablesParameter.ActualValue = value; }
207    }
208    public DoubleValue BestSolutionTrainingRSquared {
209      get { return BestSolutionTrainingRSquaredParameter.ActualValue; }
210      set { BestSolutionTrainingRSquaredParameter.ActualValue = value; }
211    }
212    public DoubleValue BestSolutionTestRSquared {
213      get { return BestSolutionTestRSquaredParameter.ActualValue; }
214      set { BestSolutionTestRSquaredParameter.ActualValue = value; }
215    }
216    public DoubleValue BestSolutionTrainingMse {
217      get { return BestSolutionTrainingMseParameter.ActualValue; }
218      set { BestSolutionTrainingMseParameter.ActualValue = value; }
219    }
220    public DoubleValue BestSolutionTestMse {
221      get { return BestSolutionTestMseParameter.ActualValue; }
222      set { BestSolutionTestMseParameter.ActualValue = value; }
223    }
224    public DoubleValue BestSolutionTrainingRelativeError {
225      get { return BestSolutionTrainingRelativeErrorParameter.ActualValue; }
226      set { BestSolutionTrainingRelativeErrorParameter.ActualValue = value; }
227    }
228    public DoubleValue BestSolutionTestRelativeError {
229      get { return BestSolutionTestRelativeErrorParameter.ActualValue; }
230      set { BestSolutionTestRelativeErrorParameter.ActualValue = value; }
231    }
232    public DoubleValue BestSolutionTrainingAccuracy {
233      get { return BestSolutionTrainingAccuracyParameter.ActualValue; }
234      set { BestSolutionTrainingAccuracyParameter.ActualValue = value; }
235    }
236    public DoubleValue BestSolutionTestAccuracy {
237      get { return BestSolutionTestAccuracyParameter.ActualValue; }
238      set { BestSolutionTestAccuracyParameter.ActualValue = value; }
239    }
240    #endregion
241
242    [StorableConstructor]
243    private TrainingBestSymbolicClassificationSolutionAnalyzer(bool deserializing) : base(deserializing) { }
244    private TrainingBestSymbolicClassificationSolutionAnalyzer(TrainingBestSymbolicClassificationSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
245    public TrainingBestSymbolicClassificationSolutionAnalyzer()
246      : base() {
247      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization."));
248      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
249      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the symbolic expression trees to analyze."));
250      Parameters.Add(new ValueParameter<BoolValue>(CalculateSolutionComplexityParameterName, "Determines if the length and height of the training best solution should be calculated.", new BoolValue(true)));
251      Parameters.Add(new ValueParameter<BoolValue>(CalculateSolutionAccuracyParameterName, "Determines if the accuracy of the training best solution on the training and test set should be calculated.", new BoolValue(true)));
252      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
253      Parameters.Add(new ValueLookupParameter<ClassificationProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
254      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));
255      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));
256      Parameters.Add(new ValueLookupParameter<BoolValue>(ApplyLinearScalingParameterName, "The switch determines if the best solution should be linearly scaled on the whole training set.", new BoolValue(false)));
257      Parameters.Add(new LookupParameter<SymbolicClassificationSolution>(BestSolutionParameterName, "The best symbolic classification solution."));
258      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best symbolic classification solution."));
259      Parameters.Add(new LookupParameter<IntValue>(BestSolutionLengthParameterName, "The length of the best symbolic classification solution."));
260      Parameters.Add(new LookupParameter<IntValue>(BestSolutionHeightParameterName, "The height of the best symbolic classfication solution."));
261      Parameters.Add(new LookupParameter<IntValue>(BestSolutionVariablesParameterName, "The number of variables used by the best symbolic classification solution."));
262      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionTrainingRSquaredParameterName, "The R² value on the training set of the best symbolic classification solution."));
263      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionTestRSquaredParameterName, "The R² value on the test set of the best symbolic classification solution."));
264      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionTrainingMseParameterName, "The mean squared error on the training set of the best symbolic classification solution."));
265      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionTestMseParameterName, "The mean squared error value on the test set of the best symbolic classification solution."));
266      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionTrainingRelativeErrorParameterName, "The relative error on the training set of the best symbolic classification solution."));
267      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionTestRelativeErrorParameterName, "The relative error value on the test set of the best symbolic classification solution."));
268      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionAccuracyTrainingParameterName, "The accuracy on the training set of the best symbolic classification  solution."));
269      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionAccuracyTestParameterName, "The accuracy on the test set of the best symbolic classification  solution."));
270      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic classification solution should be stored."));
271    }
272
273    [StorableHook(HookType.AfterDeserialization)]
274    private void AfterDeserialization() {
275      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
276        Parameters.Add(new ValueLookupParameter<BoolValue>(ApplyLinearScalingParameterName, "The switch determines if the best solution should be linearly scaled on the whole training set.", new BoolValue(false)));
277      }
278    }
279
280    public override IDeepCloneable Clone(Cloner cloner) {
281      return new TrainingBestSymbolicClassificationSolutionAnalyzer(this, cloner);
282    }
283
284    public override IOperation Apply() {
285      #region find best tree
286      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
287      SymbolicExpressionTree bestTree = null;
288      SymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
289      double[] quality = Quality.Select(x => x.Value).ToArray();
290      for (int i = 0; i < tree.Length; i++) {
291        if ((Maximization.Value && quality[i] > bestQuality) ||
292            (!Maximization.Value && quality[i] < bestQuality)) {
293          bestQuality = quality[i];
294          bestTree = tree[i];
295        }
296      }
297      #endregion
298
299      #region update best solution
300      // if the best tree is better than the current best solution => update
301      bool newBest =
302        BestSolutionQuality == null ||
303        (Maximization.Value && bestQuality > BestSolutionQuality.Value) ||
304        (!Maximization.Value && bestQuality < BestSolutionQuality.Value);
305      if (newBest) {
306        double lowerEstimationLimit = LowerEstimationLimit.Value;
307        double upperEstimationLimit = UpperEstimationLimit.Value;
308        string targetVariable = ProblemData.TargetVariable.Value;
309
310        if (ApplyLinearScaling.Value) {
311          // calculate scaling parameters and only for the best tree using the full training set
312          double alpha, beta;
313          SymbolicRegressionScaledMeanSquaredErrorEvaluator.Calculate(SymbolicExpressionTreeInterpreter, bestTree,
314            lowerEstimationLimit, upperEstimationLimit,
315            ProblemData.Dataset, targetVariable,
316            ProblemData.TrainingIndizes, out beta, out alpha);
317
318          // scale tree for solution
319          bestTree = SymbolicRegressionSolutionLinearScaler.Scale(bestTree, alpha, beta);
320        }
321        var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(),
322          bestTree);
323        var solution = new SymbolicClassificationSolution((ClassificationProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit);
324        solution.Name = BestSolutionParameterName;
325        solution.Description = "Best solution on training partition found over the whole run.";
326
327        BestSolution = solution;
328        BestSolutionQuality = new DoubleValue(bestQuality);
329
330        if (CalculateSolutionComplexity.Value) {
331          BestSolutionLength = new IntValue(solution.Model.SymbolicExpressionTree.Size);
332          BestSolutionHeight = new IntValue(solution.Model.SymbolicExpressionTree.Height);
333          BestSolutionVariables = new IntValue(solution.Model.InputVariables.Count());
334          if (!Results.ContainsKey(BestSolutionLengthParameterName)) {
335            Results.Add(new Result(BestSolutionLengthParameterName, "Length of the best solution on the training set.", BestSolutionLength));
336            Results.Add(new Result(BestSolutionHeightParameterName, "Height of the best solution on the training set.", BestSolutionHeight));
337            Results.Add(new Result(BestSolutionVariablesParameterName, "Number of variables used by the best solution on the training set.", BestSolutionVariables));
338          } else {
339            Results[BestSolutionLengthParameterName].Value = BestSolutionLength;
340            Results[BestSolutionHeightParameterName].Value = BestSolutionHeight;
341            Results[BestSolutionVariablesParameterName].Value = BestSolutionVariables;
342          }
343        }
344
345        if (CalculateSolutionAccuracy.Value) {
346          #region update R2,MSE, Rel Error
347          IEnumerable<double> trainingValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable.Value, ProblemData.TrainingIndizes);
348          IEnumerable<double> testValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable.Value, ProblemData.TestIndizes);
349          OnlineAccuracyEvaluator accuracyEvaluator = new OnlineAccuracyEvaluator();
350          OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
351          OnlineMeanAbsolutePercentageErrorEvaluator relErrorEvaluator = new OnlineMeanAbsolutePercentageErrorEvaluator();
352          OnlinePearsonsRSquaredEvaluator r2Evaluator = new OnlinePearsonsRSquaredEvaluator();
353
354          #region training
355          var originalEnumerator = trainingValues.GetEnumerator();
356          var estimatedEnumerator = solution.EstimatedTrainingClassValues.GetEnumerator();
357          while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
358            accuracyEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
359          }
360          originalEnumerator = trainingValues.GetEnumerator();
361          estimatedEnumerator = solution.EstimatedTrainingValues.GetEnumerator();
362          while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
363            mseEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
364            r2Evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
365            relErrorEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
366          }
367          double trainingAccuracy = accuracyEvaluator.Accuracy;
368          double trainingR2 = r2Evaluator.RSquared;
369          double trainingMse = mseEvaluator.MeanSquaredError;
370          double trainingRelError = relErrorEvaluator.MeanAbsolutePercentageError;
371          #endregion
372
373          accuracyEvaluator.Reset();
374          mseEvaluator.Reset();
375          relErrorEvaluator.Reset();
376          r2Evaluator.Reset();
377
378          #region test
379          originalEnumerator = testValues.GetEnumerator();
380          estimatedEnumerator = solution.EstimatedTestClassValues.GetEnumerator();
381          while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
382            accuracyEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
383          }
384          originalEnumerator = testValues.GetEnumerator();
385          estimatedEnumerator = solution.EstimatedTestValues.GetEnumerator();
386          while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
387            mseEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
388            r2Evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
389            relErrorEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
390          }
391          double testAccuracy = accuracyEvaluator.Accuracy;
392          double testR2 = r2Evaluator.RSquared;
393          double testMse = mseEvaluator.MeanSquaredError;
394          double testRelError = relErrorEvaluator.MeanAbsolutePercentageError;
395          #endregion
396          BestSolutionTrainingAccuracy = new DoubleValue(trainingAccuracy);
397          BestSolutionTestAccuracy = new DoubleValue(testAccuracy);
398          BestSolutionTrainingRSquared = new DoubleValue(trainingR2);
399          BestSolutionTestRSquared = new DoubleValue(testR2);
400          BestSolutionTrainingMse = new DoubleValue(trainingMse);
401          BestSolutionTestMse = new DoubleValue(testMse);
402          BestSolutionTrainingRelativeError = new DoubleValue(trainingRelError);
403          BestSolutionTestRelativeError = new DoubleValue(testRelError);
404
405          if (!Results.ContainsKey(BestSolutionAccuracyTrainingParameterName)) {
406            Results.Add(new Result(BestSolutionTrainingRSquaredParameterName, BestSolutionTrainingRSquared));
407            Results.Add(new Result(BestSolutionTestRSquaredParameterName, BestSolutionTestRSquared));
408            Results.Add(new Result(BestSolutionTrainingMseParameterName, BestSolutionTrainingMse));
409            Results.Add(new Result(BestSolutionTestMseParameterName, BestSolutionTestMse));
410            Results.Add(new Result(BestSolutionTrainingRelativeErrorParameterName, BestSolutionTrainingRelativeError));
411            Results.Add(new Result(BestSolutionTestRelativeErrorParameterName, BestSolutionTestRelativeError));
412            Results.Add(new Result(BestSolutionAccuracyTrainingParameterName, BestSolutionTrainingAccuracy));
413            Results.Add(new Result(BestSolutionAccuracyTestParameterName, BestSolutionTestAccuracy));
414          } else {
415            Results[BestSolutionTrainingRSquaredParameterName].Value = BestSolutionTrainingRSquared;
416            Results[BestSolutionTestRSquaredParameterName].Value = BestSolutionTestRSquared;
417            Results[BestSolutionTrainingMseParameterName].Value = BestSolutionTrainingMse;
418            Results[BestSolutionTestMseParameterName].Value = BestSolutionTestMse;
419            Results[BestSolutionTrainingRelativeErrorParameterName].Value = BestSolutionTrainingRelativeError;
420            Results[BestSolutionTestRelativeErrorParameterName].Value = BestSolutionTestRelativeError;
421            Results[BestSolutionAccuracyTrainingParameterName].Value = BestSolutionTrainingAccuracy;
422            Results[BestSolutionAccuracyTestParameterName].Value = BestSolutionTestAccuracy;
423          }
424          #endregion
425        }
426
427        if (!Results.ContainsKey(BestSolutionQualityParameterName)) {
428          Results.Add(new Result(BestSolutionQualityParameterName, BestSolutionQuality));
429          Results.Add(new Result(BestSolutionParameterName, BestSolution));
430        } else {
431          Results[BestSolutionQualityParameterName].Value = BestSolutionQuality;
432          Results[BestSolutionParameterName].Value = BestSolution;
433        }
434      }
435      #endregion
436      return base.Apply();
437    }
438  }
439}
Note: See TracBrowser for help on using the repository browser.