Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationModel.cs @ 8597

Last change on this file since 8597 was 8594, checked in by mkommend, 12 years ago

#1940: Added support in symbolic classification for different methods to create the classification ModelCreator.

  • Added ModelCreators
  • Refactored SymbolicClassificationModel and SymbolicDiscriminantFunctionClassificationModel
  • Added ModelCreatorParameter to Analyzers and Evaluators if needed
  • Corrected wiring in symbolic classification problems (single- and multiobjective
  • Adapted simplifier
File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
29  /// <summary>
30  /// Represents a symbolic classification model
31  /// </summary>
32  [StorableClass]
33  [Item(Name = "SymbolicClassificationModel", Description = "Represents a symbolic classification model.")]
34  public abstract class SymbolicClassificationModel : SymbolicDataAnalysisModel, ISymbolicClassificationModel {
35    [Storable]
36    private double lowerEstimationLimit;
37    public double LowerEstimationLimit { get { return lowerEstimationLimit; } }
38    [Storable]
39    private double upperEstimationLimit;
40    public double UpperEstimationLimit { get { return upperEstimationLimit; } }
41
42    [StorableConstructor]
43    protected SymbolicClassificationModel(bool deserializing) : base(deserializing) { }
44    protected SymbolicClassificationModel(SymbolicClassificationModel original, Cloner cloner)
45      : base(original, cloner) {
46      lowerEstimationLimit = original.lowerEstimationLimit;
47      upperEstimationLimit = original.upperEstimationLimit;
48    }
49    protected SymbolicClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
50      : base(tree, interpreter) {
51      this.lowerEstimationLimit = lowerEstimationLimit;
52      this.upperEstimationLimit = upperEstimationLimit;
53    }
54
55    public abstract IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows);
56    public abstract void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows);
57
58    public abstract ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData);
59
60    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
61      return CreateClassificationSolution(problemData);
62    }
63
64    #region scaling
65    public static void Scale(ISymbolicClassificationModel model, IClassificationProblemData problemData) {
66      var dataset = problemData.Dataset;
67      var targetVariable = problemData.TargetVariable;
68      var rows = problemData.TrainingIndices;
69      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
70      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
71      double alpha;
72      double beta;
73      OnlineCalculatorError errorState;
74      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
75      if (errorState != OnlineCalculatorError.None) return;
76
77      ConstantTreeNode alphaTreeNode = null;
78      ConstantTreeNode betaTreeNode = null;
79      // check if model has been scaled previously by analyzing the structure of the tree
80      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
81      if (startNode.GetSubtree(0).Symbol is Addition) {
82        var addNode = startNode.GetSubtree(0);
83        if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
84          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
85          var mulNode = addNode.GetSubtree(0);
86          if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
87            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
88          }
89        }
90      }
91      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
92      if (alphaTreeNode != null && betaTreeNode != null) {
93        betaTreeNode.Value *= beta;
94        alphaTreeNode.Value *= beta;
95        alphaTreeNode.Value += alpha;
96      } else {
97        var mainBranch = startNode.GetSubtree(0);
98        startNode.RemoveSubtree(0);
99        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
100        startNode.AddSubtree(scaledMainBranch);
101      }
102    }
103
104    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
105      if (alpha.IsAlmost(0.0)) {
106        return treeNode;
107      } else {
108        var addition = new Addition();
109        var node = addition.CreateTreeNode();
110        var alphaConst = MakeConstant(alpha);
111        node.AddSubtree(treeNode);
112        node.AddSubtree(alphaConst);
113        return node;
114      }
115    }
116
117    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
118      if (beta.IsAlmost(1.0)) {
119        return treeNode;
120      } else {
121        var multipliciation = new Multiplication();
122        var node = multipliciation.CreateTreeNode();
123        var betaConst = MakeConstant(beta);
124        node.AddSubtree(treeNode);
125        node.AddSubtree(betaConst);
126        return node;
127      }
128    }
129
130    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
131      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
132      node.Value = c;
133      return node;
134    }
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.