Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs @ 10915

Last change on this file since 10915 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 9.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
30  /// <summary>
31  /// Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity
32  /// </summary>
33  [StorableClass]
34  [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")]
35  public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution {
36    private const string ModelLengthResultName = "Model Length";
37    private const string ModelDepthResultName = "Model Depth";
38
39    private const string EstimationLimitsResultsResultName = "Estimation Limits Results";
40    private const string EstimationLimitsResultName = "Estimation Limits";
41    private const string TrainingUpperEstimationLimitHitsResultName = "Training Upper Estimation Limit Hits";
42    private const string TestLowerEstimationLimitHitsResultName = "Test Lower Estimation Limit Hits";
43    private const string TrainingLowerEstimationLimitHitsResultName = "Training Lower Estimation Limit Hits";
44    private const string TestUpperEstimationLimitHitsResultName = "Test Upper Estimation Limit Hits";
45    private const string TrainingNaNEvaluationsResultName = "Training NaN Evaluations";
46    private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
47
48    public new ISymbolicRegressionModel Model {
49      get { return (ISymbolicRegressionModel)base.Model; }
50      set { base.Model = value; }
51    }
52    ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
53      get { return (ISymbolicDataAnalysisModel)base.Model; }
54    }
55    public int ModelLength {
56      get { return ((IntValue)this[ModelLengthResultName].Value).Value; }
57      private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; }
58    }
59
60    public int ModelDepth {
61      get { return ((IntValue)this[ModelDepthResultName].Value).Value; }
62      private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
63    }
64
65    private ResultCollection EstimationLimitsResultCollection {
66      get { return (ResultCollection)this[EstimationLimitsResultsResultName].Value; }
67    }
68    public DoubleLimit EstimationLimits {
69      get { return (DoubleLimit)EstimationLimitsResultCollection[EstimationLimitsResultName].Value; }
70    }
71
72    public int TrainingUpperEstimationLimitHits {
73      get { return ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value; }
74      private set { ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value = value; }
75    }
76    public int TestUpperEstimationLimitHits {
77      get { return ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value; }
78      private set { ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value = value; }
79    }
80    public int TrainingLowerEstimationLimitHits {
81      get { return ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value; }
82      private set { ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value = value; }
83    }
84    public int TestLowerEstimationLimitHits {
85      get { return ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value; }
86      private set { ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value = value; }
87    }
88    public int TrainingNaNEvaluations {
89      get { return ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value; }
90      private set { ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value = value; }
91    }
92    public int TestNaNEvaluations {
93      get { return ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value; }
94      private set { ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value = value; }
95    }
96
97    [StorableConstructor]
98    private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
99    private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
100      : base(original, cloner) {
101    }
102    public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
103      : base(model, problemData) {
104      Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
105      Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
106
107      ResultCollection estimationLimitResults = new ResultCollection();
108      estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
109      estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
110      estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
111      estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
112      estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
113      estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
114      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
115      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
116
117      RecalculateResults();
118    }
119
120    public override IDeepCloneable Clone(Cloner cloner) {
121      return new SymbolicRegressionSolution(this, cloner);
122    }
123
124    [StorableHook(HookType.AfterDeserialization)]
125    private void AfterDeserialization() {
126      if (!ContainsKey(EstimationLimitsResultsResultName)) {
127        ResultCollection estimationLimitResults = new ResultCollection();
128        estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
129        estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
130        estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
131        estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
132        estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
133        estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
134        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
135        Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
136        CalculateResults();
137      }
138    }
139
140    protected override void RecalculateResults() {
141      base.RecalculateResults();
142      CalculateResults();
143    }
144
145    private void CalculateResults() {
146      ModelLength = Model.SymbolicExpressionTree.Length;
147      ModelDepth = Model.SymbolicExpressionTree.Depth;
148
149      EstimationLimits.Lower = Model.LowerEstimationLimit;
150      EstimationLimits.Upper = Model.UpperEstimationLimit;
151
152      TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
153      TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
154      TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
155      TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
156      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
157      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.