[3892] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3892] | 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 |
|
---|
[4068] | 22 | using System.Collections.Generic;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[3892] | 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.Problems.DataAnalysis.Evaluators;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public abstract class RegressionSolutionAnalyzer : SingleSuccessorOperator {
|
---|
| 35 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 36 | private const string QualityParameterName = "Quality";
|
---|
| 37 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
| 38 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
| 39 | private const string BestSolutionQualityParameterName = "BestSolutionQuality";
|
---|
[3905] | 40 | private const string GenerationsParameterName = "Generations";
|
---|
[3892] | 41 | private const string ResultsParameterName = "Results";
|
---|
[5199] | 42 | private const string BestSolutionResultName = "Best solution (on validation set)";
|
---|
[3892] | 43 | private const string BestSolutionTrainingRSquared = "Best solution R² (training)";
|
---|
| 44 | private const string BestSolutionTestRSquared = "Best solution R² (test)";
|
---|
| 45 | private const string BestSolutionTrainingMse = "Best solution mean squared error (training)";
|
---|
| 46 | private const string BestSolutionTestMse = "Best solution mean squared error (test)";
|
---|
| 47 | private const string BestSolutionTrainingRelativeError = "Best solution average relative error (training)";
|
---|
| 48 | private const string BestSolutionTestRelativeError = "Best solution average relative error (test)";
|
---|
[3905] | 49 | private const string BestSolutionGeneration = "Best solution generation";
|
---|
[3892] | 50 |
|
---|
| 51 | #region parameter properties
|
---|
| 52 | public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {
|
---|
| 53 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
| 54 | }
|
---|
| 55 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 56 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
| 57 | }
|
---|
| 58 | public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
|
---|
| 59 | get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
| 60 | }
|
---|
| 61 | public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
|
---|
| 62 | get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
| 63 | }
|
---|
| 64 | public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
|
---|
| 65 | get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
|
---|
| 66 | }
|
---|
| 67 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 68 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 69 | }
|
---|
[3905] | 70 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
[3923] | 71 | get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
[3905] | 72 | }
|
---|
[3892] | 73 | #endregion
|
---|
| 74 | #region properties
|
---|
| 75 | public DoubleValue UpperEstimationLimit {
|
---|
| 76 | get { return UpperEstimationLimitParameter.ActualValue; }
|
---|
| 77 | }
|
---|
| 78 | public DoubleValue LowerEstimationLimit {
|
---|
| 79 | get { return LowerEstimationLimitParameter.ActualValue; }
|
---|
| 80 | }
|
---|
| 81 | public ItemArray<DoubleValue> Quality {
|
---|
| 82 | get { return QualityParameter.ActualValue; }
|
---|
| 83 | }
|
---|
| 84 | public ResultCollection Results {
|
---|
| 85 | get { return ResultsParameter.ActualValue; }
|
---|
| 86 | }
|
---|
| 87 | public DataAnalysisProblemData ProblemData {
|
---|
| 88 | get { return ProblemDataParameter.ActualValue; }
|
---|
| 89 | }
|
---|
| 90 | #endregion
|
---|
| 91 |
|
---|
[4722] | 92 |
|
---|
| 93 | [StorableConstructor]
|
---|
| 94 | protected RegressionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 95 | protected RegressionSolutionAnalyzer(RegressionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
[3892] | 96 | public RegressionSolutionAnalyzer()
|
---|
| 97 | : base() {
|
---|
| 98 | Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
|
---|
| 99 | Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));
|
---|
| 100 | Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));
|
---|
| 101 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the symbolic regression trees which should be analyzed."));
|
---|
| 102 | Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best regression solution."));
|
---|
[3905] | 103 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations calculated so far."));
|
---|
[3892] | 104 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[3905] | 107 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 108 | private void AfterDeserialization() {
|
---|
[3905] | 109 | // backwards compatibility
|
---|
| 110 | if (!Parameters.ContainsKey(GenerationsParameterName)) {
|
---|
| 111 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations calculated so far."));
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[3892] | 115 | public override IOperation Apply() {
|
---|
| 116 | DoubleValue prevBestSolutionQuality = BestSolutionQualityParameter.ActualValue;
|
---|
| 117 | var bestSolution = UpdateBestSolution();
|
---|
| 118 | if (prevBestSolutionQuality == null || prevBestSolutionQuality.Value > BestSolutionQualityParameter.ActualValue.Value) {
|
---|
[3996] | 119 | RegressionSolutionAnalyzer.UpdateBestSolutionResults(bestSolution, ProblemData, Results, GenerationsParameter.ActualValue);
|
---|
[3892] | 120 | }
|
---|
| 121 |
|
---|
| 122 | return base.Apply();
|
---|
| 123 | }
|
---|
[3996] | 124 |
|
---|
[5246] | 125 | public static void UpdateBestSolutionResults(DataAnalysisSolution solution, DataAnalysisProblemData problemData, ResultCollection results, IntValue generation) {
|
---|
[3892] | 126 | #region update R2,MSE, Rel Error
|
---|
[4468] | 127 | IEnumerable<double> trainingValues = problemData.Dataset.GetEnumeratedVariableValues(problemData.TargetVariable.Value, problemData.TrainingIndizes);
|
---|
| 128 | IEnumerable<double> testValues = problemData.Dataset.GetEnumeratedVariableValues(problemData.TargetVariable.Value, problemData.TestIndizes);
|
---|
[3996] | 129 | OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
|
---|
| 130 | OnlineMeanAbsolutePercentageErrorEvaluator relErrorEvaluator = new OnlineMeanAbsolutePercentageErrorEvaluator();
|
---|
| 131 | OnlinePearsonsRSquaredEvaluator r2Evaluator = new OnlinePearsonsRSquaredEvaluator();
|
---|
[4468] | 132 |
|
---|
[3996] | 133 | #region training
|
---|
| 134 | var originalEnumerator = trainingValues.GetEnumerator();
|
---|
| 135 | var estimatedEnumerator = solution.EstimatedTrainingValues.GetEnumerator();
|
---|
| 136 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 137 | mseEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
| 138 | r2Evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
| 139 | relErrorEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
| 140 | }
|
---|
| 141 | double trainingR2 = r2Evaluator.RSquared;
|
---|
| 142 | double trainingMse = mseEvaluator.MeanSquaredError;
|
---|
| 143 | double trainingRelError = relErrorEvaluator.MeanAbsolutePercentageError;
|
---|
| 144 | #endregion
|
---|
[4468] | 145 |
|
---|
[3996] | 146 | mseEvaluator.Reset();
|
---|
| 147 | relErrorEvaluator.Reset();
|
---|
| 148 | r2Evaluator.Reset();
|
---|
[4468] | 149 |
|
---|
[3996] | 150 | #region test
|
---|
| 151 | originalEnumerator = testValues.GetEnumerator();
|
---|
| 152 | estimatedEnumerator = solution.EstimatedTestValues.GetEnumerator();
|
---|
| 153 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 154 | mseEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
| 155 | r2Evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
| 156 | relErrorEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
| 157 | }
|
---|
| 158 | double testR2 = r2Evaluator.RSquared;
|
---|
| 159 | double testMse = mseEvaluator.MeanSquaredError;
|
---|
| 160 | double testRelError = relErrorEvaluator.MeanAbsolutePercentageError;
|
---|
| 161 | #endregion
|
---|
[4468] | 162 |
|
---|
[3996] | 163 | if (results.ContainsKey(BestSolutionResultName)) {
|
---|
| 164 | results[BestSolutionResultName].Value = solution;
|
---|
| 165 | results[BestSolutionTrainingRSquared].Value = new DoubleValue(trainingR2);
|
---|
| 166 | results[BestSolutionTestRSquared].Value = new DoubleValue(testR2);
|
---|
| 167 | results[BestSolutionTrainingMse].Value = new DoubleValue(trainingMse);
|
---|
| 168 | results[BestSolutionTestMse].Value = new DoubleValue(testMse);
|
---|
| 169 | results[BestSolutionTrainingRelativeError].Value = new DoubleValue(trainingRelError);
|
---|
| 170 | results[BestSolutionTestRelativeError].Value = new DoubleValue(testRelError);
|
---|
[5246] | 171 | if (generation != null) // this check is needed because linear regression solutions do not have a generations parameter
|
---|
| 172 | results[BestSolutionGeneration].Value = new IntValue(generation.Value);
|
---|
[3892] | 173 | } else {
|
---|
[3996] | 174 | results.Add(new Result(BestSolutionResultName, solution));
|
---|
| 175 | results.Add(new Result(BestSolutionTrainingRSquared, new DoubleValue(trainingR2)));
|
---|
| 176 | results.Add(new Result(BestSolutionTestRSquared, new DoubleValue(testR2)));
|
---|
| 177 | results.Add(new Result(BestSolutionTrainingMse, new DoubleValue(trainingMse)));
|
---|
| 178 | results.Add(new Result(BestSolutionTestMse, new DoubleValue(testMse)));
|
---|
| 179 | results.Add(new Result(BestSolutionTrainingRelativeError, new DoubleValue(trainingRelError)));
|
---|
| 180 | results.Add(new Result(BestSolutionTestRelativeError, new DoubleValue(testRelError)));
|
---|
[5246] | 181 | if (generation != null)
|
---|
| 182 | results.Add(new Result(BestSolutionGeneration, new IntValue(generation.Value)));
|
---|
[3892] | 183 | }
|
---|
| 184 | #endregion
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | protected abstract DataAnalysisSolution UpdateBestSolution();
|
---|
| 188 | }
|
---|
| 189 | }
|
---|