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

Last change on this file since 16556 was 16556, checked in by chaider, 4 months ago

#2971 Added derivates of intervals as result collection to solution view

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