[3892] | 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.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
| 32 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 33 | using System.Collections.Generic;
|
---|
| 34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 35 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 36 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
| 37 |
|
---|
| 38 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
|
---|
| 39 | [StorableClass]
|
---|
| 40 | public abstract class RegressionSolutionAnalyzer : SingleSuccessorOperator {
|
---|
| 41 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 42 | private const string QualityParameterName = "Quality";
|
---|
| 43 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
| 44 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
| 45 | private const string BestSolutionQualityParameterName = "BestSolutionQuality";
|
---|
| 46 | private const string ResultsParameterName = "Results";
|
---|
| 47 | private const string BestSolutionResultName = "Best solution (on validiation set)";
|
---|
| 48 | private const string BestSolutionTrainingRSquared = "Best solution R² (training)";
|
---|
| 49 | private const string BestSolutionTestRSquared = "Best solution R² (test)";
|
---|
| 50 | private const string BestSolutionTrainingMse = "Best solution mean squared error (training)";
|
---|
| 51 | private const string BestSolutionTestMse = "Best solution mean squared error (test)";
|
---|
| 52 | private const string BestSolutionTrainingRelativeError = "Best solution average relative error (training)";
|
---|
| 53 | private const string BestSolutionTestRelativeError = "Best solution average relative error (test)";
|
---|
| 54 |
|
---|
| 55 | #region parameter properties
|
---|
| 56 | public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {
|
---|
| 57 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
| 58 | }
|
---|
| 59 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 60 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
| 61 | }
|
---|
| 62 | public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
|
---|
| 63 | get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
| 64 | }
|
---|
| 65 | public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
|
---|
| 66 | get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
| 67 | }
|
---|
| 68 | public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
|
---|
| 69 | get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
|
---|
| 70 | }
|
---|
| 71 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 72 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 73 | }
|
---|
| 74 | #endregion
|
---|
| 75 | #region properties
|
---|
| 76 | public DoubleValue UpperEstimationLimit {
|
---|
| 77 | get { return UpperEstimationLimitParameter.ActualValue; }
|
---|
| 78 | }
|
---|
| 79 | public DoubleValue LowerEstimationLimit {
|
---|
| 80 | get { return LowerEstimationLimitParameter.ActualValue; }
|
---|
| 81 | }
|
---|
| 82 | public ItemArray<DoubleValue> Quality {
|
---|
| 83 | get { return QualityParameter.ActualValue; }
|
---|
| 84 | }
|
---|
| 85 | public ResultCollection Results {
|
---|
| 86 | get { return ResultsParameter.ActualValue; }
|
---|
| 87 | }
|
---|
| 88 | public DataAnalysisProblemData ProblemData {
|
---|
| 89 | get { return ProblemDataParameter.ActualValue; }
|
---|
| 90 | }
|
---|
| 91 | #endregion
|
---|
| 92 |
|
---|
| 93 | public RegressionSolutionAnalyzer()
|
---|
| 94 | : base() {
|
---|
| 95 | Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
|
---|
| 96 | Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));
|
---|
| 97 | Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));
|
---|
| 98 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the symbolic regression trees which should be analyzed."));
|
---|
| 99 | Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best regression solution."));
|
---|
| 100 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public override IOperation Apply() {
|
---|
| 104 | DoubleValue prevBestSolutionQuality = BestSolutionQualityParameter.ActualValue;
|
---|
| 105 | var bestSolution = UpdateBestSolution();
|
---|
| 106 | if (prevBestSolutionQuality == null || prevBestSolutionQuality.Value > BestSolutionQualityParameter.ActualValue.Value) {
|
---|
| 107 | UpdateBestSolutionResults(bestSolution);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | return base.Apply();
|
---|
| 111 | }
|
---|
| 112 | private void UpdateBestSolutionResults(DataAnalysisSolution bestSolution) {
|
---|
| 113 | var solution = bestSolution;
|
---|
| 114 | #region update R2,MSE, Rel Error
|
---|
| 115 | double[] trainingValues = ProblemData.Dataset.GetVariableValues(
|
---|
| 116 | ProblemData.TargetVariable.Value,
|
---|
| 117 | ProblemData.TrainingSamplesStart.Value,
|
---|
| 118 | ProblemData.TrainingSamplesEnd.Value);
|
---|
| 119 | double[] testValues = ProblemData.Dataset.GetVariableValues(
|
---|
| 120 | ProblemData.TargetVariable.Value,
|
---|
| 121 | ProblemData.TestSamplesStart.Value,
|
---|
| 122 | ProblemData.TestSamplesEnd.Value);
|
---|
| 123 | double trainingR2 = SimpleRSquaredEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues);
|
---|
| 124 | double testR2 = SimpleRSquaredEvaluator.Calculate(testValues, solution.EstimatedTestValues);
|
---|
| 125 | double trainingMse = SimpleMSEEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues);
|
---|
| 126 | double testMse = SimpleMSEEvaluator.Calculate(testValues, solution.EstimatedTestValues);
|
---|
| 127 | double trainingRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues);
|
---|
| 128 | double testRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(testValues, solution.EstimatedTestValues);
|
---|
| 129 | if (Results.ContainsKey(BestSolutionResultName)) {
|
---|
| 130 | Results[BestSolutionResultName].Value = solution;
|
---|
| 131 | Results[BestSolutionTrainingRSquared].Value = new DoubleValue(trainingR2);
|
---|
| 132 | Results[BestSolutionTestRSquared].Value = new DoubleValue(testR2);
|
---|
| 133 | Results[BestSolutionTrainingMse].Value = new DoubleValue(trainingMse);
|
---|
| 134 | Results[BestSolutionTestMse].Value = new DoubleValue(testMse);
|
---|
| 135 | Results[BestSolutionTrainingRelativeError].Value = new DoubleValue(trainingRelError);
|
---|
| 136 | Results[BestSolutionTestRelativeError].Value = new DoubleValue(testRelError);
|
---|
| 137 | } else {
|
---|
| 138 | Results.Add(new Result(BestSolutionResultName, solution));
|
---|
| 139 | Results.Add(new Result(BestSolutionTrainingRSquared, new DoubleValue(trainingR2)));
|
---|
| 140 | Results.Add(new Result(BestSolutionTestRSquared, new DoubleValue(testR2)));
|
---|
| 141 | Results.Add(new Result(BestSolutionTrainingMse, new DoubleValue(trainingMse)));
|
---|
| 142 | Results.Add(new Result(BestSolutionTestMse, new DoubleValue(testMse)));
|
---|
| 143 | Results.Add(new Result(BestSolutionTrainingRelativeError, new DoubleValue(trainingRelError)));
|
---|
| 144 | Results.Add(new Result(BestSolutionTestRelativeError, new DoubleValue(testRelError)));
|
---|
| 145 | }
|
---|
| 146 | #endregion
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | protected abstract DataAnalysisSolution UpdateBestSolution();
|
---|
| 150 | }
|
---|
| 151 | }
|
---|