Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Analyzers/RegressionSolutionAnalyzer.cs @ 3905

Last change on this file since 3905 was 3905, checked in by gkronber, 14 years ago

Implemented calculation of variable impacts as integral over relative variable frequencies. #1036

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