[5624] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17687] | 3 | * Copyright (C) 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 |
|
---|
[9126] | 22 | using System;
|
---|
[17687] | 23 | using System.Collections.Generic;
|
---|
[5914] | 24 | using System.Drawing;
|
---|
[17687] | 25 | using System.Linq;
|
---|
[5624] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[17687] | 28 | using HEAL.Attic;
|
---|
[5624] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 31 | /// <summary>
|
---|
[5717] | 32 | /// Abstract base class for symbolic data analysis models
|
---|
[5624] | 33 | /// </summary>
|
---|
[17687] | 34 | [StorableType("EE72299A-7F04-40DA-994E-F12EF9B12CE7")]
|
---|
| 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
|
---|
[9708] | 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 | }
|
---|
[17687] | 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 |
|
---|
[5624] | 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | [StorableConstructor]
|
---|
[17687] | 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);
|
---|
[9708] | 80 | this.lowerEstimationLimit = original.lowerEstimationLimit;
|
---|
| 81 | this.upperEstimationLimit = original.upperEstimationLimit;
|
---|
[5624] | 82 | }
|
---|
[9708] | 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;
|
---|
[9708] | 90 | this.lowerEstimationLimit = lowerEstimationLimit;
|
---|
| 91 | this.upperEstimationLimit = upperEstimationLimit;
|
---|
[5624] | 92 | }
|
---|
[9126] | 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;
|
---|
[17687] | 119 | // check if model has a structure that can be re-used for scaling
|
---|
[9126] | 120 | var startNode = SymbolicExpressionTree.Root.GetSubtree(0);
|
---|
[17687] | 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);
|
---|
[9126] | 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
|
---|
[17687] | 174 |
|
---|
[5624] | 175 | }
|
---|
| 176 | }
|
---|