[3915] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
[4068] | 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
|
---|
[3915] | 31 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 32 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
| 33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Problems.DataAnalysis.Views.Symbolic {
|
---|
| 37 | public partial class InteractiveSymbolicRegressionSolutionSimplifierView : AsynchronousContentView {
|
---|
| 38 | private SymbolicExpressionTree simplifiedExpressionTree;
|
---|
| 39 | private Dictionary<SymbolicExpressionTreeNode, ConstantTreeNode> replacementNodes;
|
---|
| 40 | private Dictionary<SymbolicExpressionTreeNode, double> nodeImpacts;
|
---|
| 41 |
|
---|
| 42 | public InteractiveSymbolicRegressionSolutionSimplifierView() {
|
---|
| 43 | InitializeComponent();
|
---|
| 44 | this.replacementNodes = new Dictionary<SymbolicExpressionTreeNode, ConstantTreeNode>();
|
---|
| 45 | this.nodeImpacts = new Dictionary<SymbolicExpressionTreeNode, double>();
|
---|
| 46 | this.simplifiedExpressionTree = null;
|
---|
| 47 | this.Caption = "Interactive Solution Simplifier";
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public new SymbolicRegressionSolution Content {
|
---|
| 51 | get { return (SymbolicRegressionSolution)base.Content; }
|
---|
| 52 | set { base.Content = value; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override void RegisterContentEvents() {
|
---|
| 56 | base.RegisterContentEvents();
|
---|
| 57 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
| 58 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 59 | }
|
---|
| 60 | protected override void DeregisterContentEvents() {
|
---|
| 61 | base.DeregisterContentEvents();
|
---|
| 62 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
| 63 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
| 67 | this.CalculateReplacementNodesAndNodeImpacts();
|
---|
| 68 | }
|
---|
| 69 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
| 70 | this.CalculateReplacementNodesAndNodeImpacts();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | protected override void OnContentChanged() {
|
---|
| 74 | base.OnContentChanged();
|
---|
| 75 | this.CalculateReplacementNodesAndNodeImpacts();
|
---|
| 76 | this.viewHost.Content = this.Content;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private void CalculateReplacementNodesAndNodeImpacts() {
|
---|
| 80 | this.replacementNodes.Clear();
|
---|
| 81 | this.nodeImpacts.Clear();
|
---|
| 82 | if (Content != null && Content.Model != null && Content.ProblemData != null) {
|
---|
| 83 | SymbolicSimplifier simplifier = new SymbolicSimplifier();
|
---|
| 84 | simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
|
---|
| 85 | SymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
|
---|
| 86 | SymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
|
---|
| 87 | root.AddSubTree(start);
|
---|
| 88 | start.AddSubTree(simplifiedExpressionTree.Root);
|
---|
[4034] | 89 | int samplesStart = Content.ProblemData.TrainingSamplesStart.Value;
|
---|
| 90 | int samplesEnd = Content.ProblemData.TrainingSamplesEnd.Value;
|
---|
[3915] | 91 | double originalTrainingMeanSquaredError = SymbolicRegressionMeanSquaredErrorEvaluator.Calculate(
|
---|
| 92 | Content.Model.Interpreter, new SymbolicExpressionTree(root), Content.LowerEstimationLimit, Content.UpperEstimationLimit,
|
---|
| 93 | Content.ProblemData.Dataset, Content.ProblemData.TargetVariable.Value,
|
---|
[4034] | 94 | Enumerable.Range(samplesStart, samplesEnd - samplesStart));
|
---|
[3915] | 95 |
|
---|
| 96 | this.CalculateReplacementNodes();
|
---|
| 97 |
|
---|
| 98 | this.CalculateNodeImpacts(new SymbolicExpressionTree(root), start, originalTrainingMeanSquaredError);
|
---|
| 99 | this.treeChart.Tree = simplifiedExpressionTree;
|
---|
| 100 | this.PaintNodeImpacts();
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | private void CalculateReplacementNodes() {
|
---|
| 105 | ISymbolicExpressionTreeInterpreter interpreter = Content.Model.Interpreter;
|
---|
| 106 | IEnumerable<int> trainingSamples = Enumerable.Range(Content.ProblemData.TrainingSamplesStart.Value, Content.ProblemData.TrainingSamplesEnd.Value - Content.ProblemData.TrainingSamplesStart.Value);
|
---|
| 107 | SymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
|
---|
| 108 | SymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
|
---|
| 109 | root.AddSubTree(start);
|
---|
| 110 | SymbolicExpressionTree tree = new SymbolicExpressionTree(root);
|
---|
| 111 | foreach (SymbolicExpressionTreeNode node in this.simplifiedExpressionTree.IterateNodesPrefix()) {
|
---|
[4034] | 112 | while (start.SubTrees.Count > 0) start.RemoveSubTree(0);
|
---|
[3915] | 113 | start.AddSubTree(node);
|
---|
| 114 | double constantTreeNodeValue = interpreter.GetSymbolicExpressionTreeValues(tree, Content.ProblemData.Dataset, trainingSamples).Median();
|
---|
| 115 | ConstantTreeNode constantTreeNode = MakeConstantTreeNode(constantTreeNodeValue);
|
---|
| 116 | replacementNodes[node] = constantTreeNode;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private void CalculateNodeImpacts(SymbolicExpressionTree tree, SymbolicExpressionTreeNode currentTreeNode, double originalTrainingMeanSquaredError) {
|
---|
| 121 | foreach (SymbolicExpressionTreeNode childNode in currentTreeNode.SubTrees.ToList()) {
|
---|
| 122 | SwitchNode(currentTreeNode, childNode, replacementNodes[childNode]);
|
---|
[4034] | 123 | int samplesStart = Content.ProblemData.TrainingSamplesStart.Value;
|
---|
| 124 | int samplesEnd = Content.ProblemData.TrainingSamplesEnd.Value;
|
---|
[3915] | 125 | double newTrainingMeanSquaredError = SymbolicRegressionMeanSquaredErrorEvaluator.Calculate(
|
---|
| 126 | Content.Model.Interpreter, tree,
|
---|
| 127 | Content.LowerEstimationLimit, Content.UpperEstimationLimit,
|
---|
| 128 | Content.ProblemData.Dataset, Content.ProblemData.TargetVariable.Value,
|
---|
[4034] | 129 | Enumerable.Range(samplesStart, samplesEnd - samplesStart));
|
---|
[3915] | 130 | nodeImpacts[childNode] = newTrainingMeanSquaredError / originalTrainingMeanSquaredError;
|
---|
| 131 | SwitchNode(currentTreeNode, replacementNodes[childNode], childNode);
|
---|
| 132 | CalculateNodeImpacts(tree, childNode, originalTrainingMeanSquaredError);
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | private void SwitchNode(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode oldBranch, SymbolicExpressionTreeNode newBranch) {
|
---|
| 137 | for (int i = 0; i < root.SubTrees.Count; i++) {
|
---|
| 138 | if (root.SubTrees[i] == oldBranch) {
|
---|
| 139 | root.RemoveSubTree(i);
|
---|
| 140 | root.InsertSubTree(i, newBranch);
|
---|
| 141 | return;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
| 147 | Constant constant = new Constant();
|
---|
| 148 | constant.MinValue = value - 1;
|
---|
| 149 | constant.MaxValue = value + 1;
|
---|
| 150 | ConstantTreeNode constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
| 151 | constantTreeNode.Value = value;
|
---|
| 152 | return constantTreeNode;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
| 156 | VisualSymbolicExpressionTreeNode visualTreeNode = (VisualSymbolicExpressionTreeNode)sender;
|
---|
| 157 | foreach (SymbolicExpressionTreeNode treeNode in simplifiedExpressionTree.IterateNodesPostfix()) {
|
---|
| 158 | for (int i = 0; i < treeNode.SubTrees.Count; i++) {
|
---|
| 159 | SymbolicExpressionTreeNode subTree = treeNode.SubTrees[i];
|
---|
| 160 | if (subTree == visualTreeNode.SymbolicExpressionTreeNode) {
|
---|
| 161 | treeNode.RemoveSubTree(i);
|
---|
| 162 | if (replacementNodes.ContainsKey(subTree))
|
---|
| 163 | treeNode.InsertSubTree(i, replacementNodes[subTree]);
|
---|
| 164 | else if (subTree is ConstantTreeNode && replacementNodes.ContainsValue((ConstantTreeNode)subTree))
|
---|
| 165 | treeNode.InsertSubTree(i, replacementNodes.Where(v => v.Value == subTree).Single().Key);
|
---|
| 166 | else if (!(subTree is ConstantTreeNode))
|
---|
| 167 | throw new InvalidOperationException("Could not find replacement value.");
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | this.treeChart.Tree = simplifiedExpressionTree;
|
---|
| 172 |
|
---|
| 173 | SymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
|
---|
| 174 | SymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
|
---|
| 175 | root.AddSubTree(start);
|
---|
| 176 | SymbolicExpressionTree tree = new SymbolicExpressionTree(root);
|
---|
| 177 | start.AddSubTree(simplifiedExpressionTree.Root);
|
---|
| 178 |
|
---|
| 179 | this.Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
| 180 | this.Content.Model = new SymbolicRegressionModel(Content.Model.Interpreter, tree);
|
---|
| 181 | this.Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
| 182 |
|
---|
| 183 | this.PaintNodeImpacts();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | private void PaintNodeImpacts() {
|
---|
| 187 | var impacts = nodeImpacts.Values;
|
---|
| 188 | double max = impacts.Max();
|
---|
| 189 | double min = impacts.Min();
|
---|
| 190 | foreach (SymbolicExpressionTreeNode treeNode in simplifiedExpressionTree.IterateNodesPostfix()) {
|
---|
| 191 | if (!(treeNode is ConstantTreeNode)) {
|
---|
| 192 | double impact = this.nodeImpacts[treeNode];
|
---|
| 193 | double replacementValue = this.replacementNodes[treeNode].Value;
|
---|
| 194 | VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
| 195 |
|
---|
| 196 | if (impact < 1.0) {
|
---|
| 197 | visualTree.FillColor = Color.FromArgb((int)((1.0 - impact) * 255), Color.Red);
|
---|
| 198 | } else {
|
---|
| 199 | visualTree.FillColor = Color.FromArgb((int)((impact - 1.0) / max * 255), Color.Green);
|
---|
| 200 | }
|
---|
| 201 | visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
|
---|
| 202 | visualTree.ToolTip += Environment.NewLine + "Replacement value: " + replacementValue;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | this.PaintCollapsedNodes();
|
---|
| 206 | this.treeChart.Repaint();
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | private void PaintCollapsedNodes() {
|
---|
| 210 | foreach (SymbolicExpressionTreeNode treeNode in simplifiedExpressionTree.IterateNodesPostfix()) {
|
---|
| 211 | if (treeNode is ConstantTreeNode && replacementNodes.ContainsValue((ConstantTreeNode)treeNode))
|
---|
| 212 | this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;
|
---|
| 213 | else
|
---|
| 214 | this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.Black;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
[3927] | 217 |
|
---|
| 218 | private void btnSimplify_Click(object sender, EventArgs e) {
|
---|
| 219 | this.CalculateReplacementNodesAndNodeImpacts();
|
---|
| 220 | }
|
---|
[3915] | 221 | }
|
---|
| 222 | }
|
---|