Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs @ 14449

Last change on this file since 14449 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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