[5624] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 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.Regression {
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// Represents a symbolic regression model
|
---|
| 31 | /// </summary>
|
---|
| 32 | [StorableClass]
|
---|
[6555] | 33 | [Item(Name = "Symbolic Regression Model", Description = "Represents a symbolic regression model.")]
|
---|
[5624] | 34 | public class SymbolicRegressionModel : SymbolicDataAnalysisModel, ISymbolicRegressionModel {
|
---|
[5720] | 35 | [Storable]
|
---|
| 36 | private double lowerEstimationLimit;
|
---|
| 37 | [Storable]
|
---|
| 38 | private double upperEstimationLimit;
|
---|
[5624] | 39 |
|
---|
| 40 | [StorableConstructor]
|
---|
| 41 | protected SymbolicRegressionModel(bool deserializing) : base(deserializing) { }
|
---|
| 42 | protected SymbolicRegressionModel(SymbolicRegressionModel original, Cloner cloner)
|
---|
| 43 | : base(original, cloner) {
|
---|
[5720] | 44 | this.lowerEstimationLimit = original.lowerEstimationLimit;
|
---|
| 45 | this.upperEstimationLimit = original.upperEstimationLimit;
|
---|
[5624] | 46 | }
|
---|
[5720] | 47 | public SymbolicRegressionModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 48 | double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
|
---|
[5624] | 49 | : base(tree, interpreter) {
|
---|
[5720] | 50 | this.lowerEstimationLimit = lowerEstimationLimit;
|
---|
| 51 | this.upperEstimationLimit = upperEstimationLimit;
|
---|
[5624] | 52 | }
|
---|
| 53 |
|
---|
| 54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 55 | return new SymbolicRegressionModel(this, cloner);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[5649] | 58 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
[5720] | 59 | return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows)
|
---|
| 60 | .LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
[5624] | 61 | }
|
---|
[5818] | 62 |
|
---|
[6603] | 63 | public ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
| 64 | return new SymbolicRegressionSolution(this, problemData);
|
---|
| 65 | }
|
---|
| 66 | IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
| 67 | return CreateRegressionSolution(problemData);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[5818] | 70 | public static void Scale(SymbolicRegressionModel model, IRegressionProblemData problemData) {
|
---|
| 71 | var dataset = problemData.Dataset;
|
---|
| 72 | var targetVariable = problemData.TargetVariable;
|
---|
| 73 | var rows = problemData.TrainingIndizes;
|
---|
| 74 | var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
|
---|
[6740] | 75 | var targetValues = dataset.GetDoubleValues(targetVariable, rows);
|
---|
[5818] | 76 | double alpha;
|
---|
| 77 | double beta;
|
---|
[5942] | 78 | OnlineCalculatorError errorState;
|
---|
[5894] | 79 | OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
|
---|
[5942] | 80 | if (errorState != OnlineCalculatorError.None) return;
|
---|
[5818] | 81 |
|
---|
| 82 | ConstantTreeNode alphaTreeNode = null;
|
---|
| 83 | ConstantTreeNode betaTreeNode = null;
|
---|
| 84 | // check if model has been scaled previously by analyzing the structure of the tree
|
---|
| 85 | var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
|
---|
| 86 | if (startNode.GetSubtree(0).Symbol is Addition) {
|
---|
| 87 | var addNode = startNode.GetSubtree(0);
|
---|
[6803] | 88 | if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
|
---|
[5818] | 89 | alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
|
---|
| 90 | var mulNode = addNode.GetSubtree(0);
|
---|
[6803] | 91 | if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
|
---|
[5818] | 92 | betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
|
---|
| 97 | if (alphaTreeNode != null && betaTreeNode != null) {
|
---|
| 98 | betaTreeNode.Value *= beta;
|
---|
| 99 | alphaTreeNode.Value *= beta;
|
---|
| 100 | alphaTreeNode.Value += alpha;
|
---|
| 101 | } else {
|
---|
| 102 | var mainBranch = startNode.GetSubtree(0);
|
---|
| 103 | startNode.RemoveSubtree(0);
|
---|
[6234] | 104 | var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
|
---|
[5818] | 105 | startNode.AddSubtree(scaledMainBranch);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
|
---|
| 110 | if (alpha.IsAlmost(0.0)) {
|
---|
| 111 | return treeNode;
|
---|
| 112 | } else {
|
---|
[6234] | 113 | var addition = new Addition();
|
---|
[6233] | 114 | var node = addition.CreateTreeNode();
|
---|
[5818] | 115 | var alphaConst = MakeConstant(alpha);
|
---|
| 116 | node.AddSubtree(treeNode);
|
---|
| 117 | node.AddSubtree(alphaConst);
|
---|
| 118 | return node;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[6233] | 122 | private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
|
---|
[5818] | 123 | if (beta.IsAlmost(1.0)) {
|
---|
| 124 | return treeNode;
|
---|
| 125 | } else {
|
---|
[6234] | 126 | var multipliciation = new Multiplication();
|
---|
[6233] | 127 | var node = multipliciation.CreateTreeNode();
|
---|
[5818] | 128 | var betaConst = MakeConstant(beta);
|
---|
| 129 | node.AddSubtree(treeNode);
|
---|
| 130 | node.AddSubtree(betaConst);
|
---|
| 131 | return node;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | private static ISymbolicExpressionTreeNode MakeConstant(double c) {
|
---|
| 136 | var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
|
---|
| 137 | node.Value = c;
|
---|
| 138 | return node;
|
---|
| 139 | }
|
---|
[5624] | 140 | }
|
---|
| 141 | }
|
---|