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