source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs @ 16635

Last change on this file since 16635 was 16635, checked in by gkronber, 4 months ago

#2971: fixed updating of IntervalResults for SymbolicRegressionSolution

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