Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7800 was 7800, checked in by bburlacu, 11 years ago

#1772: Fixed display of genetic fragments (received via crossover) in the GenealogyGraphView. Added parent, offspring and fragment lengths histogram.

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