1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
|
---|
30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
|
---|
33 | public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
|
---|
34 | private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
|
---|
35 | private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
|
---|
36 | private enum TreeState { Valid, Invalid }
|
---|
37 |
|
---|
38 | public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
|
---|
39 | InitializeComponent();
|
---|
40 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
41 | nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
42 | this.Caption = "Interactive Solution Simplifier";
|
---|
43 | }
|
---|
44 |
|
---|
45 | public new ISymbolicDataAnalysisSolution Content {
|
---|
46 | get { return (ISymbolicDataAnalysisSolution)base.Content; }
|
---|
47 | set { base.Content = value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override void RegisterContentEvents() {
|
---|
51 | base.RegisterContentEvents();
|
---|
52 | Content.ModelChanged += Content_Changed;
|
---|
53 | Content.ProblemDataChanged += Content_Changed;
|
---|
54 | treeChart.Repainted += treeChart_Repainted;
|
---|
55 | }
|
---|
56 | protected override void DeregisterContentEvents() {
|
---|
57 | base.DeregisterContentEvents();
|
---|
58 | Content.ModelChanged -= Content_Changed;
|
---|
59 | Content.ProblemDataChanged -= Content_Changed;
|
---|
60 | treeChart.Repainted -= treeChart_Repainted;
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void Content_Changed(object sender, EventArgs e) {
|
---|
64 | UpdateView();
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected override void OnContentChanged() {
|
---|
68 | base.OnContentChanged();
|
---|
69 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
70 | UpdateView();
|
---|
71 | viewHost.Content = this.Content;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void treeChart_Repainted(object sender, EventArgs e) {
|
---|
75 | if (nodeImpacts != null && nodeImpacts.Count > 0)
|
---|
76 | PaintNodeImpacts();
|
---|
77 | }
|
---|
78 |
|
---|
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));
|
---|
83 |
|
---|
84 | var replacementValues = CalculateReplacementValues(tree);
|
---|
85 | foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
|
---|
86 | foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
|
---|
87 | }
|
---|
88 |
|
---|
89 | nodeImpacts = CalculateImpactValues(tree);
|
---|
90 | PaintNodeImpacts();
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
|
---|
94 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
|
---|
95 | protected abstract void UpdateModel(ISymbolicExpressionTree tree);
|
---|
96 |
|
---|
97 | private static ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
98 | var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
|
---|
99 | var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
100 | constantTreeNode.Value = value;
|
---|
101 | return constantTreeNode;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
105 | var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
|
---|
106 | if (visualNode.Content == null) { throw new Exception("Visual node content cannot be null."); }
|
---|
107 | var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
|
---|
108 | if (!foldedNodes.ContainsKey(symbExprTreeNode)) return; // constant nodes cannot be folded
|
---|
109 | var parent = symbExprTreeNode.Parent;
|
---|
110 | int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
|
---|
111 | SwitchNodeWithReplacementNode(parent, indexOfSubtree);
|
---|
112 | UpdateModel(Content.Model.SymbolicExpressionTree);
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
|
---|
116 | ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
|
---|
117 | if (foldedNodes.ContainsKey(subTree)) {
|
---|
118 | parent.RemoveSubtree(subTreeIndex);
|
---|
119 | var replacementNode = foldedNodes[subTree];
|
---|
120 | parent.InsertSubtree(subTreeIndex, replacementNode);
|
---|
121 | // exchange key and value
|
---|
122 | foldedNodes.Remove(subTree);
|
---|
123 | foldedNodes.Add(replacementNode, subTree);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void PaintNodeImpacts() {
|
---|
128 | var impacts = nodeImpacts.Values;
|
---|
129 | double max = impacts.Max();
|
---|
130 | double min = impacts.Min();
|
---|
131 | foreach (var treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
|
---|
132 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
133 |
|
---|
134 | if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
|
---|
135 | visualTree.ToolTip = visualTree.Content.ToString(); // to avoid duplicate tooltips
|
---|
136 | double impact = nodeImpacts[treeNode];
|
---|
137 |
|
---|
138 | // impact = 0 if no change
|
---|
139 | // impact < 0 if new solution is better
|
---|
140 | // impact > 0 if new solution is worse
|
---|
141 | if (impact < 0.0) {
|
---|
142 | // min is guaranteed to be < 0
|
---|
143 | visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
|
---|
144 | } else if (impact.IsAlmost(0.0)) {
|
---|
145 | visualTree.FillColor = Color.White;
|
---|
146 | } else {
|
---|
147 | // max is guaranteed to be > 0
|
---|
148 | visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
|
---|
149 | }
|
---|
150 | visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
|
---|
151 | var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
|
---|
152 | if (constantReplacementNode != null) {
|
---|
153 | visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if (visualTree != null)
|
---|
157 | if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
|
---|
158 | visualTree.LineColor = Color.DarkOrange;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | treeChart.RepaintNodes();
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void btnSimplify_Click(object sender, EventArgs e) {
|
---|
165 | var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
|
---|
166 | var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
|
---|
167 | UpdateModel(simplifiedExpressionTree);
|
---|
168 | }
|
---|
169 |
|
---|
170 | protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
|
---|
171 | }
|
---|
172 | }
|
---|