Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16773 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 7.2 KB
RevLine 
[5624]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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.Encodings.SymbolicExpressionTreeEncoding;
[16565]28using HEAL.Attic;
[5624]29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31  /// <summary>
[5717]32  /// Abstract base class for symbolic data analysis models
[5624]33  /// </summary>
[16565]34  [StorableType("EE72299A-7F04-40DA-994E-F12EF9B12CE7")]
[16243]35  public abstract class SymbolicDataAnalysisModel : DataAnalysisModel, ISymbolicDataAnalysisModel {
[7201]36    public static new Image StaticItemImage {
[5649]37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
38    }
39
[5624]40    #region properties
[9587]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; } }
[5624]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    }
[13921]59
[16243]60    public override IEnumerable<string> VariablesUsedForPrediction {
[13921]61      get {
[13941]62        var variables =
[13921]63          SymbolicExpressionTree.IterateNodesPrefix()
[14826]64            .OfType<IVariableTreeNode>()
[13921]65            .Select(x => x.VariableName)
[13941]66            .Distinct();
67
[14826]68        return variables.OrderBy(x => x);
[13921]69      }
70    }
71
[5624]72    #endregion
73
74    [StorableConstructor]
[16565]75    protected SymbolicDataAnalysisModel(StorableConstructorFlag _) : base(_) { }
[5624]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);
[9587]80      this.lowerEstimationLimit = original.lowerEstimationLimit;
81      this.upperEstimationLimit = original.upperEstimationLimit;
[5624]82    }
[9587]83    protected SymbolicDataAnalysisModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
84       double lowerEstimationLimit, double upperEstimationLimit)
[5624]85      : base() {
[5649]86      this.name = ItemName;
87      this.description = ItemDescription;
[5624]88      this.symbolicExpressionTree = tree;
89      this.interpreter = interpreter;
[9587]90      this.lowerEstimationLimit = lowerEstimationLimit;
91      this.upperEstimationLimit = upperEstimationLimit;
[5624]92    }
[8664]93
94    #region Scaling
[8972]95    protected void Scale(IDataAnalysisProblemData problemData, string targetVariable) {
[8664]96      var dataset = problemData.Dataset;
97      var rows = problemData.TrainingIndices;
[8972]98      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows);
[8664]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;
[16290]119      // check if model has a structure that can be re-used for scaling
[8972]120      var startNode = SymbolicExpressionTree.Root.GetSubtree(0);
[16290]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);
[8664]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
[13921]174
[5624]175  }
176}
Note: See TracBrowser for help on using the repository browser.