[5717] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
|
---|
| 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 31 |
|
---|
| 32 | namespace 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();
|
---|
[5736] | 50 | root.AddSubtree(start);
|
---|
[5717] | 51 | tempTree = new SymbolicExpressionTree(root);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | protected override void UpdateModel(ISymbolicExpressionTree tree) {
|
---|
[5736] | 55 | Content.Model = new SymbolicDiscriminantFunctionClassificationModel(tree, Content.Model.Interpreter);
|
---|
| 56 | Content.SetClassDistibutionCutPointThresholds();
|
---|
[5717] | 57 | }
|
---|
| 58 |
|
---|
| 59 | protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
|
---|
| 60 | Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
[5993] | 61 | foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
|
---|
| 62 | replacementValues[node] = CalculateReplacementValue(node, tree);
|
---|
[5717] | 63 | }
|
---|
| 64 | return replacementValues;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
|
---|
| 68 | var interpreter = Content.Model.Interpreter;
|
---|
| 69 | var dataset = Content.ProblemData.Dataset;
|
---|
| 70 | var rows = Content.ProblemData.TrainingIndizes;
|
---|
| 71 | string targetVariable = Content.ProblemData.TargetVariable;
|
---|
| 72 | Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
[5736] | 73 | List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
|
---|
[5717] | 74 |
|
---|
| 75 | var targetClassValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
|
---|
| 76 | var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
|
---|
[5736] | 77 | .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
|
---|
[5717] | 78 | .ToArray();
|
---|
| 79 | double[] classValues;
|
---|
| 80 | double[] thresholds;
|
---|
| 81 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, originalOutput, targetClassValues, out classValues, out thresholds);
|
---|
[5736] | 82 | var classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
|
---|
| 83 | classifier.SetThresholdsAndClassValues(thresholds, classValues);
|
---|
[5942] | 84 | OnlineCalculatorError errorState;
|
---|
| 85 | double originalAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows), out errorState);
|
---|
| 86 | if (errorState != OnlineCalculatorError.None) originalAccuracy = 0.0;
|
---|
[5717] | 87 |
|
---|
| 88 | foreach (ISymbolicExpressionTreeNode node in nodes) {
|
---|
| 89 | var parent = node.Parent;
|
---|
[5993] | 90 | constantNode.Value = CalculateReplacementValue(node, tree);
|
---|
[5717] | 91 | ISymbolicExpressionTreeNode replacementNode = constantNode;
|
---|
| 92 | SwitchNode(parent, node, replacementNode);
|
---|
[5736] | 93 | var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
|
---|
| 94 | .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
|
---|
| 95 | .ToArray();
|
---|
[5717] | 96 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(Content.ProblemData, newOutput, targetClassValues, out classValues, out thresholds);
|
---|
[5736] | 97 | classifier = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
|
---|
| 98 | classifier.SetThresholdsAndClassValues(thresholds, classValues);
|
---|
[5942] | 99 | double newAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, classifier.GetEstimatedClassValues(dataset, rows), out errorState);
|
---|
| 100 | if (errorState != OnlineCalculatorError.None) newAccuracy = 0.0;
|
---|
[5717] | 101 |
|
---|
| 102 | // impact = 0 if no change
|
---|
| 103 | // impact < 0 if new solution is better
|
---|
| 104 | // impact > 0 if new solution is worse
|
---|
| 105 | impactValues[node] = originalAccuracy - newAccuracy;
|
---|
| 106 | SwitchNode(parent, replacementNode, node);
|
---|
| 107 | }
|
---|
| 108 | return impactValues;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[5993] | 111 | private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
|
---|
| 112 | // remove old ADFs
|
---|
| 113 | while (tempTree.Root.SubtreesCount > 1) tempTree.Root.RemoveSubtree(1);
|
---|
| 114 | // clone ADFs of source tree
|
---|
| 115 | for (int i = 1; i < sourceTree.Root.SubtreesCount; i++) {
|
---|
| 116 | tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
|
---|
| 117 | }
|
---|
[5736] | 118 | var start = tempTree.Root.GetSubtree(0);
|
---|
| 119 | while (start.SubtreesCount > 0) start.RemoveSubtree(0);
|
---|
| 120 | start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
|
---|
[5717] | 121 | var interpreter = Content.Model.Interpreter;
|
---|
| 122 | var rows = Content.ProblemData.TrainingIndizes;
|
---|
| 123 | return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows).Median();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
|
---|
[5736] | 128 | for (int i = 0; i < root.SubtreesCount; i++) {
|
---|
| 129 | if (root.GetSubtree(i) == oldBranch) {
|
---|
| 130 | root.RemoveSubtree(i);
|
---|
| 131 | root.InsertSubtree(i, newBranch);
|
---|
[5717] | 132 | return;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|