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