Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs @ 10884

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

#1772: Simplified genealogy graph and fragment graph creation:

  • the genealogy graph now has a 1-1 mapping between content and vertices (as opposed to 1-n as it was previously, to account for elites); this required changes to the directed graph implementation
  • the fragment graph only contains bifurcation points (where the subtree contains the fragment so tracing must be done both ways (in the root parent AND in the other parent). in the other cases, tracing is restarted from the parent genealogy graph node.
File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
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.EvolutionTracking;
30using HeuristicLab.EvolutionTracking.Views;
31using HeuristicLab.MainForm;
32
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
35  [View("SymboldDataAnalysisGenealogyView")]
36  [Content(typeof(IGenealogyGraph<ISymbolicExpressionTree>), IsDefaultView = true)]
37  public partial class SymboldDataAnalysisGenealogyView : ItemView {
38    private readonly ISymbolicExpressionTreeNodeSimilarityComparer comparer;
39
40    public SymboldDataAnalysisGenealogyView() {
41      InitializeComponent();
42
43      comparer = new SymbolicExpressionTreeNodeSimilarityComparer();
44    }
45
46    public new IGenealogyGraph<ISymbolicExpressionTree> Content {
47      get { return (IGenealogyGraph<ISymbolicExpressionTree>)base.Content; }
48      set { base.Content = value; }
49    }
50
51    #region event handlers
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55      if (Content != null) {
56        genealogyGraphChart.GenealogyGraph = Content;
57      }
58    }
59
60    #endregion
61
62    protected override void RegisterContentEvents() {
63      genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
64      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionNodeClicked;
65      base.RegisterContentEvents();
66    }
67
68    protected override void DeregisterContentEvents() {
69      base.DeregisterContentEvents();
70      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
71      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionNodeClicked;
72    }
73
74    public void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs args) {
75      var visualNode = (VisualGenealogyGraphNode)sender;
76      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data;
77      var tree = graphNode.Content;
78      symbolicExpressionTreeChart.Tree = tree;
79
80      if (graphNode.InArcs.Any()) {
81        var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
82        if (fragment != null) {
83          treeChart_HighlightSubtree(graphNode.Content.NodeAt(fragment.Index1));
84        }
85      }
86    }
87
88    public void treeChart_SymbolicExpressionNodeClicked(object sender, MouseEventArgs args) {
89      var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender;
90      var subtree = visualNode.Content;
91
92      // highlight the selected subtree inside the displayed tree on the right hand side
93      treeChart_ClearColors();
94      treeChart_HighlightSubtree(subtree);
95
96      bool trace = genealogyGraphChart.TraceFragments; // check whether the mode is 'trace' or 'match'
97
98      if (trace) {
99        // perform fragment tracing
100        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
101        var subtreeIndex = graphNode.Content.IterateNodesPrefix().ToList().IndexOf(subtree);
102        var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex);
103        if (fragmentGraph.Nodes.Any()) {
104          MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen
105        }
106      } else {
107        // perform matching like it was done before
108        // currently there is no possibility to specify the subtree matching criteria
109        var trees = Content.Nodes.Select(x => x.Content);
110        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
111
112        var matchingVertices = matchingTrees.Select(x => Content[x]).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>();
113        graphChart_highlightMatchingVertices(matchingVertices);
114      }
115    }
116
117    private void graphChart_highlightMatchingVertices(IEnumerable<IGenealogyGraphNode> vertices) {
118      genealogyGraphChart.Chart.UpdateEnabled = false;
119      genealogyGraphChart.ClearPrimitives();
120      genealogyGraphChart.HighlightNodes(vertices);
121      genealogyGraphChart.Chart.UpdateEnabled = true;
122      genealogyGraphChart.Chart.EnforceUpdate();
123    }
124
125    private void treeChart_ClearColors() {
126      foreach (var node in symbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
127        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
128        if (visualNode != null) {
129          visualNode.LineColor = Color.Black;
130          visualNode.FillColor = Color.Transparent;
131        }
132      }
133      symbolicExpressionTreeChart.RepaintNodes();
134    }
135
136    private void treeChart_HighlightSubtree(ISymbolicExpressionTreeNode subtree) {
137      foreach (var s in subtree.IterateNodesPrefix()) {
138        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(s);
139        visualNode.LineColor = Color.RoyalBlue;
140        visualNode.FillColor = Color.LightBlue;
141
142        foreach (var c in s.Subtrees) {
143          var visualArc = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(s, c);
144          visualArc.LineColor = Color.RoyalBlue;
145        }
146      }
147      symbolicExpressionTreeChart.RepaintNodes();
148    }
149
150    #region events for configuring the behavior of the genealogy chart (trace/match, simple lineages, etc)
151    private void trace_checkBox_CheckedChanged(object sender, System.EventArgs e) {
152      genealogyGraphChart.TraceFragments = trace_checkBox.Checked;
153    }
154
155    private void simpleLineages_checkBox_CheckedChanged(object sender, System.EventArgs e) {
156      genealogyGraphChart.SimpleLineages = simpleLineages_checkBox.Checked;
157    }
158
159    private void lockGraph_checkBox_CheckedChanged(object sender, System.EventArgs e) {
160      genealogyGraphChart.LockGenealogy = lockGraph_checkBox.Checked;
161    }
162    #endregion
163  }
164}
Note: See TracBrowser for help on using the repository browser.