source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView.cs @ 5717

Last change on this file since 5717 was 5717, checked in by gkronber, 11 years ago

#1418 Implemented interactive simplifier views for symbolic classification and regression.

File size: 6.8 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;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views {
33  public partial class InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
34    private readonly ConstantTreeNode constantNode;
35    private readonly SymbolicExpressionTree tempTree;
36
37    public new SymbolicDiscriminantFunctionClassificationSolution Content {
38      get { return (SymbolicDiscriminantFunctionClassificationSolution)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView()
43      : base() {
44      InitializeComponent();
45      this.Caption = "Interactive Classification Solution Simplifier";
46
47      constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
48      ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
49      ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
50      root.AddSubTree(start);
51      tempTree = new SymbolicExpressionTree(root);
52    }
53
54    protected override void UpdateModel(ISymbolicExpressionTree tree) {
55      var dataset = Content.ProblemData.Dataset;
56      var interpreter = Content.Model.Interpreter;
57      var rows = Content.ProblemData.TrainingIndizes;
58      string targetVariable = Content.ProblemData.TargetVariable;
59
60      var targetClassValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
61      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
62        .ToArray();
63      double[] classValues;
64      double[] thresholds;
65      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, originalOutput, targetClassValues, out classValues, out thresholds);
66      Content.Model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter, classValues, thresholds);
67    }
68
69    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
70      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
71      foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {
72        if (!(node.Symbol is ProgramRootSymbol || node.Symbol is StartSymbol)) {
73          replacementValues[node] = CalculateReplacementValue(node);
74        }
75      }
76      return replacementValues;
77    }
78
79    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
80      var interpreter = Content.Model.Interpreter;
81      var dataset = Content.ProblemData.Dataset;
82      var rows = Content.ProblemData.TrainingIndizes;
83      string targetVariable = Content.ProblemData.TargetVariable;
84      Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
85      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubTree(0).GetSubTree(0).IterateNodesPostfix().ToList();
86
87      var targetClassValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
88      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
89        .ToArray();
90      double[] classValues;
91      double[] thresholds;
92      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, originalOutput, targetClassValues, out classValues, out thresholds);
93      var classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, classValues, thresholds);
94      double originalAccuracy = OnlineAccuracyEvaluator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows));
95
96      foreach (ISymbolicExpressionTreeNode node in nodes) {
97        var parent = node.Parent;
98        constantNode.Value = CalculateReplacementValue(node);
99        ISymbolicExpressionTreeNode replacementNode = constantNode;
100        SwitchNode(parent, node, replacementNode);
101        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
102        NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, newOutput, targetClassValues, out classValues, out thresholds);
103        classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, classValues, thresholds);
104        double newAccuracy = OnlineAccuracyEvaluator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows));
105
106        // impact = 0 if no change
107        // impact < 0 if new solution is better
108        // impact > 0 if new solution is worse
109        impactValues[node] = originalAccuracy - newAccuracy;
110        SwitchNode(parent, replacementNode, node);
111      }
112      return impactValues;
113    }
114
115    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node) {
116      var start = tempTree.Root.GetSubTree(0);
117      while (start.SubTrees.Count() > 0) start.RemoveSubTree(0);
118      start.AddSubTree((ISymbolicExpressionTreeNode)node.Clone());
119      var interpreter = Content.Model.Interpreter;
120      var rows = Content.ProblemData.TrainingIndizes;
121      return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows).Median();
122    }
123
124
125    private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
126      for (int i = 0; i < root.SubTrees.Count(); i++) {
127        if (root.GetSubTree(i) == oldBranch) {
128          root.RemoveSubTree(i);
129          root.InsertSubTree(i, newBranch);
130          return;
131        }
132      }
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.