1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core.Views;
|
---|
8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
|
---|
10 | using HeuristicLab.MainForm;
|
---|
11 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.EvolutionaryTracking.Views {
|
---|
14 | [View("LineageExplorerView")]
|
---|
15 | [Content(typeof(LineageExplorer), IsDefaultView = false)]
|
---|
16 | public sealed partial class LineageExplorerView : ItemView {
|
---|
17 | private Dictionary<TreeNode, ISymbolicExpressionTree> treeMap;
|
---|
18 | private List<SymbolicExpressionGenealogyGraphNode> lineage;
|
---|
19 |
|
---|
20 | private Dictionary<ISymbolicExpressionTree, Dictionary<ISymbolicExpressionTreeNode, double>> fragmentFrequencies;
|
---|
21 |
|
---|
22 | public new LineageExplorer Content {
|
---|
23 | get { return (LineageExplorer)base.Content; }
|
---|
24 | set { base.Content = value; }
|
---|
25 | }
|
---|
26 |
|
---|
27 | public LineageExplorerView() {
|
---|
28 | InitializeComponent();
|
---|
29 | treeMap = new Dictionary<TreeNode, ISymbolicExpressionTree>();
|
---|
30 | }
|
---|
31 |
|
---|
32 | protected override void DeregisterContentEvents() {
|
---|
33 | // TODO: Deregister your event handlers here
|
---|
34 | base.DeregisterContentEvents();
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected override void RegisterContentEvents() {
|
---|
38 | base.RegisterContentEvents();
|
---|
39 | // TODO: Register your event handlers here
|
---|
40 | }
|
---|
41 |
|
---|
42 | #region Event Handlers (Content)
|
---|
43 | // TODO: Put event handlers of the content here
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | protected override void OnContentChanged() {
|
---|
47 | base.OnContentChanged();
|
---|
48 | if (Content == null) {
|
---|
49 | } else {
|
---|
50 | var tuples = Content.Trees.OrderByDescending(x => x.Item2).ToList();
|
---|
51 | scopeListTreeView.Nodes.AddRange(tuples.Select(x => new TreeNode(x.Item2.ToString())).ToArray());
|
---|
52 |
|
---|
53 | for (int i = 0; i != tuples.Count; ++i) {
|
---|
54 | treeMap.Add(scopeListTreeView.Nodes[i], tuples[i].Item1);
|
---|
55 | double quality = tuples[i].Item2;
|
---|
56 | int colorIndex = (int)Math.Floor(quality * ColorGradient.Colors.Count);
|
---|
57 | var color = ColorGradient.Colors[colorIndex];
|
---|
58 | scopeListTreeView.Nodes[i].ForeColor = color;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | karyograph1.Trees = Content.Trees.OrderByDescending(x => x.Item2).Select(x => x.Item1).ToList();
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected override void SetEnabledStateOfControls() {
|
---|
66 | base.SetEnabledStateOfControls();
|
---|
67 | // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void scopeListTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
71 | var treeNode = scopeListTreeView.SelectedNode;
|
---|
72 | var symbExprTree = treeMap[treeNode];
|
---|
73 |
|
---|
74 | fragmentFrequencies = fragmentFrequencies ?? new Dictionary<ISymbolicExpressionTree, Dictionary<ISymbolicExpressionTreeNode, double>>();
|
---|
75 |
|
---|
76 | if (fragmentFrequencies.ContainsKey(symbExprTree) && false) {
|
---|
77 | // colorize tree nodes according to fragment frequencies
|
---|
78 | symbolicExpressionTreeChart.SuspendRepaint = true;
|
---|
79 | var frequencies = fragmentFrequencies[symbExprTree];
|
---|
80 |
|
---|
81 | foreach (var node in symbExprTree.IterateNodesBreadth()) {
|
---|
82 | double freq = frequencies[node];
|
---|
83 | int index = (int)Math.Floor(freq * ColorGradient.Colors.Count / scopeListTreeView.GetNodeCount(false));
|
---|
84 | if (index == ColorGradient.Colors.Count) index--;
|
---|
85 | var color = ColorGradient.Colors[index];
|
---|
86 | var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
|
---|
87 | visualNode.FillColor = Color.FromArgb(200, color);
|
---|
88 | }
|
---|
89 | symbolicExpressionTreeChart.SuspendRepaint = false;
|
---|
90 | symbolicExpressionTreeChart.Repaint();
|
---|
91 | }
|
---|
92 | // highlight crossover fragment
|
---|
93 | HighlightFragment(symbExprTree);
|
---|
94 |
|
---|
95 | var graphNode = Content.GenealogyGraph.GetGraphNodes(symbExprTree).Last();
|
---|
96 | lineage = lineage ?? new List<SymbolicExpressionGenealogyGraphNode>();
|
---|
97 | lineage.Clear();
|
---|
98 | lineage.Add(graphNode);
|
---|
99 |
|
---|
100 | var gn = graphNode;
|
---|
101 | while (gn.InEdges != null && gn.InEdges.Count != 0) {
|
---|
102 | gn = (SymbolicExpressionGenealogyGraphNode)gn.InEdges[0].Source;
|
---|
103 | lineage.Add(gn);
|
---|
104 | }
|
---|
105 | lineage.Reverse(); // increasing order by generation
|
---|
106 |
|
---|
107 | // update the righthand side tree view
|
---|
108 | qualityImprovementTreeView.Nodes.Clear();
|
---|
109 | for (int i = 0; i != lineage.Count; ++i) {
|
---|
110 | var node = new TreeNode(string.Format("Generation {0}", lineage[i].Rank));
|
---|
111 | qualityImprovementTreeView.Nodes.Add(node);
|
---|
112 | var quality = lineage[i].Quality;
|
---|
113 | int colorIndex = (int)Math.Floor(quality * ColorGradient.Colors.Count);
|
---|
114 | var color = ColorGradient.Colors[colorIndex];
|
---|
115 | node.ForeColor = color;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /// <summary>
|
---|
120 | /// Given a symbolic expression tree, find the corresponding vertex in the genealogy graph, and highlight the fragment
|
---|
121 | /// that is saved as a data member in the incoming arc from the parent in the previous generation
|
---|
122 | /// </summary>
|
---|
123 | /// <param name="symbExprTree"></param>
|
---|
124 | private void HighlightFragment(ISymbolicExpressionTree symbExprTree) {
|
---|
125 | // update the symbolic expression tree chart with the new selected tree:
|
---|
126 | symbolicExpressionTreeChart.SuspendRepaint = true;
|
---|
127 | symbolicExpressionTreeChart.Tree = symbExprTree;
|
---|
128 | var matchingNodes = Content.GenealogyGraph.GetGraphNodes(symbExprTree);
|
---|
129 | SymbolicExpressionGenealogyGraphNode graphNode = matchingNodes.First();
|
---|
130 | if (graphNode.InEdges == null) {
|
---|
131 | symbolicExpressionTreeChart.SuspendRepaint = false;
|
---|
132 | symbolicExpressionTreeChart.Repaint();
|
---|
133 | return;
|
---|
134 | }
|
---|
135 | var fragment = graphNode.InEdges.Count == 1 ? (IFragment)graphNode.InEdges[0].Data : (IFragment)graphNode.InEdges[1].Data;
|
---|
136 | if (fragment.Root == null) {
|
---|
137 | symbolicExpressionTreeChart.SuspendRepaint = false;
|
---|
138 | symbolicExpressionTreeChart.Repaint();
|
---|
139 | return;
|
---|
140 | }
|
---|
141 | foreach (var node in fragment.Root.IterateNodesBreadth()) {
|
---|
142 | var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
|
---|
143 | visualNode.LineColor = Color.DodgerBlue;
|
---|
144 | }
|
---|
145 | symbolicExpressionTreeChart.SuspendRepaint = false;
|
---|
146 | symbolicExpressionTreeChart.Repaint();
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void qualityImprovementTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
150 | var tree = lineage[qualityImprovementTreeView.SelectedNode.Index].SymbolicExpressionTree;
|
---|
151 | HighlightFragment(tree);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void calculateFragmentFrequencyButton_Click(object sender, EventArgs e) {
|
---|
155 | // the individual can occur multiple times in the graph if it is(was) elite. we take the first occurence.
|
---|
156 | var graphNode = Content.GenealogyGraph.GetGraphNodes(symbolicExpressionTreeChart.Tree).First();
|
---|
157 | var trees = Content.GenealogyGraph.Nodes.Where(x => x.Rank.IsAlmost(graphNode.Rank)).Select(x => x.SymbolicExpressionTree).ToList();
|
---|
158 |
|
---|
159 | var fragments = graphNode.SymbolicExpressionTree.IterateNodesBreadth().Select(n => new Fragment { Root = n });
|
---|
160 |
|
---|
161 | var similarityComparer = new SymbolicExpressionTreeNodeSimilarityComparer {
|
---|
162 | MatchConstantValues = false,
|
---|
163 | MatchVariableNames = true,
|
---|
164 | MatchVariableWeights = false
|
---|
165 | };
|
---|
166 |
|
---|
167 | var canonicalSorter = new SymbolicExpressionTreeCanonicalSorter();
|
---|
168 |
|
---|
169 | fragmentFrequencies = fragmentFrequencies ?? new Dictionary<ISymbolicExpressionTree, Dictionary<ISymbolicExpressionTreeNode, double>>();
|
---|
170 |
|
---|
171 | if (!fragmentFrequencies.ContainsKey(graphNode.SymbolicExpressionTree)) {
|
---|
172 | fragmentFrequencies[graphNode.SymbolicExpressionTree] = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
173 | }
|
---|
174 |
|
---|
175 | fragmentFrequencies[graphNode.SymbolicExpressionTree].Clear();
|
---|
176 |
|
---|
177 | symbolicExpressionTreeChart.SuspendRepaint = true;
|
---|
178 | foreach (var f in fragments) {
|
---|
179 | double freq = 0;
|
---|
180 |
|
---|
181 | foreach (var t in trees) {
|
---|
182 | if (t.Root.ContainsFragment(f, similarityComparer)) ++freq;
|
---|
183 | }
|
---|
184 |
|
---|
185 | fragmentFrequencies[graphNode.SymbolicExpressionTree].Add(f.Root, freq);
|
---|
186 |
|
---|
187 | // colorize tree nodes according to fragment frequencies
|
---|
188 | int index = (int)Math.Floor(freq * ColorGradient.Colors.Count / trees.Count);
|
---|
189 | if (index == ColorGradient.Colors.Count) index--;
|
---|
190 | var color = ColorGradient.Colors[index];
|
---|
191 | foreach (var node in f.Root.IterateNodesBreadth()) {
|
---|
192 | var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
|
---|
193 | visualNode.FillColor = Color.FromArgb(200, color);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | symbolicExpressionTreeChart.SuspendRepaint = false;
|
---|
198 | symbolicExpressionTreeChart.Repaint();
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void symbolicExpressionTreeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
|
---|
202 | }
|
---|
203 |
|
---|
204 | private void showGenealogyButton_Click(object sender, EventArgs e) {
|
---|
205 | var tree = symbolicExpressionTreeChart.Tree;
|
---|
206 | if (tree == null) return;
|
---|
207 | var graphNode = Content.GenealogyGraph.GetGraphNodes(tree).First();
|
---|
208 |
|
---|
209 | var graph = new SymbolicExpressionTreeGenealogyGraph();
|
---|
210 | var ancestors = graphNode.Ancestors();
|
---|
211 | foreach (var ancestorNode in ancestors) {
|
---|
212 | graph.AddNode(ancestorNode);
|
---|
213 | }
|
---|
214 | using (var dialog = new GenealogyGraphDialog { Graph = graph }) {
|
---|
215 | dialog.ShowDialog(this);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | private void button1_Click(object sender, EventArgs e) {
|
---|
220 | using (var dialog = new FrequentFragmentsDialog()) {
|
---|
221 | var panel = new Panel { Dock = DockStyle.Left, Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom };
|
---|
222 |
|
---|
223 | panel.Controls.Add(new SymbolicExpressionTreeChart() { Tree = treeMap.Values.First() });
|
---|
224 | panel.Controls.Add(new Label { Text = "Fragment frequency." });
|
---|
225 |
|
---|
226 | dialog.fragmentsPanel.Controls.Add(panel);
|
---|
227 |
|
---|
228 | dialog.ShowDialog(this);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | private void showFrequentFragmentsButton_Click(object sender, EventArgs e) {
|
---|
233 | int bestN;
|
---|
234 | bool success = int.TryParse(bestNTextBox.Text, out bestN);
|
---|
235 | if (!success) bestN = 10;
|
---|
236 | var tuples = Content.Trees.Select(t => new { Tree = t.Item1, Quality = t.Item2 }).OrderByDescending(x => x.Quality).ToList();
|
---|
237 | var bestTrees = tuples.Take(bestN).Select(x => x.Tree).ToList();
|
---|
238 |
|
---|
239 | var similarityComparer = new SymbolicExpressionTreeNodeSimilarityComparer {
|
---|
240 | MatchConstantValues = false,
|
---|
241 | MatchVariableNames = true,
|
---|
242 | MatchVariableWeights = false
|
---|
243 | };
|
---|
244 |
|
---|
245 | var fragments = bestTrees.SelectMany(t => t.IterateNodesBreadth()).Where(n => n.GetLength() >= 3).Distinct(similarityComparer).Select(n => new Fragment { Root = n });
|
---|
246 |
|
---|
247 | double bestNQuality = tuples.Take(bestN).Sum(x => x.Quality); // average quality of best N individuals in the population
|
---|
248 |
|
---|
249 | using (var dialog = new FrequentFragmentsDialog()) {
|
---|
250 | foreach (var fragment in fragments.Take(20)) {
|
---|
251 | double avgQuality = bestNQuality;
|
---|
252 | double frequency = bestN;
|
---|
253 | foreach (var t in tuples.Skip(bestN)) {
|
---|
254 | var tree = t.Tree;
|
---|
255 | if (tree.Root.ContainsFragment(fragment, similarityComparer)) {
|
---|
256 | avgQuality += t.Quality;
|
---|
257 | frequency++;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | if (frequency > 0.2) {
|
---|
261 | var panel = new Panel {
|
---|
262 | Dock = DockStyle.Left,
|
---|
263 | Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom
|
---|
264 | };
|
---|
265 |
|
---|
266 | panel.Controls.Add(new SymbolicExpressionTreeChart() { Tree = treeMap.Values.First() });
|
---|
267 | panel.Controls.Add(new Label { Text = "Fragment frequency." });
|
---|
268 |
|
---|
269 | dialog.fragmentsPanel.Controls.Add(panel);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | dialog.ShowDialog(this);
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | #region Event Handlers (child controls)
|
---|
277 | // TODO: Put event handlers of child controls here.
|
---|
278 | #endregion
|
---|
279 | }
|
---|
280 | }
|
---|