Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionSolution.cs @ 5606

Last change on this file since 5606 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
31
32namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
33  /// <summary>
34  /// Represents a solution for a symbolic regression problem which can be visualized in the GUI.
35  /// </summary>
36  [Item("SymbolicRegressionSolution", "Represents a solution for a symbolic regression problem which can be visualized in the GUI.")]
37  [StorableClass]
38  public class SymbolicRegressionSolution : DataAnalysisSolution {
39    public override Image ItemImage {
40      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
41    }
42
43    public new SymbolicRegressionModel Model {
44      get { return (SymbolicRegressionModel)base.Model; }
45      set { base.Model = value; }
46    }
47
48    protected List<double> estimatedValues;
49    public override IEnumerable<double> EstimatedValues {
50      get {
51        if (estimatedValues == null) RecalculateEstimatedValues();
52        return estimatedValues;
53      }
54    }
55
56    public override IEnumerable<double> EstimatedTrainingValues {
57      get { return GetEstimatedValues(ProblemData.TrainingIndizes); }
58    }
59
60    public override IEnumerable<double> EstimatedTestValues {
61      get { return GetEstimatedValues(ProblemData.TestIndizes); }
62    }
63
64    [StorableConstructor]
65    protected SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
66    protected SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
67      : base(original, cloner) {
68    }
69    public SymbolicRegressionSolution(DataAnalysisProblemData problemData, SymbolicRegressionModel model, double lowerEstimationLimit, double upperEstimationLimit)
70      : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
71      this.Model = model;
72    }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new SymbolicRegressionSolution(this, cloner);
76    }
77
78    protected override void RecalculateEstimatedValues() {
79      int minLag = GetMinimumLagFromTree(Model.SymbolicExpressionTree.Root);
80      IEnumerable<double> calculatedValues =
81          from x in Model.GetEstimatedValues(ProblemData, 0 - minLag, ProblemData.Dataset.Rows)
82          let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, x))
83          select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX;
84      estimatedValues = Enumerable.Repeat(UpperEstimationLimit, Math.Abs(minLag)).Concat(calculatedValues).ToList();
85      OnEstimatedValuesChanged();
86    }
87
88    public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
89      if (estimatedValues == null) RecalculateEstimatedValues();
90      foreach (int row in rows)
91        yield return estimatedValues[row];
92    }
93
94    protected int GetMinimumLagFromTree(SymbolicExpressionTreeNode node) {
95      if (node == null) return 0;
96      int lag = 0;
97
98      var laggedTreeNode = node as ILaggedTreeNode;
99      if (laggedTreeNode != null) lag += laggedTreeNode.Lag;
100      else if (node.Symbol is Derivative) lag -= 4;
101
102      int subtreeLag = 0;
103      foreach (var subtree in node.SubTrees) {
104        subtreeLag = Math.Min(subtreeLag, GetMinimumLagFromTree(subtree));
105      }
106      return lag + subtreeLag;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.