Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5942 was 5942, checked in by mkommend, 14 years ago

#1453: Renamed IOnlineEvaluator to IOnlineCalculator

File size: 6.7 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      Content.Model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter);
56      Content.SetClassDistibutionCutPointThresholds();
57    }
58
59    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
60      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
61      foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {
62        if (!(node.Symbol is ProgramRootSymbol || node.Symbol is StartSymbol)) {
63          replacementValues[node] = CalculateReplacementValue(node);
64        }
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.TrainingIndizes;
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.GetEnumeratedVariableValues(targetVariable, rows);
78      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
79        .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
80        .ToArray();
81      double[] classValues;
82      double[] thresholds;
83      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, originalOutput, targetClassValues, out classValues, out thresholds);
84      var classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
85      classifier.SetThresholdsAndClassValues(thresholds, classValues);
86      OnlineCalculatorError errorState;
87      double originalAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows), out errorState);
88      if (errorState != OnlineCalculatorError.None) originalAccuracy = 0.0;
89
90      foreach (ISymbolicExpressionTreeNode node in nodes) {
91        var parent = node.Parent;
92        constantNode.Value = CalculateReplacementValue(node);
93        ISymbolicExpressionTreeNode replacementNode = constantNode;
94        SwitchNode(parent, node, replacementNode);
95        var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
96          .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
97          .ToArray();
98        NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, newOutput, targetClassValues, out classValues, out thresholds);
99        classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
100        classifier.SetThresholdsAndClassValues(thresholds, classValues);
101        double newAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows), out errorState);
102        if (errorState != OnlineCalculatorError.None) newAccuracy = 0.0;
103
104        // impact = 0 if no change
105        // impact < 0 if new solution is better
106        // impact > 0 if new solution is worse
107        impactValues[node] = originalAccuracy - newAccuracy;
108        SwitchNode(parent, replacementNode, node);
109      }
110      return impactValues;
111    }
112
113    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node) {
114      var start = tempTree.Root.GetSubtree(0);
115      while (start.SubtreesCount > 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.SubtreesCount; i++) {
125        if (root.GetSubtree(i) == oldBranch) {
126          root.RemoveSubtree(i);
127          root.InsertSubtree(i, newBranch);
128          return;
129        }
130      }
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.