Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Fixed a small bug in the TracingSymbolicExpressionTreeCrossover. Fixed bug in fragment matching code.

File size: 8.6 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]);
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          foreach (var node in fragment.IterateNodesBreadth()) {
114            var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
115            visualNode.FillColor = Color.LightGreen;
116          }
117          symbolicExpressionTreeChart.Repaint();
118        }
119      }
120    }
121
122    private void moveModeButton_CheckedChanged(object sender, EventArgs e) {
123      var btn = (RadioButton)sender;
124      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Move; }
125    }
126
127    private void zoomModeButton_CheckedChanged(object sender, EventArgs e) {
128      var btn = (RadioButton)sender;
129      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Zoom; }
130    }
131
132    private void selectModeButton_CheckedChanged(object sender, EventArgs e) {
133      var btn = (RadioButton)sender;
134      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Select; }
135    }
136
137    private void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
138      _selectedVisualSymbolicExpressionTreeNode = (VisualSymbolicExpressionTreeNode)sender;
139      // find out which individuals in the genealogy graph contain this specific subtree
140      var treeNode = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
141      Color[] colors = { Color.LightSkyBlue, Color.PaleGreen, Color.Tan };
142
143      genealogyGraphChart.ClearAllNodes(); // clear node colors
144      // color each graph node according to the degree to which it matches the selected tree fragment
145      foreach (var i in Enum.GetValues(typeof(SymbolicExpressionTreeMatching.SimilarityLevel)).Cast<int>().Reverse()) {
146        var owners = genealogyGraphChart.Graph.TraceFragment(treeNode, i).ToList();
147        if (owners.Any()) genealogyGraphChart.HighlightNodes(owners, colors[i]); // highlight matching individuals from the genealogy
148      }
149
150      // highlight subtree nodes in the tree chart
151      foreach (var visualNode in symbolicExpressionTreeChart.Tree.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
152        visualNode.FillColor = Color.Transparent;
153
154      foreach (var visualNode in treeNode.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
155        visualNode.FillColor = Color.LightBlue;
156
157      // refresh the tree chart
158      symbolicExpressionTreeChart.Repaint();
159    }
160
161    private void similarityModeSelector_SelectedIndexChanged(object sender, EventArgs e) {
162      if (_selectedVisualSymbolicExpressionTreeNode == null) return;
163      var treeNode = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
164      var owners = genealogyGraphChart.Graph.TraceFragment(treeNode, similarityModeSelector.SelectedIndex).ToList();
165      if (owners.Any()) {
166        genealogyGraphChart.ClearAllNodes(); // clear the fill color of all nodes
167        genealogyGraphChart.HighlightNodes(owners, Color.LightSkyBlue); // highlight matching individuals from the genealogy
168      }
169      // highlight subtree nodes in the tree chart
170      foreach (var visualNode in symbolicExpressionTreeChart.Tree.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
171        visualNode.FillColor = Color.Transparent;
172      foreach (var visualNode in treeNode.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
173        visualNode.FillColor = Color.LightBlue;
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.