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