Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs @ 17928

Last change on this file since 17928 was 17911, checked in by gkronber, 4 years ago

#3073 code improvements after reintegration of branch

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