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