Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationSolutionImpactValuesCalculator.cs @ 8935

Last change on this file since 8935 was 8935, checked in by bburlacu, 11 years ago

#1763: Bugfixes and refactoring as suggested in the comments above.

File size: 4.9 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
29  public class SymbolicDiscriminantFunctionClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
30    public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateReplacementValues(ISymbolicExpressionTree tree,
31                                                                                                       ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
32                                                                                                       IDataAnalysisProblemData problemData) {
33      return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()
34             select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateReplacementValue(node, tree, interpreter, problemData));
35    }
36
37    public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateImpactValues(ISymbolicExpressionTree tree,
38                                                                                                  ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
39                                                                                                  IDataAnalysisProblemData classificationProblemData,
40                                                                                                  double lowerEstimationLimit, double upperEstimationLimit) {
41      var problemData = (IClassificationProblemData)classificationProblemData;
42      var dataset = problemData.Dataset;
43      var rows = problemData.TrainingIndices;
44      string targetVariable = problemData.TargetVariable;
45      var targetClassValues = dataset.GetDoubleValues(targetVariable, rows);
46      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit).ToArray();
47      OnlineCalculatorError errorState;
48      double originalGini = NormalizedGiniCalculator.Calculate(targetClassValues, originalOutput, out errorState);
49      if (errorState != OnlineCalculatorError.None) originalGini = 0.0;
50
51      return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix()
52             select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateImpact(tree, originalGini, node, interpreter, problemData, lowerEstimationLimit, upperEstimationLimit));
53    }
54
55    private static double CalculateImpact(ISymbolicExpressionTree tree, double originalQuality, ISymbolicExpressionTreeNode node,
56                                          ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IClassificationProblemData problemData,
57                                          double lowerEstimationLimit, double upperEstimationLimit) {
58      var dataset = problemData.Dataset;
59      var rows = problemData.TrainingIndices.ToList();
60      string targetVariable = problemData.TargetVariable;
61      var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
62
63      var parent = node.Parent;
64      var constantNode = (ConstantTreeNode)new Constant().CreateTreeNode();
65      constantNode.Value = CalculateReplacementValue(node, tree, interpreter, problemData);
66      SwitchNode(parent, node, constantNode);
67      var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
68                                 .LimitToRange(lowerEstimationLimit, upperEstimationLimit)
69                                 .ToArray();
70      OnlineCalculatorError errorState;
71      double quality = NormalizedGiniCalculator.Calculate(targetValues, newOutput, out errorState);
72      if (errorState != OnlineCalculatorError.None) quality = 0.0;
73      SwitchNode(parent, constantNode, node);
74      // impact = 0 if no change
75      // impact < 0 if new solution is better
76      // impact > 0 if new solution is worse
77      return originalQuality - quality;
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.