Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3128_Prediction_Intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs @ 17991

Last change on this file since 17991 was 17991, checked in by gkronber, 3 years ago

#3128: first dump of exploratory work-in-progress code to make sure the working copy is not lost.

File size: 12.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  /// <summary>
33  /// Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity
34  /// </summary>
35  [StorableType("88E56AF9-AD72-47E4-A613-8875703BD927")]
36  [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")]
37  public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution {
38    private const string ModelLengthResultName = "Model Length";
39    private const string ModelDepthResultName = "Model Depth";
40
41    private const string EstimationLimitsResultsResultName = "Estimation Limits Results";
42    private const string EstimationLimitsResultName = "Estimation Limits";
43    private const string TrainingUpperEstimationLimitHitsResultName = "Training Upper Estimation Limit Hits";
44    private const string TestLowerEstimationLimitHitsResultName = "Test Lower Estimation Limit Hits";
45    private const string TrainingLowerEstimationLimitHitsResultName = "Training Lower Estimation Limit Hits";
46    private const string TestUpperEstimationLimitHitsResultName = "Test Upper Estimation Limit Hits";
47    private const string TrainingNaNEvaluationsResultName = "Training NaN Evaluations";
48    private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
49
50    private const string ModelBoundsResultName = "Model Bounds";
51
52    public new ISymbolicRegressionModel Model {
53      get { return (ISymbolicRegressionModel)base.Model; }
54      set { base.Model = value; }
55    }
56    ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
57      get { return (ISymbolicDataAnalysisModel)base.Model; }
58    }
59    public int ModelLength {
60      get { return ((IntValue)this[ModelLengthResultName].Value).Value; }
61      private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; }
62    }
63
64    public int ModelDepth {
65      get { return ((IntValue)this[ModelDepthResultName].Value).Value; }
66      private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
67    }
68
69    private ResultCollection EstimationLimitsResultCollection {
70      get { return (ResultCollection)this[EstimationLimitsResultsResultName].Value; }
71    }
72    public DoubleLimit EstimationLimits {
73      get { return (DoubleLimit)EstimationLimitsResultCollection[EstimationLimitsResultName].Value; }
74    }
75
76    public int TrainingUpperEstimationLimitHits {
77      get { return ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value; }
78      private set { ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value = value; }
79    }
80    public int TestUpperEstimationLimitHits {
81      get { return ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value; }
82      private set { ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value = value; }
83    }
84    public int TrainingLowerEstimationLimitHits {
85      get { return ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value; }
86      private set { ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value = value; }
87    }
88    public int TestLowerEstimationLimitHits {
89      get { return ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value; }
90      private set { ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value = value; }
91    }
92    public int TrainingNaNEvaluations {
93      get { return ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value; }
94      private set { ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value = value; }
95    }
96    public int TestNaNEvaluations {
97      get { return ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value; }
98      private set { ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value = value; }
99    }
100
101    public IntervalCollection ModelBoundsCollection {
102      get {
103        if (!ContainsKey(ModelBoundsResultName)) return null;
104        return (IntervalCollection)this[ModelBoundsResultName].Value;
105      }
106      private set {
107        if (ContainsKey(ModelBoundsResultName)) {
108          this[ModelBoundsResultName].Value = value;
109        } else {
110          Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", value));
111        }
112      }
113    }
114
115    IConfidenceRegressionModel IConfidenceRegressionSolution.Model => Model;
116
117    [StorableConstructor]
118    private SymbolicRegressionSolution(StorableConstructorFlag _) : base(_) { }
119    private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
120      : base(original, cloner) {
121    }
122    public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
123      : base(model, problemData) {
124      foreach (var node in model.SymbolicExpressionTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
125        node.SetGrammar(null);
126
127      Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
128      Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
129
130      ResultCollection estimationLimitResults = new ResultCollection();
131      estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
132      estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
133      estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
134      estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
135      estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
136      estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
137      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
138      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
139
140      if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree))
141        Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", new IntervalCollection()));
142
143      RecalculateResults();
144    }
145
146    public override IDeepCloneable Clone(Cloner cloner) {
147      return new SymbolicRegressionSolution(this, cloner);
148    }
149
150    [StorableHook(HookType.AfterDeserialization)]
151    private void AfterDeserialization() {
152      if (!ContainsKey(EstimationLimitsResultsResultName)) {
153        ResultCollection estimationLimitResults = new ResultCollection();
154        estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
155        estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
156        estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
157        estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
158        estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
159        estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
160        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
161        Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
162        CalculateResults();
163      }
164
165      if (!ContainsKey(ModelBoundsResultName)) {
166        if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree)) {
167          Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", new IntervalCollection()));
168          CalculateResults();
169        }
170      }
171    }
172
173    protected override void RecalculateResults() {
174      base.RecalculateResults();
175      CalculateResults();
176    }
177
178    private void CalculateResults() {
179      ModelLength = Model.SymbolicExpressionTree.Length;
180      ModelDepth = Model.SymbolicExpressionTree.Depth;
181
182      EstimationLimits.Lower = Model.LowerEstimationLimit;
183      EstimationLimits.Upper = Model.UpperEstimationLimit;
184
185      TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
186      TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
187      TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
188      TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
189      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
190      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
191
192      //Check if the tree contains unknown symbols for the interval calculation
193      if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree))
194        ModelBoundsCollection = CalculateModelIntervals(this);
195    }
196
197    private static IntervalCollection CalculateModelIntervals(ISymbolicRegressionSolution solution) {
198      var intervalEvaluation = new IntervalCollection();
199      var interpreter = new IntervalInterpreter();
200      var problemData = solution.ProblemData;
201      var model = solution.Model;
202      var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
203
204      intervalEvaluation.AddInterval($"Target {problemData.TargetVariable}", new Interval(variableRanges[problemData.TargetVariable].LowerBound, variableRanges[problemData.TargetVariable].UpperBound));
205      intervalEvaluation.AddInterval("Model", interpreter.GetSymbolicExpressionTreeInterval(model.SymbolicExpressionTree, variableRanges));
206
207      if (DerivativeCalculator.IsCompatible(model.SymbolicExpressionTree)) {
208        foreach (var inputVariable in model.VariablesUsedForPrediction.OrderBy(v => v, new NaturalStringComparer())) {
209          var derivedModel = DerivativeCalculator.Derive(model.SymbolicExpressionTree, inputVariable);
210          var derivedResultInterval = interpreter.GetSymbolicExpressionTreeInterval(derivedModel, variableRanges);
211
212          intervalEvaluation.AddInterval(" ∂f/∂" + inputVariable, new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));
213        }
214      }
215
216      return intervalEvaluation;
217    }
218
219    public IEnumerable<double> EstimatedVariances => GetEstimatedVariances(ProblemData.AllIndices);
220
221    public IEnumerable<double> EstimatedTrainingVariances => GetEstimatedVariances(ProblemData.TestIndices);
222
223    public IEnumerable<double> EstimatedTestVariances => GetEstimatedVariances(ProblemData.TestIndices);
224
225
226    public IEnumerable<double> GetEstimatedVariances(IEnumerable<int> rows) {
227      return Model.GetEstimatedVariances(ProblemData.Dataset, rows);
228    }
229  }
230}
Note: See TracBrowser for help on using the repository browser.