[3915] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9708] | 3 | * Copyright (C) 2002-2013 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 {
|
---|
[9126] | 34 | private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
|
---|
[5699] | 35 | private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
|
---|
[9126] | 36 | private enum TreeState { Valid, Invalid }
|
---|
[3915] | 37 |
|
---|
[5699] | 38 | public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
|
---|
[3915] | 39 | InitializeComponent();
|
---|
[9126] | 40 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
| 41 | 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();
|
---|
[9126] | 52 | Content.ModelChanged += Content_Changed;
|
---|
| 53 | Content.ProblemDataChanged += Content_Changed;
|
---|
[10681] | 54 | treeChart.Repainted += treeChart_Repainted;
|
---|
[3915] | 55 | }
|
---|
| 56 | protected override void DeregisterContentEvents() {
|
---|
| 57 | base.DeregisterContentEvents();
|
---|
[9126] | 58 | Content.ModelChanged -= Content_Changed;
|
---|
| 59 | Content.ProblemDataChanged -= Content_Changed;
|
---|
[10681] | 60 | treeChart.Repainted -= treeChart_Repainted;
|
---|
[3915] | 61 | }
|
---|
| 62 |
|
---|
[9126] | 63 | private void Content_Changed(object sender, EventArgs e) {
|
---|
| 64 | UpdateView();
|
---|
[3915] | 65 | }
|
---|
[5699] | 66 |
|
---|
[3915] | 67 | protected override void OnContentChanged() {
|
---|
| 68 | base.OnContentChanged();
|
---|
[9126] | 69 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
| 70 | UpdateView();
|
---|
| 71 | viewHost.Content = this.Content;
|
---|
[3915] | 72 | }
|
---|
| 73 |
|
---|
[10681] | 74 | private void treeChart_Repainted(object sender, EventArgs e) {
|
---|
| 75 | if (nodeImpacts != null && nodeImpacts.Count > 0)
|
---|
| 76 | PaintNodeImpacts();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[9126] | 79 | private void UpdateView() {
|
---|
| 80 | if (Content == null || Content.Model == null || Content.ProblemData == null) return;
|
---|
| 81 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
| 82 | treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
|
---|
[3915] | 83 |
|
---|
[10681] | 84 | var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree);
|
---|
| 85 | nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
|
---|
| 86 | var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
|
---|
[9126] | 87 | foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
|
---|
| 88 | foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
|
---|
| 89 | }
|
---|
| 90 | PaintNodeImpacts();
|
---|
[3915] | 91 | }
|
---|
| 92 |
|
---|
[5717] | 93 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
|
---|
| 94 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
|
---|
[10681] | 95 | protected abstract Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree);
|
---|
[5717] | 96 | protected abstract void UpdateModel(ISymbolicExpressionTree tree);
|
---|
[3915] | 97 |
|
---|
[9126] | 98 | private static ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
| 99 | var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
|
---|
| 100 | var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
[3915] | 101 | constantTreeNode.Value = value;
|
---|
| 102 | return constantTreeNode;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
[10681] | 106 | var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
|
---|
| 107 | var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
|
---|
[9126] | 108 | if (symbExprTreeNode == null) return;
|
---|
[5722] | 109 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
[9126] | 110 | var parent = symbExprTreeNode.Parent;
|
---|
| 111 | int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
|
---|
[9708] | 112 | if (foldedNodes.ContainsKey(symbExprTreeNode)) {
|
---|
[9126] | 113 | // undo node folding
|
---|
| 114 | SwitchNodeWithReplacementNode(parent, indexOfSubtree);
|
---|
[3915] | 115 | }
|
---|
[9126] | 116 | UpdateModel(tree);
|
---|
[3915] | 117 | }
|
---|
| 118 |
|
---|
[5729] | 119 | private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
|
---|
[5736] | 120 | ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
|
---|
| 121 | parent.RemoveSubtree(subTreeIndex);
|
---|
[9126] | 122 | if (foldedNodes.ContainsKey(subTree)) {
|
---|
| 123 | var replacementNode = foldedNodes[subTree];
|
---|
[5736] | 124 | parent.InsertSubtree(subTreeIndex, replacementNode);
|
---|
[5729] | 125 | // exchange key and value
|
---|
[9126] | 126 | foldedNodes.Remove(subTree);
|
---|
| 127 | foldedNodes.Add(replacementNode, subTree);
|
---|
[5729] | 128 | }
|
---|
[5455] | 129 | }
|
---|
| 130 |
|
---|
[3915] | 131 | private void PaintNodeImpacts() {
|
---|
| 132 | var impacts = nodeImpacts.Values;
|
---|
| 133 | double max = impacts.Max();
|
---|
| 134 | double min = impacts.Min();
|
---|
[10681] | 135 | foreach (var treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
|
---|
| 136 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
[9126] | 137 |
|
---|
[4477] | 138 | if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
|
---|
[5729] | 139 | double impact = nodeImpacts[treeNode];
|
---|
[5722] | 140 |
|
---|
[5717] | 141 | // impact = 0 if no change
|
---|
| 142 | // impact < 0 if new solution is better
|
---|
| 143 | // impact > 0 if new solution is worse
|
---|
| 144 | if (impact < 0.0) {
|
---|
[5729] | 145 | // min is guaranteed to be < 0
|
---|
[5717] | 146 | visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
|
---|
[5729] | 147 | } else if (impact.IsAlmost(0.0)) {
|
---|
| 148 | visualTree.FillColor = Color.White;
|
---|
[5717] | 149 | } else {
|
---|
[5729] | 150 | // max is guaranteed to be > 0
|
---|
[5717] | 151 | visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
|
---|
| 152 | }
|
---|
[3915] | 153 | visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
|
---|
[9126] | 154 | var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
|
---|
[5729] | 155 | if (constantReplacementNode != null) {
|
---|
| 156 | visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
|
---|
| 157 | }
|
---|
[6108] | 158 | }
|
---|
[9126] | 159 | if (visualTree != null)
|
---|
[9708] | 160 | if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
|
---|
[9126] | 161 | visualTree.LineColor = Color.DarkOrange;
|
---|
| 162 | }
|
---|
[3915] | 163 | }
|
---|
[9126] | 164 | treeChart.RepaintNodes();
|
---|
[3915] | 165 | }
|
---|
| 166 |
|
---|
[3927] | 167 | private void btnSimplify_Click(object sender, EventArgs e) {
|
---|
[9126] | 168 | var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
|
---|
[5722] | 169 | var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
|
---|
| 170 | UpdateModel(simplifiedExpressionTree);
|
---|
[3927] | 171 | }
|
---|
[6256] | 172 |
|
---|
| 173 | protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
|
---|
[3915] | 174 | }
|
---|
| 175 | }
|
---|