1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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, ISymbolicExpressionTreeNode> changedNodes;
|
---|
36 | private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
|
---|
37 | private enum TreeState { Valid, Invalid }
|
---|
38 | private TreeState treeState;
|
---|
39 |
|
---|
40 | public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
|
---|
41 | InitializeComponent();
|
---|
42 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
43 | changedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
44 | nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
45 | this.Caption = "Interactive Solution Simplifier";
|
---|
46 |
|
---|
47 | // initialize the tree modifier that will be used to perform edit operations over the tree
|
---|
48 | treeChart.ModifyTree = Modify;
|
---|
49 | }
|
---|
50 |
|
---|
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 |
|
---|
115 | public new ISymbolicDataAnalysisSolution Content {
|
---|
116 | get { return (ISymbolicDataAnalysisSolution)base.Content; }
|
---|
117 | set { base.Content = value; }
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected override void RegisterContentEvents() {
|
---|
121 | base.RegisterContentEvents();
|
---|
122 | Content.ModelChanged += Content_Changed;
|
---|
123 | Content.ProblemDataChanged += Content_Changed;
|
---|
124 | treeChart.Repainted += treeChart_Repainted;
|
---|
125 | }
|
---|
126 | protected override void DeregisterContentEvents() {
|
---|
127 | base.DeregisterContentEvents();
|
---|
128 | Content.ModelChanged -= Content_Changed;
|
---|
129 | Content.ProblemDataChanged -= Content_Changed;
|
---|
130 | treeChart.Repainted -= treeChart_Repainted;
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void Content_Changed(object sender, EventArgs e) {
|
---|
134 | UpdateView();
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected override void OnContentChanged() {
|
---|
138 | base.OnContentChanged();
|
---|
139 | foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
140 | UpdateView();
|
---|
141 | viewHost.Content = this.Content;
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void treeChart_Repainted(object sender, EventArgs e) {
|
---|
145 | if (nodeImpacts != null && nodeImpacts.Count > 0)
|
---|
146 | PaintNodeImpacts();
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void UpdateView() {
|
---|
150 | if (Content == null || Content.Model == null || Content.ProblemData == null) return;
|
---|
151 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
152 | treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
|
---|
153 |
|
---|
154 | var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree);
|
---|
155 | var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
|
---|
156 | foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
|
---|
157 | foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
|
---|
158 | }
|
---|
159 | nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
|
---|
160 | PaintNodeImpacts();
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
|
---|
164 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
|
---|
165 | protected abstract Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree);
|
---|
166 | protected abstract void UpdateModel(ISymbolicExpressionTree tree);
|
---|
167 |
|
---|
168 | private static ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
169 | var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
|
---|
170 | var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
171 | constantTreeNode.Value = value;
|
---|
172 | return constantTreeNode;
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
176 | if (treeState == TreeState.Invalid) return;
|
---|
177 | var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
|
---|
178 | if (visualNode.Content == null) { throw new Exception("VisualNode content cannot be null."); }
|
---|
179 | var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content;
|
---|
180 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
181 | var parent = symbExprTreeNode.Parent;
|
---|
182 | int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode);
|
---|
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)) {
|
---|
190 | // undo node folding
|
---|
191 | SwitchNodeWithReplacementNode(parent, indexOfSubtree);
|
---|
192 | }
|
---|
193 | UpdateModel(tree);
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
|
---|
197 | ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
|
---|
198 | if (foldedNodes.ContainsKey(subTree)) {
|
---|
199 | parent.RemoveSubtree(subTreeIndex);
|
---|
200 | var replacementNode = foldedNodes[subTree];
|
---|
201 | parent.InsertSubtree(subTreeIndex, replacementNode);
|
---|
202 | // exchange key and value
|
---|
203 | foldedNodes.Remove(subTree);
|
---|
204 | foldedNodes.Add(replacementNode, subTree);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | private void PaintNodeImpacts() {
|
---|
209 | var impacts = nodeImpacts.Values;
|
---|
210 | double max = impacts.Max();
|
---|
211 | double min = impacts.Min();
|
---|
212 | foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
|
---|
213 | VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
214 |
|
---|
215 | if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
|
---|
216 | double impact = nodeImpacts[treeNode];
|
---|
217 |
|
---|
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) {
|
---|
222 | // min is guaranteed to be < 0
|
---|
223 | visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
|
---|
224 | } else if (impact.IsAlmost(0.0)) {
|
---|
225 | visualTree.FillColor = Color.White;
|
---|
226 | } else {
|
---|
227 | // max is guaranteed to be > 0
|
---|
228 | visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
|
---|
229 | }
|
---|
230 | visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
|
---|
231 | var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
|
---|
232 | if (constantReplacementNode != null) {
|
---|
233 | visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | if (visualTree != null)
|
---|
237 | if (changedNodes.ContainsKey(treeNode)) {
|
---|
238 | visualTree.LineColor = Color.DodgerBlue;
|
---|
239 | } else if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
|
---|
240 | visualTree.LineColor = Color.DarkOrange;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | treeChart.RepaintNodes();
|
---|
244 | }
|
---|
245 |
|
---|
246 | private void btnSimplify_Click(object sender, EventArgs e) {
|
---|
247 | var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
|
---|
248 | var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
|
---|
249 | UpdateModel(simplifiedExpressionTree);
|
---|
250 | }
|
---|
251 |
|
---|
252 | protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
|
---|
253 | }
|
---|
254 | }
|
---|