Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicDiscriminantFunctionClassificationSolutionSimplifierView.cs @ 6415

Last change on this file since 6415 was 6415, checked in by mkommend, 13 years ago

#1479: Merged trunk changes, refactored grammar editor and added copy functionality.

File size: 6.9 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.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      Content.Model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter);
53    }
54
55    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
56      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
57      foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
58        replacementValues[node] = CalculateReplacementValue(node, tree);
59      }
60      return replacementValues;
61    }
62
63    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
64      var interpreter = Content.Model.Interpreter;
65      var dataset = Content.ProblemData.Dataset;
66      var rows = Content.ProblemData.TrainingIndizes;
67      string targetVariable = Content.ProblemData.TargetVariable;
68      Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
69      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
70
71      var targetClassValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
72      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
73        .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
74        .ToArray();
75      double[] classValues;
76      double[] thresholds;
77      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, originalOutput, targetClassValues, out classValues, out thresholds);
78      var classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
79      classifier.SetThresholdsAndClassValues(thresholds, classValues);
80      OnlineCalculatorError errorState;
81      double originalAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows), out errorState);
82      if (errorState != OnlineCalculatorError.None) originalAccuracy = 0.0;
83
84      foreach (ISymbolicExpressionTreeNode node in nodes) {
85        var parent = node.Parent;
86        constantNode.Value = CalculateReplacementValue(node, tree);
87        ISymbolicExpressionTreeNode replacementNode = constantNode;
88        SwitchNode(parent, node, replacementNode);
89        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
90          .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
91          .ToArray();
92        NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, newOutput, targetClassValues, out classValues, out thresholds);
93        classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
94        classifier.SetThresholdsAndClassValues(thresholds, classValues);
95        double newAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows), out errorState);
96        if (errorState != OnlineCalculatorError.None) newAccuracy = 0.0;
97
98        // impact = 0 if no change
99        // impact < 0 if new solution is better
100        // impact > 0 if new solution is worse
101        impactValues[node] = originalAccuracy - newAccuracy;
102        SwitchNode(parent, replacementNode, node);
103      }
104      return impactValues;
105    }
106
107    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
108      // remove old ADFs
109      while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
110      // clone ADFs of source tree
111      for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
112        tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
113      }
114      var start = tempTree.Root.GetSubtree(0);
115      while (start.SubtreeCount > 0) start.RemoveSubtree(0);
116      start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
117      var interpreter = Content.Model.Interpreter;
118      var rows = Content.ProblemData.TrainingIndizes;
119      return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows).Median();
120    }
121
122
123    private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
124      for (int i = 0; i < root.SubtreeCount; i++) {
125        if (root.GetSubtree(i) == oldBranch) {
126          root.RemoveSubtree(i);
127          root.InsertSubtree(i, newBranch);
128          return;
129        }
130      }
131    }
132
133    protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
134
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.