Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17538 was 17538, checked in by mkommend, 4 years ago

#2971: Changed logic when intervals are calculated in symbolic regression solutions and simplifier.

File size: 11.7 KB
RevLine 
[16927]1#region License Information
[5607]2/* HeuristicLab
[17206]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5607]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
[8723]22using System.Linq;
[16713]23using HEAL.Attic;
[5607]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
[11332]27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5914]28using HeuristicLab.Optimization;
[5607]29
[5624]30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[5607]31  /// <summary>
32  /// Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity
33  /// </summary>
[16641]34  [StorableType("88E56AF9-AD72-47E4-A613-8875703BD927")]
[5607]35  [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")]
[5717]36  public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution {
[5975]37    private const string ModelLengthResultName = "Model Length";
38    private const string ModelDepthResultName = "Model Depth";
[5736]39
[8723]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
[17509]49    private const string ModelBoundsResultName = "Model Bounds";
[16556]50
[5624]51    public new ISymbolicRegressionModel Model {
52      get { return (ISymbolicRegressionModel)base.Model; }
[5717]53      set { base.Model = value; }
[5607]54    }
[5624]55    ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
56      get { return (ISymbolicDataAnalysisModel)base.Model; }
[5607]57    }
[5736]58    public int ModelLength {
59      get { return ((IntValue)this[ModelLengthResultName].Value).Value; }
60      private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; }
61    }
[5607]62
[5736]63    public int ModelDepth {
64      get { return ((IntValue)this[ModelDepthResultName].Value).Value; }
65      private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
66    }
67
[8723]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
[17509]100    public IntervalCollection ModelBoundsCollection {
101      get { return (IntervalCollection)this[ModelBoundsResultName].Value; }
102      private set { this[ModelBoundsResultName].Value = value; }
[16627]103    }
[16556]104
105
[16627]106
[5607]107    [StorableConstructor]
[16627]108    private SymbolicRegressionSolution(StorableConstructorFlag _) : base(_) { }
[5717]109    private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
[5607]110      : base(original, cloner) {
111    }
[5624]112    public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
113      : base(model, problemData) {
[11332]114      foreach (var node in model.SymbolicExpressionTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
115        node.SetGrammar(null);
116
[5736]117      Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
118      Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
[8723]119
120      ResultCollection estimationLimitResults = new ResultCollection();
121      estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
122      estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
123      estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
124      estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
125      estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
126      estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
127      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
128      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
[17538]129
130      if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree))
131        Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", new IntervalCollection()));
132
[6588]133      RecalculateResults();
[5607]134    }
135
136    public override IDeepCloneable Clone(Cloner cloner) {
137      return new SymbolicRegressionSolution(this, cloner);
138    }
[5729]139
[8723]140    [StorableHook(HookType.AfterDeserialization)]
141    private void AfterDeserialization() {
142      if (!ContainsKey(EstimationLimitsResultsResultName)) {
143        ResultCollection estimationLimitResults = new ResultCollection();
144        estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
145        estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
146        estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
147        estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
148        estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
149        estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
150        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
151        Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
152        CalculateResults();
153      }
[17509]154
155      if (!ContainsKey(ModelBoundsResultName)) {
156        Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", new IntervalCollection()));
157        CalculateResults();
158      }
[8723]159    }
160
[6411]161    protected override void RecalculateResults() {
[6602]162      base.RecalculateResults();
[8723]163      CalculateResults();
164    }
165
166    private void CalculateResults() {
[5736]167      ModelLength = Model.SymbolicExpressionTree.Length;
168      ModelDepth = Model.SymbolicExpressionTree.Depth;
[8723]169
170      EstimationLimits.Lower = Model.LowerEstimationLimit;
171      EstimationLimits.Upper = Model.UpperEstimationLimit;
172
173      TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
174      TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
175      TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
176      TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
177      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
178      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
[16627]179
[17509]180      //Check if the tree contains unknown symbols for the interval calculation
[17538]181      if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree))
[17509]182        ModelBoundsCollection = CalculateModelIntervals(this);
[5736]183    }
[16851]184
[16904]185    private static IntervalCollection CalculateModelIntervals(ISymbolicRegressionSolution solution) {
186      var intervalEvaluation = new IntervalCollection();
[16851]187      var interpreter = new IntervalInterpreter();
188      var problemData = solution.ProblemData;
189      var model = solution.Model;
[16927]190      var variableRanges = problemData.VariableRanges.GetIntervals();
[16851]191
[16927]192      intervalEvaluation.AddInterval($"Target {problemData.TargetVariable}", new Interval(variableRanges[problemData.TargetVariable].LowerBound, variableRanges[problemData.TargetVariable].UpperBound));
193      intervalEvaluation.AddInterval("Model Interval", interpreter.GetSymbolicExpressionTreeInterval(model.SymbolicExpressionTree, variableRanges));
[16851]194
[17538]195      if (DerivativeCalculator.IsCompatible(model.SymbolicExpressionTree)) {
196        foreach (var inputVariable in model.VariablesUsedForPrediction.OrderBy(v => v, new NaturalStringComparer())) {
197          var derivedModel = DerivativeCalculator.Derive(model.SymbolicExpressionTree, inputVariable);
198          var derivedResultInterval = interpreter.GetSymbolicExpressionTreeInterval(derivedModel, variableRanges);
199
200          intervalEvaluation.AddInterval(" ∂f/∂" + inputVariable, new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));
201        }
[16851]202      }
203
204      return intervalEvaluation;
205    }
[5607]206  }
207}
Note: See TracBrowser for help on using the repository browser.