Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs @ 12395

Last change on this file since 12395 was 12395, checked in by dglaser, 10 years ago

#2388 merged trunk into hive statistics branch

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  /// <summary>
31  /// Abstract base class for symbolic data analysis models
32  /// </summary>
33  [StorableClass]
34  public abstract class SymbolicDataAnalysisModel : NamedItem, ISymbolicDataAnalysisModel {
35    public static new Image StaticItemImage {
36      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
37    }
38
39    #region properties
40    [Storable]
41    private double lowerEstimationLimit;
42    public double LowerEstimationLimit { get { return lowerEstimationLimit; } }
43    [Storable]
44    private double upperEstimationLimit;
45    public double UpperEstimationLimit { get { return upperEstimationLimit; } }
46
47    [Storable]
48    private ISymbolicExpressionTree symbolicExpressionTree;
49    public ISymbolicExpressionTree SymbolicExpressionTree {
50      get { return symbolicExpressionTree; }
51    }
52
53    [Storable]
54    private ISymbolicDataAnalysisExpressionTreeInterpreter interpreter;
55    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {
56      get { return interpreter; }
57    }
58    #endregion
59
60    [StorableConstructor]
61    protected SymbolicDataAnalysisModel(bool deserializing) : base(deserializing) { }
62    protected SymbolicDataAnalysisModel(SymbolicDataAnalysisModel original, Cloner cloner)
63      : base(original, cloner) {
64      this.symbolicExpressionTree = cloner.Clone(original.symbolicExpressionTree);
65      this.interpreter = cloner.Clone(original.interpreter);
66      this.lowerEstimationLimit = original.lowerEstimationLimit;
67      this.upperEstimationLimit = original.upperEstimationLimit;
68    }
69    protected SymbolicDataAnalysisModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
70       double lowerEstimationLimit, double upperEstimationLimit)
71      : base() {
72      this.name = ItemName;
73      this.description = ItemDescription;
74      this.symbolicExpressionTree = tree;
75      this.interpreter = interpreter;
76      this.lowerEstimationLimit = lowerEstimationLimit;
77      this.upperEstimationLimit = upperEstimationLimit;
78    }
79
80    #region Scaling
81    protected void Scale(IDataAnalysisProblemData problemData, string targetVariable) {
82      var dataset = problemData.Dataset;
83      var rows = problemData.TrainingIndices;
84      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows);
85      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
86
87      var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
88      var targetValuesEnumerator = targetValues.GetEnumerator();
89      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
90      while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
91        double target = targetValuesEnumerator.Current;
92        double estimated = estimatedValuesEnumerator.Current;
93        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
94          linearScalingCalculator.Add(estimated, target);
95      }
96      if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
97        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
98
99      double alpha = linearScalingCalculator.Alpha;
100      double beta = linearScalingCalculator.Beta;
101      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) return;
102
103      ConstantTreeNode alphaTreeNode = null;
104      ConstantTreeNode betaTreeNode = null;
105      // check if model has been scaled previously by analyzing the structure of the tree
106      var startNode = SymbolicExpressionTree.Root.GetSubtree(0);
107      if (startNode.GetSubtree(0).Symbol is Addition) {
108        var addNode = startNode.GetSubtree(0);
109        if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
110          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
111          var mulNode = addNode.GetSubtree(0);
112          if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
113            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
114          }
115        }
116      }
117      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
118      if (alphaTreeNode != null && betaTreeNode != null) {
119        betaTreeNode.Value *= beta;
120        alphaTreeNode.Value *= beta;
121        alphaTreeNode.Value += alpha;
122      } else {
123        var mainBranch = startNode.GetSubtree(0);
124        startNode.RemoveSubtree(0);
125        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
126        startNode.AddSubtree(scaledMainBranch);
127      }
128    }
129
130    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
131      if (alpha.IsAlmost(0.0)) {
132        return treeNode;
133      } else {
134        var addition = new Addition();
135        var node = addition.CreateTreeNode();
136        var alphaConst = MakeConstant(alpha);
137        node.AddSubtree(treeNode);
138        node.AddSubtree(alphaConst);
139        return node;
140      }
141    }
142
143    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
144      if (beta.IsAlmost(1.0)) {
145        return treeNode;
146      } else {
147        var multipliciation = new Multiplication();
148        var node = multipliciation.CreateTreeNode();
149        var betaConst = MakeConstant(beta);
150        node.AddSubtree(treeNode);
151        node.AddSubtree(betaConst);
152        return node;
153      }
154    }
155
156    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
157      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
158      node.Value = c;
159      return node;
160    }
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.