[3915] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3915] | 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;
|
---|
[4068] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
|
---|
[3915] | 30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 32 |
|
---|
[5699] | 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
|
---|
| 34 | public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
|
---|
[5729] | 35 | private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes;
|
---|
[5699] | 36 | private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
|
---|
[3915] | 37 |
|
---|
[5699] | 38 | public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
|
---|
[3915] | 39 | InitializeComponent();
|
---|
[5729] | 40 | this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
[5699] | 41 | this.nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
[3915] | 42 | this.Caption = "Interactive Solution Simplifier";
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[5699] | 45 | public new ISymbolicDataAnalysisSolution Content {
|
---|
| 46 | get { return (ISymbolicDataAnalysisSolution)base.Content; }
|
---|
[3915] | 47 | set { base.Content = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | protected override void RegisterContentEvents() {
|
---|
| 51 | base.RegisterContentEvents();
|
---|
| 52 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
| 53 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
| 54 | }
|
---|
| 55 | protected override void DeregisterContentEvents() {
|
---|
| 56 | base.DeregisterContentEvents();
|
---|
| 57 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
| 58 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
[5699] | 62 | OnModelChanged();
|
---|
[3915] | 63 | }
|
---|
| 64 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
[5699] | 65 | OnProblemDataChanged();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | protected virtual void OnModelChanged() {
|
---|
[3915] | 69 | this.CalculateReplacementNodesAndNodeImpacts();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[5699] | 72 | protected virtual void OnProblemDataChanged() {
|
---|
| 73 | this.CalculateReplacementNodesAndNodeImpacts();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[3915] | 76 | protected override void OnContentChanged() {
|
---|
| 77 | base.OnContentChanged();
|
---|
| 78 | this.CalculateReplacementNodesAndNodeImpacts();
|
---|
| 79 | this.viewHost.Content = this.Content;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private void CalculateReplacementNodesAndNodeImpacts() {
|
---|
| 83 | if (Content != null && Content.Model != null && Content.ProblemData != null) {
|
---|
[5722] | 84 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
| 85 | var replacementValues = CalculateReplacementValues(tree);
|
---|
[5699] | 86 | foreach (var pair in replacementValues) {
|
---|
[5729] | 87 | if (!(pair.Key is ConstantTreeNode)) {
|
---|
| 88 | replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
|
---|
| 89 | }
|
---|
[5699] | 90 | }
|
---|
[5722] | 91 | nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree);
|
---|
[3915] | 92 |
|
---|
[5455] | 93 | // automatically fold all branches with impact = 1
|
---|
[5736] | 94 | List<ISymbolicExpressionTreeNode> nodeList = Content.Model.SymbolicExpressionTree.Root.GetSubtree(0).IterateNodesPrefix().ToList();
|
---|
[5455] | 95 | foreach (var parent in nodeList) {
|
---|
[5736] | 96 | for (int subTreeIndex = 0; subTreeIndex < parent.SubtreesCount; subTreeIndex++) {
|
---|
| 97 | var child = parent.GetSubtree(subTreeIndex);
|
---|
[5993] | 98 | if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(0.0)) {
|
---|
[5729] | 99 | SwitchNodeWithReplacementNode(parent, subTreeIndex);
|
---|
[5455] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[5993] | 103 | // show only interesting part of solution
|
---|
| 104 | if (tree.Root.SubtreesCount > 1)
|
---|
| 105 | this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs
|
---|
| 106 | else
|
---|
| 107 | this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB
|
---|
[3915] | 108 | this.PaintNodeImpacts();
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[5717] | 112 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
|
---|
| 113 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
|
---|
| 114 | protected abstract void UpdateModel(ISymbolicExpressionTree tree);
|
---|
[3915] | 115 |
|
---|
| 116 | private ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
| 117 | Constant constant = new Constant();
|
---|
| 118 | constant.MinValue = value - 1;
|
---|
| 119 | constant.MaxValue = value + 1;
|
---|
| 120 | ConstantTreeNode constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
| 121 | constantTreeNode.Value = value;
|
---|
| 122 | return constantTreeNode;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
| 126 | VisualSymbolicExpressionTreeNode visualTreeNode = (VisualSymbolicExpressionTreeNode)sender;
|
---|
[5722] | 127 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
| 128 | foreach (SymbolicExpressionTreeNode treeNode in tree.IterateNodesPostfix()) {
|
---|
[5736] | 129 | for (int i = 0; i < treeNode.SubtreesCount; i++) {
|
---|
| 130 | ISymbolicExpressionTreeNode subTree = treeNode.GetSubtree(i);
|
---|
[5993] | 131 | // only allow to replace nodes for which a replacement value is known (replacement value for ADF nodes are not available)
|
---|
| 132 | if (subTree == visualTreeNode.SymbolicExpressionTreeNode && replacementNodes.ContainsKey(subTree)) {
|
---|
| 133 | double replacementImpact = nodeImpacts.ContainsKey(replacementNodes[subTree]) ? nodeImpacts[replacementNodes[subTree]] : 0.0;
|
---|
| 134 | double originalImpact = nodeImpacts.ContainsKey(subTree) ? nodeImpacts[subTree] : 0.0;
|
---|
[5729] | 135 | SwitchNodeWithReplacementNode(treeNode, i);
|
---|
[5993] | 136 |
|
---|
| 137 | // show only interesting part of solution
|
---|
| 138 | if (tree.Root.SubtreesCount > 1)
|
---|
| 139 | this.treeChart.Tree = new SymbolicExpressionTree(tree.Root); // RPB + ADFs
|
---|
| 140 | else
|
---|
| 141 | this.treeChart.Tree = new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0)); // 1st child of RPB
|
---|
| 142 | if (!(originalImpact.IsAlmost(0.0) && replacementImpact.IsAlmost(0.0))) {
|
---|
| 143 | // update everything after the change if necessary (impact != 0)
|
---|
| 144 | UpdateModel(tree);
|
---|
| 145 | } else {
|
---|
| 146 | // both impacts are zero, so we only need to repaint the nodes
|
---|
| 147 | PaintNodeImpacts();
|
---|
| 148 | }
|
---|
| 149 | return; // break all loops
|
---|
[3915] | 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[5729] | 155 | private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
|
---|
[5736] | 156 | ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
|
---|
| 157 | parent.RemoveSubtree(subTreeIndex);
|
---|
[5729] | 158 | if (replacementNodes.ContainsKey(subTree)) {
|
---|
| 159 | var replacementNode = replacementNodes[subTree];
|
---|
[5736] | 160 | parent.InsertSubtree(subTreeIndex, replacementNode);
|
---|
[5729] | 161 | // exchange key and value
|
---|
| 162 | replacementNodes.Remove(subTree);
|
---|
| 163 | replacementNodes.Add(replacementNode, subTree);
|
---|
| 164 | }
|
---|
[5455] | 165 | }
|
---|
| 166 |
|
---|
[3915] | 167 | private void PaintNodeImpacts() {
|
---|
| 168 | var impacts = nodeImpacts.Values;
|
---|
| 169 | double max = impacts.Max();
|
---|
| 170 | double min = impacts.Min();
|
---|
[5722] | 171 | foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
|
---|
[4477] | 172 | if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
|
---|
[5729] | 173 | double impact = nodeImpacts[treeNode];
|
---|
[3915] | 174 | VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
[5722] | 175 |
|
---|
[5717] | 176 | // impact = 0 if no change
|
---|
| 177 | // impact < 0 if new solution is better
|
---|
| 178 | // impact > 0 if new solution is worse
|
---|
| 179 | if (impact < 0.0) {
|
---|
[5729] | 180 | // min is guaranteed to be < 0
|
---|
[5717] | 181 | visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
|
---|
[5729] | 182 | } else if (impact.IsAlmost(0.0)) {
|
---|
| 183 | visualTree.FillColor = Color.White;
|
---|
[5717] | 184 | } else {
|
---|
[5729] | 185 | // max is guaranteed to be > 0
|
---|
[5717] | 186 | visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
|
---|
| 187 | }
|
---|
[3915] | 188 | visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
|
---|
[5729] | 189 | var constantReplacementNode = replacementNodes[treeNode] as ConstantTreeNode;
|
---|
| 190 | if (constantReplacementNode != null) {
|
---|
| 191 | visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
|
---|
| 192 | }
|
---|
[5993] | 193 | }
|
---|
[3915] | 194 | }
|
---|
| 195 | this.PaintCollapsedNodes();
|
---|
| 196 | this.treeChart.Repaint();
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | private void PaintCollapsedNodes() {
|
---|
[5722] | 200 | foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
|
---|
[5729] | 201 | if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode))
|
---|
[3915] | 202 | this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;
|
---|
[4477] | 203 | else {
|
---|
| 204 | VisualSymbolicExpressionTreeNode visNode = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
| 205 | if (visNode != null)
|
---|
| 206 | visNode.LineColor = Color.Black;
|
---|
| 207 | }
|
---|
[3915] | 208 | }
|
---|
| 209 | }
|
---|
[3927] | 210 |
|
---|
| 211 | private void btnSimplify_Click(object sender, EventArgs e) {
|
---|
[5722] | 212 | SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
|
---|
| 213 | var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
|
---|
| 214 | UpdateModel(simplifiedExpressionTree);
|
---|
[3927] | 215 | }
|
---|
[3915] | 216 | }
|
---|
| 217 | }
|
---|