1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using System;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
35 | /// <summary>
|
---|
36 | /// Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity
|
---|
37 | /// </summary>
|
---|
38 | [StorableClass]
|
---|
39 | [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")]
|
---|
40 | public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution {
|
---|
41 | private const string ModelLengthResultName = "ModelLength";
|
---|
42 | private const string ModelDepthResultName = "ModelDepth";
|
---|
43 |
|
---|
44 | public new ISymbolicRegressionModel Model {
|
---|
45 | get { return (ISymbolicRegressionModel)base.Model; }
|
---|
46 | set { base.Model = value; }
|
---|
47 | }
|
---|
48 | ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
|
---|
49 | get { return (ISymbolicDataAnalysisModel)base.Model; }
|
---|
50 | }
|
---|
51 | public int ModelLength {
|
---|
52 | get { return ((IntValue)this[ModelLengthResultName].Value).Value; }
|
---|
53 | private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public int ModelDepth {
|
---|
57 | get { return ((IntValue)this[ModelDepthResultName].Value).Value; }
|
---|
58 | private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
|
---|
63 | private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
|
---|
64 | : base(original, cloner) {
|
---|
65 | }
|
---|
66 | public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
|
---|
67 | : base(model, problemData) {
|
---|
68 | Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
|
---|
69 | Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
|
---|
70 | RecalculateResults();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
74 | return new SymbolicRegressionSolution(this, cloner);
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override void OnModelChanged(EventArgs e) {
|
---|
78 | base.OnModelChanged(e);
|
---|
79 | RecalculateResults();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private new void RecalculateResults() {
|
---|
83 | ModelLength = Model.SymbolicExpressionTree.Length;
|
---|
84 | ModelDepth = Model.SymbolicExpressionTree.Depth;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void ScaleModel() {
|
---|
88 | var dataset = ProblemData.Dataset;
|
---|
89 | var targetVariable = ProblemData.TargetVariable;
|
---|
90 | var rows = ProblemData.TrainingIndizes;
|
---|
91 | var estimatedValues = GetEstimatedValues(rows);
|
---|
92 | var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
|
---|
93 | double alpha;
|
---|
94 | double beta;
|
---|
95 | OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
|
---|
96 |
|
---|
97 | ConstantTreeNode alphaTreeNode = null;
|
---|
98 | ConstantTreeNode betaTreeNode = null;
|
---|
99 | // check if model has been scaled previously by analyzing the structure of the tree
|
---|
100 | var startNode = Model.SymbolicExpressionTree.Root.GetSubtree(0);
|
---|
101 | if (startNode.GetSubtree(0).Symbol is Addition) {
|
---|
102 | var addNode = startNode.GetSubtree(0);
|
---|
103 | if (addNode.SubtreesCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
|
---|
104 | alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
|
---|
105 | var mulNode = addNode.GetSubtree(0);
|
---|
106 | if (mulNode.SubtreesCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
|
---|
107 | betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
|
---|
112 | if (alphaTreeNode != null && betaTreeNode != null) {
|
---|
113 | betaTreeNode.Value *= beta;
|
---|
114 | alphaTreeNode.Value *= beta;
|
---|
115 | alphaTreeNode.Value += alpha;
|
---|
116 | } else {
|
---|
117 | var mainBranch = startNode.GetSubtree(0);
|
---|
118 | startNode.RemoveSubtree(0);
|
---|
119 | var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
|
---|
120 | startNode.AddSubtree(scaledMainBranch);
|
---|
121 | }
|
---|
122 |
|
---|
123 | OnModelChanged(EventArgs.Empty);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
|
---|
127 | if (alpha.IsAlmost(0.0)) {
|
---|
128 | return treeNode;
|
---|
129 | } else {
|
---|
130 | var node = (new Addition()).CreateTreeNode();
|
---|
131 | var alphaConst = MakeConstant(alpha);
|
---|
132 | node.AddSubtree(treeNode);
|
---|
133 | node.AddSubtree(alphaConst);
|
---|
134 | return node;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
|
---|
139 | if (beta.IsAlmost(1.0)) {
|
---|
140 | return treeNode;
|
---|
141 | } else {
|
---|
142 | var node = (new Multiplication()).CreateTreeNode();
|
---|
143 | var betaConst = MakeConstant(beta);
|
---|
144 | node.AddSubtree(treeNode);
|
---|
145 | node.AddSubtree(betaConst);
|
---|
146 | return node;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | private static ISymbolicExpressionTreeNode MakeConstant(double c) {
|
---|
151 | var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
|
---|
152 | node.Value = c;
|
---|
153 | return node;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|