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