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

Last change on this file since 16830 was 16830, checked in by chaider, 3 years ago

#2971 Fixed review comments

File size: 11.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 IntervalEvaluationResultName = "Interval Evaluation";
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
101    public NamedIntervals IntervalEvaluationCollection {
102      get { return (NamedIntervals)this[IntervalEvaluationResultName].Value; }
103      private set { this[IntervalEvaluationResultName].Value = value; }
104    }
105
106
107
108    [StorableConstructor]
109    private SymbolicRegressionSolution(StorableConstructorFlag _) : base(_) { }
110    private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
111      : base(original, cloner) {
112    }
113    public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
114      : base(model, problemData) {
115      foreach (var node in model.SymbolicExpressionTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
116        node.SetGrammar(null);
117
118      Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
119      Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
120
121      ResultCollection estimationLimitResults = new ResultCollection();
122      estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
123      estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
124      estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
125      estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
126      estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
127      estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
128      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
129      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
130      Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", GetIntervalEvaluations()));
131      RecalculateResults();
132    }
133
134    public override IDeepCloneable Clone(Cloner cloner) {
135      return new SymbolicRegressionSolution(this, cloner);
136    }
137
138    [StorableHook(HookType.AfterDeserialization)]
139    private void AfterDeserialization() {
140      if (!ContainsKey(EstimationLimitsResultsResultName)) {
141        ResultCollection estimationLimitResults = new ResultCollection();
142        estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
143        estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
144        estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
145        estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
146        estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
147        estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
148        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
149        Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
150        Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", GetIntervalEvaluations()));
151        CalculateResults();
152      }
153    }
154
155    protected override void RecalculateResults() {
156      base.RecalculateResults();
157      CalculateResults();
158    }
159
160    private NamedIntervals GetIntervalEvaluations() {
161      var intervalEvaluation = new NamedIntervals();
162      var interpreter = new IntervalInterpreter();
163      var variableRanges = ProblemData.VariableRanges.VariableIntervals;
164
165      if (variableRanges != null) {
166        intervalEvaluation.VariableIntervals.Add($"Target {ProblemData.TargetVariable}", new Interval(variableRanges[ProblemData.TargetVariable].LowerBound, variableRanges[ProblemData.TargetVariable].UpperBound));
167        intervalEvaluation.VariableIntervals.Add("Modell Interval", interpreter.GetSymbolicExpressionTreeInterval(Model.SymbolicExpressionTree, variableRanges));
168     
169        foreach (var derivate in variableRanges) {
170          if (derivate.Key != ProblemData.TargetVariable) {
171            var derived = DerivativeCalculator.Derive(Model.SymbolicExpressionTree, derivate.Key);
172            var derivedResultInterval = interpreter.GetSymbolicExpressionTreeInterval(derived, variableRanges);
173
174            intervalEvaluation.VariableIntervals.Add(" \u2202f/\u2202" + derivate.Key,
175              new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));
176          }
177        }
178      }
179      return intervalEvaluation;
180    }
181
182    private void CalculateResults() {
183      ModelLength = Model.SymbolicExpressionTree.Length;
184      ModelDepth = Model.SymbolicExpressionTree.Depth;
185
186      EstimationLimits.Lower = Model.LowerEstimationLimit;
187      EstimationLimits.Upper = Model.UpperEstimationLimit;
188
189      TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
190      TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
191      TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
192      TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
193      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
194      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
195
196      IntervalEvaluationCollection = GetIntervalEvaluations();
197    }
198  }
199}
Note: See TracBrowser for help on using the repository browser.