Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView.cs @ 8550

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

#1919: Changed threshold calculation methods to instance instead of static methods.

File size: 6.7 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;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views {
30  public partial class InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
31    private readonly ConstantTreeNode constantNode;
32    private readonly SymbolicExpressionTree tempTree;
33
34    public new SymbolicDiscriminantFunctionClassificationSolution Content {
35      get { return (SymbolicDiscriminantFunctionClassificationSolution)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView()
40      : base() {
41      InitializeComponent();
42      this.Caption = "Interactive Classification Solution Simplifier";
43
44      constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
45      ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
46      ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
47      root.AddSubtree(start);
48      tempTree = new SymbolicExpressionTree(root);
49    }
50
51    protected override void UpdateModel(ISymbolicExpressionTree tree) {
52      var model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter);
53      // the default policy for setting thresholds in classification models is the accuarcy maximizing policy.
54      // This is rather slow to calculate and can lead to a very laggy UI in the interactive solution simplifier.
55      // However, since we automatically prune sub-trees based on the threshold reaching the maximum accuracy we must
56      // also use maximum accuracy threshold calculation here in order to prevent incoherent behavior of the simplifier.
57      model.SetAccuracyMaximizingThresholds(Content.ProblemData);
58      Content.Model = model;
59    }
60
61    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
62      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
63      foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
64        replacementValues[node] = CalculateReplacementValue(node, tree);
65      }
66      return replacementValues;
67    }
68
69    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
70      var interpreter = Content.Model.Interpreter;
71      var dataset = Content.ProblemData.Dataset;
72      var rows = Content.ProblemData.TrainingIndices;
73      string targetVariable = Content.ProblemData.TargetVariable;
74      Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
75      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
76
77      var targetClassValues = dataset.GetDoubleValues(targetVariable, rows);
78      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
79        .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
80        .ToArray();
81      OnlineCalculatorError errorState;
82      double originalGini = NormalizedGiniCalculator.Calculate(targetClassValues, originalOutput, out errorState);
83      if (errorState != OnlineCalculatorError.None) originalGini = 0.0;
84
85      foreach (ISymbolicExpressionTreeNode node in nodes) {
86        var parent = node.Parent;
87        constantNode.Value = CalculateReplacementValue(node, tree);
88        ISymbolicExpressionTreeNode replacementNode = constantNode;
89        SwitchNode(parent, node, replacementNode);
90        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
91          .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
92          .ToArray();
93        double newGini = NormalizedGiniCalculator.Calculate(targetClassValues, newOutput, out errorState);
94        if (errorState != OnlineCalculatorError.None) newGini = 0.0;
95
96        // impact = 0 if no change
97        // impact < 0 if new solution is better
98        // impact > 0 if new solution is worse
99        impactValues[node] = originalGini - newGini;
100        SwitchNode(parent, replacementNode, node);
101      }
102      return impactValues;
103    }
104
105    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
106      // remove old ADFs
107      while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
108      // clone ADFs of source tree
109      for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
110        tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
111      }
112      var start = tempTree.Root.GetSubtree(0);
113      while (start.SubtreeCount > 0) start.RemoveSubtree(0);
114      start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
115      var interpreter = Content.Model.Interpreter;
116      var rows = Content.ProblemData.TrainingIndices;
117      return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows).Median();
118    }
119
120
121    private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
122      for (int i = 0; i < root.SubtreeCount; i++) {
123        if (root.GetSubtree(i) == oldBranch) {
124          root.RemoveSubtree(i);
125          root.InsertSubtree(i, newBranch);
126          return;
127        }
128      }
129    }
130
131    protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
132
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.