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> replacementNodes;
|
---|
35 | private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
|
---|
36 |
|
---|
37 | public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
|
---|
38 | InitializeComponent();
|
---|
39 | replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
|
---|
40 | nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
41 | this.Caption = "Interactive Solution Simplifier";
|
---|
42 | }
|
---|
43 |
|
---|
44 | public new ISymbolicDataAnalysisSolution Content {
|
---|
45 | get { return (ISymbolicDataAnalysisSolution)base.Content; }
|
---|
46 | set { base.Content = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected override void RegisterContentEvents() {
|
---|
50 | base.RegisterContentEvents();
|
---|
51 | Content.ModelChanged += Content_Changed;
|
---|
52 | Content.ProblemDataChanged += Content_Changed;
|
---|
53 | }
|
---|
54 | protected override void DeregisterContentEvents() {
|
---|
55 | base.DeregisterContentEvents();
|
---|
56 | Content.ModelChanged -= Content_Changed;
|
---|
57 | Content.ProblemDataChanged -= Content_Changed;
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void Content_Changed(object sender, EventArgs e) {
|
---|
61 | UpdateView();
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected override void OnContentChanged() {
|
---|
65 | base.OnContentChanged();
|
---|
66 | UpdateView();
|
---|
67 | viewHost.Content = this.Content;
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void UpdateView() {
|
---|
71 | if (Content == null || Content.Model == null || Content.ProblemData == null) return;
|
---|
72 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
73 |
|
---|
74 | var replacementValues = CalculateReplacementValues(tree);
|
---|
75 | foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
|
---|
76 | replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
|
---|
77 | }
|
---|
78 |
|
---|
79 | nodeImpacts = CalculateImpactValues(tree);
|
---|
80 |
|
---|
81 | var model = Content.Model.SymbolicExpressionTree;
|
---|
82 | treeChart.Tree = model.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(model.Root) : new SymbolicExpressionTree(model.Root.GetSubtree(0).GetSubtree(0));
|
---|
83 | PaintNodeImpacts();
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree);
|
---|
87 | protected abstract Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree);
|
---|
88 | protected abstract void UpdateModel(ISymbolicExpressionTree tree);
|
---|
89 |
|
---|
90 | private static ConstantTreeNode MakeConstantTreeNode(double value) {
|
---|
91 | var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
|
---|
92 | var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
93 | constantTreeNode.Value = value;
|
---|
94 | return constantTreeNode;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
98 | if (!treeChart.TreeValid) return;
|
---|
99 | var visualNode = (VisualSymbolicExpressionTreeNode)sender;
|
---|
100 | var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.SymbolicExpressionTreeNode;
|
---|
101 | if (symbExprTreeNode == null) return;
|
---|
102 | if (!replacementNodes.ContainsKey(symbExprTreeNode)) return;
|
---|
103 | var tree = Content.Model.SymbolicExpressionTree;
|
---|
104 | var replacementNode = replacementNodes[symbExprTreeNode];
|
---|
105 | int indexOfReplacementNode = symbExprTreeNode.Parent.IndexOfSubtree(symbExprTreeNode);
|
---|
106 | SwitchNodeWithReplacementNode(symbExprTreeNode.Parent, indexOfReplacementNode);
|
---|
107 | // show only interesting part of solution
|
---|
108 | treeChart.Tree = tree.Root.SubtreeCount > 1
|
---|
109 | ? new SymbolicExpressionTree(tree.Root)
|
---|
110 | : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
|
---|
111 | UpdateModel(tree);
|
---|
112 | var vNode = treeChart.GetVisualSymbolicExpressionTreeNode(replacementNode);
|
---|
113 | vNode.SymbolicExpressionTreeNode = replacementNode;
|
---|
114 |
|
---|
115 | vNode.LineColor = replacementNode is ConstantTreeNode ? Color.DarkOrange : Color.Black;
|
---|
116 | treeChart.RepaintNode(vNode);
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void treeChart_SymbolicExpressionTreeChanged(object sender, EventArgs e) {
|
---|
120 | UpdateModel(Content.Model.SymbolicExpressionTree);
|
---|
121 | UpdateView();
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
|
---|
125 | ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
|
---|
126 | parent.RemoveSubtree(subTreeIndex);
|
---|
127 | if (replacementNodes.ContainsKey(subTree)) {
|
---|
128 | var replacementNode = replacementNodes[subTree];
|
---|
129 | parent.InsertSubtree(subTreeIndex, replacementNode);
|
---|
130 | // exchange key and value
|
---|
131 | replacementNodes.Remove(subTree);
|
---|
132 | replacementNodes.Add(replacementNode, subTree);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void PaintNodeImpacts() {
|
---|
137 | var impacts = nodeImpacts.Values;
|
---|
138 | double max = impacts.Max();
|
---|
139 | double min = impacts.Min();
|
---|
140 | foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
|
---|
141 | VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
|
---|
142 |
|
---|
143 | if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
|
---|
144 | double impact = nodeImpacts[treeNode];
|
---|
145 |
|
---|
146 | // impact = 0 if no change
|
---|
147 | // impact < 0 if new solution is better
|
---|
148 | // impact > 0 if new solution is worse
|
---|
149 | if (impact < 0.0) {
|
---|
150 | // min is guaranteed to be < 0
|
---|
151 | visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
|
---|
152 | } else if (impact.IsAlmost(0.0)) {
|
---|
153 | visualTree.FillColor = Color.White;
|
---|
154 | } else {
|
---|
155 | // max is guaranteed to be > 0
|
---|
156 | visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
|
---|
157 | }
|
---|
158 | visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
|
---|
159 | var constantReplacementNode = replacementNodes[treeNode] as ConstantTreeNode;
|
---|
160 | if (constantReplacementNode != null) {
|
---|
161 | visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | if (visualTree != null && treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode)) {
|
---|
165 | visualTree.LineColor = Color.DarkOrange;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | // repaint nodes and refresh
|
---|
169 | treeChart.RepaintNodes();
|
---|
170 | treeChart.Refresh();
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void btnSimplify_Click(object sender, EventArgs e) {
|
---|
174 | var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
|
---|
175 | var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree);
|
---|
176 | UpdateModel(simplifiedExpressionTree);
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
|
---|
180 | }
|
---|
181 | }
|
---|