Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Implemented new View, improved functionality (tracking of fragments and operators)

File size: 8.8 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Visualization;
31
32namespace HeuristicLab.EvolutionaryTracking.Views {
33  [View("SymbolicExpressionTreeGenealogyGraph")]
34  [Content(typeof(SymbolicExpressionTreeGenealogyGraph), IsDefaultView = true)]
35  public sealed partial class GenealogyGraphView : ItemView {
36    private VisualSymbolicExpressionTreeNode _selectedVisualSymbolicExpressionTreeNode;
37
38    public new SymbolicExpressionTreeGenealogyGraph Content {
39      get { return (SymbolicExpressionTreeGenealogyGraph)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public GenealogyGraphView()
44      : base() {
45      InitializeComponent();
46      similarityModeSelector.SelectedIndex = 0; // set default similarity mode to "exact"
47    }
48
49    protected override void DeregisterContentEvents() {
50      // TODO: Deregister your event handlers here
51      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
52      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionTreeNodeClicked;
53      base.DeregisterContentEvents();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      // TODO: Register your event handlers here
59      genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
60      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionTreeNodeClicked;
61    }
62
63    #region Event Handlers (Content)
64    // TODO: Put event handlers of the content here
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      if (Content == null)
68        genealogyGraphChart.Graph = null;
69      else {
70        genealogyGraphChart.Graph = Content;
71        var best = Content.Values.OrderByDescending(x => x.Quality).First();
72        symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)best.Data;
73      }
74    }
75
76    private void Content_GraphChanged(object sender, EventArgs e) {
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      genealogyGraphChart.Enabled = Content != null;
82    }
83    #endregion
84
85    #region Event Handlers (child controls)
86
87    // TODO: Put event handlers of child controls here.
88    private void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
89      // the hierarchy goes like this: VisualGenealogyGraphNode --> GenealogyGraphNode --> symbolicExpressionTree
90      var visualGenealogyGraphNode = (VisualGenealogyGraphNode)sender;
91      var genealogyGraphNode = (GenealogyGraphNode)visualGenealogyGraphNode.Data;
92      symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)genealogyGraphNode.Data;
93      if (_selectedVisualSymbolicExpressionTreeNode != null) {
94        var nodes = symbolicExpressionTreeChart.Tree.IterateNodesPostfix().ToArray();
95        var fragments = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode.IterateNodesPostfix().ToArray();
96        int index = SymbolicExpressionTreeGenealogyGraph.FindMatch(nodes, fragments, similarityModeSelector.SelectedIndex);
97        if (index != -1) {
98          _selectedVisualSymbolicExpressionTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(nodes[index + fragments.Count() - 1]);
99          var subtree = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
100          foreach (var visualNode in subtree.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node))) {
101            visualNode.FillColor = Color.LightBlue;
102          }
103          symbolicExpressionTreeChart.Repaint();
104        }
105      }
106      // the cutpoint index represents the index of the node/subtree (when the tree is enumerated in postfix order)
107      // that was changed (by mutation) or received from a parent (by crossover)
108      if (visualGenealogyGraphNode.Data.CutpointIndex != -1) {
109        var gNode = visualGenealogyGraphNode.Data;
110        var symbExprTree = gNode.Data as ISymbolicExpressionTree;
111        if (symbExprTree == null) return;
112        var nodes = symbExprTree.IterateNodesPostfix();
113        var fragment = nodes.ElementAt(gNode.CutpointIndex);
114        if (fragment != null) foreach (var node in fragment.IterateNodesPostfix()) {
115            var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
116            visualNode.FillColor = Color.LightPink;
117          }
118        symbolicExpressionTreeChart.Repaint();
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(SymbolicExpressionTreeGenealogyGraph.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.