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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
|
---|
28 |
|
---|
29 | namespace 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 | /// <summary>
|
---|
52 | /// It is necessary to create new models of an unknown type with new trees in the simplifier.
|
---|
53 | /// For this purpose the cloner is used by registering the new tree as already cloned object and invoking the clone mechanism.
|
---|
54 | /// This results in a new model of the same type as the old one with an exchanged tree.
|
---|
55 | /// </summary>
|
---|
56 | /// <param name="tree">The new tree that should be included in the new object</param>
|
---|
57 | /// <returns></returns>
|
---|
58 | protected ISymbolicClassificationModel CreateModel(ISymbolicExpressionTree tree) {
|
---|
59 | var cloner = new Cloner();
|
---|
60 | cloner.RegisterClonedObject(Content.Model.SymbolicExpressionTree, tree);
|
---|
61 |
|
---|
62 | var model = (ISymbolicClassificationModel)Content.Model.Clone(cloner);
|
---|
63 | model.RecalculateModelParameters(Content.ProblemData, Content.ProblemData.TrainingIndices);
|
---|
64 | return model;
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
|
---|
68 | Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
69 | foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
|
---|
70 | replacementValues[node] = CalculateReplacementValue(node, tree);
|
---|
71 | }
|
---|
72 | return replacementValues;
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
|
---|
76 | var model = Content.Model;
|
---|
77 | var dataset = Content.ProblemData.Dataset;
|
---|
78 | var rows = Content.ProblemData.TrainingIndices;
|
---|
79 | string targetVariable = Content.ProblemData.TargetVariable;
|
---|
80 | Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
81 | List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
|
---|
82 |
|
---|
83 | var targetClassValues = dataset.GetDoubleValues(targetVariable, rows);
|
---|
84 | var originalClassValues = model.GetEstimatedClassValues(dataset, rows);
|
---|
85 | OnlineCalculatorError errorState;
|
---|
86 | double originalAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, originalClassValues, out errorState);
|
---|
87 | if (errorState != OnlineCalculatorError.None) originalAccuracy = 0.0;
|
---|
88 |
|
---|
89 | foreach (ISymbolicExpressionTreeNode node in nodes) {
|
---|
90 | var parent = node.Parent;
|
---|
91 | constantNode.Value = CalculateReplacementValue(node, tree);
|
---|
92 | ISymbolicExpressionTreeNode replacementNode = constantNode;
|
---|
93 | SwitchNode(parent, node, replacementNode);
|
---|
94 | var newModel = CreateModel(tree);
|
---|
95 | var newClassValues = newModel.GetEstimatedClassValues(dataset, rows);
|
---|
96 | double newAccuracy = OnlineAccuracyCalculator.Calculate(targetClassValues, newClassValues, out errorState);
|
---|
97 | if (errorState != OnlineCalculatorError.None) newAccuracy = 0.0;
|
---|
98 |
|
---|
99 | // impact = 0 if no change
|
---|
100 | // impact < 0 if new solution is better
|
---|
101 | // impact > 0 if new solution is worse
|
---|
102 | impactValues[node] = originalAccuracy - newAccuracy;
|
---|
103 | SwitchNode(parent, replacementNode, node);
|
---|
104 | }
|
---|
105 | return impactValues;
|
---|
106 | }
|
---|
107 |
|
---|
108 | private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
|
---|
109 | // remove old ADFs
|
---|
110 | while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
|
---|
111 | // clone ADFs of source tree
|
---|
112 | for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
|
---|
113 | tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
|
---|
114 | }
|
---|
115 | var start = tempTree.Root.GetSubtree(0);
|
---|
116 | while (start.SubtreeCount > 0) start.RemoveSubtree(0);
|
---|
117 | start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
|
---|
118 | var interpreter = Content.Model.Interpreter;
|
---|
119 | var rows = Content.ProblemData.TrainingIndices;
|
---|
120 | return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows)
|
---|
121 | .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit).Median();
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
|
---|
126 | for (int i = 0; i < root.SubtreeCount; i++) {
|
---|
127 | if (root.GetSubtree(i) == oldBranch) {
|
---|
128 | root.RemoveSubtree(i);
|
---|
129 | root.InsertSubtree(i, newBranch);
|
---|
130 | return;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
|
---|
136 |
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|