Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3127-MRGP-VarPro-Exploration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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