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