Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/GenealogyGraphView.cs @ 8248

Last change on this file since 8248 was 8248, checked in by bburlacu, 12 years ago

#1772: Separated instance-specific attributes of graph node objects from the generic genealogy graph, into metadata objects kept by the specific graph class which corresponds to the specific problem instance (so for instance the SymbolicExpressionTreeGenealogyGraph will keep info about node ranks and qualities etc because that info is specific to symbolic data analysis problems).

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32using HeuristicLab.Visualization;
33
34namespace HeuristicLab.EvolutionaryTracking.Views {
35  [View("SymbolicExpressionTreeGenealogyGraph")]
36  [Content(typeof(SymbolicExpressionTreeGenealogyGraph), IsDefaultView = true)]
37  public sealed partial class GenealogyGraphView : ItemView {
38    private VisualSymbolicExpressionTreeNode _selectedVisualSymbolicExpressionTreeNode;
39
40    public new SymbolicExpressionTreeGenealogyGraph Content {
41      get { return (SymbolicExpressionTreeGenealogyGraph)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public GenealogyGraphView()
46      : base() {
47      InitializeComponent();
48      similarityModeSelector.SelectedIndex = 0; // set default similarity mode to "exact"
49    }
50
51    protected override void DeregisterContentEvents() {
52      // TODO: Deregister your event handlers here
53      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
54      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionTreeNodeClicked;
55      base.DeregisterContentEvents();
56    }
57
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      // TODO: Register your event handlers here
61      genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
62      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionTreeNodeClicked;
63    }
64
65    #region Event Handlers (Content)
66    // TODO: Put event handlers of the content here
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      if (Content == null)
70        genealogyGraphChart.Graph = null;
71      else {
72        genealogyGraphChart.Graph = Content;
73        var best = Content.Values.OrderByDescending(x => genealogyGraphChart.Graph[x].Quality).First();
74        symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)best.Data;
75      }
76    }
77
78    private void Content_GraphChanged(object sender, EventArgs e) {
79    }
80
81    protected override void SetEnabledStateOfControls() {
82      base.SetEnabledStateOfControls();
83      genealogyGraphChart.Enabled = Content != null;
84    }
85    #endregion
86
87    #region Event Handlers (child controls)
88
89    // TODO: Put event handlers of child controls here.
90    private void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
91      // the type hierarchy goes like this: VisualGenealogyGraphNode --> GenealogyGraphNode --> symbolicExpressionTree
92      var visualGenealogyGraphNode = (VisualGenealogyGraphNode)sender;
93      var genealogyGraphNode = (GenealogyGraphNode)visualGenealogyGraphNode.Data;
94      symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)genealogyGraphNode.Data;
95      // highlight the relevant fragment in the symbolic expression tree
96      if (_selectedVisualSymbolicExpressionTreeNode != null) {
97        var nodes = symbolicExpressionTreeChart.Tree.IterateNodesBreadth() as List<ISymbolicExpressionTreeNode>;
98        var fragments = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode.IterateNodesBreadth() as List<ISymbolicExpressionTreeNode>;
99        int index = SymbolicExpressionTreeMatching.FindMatch(nodes, fragments, similarityModeSelector.SelectedIndex);
100        if (index != -1) {
101          _selectedVisualSymbolicExpressionTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(nodes[index]);
102          var subtree = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
103          foreach (var visualNode in subtree.IterateNodesBreadth().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node))) {
104            visualNode.FillColor = Color.LightBlue;
105          }
106          symbolicExpressionTreeChart.Repaint();
107        }
108      }
109      var tree = symbolicExpressionTreeChart.Tree;
110      var graphNode = Content.GetNode(tree);
111      if (graphNode.InEdges != null) {
112        var arc = graphNode.InEdges.Find(a => a.Data != null);
113        if (arc != null) {
114          var fragment = arc.Data as ISymbolicExpressionTreeNode;
115          foreach (var node in fragment.IterateNodesBreadth()) {
116            var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
117            if (visualNode == null)
118              continue;
119            visualNode.FillColor = Color.LightGreen;
120          }
121          symbolicExpressionTreeChart.Repaint();
122        }
123      }
124    }
125
126    private void moveModeButton_CheckedChanged(object sender, EventArgs e) {
127      var btn = (RadioButton)sender;
128      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Move; }
129    }
130
131    private void zoomModeButton_CheckedChanged(object sender, EventArgs e) {
132      var btn = (RadioButton)sender;
133      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Zoom; }
134    }
135
136    private void selectModeButton_CheckedChanged(object sender, EventArgs e) {
137      var btn = (RadioButton)sender;
138      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Select; }
139    }
140
141    private void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
142      _selectedVisualSymbolicExpressionTreeNode = (VisualSymbolicExpressionTreeNode)sender;
143      // find out which individuals in the genealogy graph contain this specific subtree
144      var treeNode = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
145      Color[] colors = { Color.LightSkyBlue, Color.PaleGreen, Color.Tan };
146      // update visual graph nodes
147      genealogyGraphChart.Chart.UpdateEnabled = false;
148      genealogyGraphChart.ClearAllNodes(); // clear node colors
149      // color each graph node according to the degree to which it matches the selected tree fragment
150      foreach (var i in Enum.GetValues(typeof(SymbolicExpressionTreeMatching.SimilarityLevel)).Cast<int>().Reverse()) {
151        var owners = genealogyGraphChart.Graph.TraceFragment(treeNode, i).ToList();
152        if (owners.Any()) genealogyGraphChart.HighlightNodes(owners, colors[i]); // highlight matching individuals from the genealogy
153      }
154      genealogyGraphChart.Chart.UpdateEnabled = true;
155      genealogyGraphChart.Chart.EnforceUpdate();
156
157      // highlight subtree nodes in the tree chart
158      foreach (var visualNode in symbolicExpressionTreeChart.Tree.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
159        visualNode.FillColor = Color.Transparent;
160
161      foreach (var visualNode in treeNode.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
162        visualNode.FillColor = Color.LightBlue;
163
164      // refresh the tree chart
165      symbolicExpressionTreeChart.Repaint();
166    }
167
168    private void similarityModeSelector_SelectedIndexChanged(object sender, EventArgs e) {
169      if (_selectedVisualSymbolicExpressionTreeNode == null) return;
170      var treeNode = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
171      var owners = genealogyGraphChart.Graph.TraceFragment(treeNode, similarityModeSelector.SelectedIndex).ToList();
172      if (owners.Any()) {
173        genealogyGraphChart.Chart.UpdateEnabled = false;
174        genealogyGraphChart.ClearAllNodes(); // clear the fill color of all nodes
175        genealogyGraphChart.HighlightNodes(owners, Color.LightSkyBlue); // highlight matching individuals from the genealogy
176        genealogyGraphChart.Chart.UpdateEnabled = true;
177      }
178      genealogyGraphChart.Chart.EnforceUpdate();
179      // highlight subtree nodes in the tree chart
180      foreach (var visualNode in symbolicExpressionTreeChart.Tree.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
181        visualNode.FillColor = Color.Transparent;
182      foreach (var visualNode in treeNode.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
183        visualNode.FillColor = Color.LightBlue;
184    }
185    #endregion
186
187    private void inDegreeButton_Click(object sender, EventArgs e) {
188      genealogyGraphChart.HighlightInDegree();
189    }
190
191    private void outDegreeButton_Click(object sender, EventArgs e) {
192      genealogyGraphChart.HighlightOutDegree();
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.