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