Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13847 was 13241, checked in by gkronber, 9 years ago

#2175: merged all changes from branch to trunk (preserving the original .csproj-files so that I don't have to change reference and output paths)

File size: 9.4 KB
RevLine 
[5607]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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;
[5607]23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
[11332]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5914]27using HeuristicLab.Optimization;
[5607]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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>
34  [StorableClass]
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
[5624]49    public new ISymbolicRegressionModel Model {
50      get { return (ISymbolicRegressionModel)base.Model; }
[5717]51      set { base.Model = value; }
[5607]52    }
[5624]53    ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
54      get { return (ISymbolicDataAnalysisModel)base.Model; }
[5607]55    }
[5736]56    public int ModelLength {
57      get { return ((IntValue)this[ModelLengthResultName].Value).Value; }
58      private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; }
59    }
[5607]60
[5736]61    public int ModelDepth {
62      get { return ((IntValue)this[ModelDepthResultName].Value).Value; }
63      private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
64    }
65
[8723]66    private ResultCollection EstimationLimitsResultCollection {
67      get { return (ResultCollection)this[EstimationLimitsResultsResultName].Value; }
68    }
69    public DoubleLimit EstimationLimits {
70      get { return (DoubleLimit)EstimationLimitsResultCollection[EstimationLimitsResultName].Value; }
71    }
72
73    public int TrainingUpperEstimationLimitHits {
74      get { return ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value; }
75      private set { ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value = value; }
76    }
77    public int TestUpperEstimationLimitHits {
78      get { return ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value; }
79      private set { ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value = value; }
80    }
81    public int TrainingLowerEstimationLimitHits {
82      get { return ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value; }
83      private set { ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value = value; }
84    }
85    public int TestLowerEstimationLimitHits {
86      get { return ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value; }
87      private set { ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value = value; }
88    }
89    public int TrainingNaNEvaluations {
90      get { return ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value; }
91      private set { ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value = value; }
92    }
93    public int TestNaNEvaluations {
94      get { return ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value; }
95      private set { ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value = value; }
96    }
97
[5607]98    [StorableConstructor]
[5717]99    private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
100    private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
[5607]101      : base(original, cloner) {
102    }
[5624]103    public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
104      : base(model, problemData) {
[11332]105      foreach (var node in model.SymbolicExpressionTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
106        node.SetGrammar(null);
107
[5736]108      Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
109      Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
[8723]110
111      ResultCollection estimationLimitResults = new ResultCollection();
112      estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
113      estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
114      estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
115      estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
116      estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
117      estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
118      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
119      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
[6588]120      RecalculateResults();
[5607]121    }
122
123    public override IDeepCloneable Clone(Cloner cloner) {
124      return new SymbolicRegressionSolution(this, cloner);
125    }
[5729]126
[8723]127    [StorableHook(HookType.AfterDeserialization)]
128    private void AfterDeserialization() {
129      if (!ContainsKey(EstimationLimitsResultsResultName)) {
130        ResultCollection estimationLimitResults = new ResultCollection();
131        estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
132        estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
133        estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
134        estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
135        estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
136        estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
137        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
138        Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
139        CalculateResults();
140      }
141    }
142
[6411]143    protected override void RecalculateResults() {
[6602]144      base.RecalculateResults();
[8723]145      CalculateResults();
146    }
147
148    private void CalculateResults() {
[5736]149      ModelLength = Model.SymbolicExpressionTree.Length;
150      ModelDepth = Model.SymbolicExpressionTree.Depth;
[8723]151
152      EstimationLimits.Lower = Model.LowerEstimationLimit;
153      EstimationLimits.Upper = Model.UpperEstimationLimit;
154
155      TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
156      TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
157      TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
158      TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
159      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
160      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
[5736]161    }
[5607]162  }
163}
Note: See TracBrowser for help on using the repository browser.