Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs @ 5733

Last change on this file since 5733 was 5733, checked in by mkommend, 13 years ago

#1418: Corrected problem interfaces & unified naming of subtrees.

File size: 5.3 KB
Line 
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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Optimization;
32using System;
33
34namespace 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    public new ISymbolicRegressionModel Model {
42      get { return (ISymbolicRegressionModel)base.Model; }
43      set { base.Model = value; }
44    }
45    ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
46      get { return (ISymbolicDataAnalysisModel)base.Model; }
47    }
48
49    [StorableConstructor]
50    private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
51    private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
52      : base(original, cloner) {
53    }
54    public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
55      : base(model, problemData) {
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new SymbolicRegressionSolution(this, cloner);
60    }
61
62    public void ScaleModel() {
63      var dataset = ProblemData.Dataset;
64      var targetVariable = ProblemData.TargetVariable;
65      var rows = ProblemData.TrainingIndizes;
66      var estimatedValues = GetEstimatedValues(rows);
67      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
68      double alpha;
69      double beta;
70      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
71
72      ConstantTreeNode alphaTreeNode = null;
73      ConstantTreeNode betaTreeNode = null;
74      // check if model has been scaled previously by analyzing the structure of the tree
75      var startNode = Model.SymbolicExpressionTree.Root.GetSubtree(0);
76      if (startNode.GetSubtree(0).Symbol is Addition) {
77        var addNode = startNode.GetSubtree(0);
78        if (addNode.SubtreesCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
79          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
80          var mulNode = addNode.GetSubtree(0);
81          if (mulNode.SubtreesCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
82            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
83          }
84        }
85      }
86      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
87      if (alphaTreeNode != null && betaTreeNode != null) {
88        betaTreeNode.Value *= beta;
89        alphaTreeNode.Value *= beta;
90        alphaTreeNode.Value += alpha;
91      } else {
92        var mainBranch = startNode.GetSubtree(0);
93        startNode.RemoveSubtree(0);
94        var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
95        startNode.AddSubtree(scaledMainBranch);
96      }
97
98      OnModelChanged(EventArgs.Empty);
99    }
100
101    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
102      if (alpha.IsAlmost(0.0)) {
103        return treeNode;
104      } else {
105        var node = (new Addition()).CreateTreeNode();
106        var alphaConst = MakeConstant(alpha);
107        node.AddSubtree(treeNode);
108        node.AddSubtree(alphaConst);
109        return node;
110      }
111    }
112
113    private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
114      if (beta.IsAlmost(1.0)) {
115        return treeNode;
116      } else {
117        var node = (new Multiplication()).CreateTreeNode();
118        var betaConst = MakeConstant(beta);
119        node.AddSubtree(treeNode);
120        node.AddSubtree(betaConst);
121        return node;
122      }
123    }
124
125    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
126      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
127      node.Value = c;
128      return node;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.