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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
31 | /// <summary>
|
---|
32 | /// Represents a symbolic classification model
|
---|
33 | /// </summary>
|
---|
34 | [StorableClass]
|
---|
35 | [Item(Name = "SymbolicDiscriminantFunctionClassificationModel", Description = "Represents a symbolic classification model unsing a discriminant function.")]
|
---|
36 | public class SymbolicDiscriminantFunctionClassificationModel : SymbolicDataAnalysisModel, ISymbolicDiscriminantFunctionClassificationModel {
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | private double[] thresholds;
|
---|
40 | public IEnumerable<double> Thresholds {
|
---|
41 | get { return (IEnumerable<double>)thresholds.Clone(); }
|
---|
42 | private set { thresholds = value.ToArray(); }
|
---|
43 | }
|
---|
44 | [Storable]
|
---|
45 | private double[] classValues;
|
---|
46 | public IEnumerable<double> ClassValues {
|
---|
47 | get { return (IEnumerable<double>)classValues.Clone(); }
|
---|
48 | private set { classValues = value.ToArray(); }
|
---|
49 | }
|
---|
50 | [Storable]
|
---|
51 | private double lowerEstimationLimit;
|
---|
52 | public double LowerEstimationLimit { get { return lowerEstimationLimit; } }
|
---|
53 | [Storable]
|
---|
54 | private double upperEstimationLimit;
|
---|
55 | public double UpperEstimationLimit { get { return upperEstimationLimit; } }
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | protected SymbolicDiscriminantFunctionClassificationModel(bool deserializing) : base(deserializing) { }
|
---|
59 | protected SymbolicDiscriminantFunctionClassificationModel(SymbolicDiscriminantFunctionClassificationModel original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
61 | classValues = (double[])original.classValues.Clone();
|
---|
62 | thresholds = (double[])original.thresholds.Clone();
|
---|
63 | lowerEstimationLimit = original.lowerEstimationLimit;
|
---|
64 | upperEstimationLimit = original.upperEstimationLimit;
|
---|
65 | }
|
---|
66 | public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
67 | double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
|
---|
68 | : base(tree, interpreter) {
|
---|
69 | thresholds = new double[] { double.NegativeInfinity };
|
---|
70 | classValues = new double[] { 0.0 };
|
---|
71 | this.lowerEstimationLimit = lowerEstimationLimit;
|
---|
72 | this.upperEstimationLimit = upperEstimationLimit;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
76 | return new SymbolicDiscriminantFunctionClassificationModel(this, cloner);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
|
---|
80 | var classValuesArr = classValues.ToArray();
|
---|
81 | var thresholdsArr = thresholds.ToArray();
|
---|
82 | if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException();
|
---|
83 |
|
---|
84 | this.classValues = classValuesArr;
|
---|
85 | this.thresholds = thresholdsArr;
|
---|
86 | OnThresholdsChanged(EventArgs.Empty);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
90 | return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows)
|
---|
91 | .LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
95 | foreach (var x in GetEstimatedValues(dataset, rows)) {
|
---|
96 | int classIndex = 0;
|
---|
97 | // find first threshold value which is larger than x => class index = threshold index + 1
|
---|
98 | for (int i = 0; i < thresholds.Length; i++) {
|
---|
99 | if (x > thresholds[i]) classIndex++;
|
---|
100 | else break;
|
---|
101 | }
|
---|
102 | yield return classValues.ElementAt(classIndex - 1);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public SymbolicDiscriminantFunctionClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
107 | return new SymbolicDiscriminantFunctionClassificationSolution(this, problemData);
|
---|
108 | }
|
---|
109 | IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
110 | return CreateClassificationSolution(problemData);
|
---|
111 | }
|
---|
112 | IDiscriminantFunctionClassificationSolution IDiscriminantFunctionClassificationModel.CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
|
---|
113 | return CreateClassificationSolution(problemData);
|
---|
114 | }
|
---|
115 |
|
---|
116 | #region events
|
---|
117 | public event EventHandler ThresholdsChanged;
|
---|
118 | protected virtual void OnThresholdsChanged(EventArgs e) {
|
---|
119 | var listener = ThresholdsChanged;
|
---|
120 | if (listener != null) listener(this, e);
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | public static void Scale(SymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
|
---|
125 | var dataset = problemData.Dataset;
|
---|
126 | var targetVariable = problemData.TargetVariable;
|
---|
127 | var rows = problemData.TrainingIndizes;
|
---|
128 | var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
|
---|
129 | var targetValues = dataset.GetDoubleValues(targetVariable, rows);
|
---|
130 | double alpha;
|
---|
131 | double beta;
|
---|
132 | OnlineCalculatorError errorState;
|
---|
133 | OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
|
---|
134 | if (errorState != OnlineCalculatorError.None) return;
|
---|
135 |
|
---|
136 | ConstantTreeNode alphaTreeNode = null;
|
---|
137 | ConstantTreeNode betaTreeNode = null;
|
---|
138 | // check if model has been scaled previously by analyzing the structure of the tree
|
---|
139 | var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
|
---|
140 | if (startNode.GetSubtree(0).Symbol is Addition) {
|
---|
141 | var addNode = startNode.GetSubtree(0);
|
---|
142 | if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
|
---|
143 | alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
|
---|
144 | var mulNode = addNode.GetSubtree(0);
|
---|
145 | if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
|
---|
146 | betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
|
---|
151 | if (alphaTreeNode != null && betaTreeNode != null) {
|
---|
152 | betaTreeNode.Value *= beta;
|
---|
153 | alphaTreeNode.Value *= beta;
|
---|
154 | alphaTreeNode.Value += alpha;
|
---|
155 | } else {
|
---|
156 | var mainBranch = startNode.GetSubtree(0);
|
---|
157 | startNode.RemoveSubtree(0);
|
---|
158 | var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
|
---|
159 | startNode.AddSubtree(scaledMainBranch);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
|
---|
164 | if (alpha.IsAlmost(0.0)) {
|
---|
165 | return treeNode;
|
---|
166 | } else {
|
---|
167 | var addition = new Addition();
|
---|
168 | var node = addition.CreateTreeNode();
|
---|
169 | var alphaConst = MakeConstant(alpha);
|
---|
170 | node.AddSubtree(treeNode);
|
---|
171 | node.AddSubtree(alphaConst);
|
---|
172 | return node;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
|
---|
177 | if (beta.IsAlmost(1.0)) {
|
---|
178 | return treeNode;
|
---|
179 | } else {
|
---|
180 | var multipliciation = new Multiplication();
|
---|
181 | var node = multipliciation.CreateTreeNode();
|
---|
182 | var betaConst = MakeConstant(beta);
|
---|
183 | node.AddSubtree(treeNode);
|
---|
184 | node.AddSubtree(betaConst);
|
---|
185 | return node;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | private static ISymbolicExpressionTreeNode MakeConstant(double c) {
|
---|
190 | var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
|
---|
191 | node.Value = c;
|
---|
192 | return node;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|