Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicClassificationSolutionSimplifierViewBase.cs @ 8636

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

#1924:

  • Changed the accuracy threshold calculator to eliminate the necessity the the class values are ordered.
  • Adapted the symbolic classification simplifier to work with all ISymbolicClassificationModels.
  • Corrected ROCCurvesView to also work if the class values are not sorted.
File size: 6.1 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 abstract partial class InteractiveSymbolicClassificationSolutionSimplifierViewBase : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
31    private readonly ConstantTreeNode constantNode;
32    private readonly SymbolicExpressionTree tempTree;
33
34    public new ISymbolicClassificationSolution Content {
35      get { return (ISymbolicClassificationSolution)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public InteractiveSymbolicClassificationSolutionSimplifierViewBase()
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 ISymbolicClassificationModel CreateModel(ISymbolicExpressionTree tree) {
52      var cloner = new Cloner();
53      cloner.RegisterClonedObject(Content.Model.SymbolicExpressionTree, tree);
54
55      var model = (ISymbolicClassificationModel)Content.Model.Clone(cloner);
56      model.RecalculateModelParameters(Content.ProblemData, Content.ProblemData.TrainingIndices);
57      return model;
58    }
59
60    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
61      Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
62      foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
63        replacementValues[node] = CalculateReplacementValue(node, tree);
64      }
65      return replacementValues;
66    }
67
68    protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
69      var model = Content.Model;
70      var dataset = Content.ProblemData.Dataset;
71      var rows = Content.ProblemData.TrainingIndices;
72      string targetVariable = Content.ProblemData.TargetVariable;
73      Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
74      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
75
76      var targetClassValues = dataset.GetDoubleValues(targetVariable, rows);
77      var originalClassValues = model.GetEstimatedClassValues(dataset, rows);
78      OnlineCalculatorError errorState;
79      double originalAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
80      if (errorState != OnlineCalculatorError.None) originalAccuracy = 0.0;
81
82      foreach (ISymbolicExpressionTreeNode node in nodes) {
83        var parent = node.Parent;
84        constantNode.Value = CalculateReplacementValue(node, tree);
85        ISymbolicExpressionTreeNode replacementNode = constantNode;
86        SwitchNode(parent, node, replacementNode);
87        var newModel = CreateModel(tree);
88        var newClassValues = newModel.GetEstimatedClassValues(dataset, rows);
89        double newAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, newClassValues, out errorState);
90        if (errorState != OnlineCalculatorError.None) newAccuracy = 0.0;
91
92        // impact = 0 if no change
93        // impact < 0 if new solution is better
94        // impact > 0 if new solution is worse
95        impactValues[node] = originalAccuracy - newAccuracy;
96        SwitchNode(parent, replacementNode, node);
97      }
98      return impactValues;
99    }
100
101    private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
102      // remove old ADFs
103      while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
104      // clone ADFs of source tree
105      for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
106        tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
107      }
108      var start = tempTree.Root.GetSubtree(0);
109      while (start.SubtreeCount > 0) start.RemoveSubtree(0);
110      start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
111      var interpreter = Content.Model.Interpreter;
112      var rows = Content.ProblemData.TrainingIndices;
113      return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows).Median();
114    }
115
116
117    private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
118      for (int i = 0; i < root.SubtreeCount; i++) {
119        if (root.GetSubtree(i) == oldBranch) {
120          root.RemoveSubtree(i);
121          root.InsertSubtree(i, newBranch);
122          return;
123        }
124      }
125    }
126
127    protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
128
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.