[3915] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[10798] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
[5699] | 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
|
---|
| 33 | public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
|
---|
[9006] | 34 | private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
|
---|
[10798] | 35 | private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> changedNodes;
|
---|
[5699] | 36 | private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
|
---|
[9006] | 37 | private enum TreeState { Valid, Invalid }
|
---|
[10798] | 38 | private TreeState treeState;
|
---|
[3915] | 39 |
|
---|
[5699] | 40 | public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
|
---|
[3915] | 41 | InitializeComponent();
|
---|
[9006] | 42 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
[10798] | 43 | changedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
[8946] | 44 | nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
[3915] | 45 | this.Caption = "Interactive Solution Simplifier";
|
---|
[10798] | 46 |
|
---|
| 47 | // initialize the tree modifier that will be used to perform edit operations over the tree
|
---|
| 48 | treeChart.ModifyTree = Modify;
|
---|
[3915] | 49 | }
|
---|
| 50 |
|
---|
[10798] | 51 | /// <summary>
|
---|
| 52 | /// Remove, Replace or Insert subtrees
|
---|
| 53 | /// </summary>
|
---|
| 54 | /// <param name="tree">The symbolic expression tree</param>
|
---|
| 55 | /// <param name="parent">The insertion point (ie, the parent node who will receive a new child)</param>
|
---|
| 56 | /// <param name="oldChild">The subtree to be replaced</param>
|
---|
| 57 | /// <param name="newChild">The replacement subtree</param>
|
---|
| 58 | /// <param name="removeSubtree">Flag used to indicate if whole subtrees should be removed (default behavior), or just the subtree root</param>
|
---|
| 59 | private void Modify(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true) {
|
---|
| 60 | if (oldChild == null && newChild == null) throw new ArgumentNullException("Cannot deduce operation type from the arguments. Please provide non null operands.");
|
---|
| 61 | if (oldChild == null) { // insertion operation
|
---|
| 62 | parent.AddSubtree(newChild);
|
---|
| 63 | newChild.Parent = parent;
|
---|
| 64 | } else if (newChild == null) { // removal operation
|
---|
| 65 | parent.RemoveSubtree(parent.IndexOfSubtree(oldChild));
|
---|
| 66 | changedNodes.Remove(oldChild);
|
---|
| 67 | foldedNodes.Remove(oldChild);
|
---|
| 68 | if (removeSubtree) {
|
---|
| 69 | foreach (var subtree in oldChild.IterateNodesPrefix()) {
|
---|
| 70 | changedNodes.Remove(subtree);
|
---|
| 71 | foldedNodes.Remove(subtree);
|
---|
| 72 | }
|
---|
| 73 | } else {
|
---|
| 74 | for (int i = oldChild.SubtreeCount - 1; i >= 0; --i) {
|
---|
| 75 | var subtree = oldChild.GetSubtree(i);
|
---|
| 76 | oldChild.RemoveSubtree(i);
|
---|
| 77 | parent.AddSubtree(subtree);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | } else { // replacement operation
|
---|
| 81 | var replacementIndex = parent.IndexOfSubtree(oldChild);
|
---|
| 82 | parent.RemoveSubtree(replacementIndex);
|
---|
| 83 | parent.InsertSubtree(replacementIndex, newChild);
|
---|
| 84 | newChild.Parent = parent;
|
---|
| 85 | if (changedNodes.ContainsKey(oldChild)) {
|
---|
| 86 | changedNodes.Add(newChild, changedNodes[oldChild]); // so that on double click the original node is restored
|
---|
| 87 | changedNodes.Remove(oldChild);
|
---|
| 88 | } else {
|
---|
| 89 | changedNodes.Add(newChild, oldChild);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | treeState = IsValid(tree) ? TreeState.Valid : TreeState.Invalid;
|
---|
| 93 | if (treeState == TreeState.Valid) UpdateModel(Content.Model.SymbolicExpressionTree);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private bool IsValid(ISymbolicExpressionTree tree) {
|
---|
| 97 | treeChart.Tree = tree;
|
---|
| 98 | treeChart.Repaint();
|
---|
| 99 | bool valid = !tree.IterateNodesPostfix().Any(node => node.SubtreeCount < node.Symbol.MinimumArity || node.SubtreeCount > node.Symbol.MaximumArity);
|
---|
| 100 | if (valid) {
|
---|
| 101 | btnOptimizeConstants.Enabled = true;
|
---|
| 102 | btnSimplify.Enabled = true;
|
---|
| 103 | treeStatusValue.Text = "Valid";
|
---|
| 104 | treeStatusValue.ForeColor = Color.Green;
|
---|
| 105 | } else {
|
---|
| 106 | btnOptimizeConstants.Enabled = false;
|
---|
| 107 | btnSimplify.Enabled = false;
|
---|
| 108 | treeStatusValue.Text = "Invalid";
|
---|
| 109 | treeStatusValue.ForeColor = Color.Red;
|
---|
| 110 | }
|
---|
| 111 | this.Refresh();
|
---|
| 112 | return valid;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[5699] | 115 | public new ISymbolicDataAnalysisSolution Content {
|
---|
| 116 | get { return (ISymbolicDataAnalysisSolution)base.Content; }
|
---|
[3915] | 117 | set { base.Content = value; }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | protected override void RegisterContentEvents() {
|
---|
| 121 | base.RegisterContentEvents();
|
---|
[8946] | 122 | Content.ModelChanged += Content_Changed;
|
---|
| 123 | Content.ProblemDataChanged += Content_Changed;
|
---|
[10564] | 124 | treeChart.Repainted += treeChart_Repainted;
|
---|
[3915] | 125 | }
|
---|
| 126 | protected override void DeregisterContentEvents() {
|
---|
| 127 | base.DeregisterContentEvents();
|
---|
[8946] | 128 | Content.ModelChanged -= Content_Changed;
|
---|
| 129 | Content.ProblemDataChanged -= Content_Changed;
|
---|
[10564] | 130 | treeChart.Repainted -= treeChart_Repainted;
|
---|
[3915] | 131 | }
|
---|
| 132 |
|
---|
[8946] | 133 | private void Content_Changed(object sender, EventArgs e) {
|
---|
| 134 | UpdateView();
|
---|
[3915] | 135 | }
|
---|
[5699] | 136 |
|
---|
[3915] | 137 | protected override void OnContentChanged() {
|
---|
| 138 | base.OnContentChanged();
|
---|
[9006] | 139 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
[8946] | 140 | UpdateView();
|
---|
| 141 | viewHost.Content = this.Content;
|
---|
[3915] | 142 | }
|
---|
| 143 |
|
---|
[10564] | 144 | private void treeChart_Repainted(object sender, EventArgs e) {
|
---|
| 145 | if (nodeImpacts != null && nodeImpacts.Count > 0)
|
---|
| 146 | PaintNodeImpacts();
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[8946] | 149 | private void UpdateView() {
|
---|
| 150 | if (Content == null || Content.Model == null || Content.ProblemData == null) return;
|
---|
| 151 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
[8990] | 152 | treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
|
---|
[3915] | 153 |
|
---|
[10492] | 154 | var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree);
|
---|
| 155 | var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
|
---|
[8946] | 156 | foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
|
---|
[9006] | 157 | foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
|
---|
[8946] | 158 | }
|
---|
[10798] | 159 | nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
|
---|
[8946] | 160 | PaintNodeImpacts();
|
---|
[3915] | 161 | }
|
---|
| 162 |
|
---|
[5717] | 163 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
|
---|
| 164 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
|
---|
[10492] | 165 | protected abstract Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree);
|
---|
[5717] | 166 | protected abstract void UpdateModel(ISymbolicExpressionTree tree);
|
---|
[3915] | 167 |
|
---|
[8946] | 168 | private static ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
| 169 | var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
|
---|
| 170 | var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
[3915] | 171 | constantTreeNode.Value = value;
|
---|
| 172 | return constantTreeNode;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
[10798] | 176 | if (treeState == TreeState.Invalid) return;
|
---|
[10521] | 177 | var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
|
---|
[10798] | 178 | if (visualNode.Content == null) { throw new Exception("VisualNode content cannot be null."); }
|
---|
[10521] | 179 | var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
|
---|
[5722] | 180 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
[9006] | 181 | var parent = symbExprTreeNode.Parent;
|
---|
| 182 | int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
|
---|
[10798] | 183 | if (changedNodes.ContainsKey(symbExprTreeNode)) {
|
---|
| 184 | // undo node change
|
---|
| 185 | parent.RemoveSubtree(indexOfSubtree);
|
---|
| 186 | var originalNode = changedNodes[symbExprTreeNode];
|
---|
| 187 | parent.InsertSubtree(indexOfSubtree, originalNode);
|
---|
| 188 | changedNodes.Remove(symbExprTreeNode);
|
---|
| 189 | } else if (foldedNodes.ContainsKey(symbExprTreeNode)) {
|
---|
[9043] | 190 | // undo node folding
|
---|
[9006] | 191 | SwitchNodeWithReplacementNode(parent, indexOfSubtree);
|
---|
| 192 | }
|
---|
[9055] | 193 | UpdateModel(tree);
|
---|
[3915] | 194 | }
|
---|
| 195 |
|
---|
[5729] | 196 | private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
|
---|
[5736] | 197 | ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
|
---|
[9006] | 198 | if (foldedNodes.ContainsKey(subTree)) {
|
---|
[10798] | 199 | parent.RemoveSubtree(subTreeIndex);
|
---|
[9006] | 200 | var replacementNode = foldedNodes[subTree];
|
---|
[5736] | 201 | parent.InsertSubtree(subTreeIndex, replacementNode);
|
---|
[5729] | 202 | // exchange key and value
|
---|
[9006] | 203 | foldedNodes.Remove(subTree);
|
---|
| 204 | foldedNodes.Add(replacementNode, subTree);
|
---|
[5729] | 205 | }
|
---|
[5455] | 206 | }
|
---|
| 207 |
|
---|
[3915] | 208 | private void PaintNodeImpacts() {
|
---|
| 209 | var impacts = nodeImpacts.Values;
|
---|
| 210 | double max = impacts.Max();
|
---|
| 211 | double min = impacts.Min();
|
---|
[10798] | 212 | foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
|
---|
[10521] | 213 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
[8946] | 214 |
|
---|
[4477] | 215 | if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
|
---|
[5729] | 216 | double impact = nodeImpacts[treeNode];
|
---|
[5722] | 217 |
|
---|
[5717] | 218 | // impact = 0 if no change
|
---|
| 219 | // impact < 0 if new solution is better
|
---|
| 220 | // impact > 0 if new solution is worse
|
---|
| 221 | if (impact < 0.0) {
|
---|
[5729] | 222 | // min is guaranteed to be < 0
|
---|
[5717] | 223 | visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
|
---|
[5729] | 224 | } else if (impact.IsAlmost(0.0)) {
|
---|
| 225 | visualTree.FillColor = Color.White;
|
---|
[5717] | 226 | } else {
|
---|
[5729] | 227 | // max is guaranteed to be > 0
|
---|
[5717] | 228 | visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
|
---|
| 229 | }
|
---|
[3915] | 230 | visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
|
---|
[9006] | 231 | var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
|
---|
[5729] | 232 | if (constantReplacementNode != null) {
|
---|
| 233 | visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
|
---|
| 234 | }
|
---|
[6108] | 235 | }
|
---|
[9043] | 236 | if (visualTree != null)
|
---|
[10798] | 237 | if (changedNodes.ContainsKey(treeNode)) {
|
---|
| 238 | visualTree.LineColor = Color.DodgerBlue;
|
---|
| 239 | } else if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
|
---|
[9043] | 240 | visualTree.LineColor = Color.DarkOrange;
|
---|
| 241 | }
|
---|
[3915] | 242 | }
|
---|
[8980] | 243 | treeChart.RepaintNodes();
|
---|
[3915] | 244 | }
|
---|
[3927] | 245 |
|
---|
| 246 | private void btnSimplify_Click(object sender, EventArgs e) {
|
---|
[8946] | 247 | var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
|
---|
[5722] | 248 | var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
|
---|
| 249 | UpdateModel(simplifiedExpressionTree);
|
---|
[3927] | 250 | }
|
---|
[6256] | 251 |
|
---|
| 252 | protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
|
---|
[3915] | 253 | }
|
---|
| 254 | }
|
---|