Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/LineageExplorerView.cs @ 9250

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

#1772: Updated LineageExplorerView and added functionality for highlighting common fragments in a given tree. Fixed view layout and element anchoring.

File size: 7.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Windows.Forms;
6using HeuristicLab.Common;
7using HeuristicLab.Core.Views;
8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
9using HeuristicLab.MainForm;
10using HeuristicLab.Problems.DataAnalysis.Symbolic;
11
12namespace HeuristicLab.EvolutionaryTracking.Views {
13  [View("LineageExplorerView")]
14  [Content(typeof(LineageExplorer), IsDefaultView = false)]
15  public sealed partial class LineageExplorerView : ItemView {
16    private Dictionary<TreeNode, ISymbolicExpressionTree> treeMap;
17    private List<SymbolicExpressionGenealogyGraphNode> lineage;
18
19    public new LineageExplorer Content {
20      get { return (LineageExplorer)base.Content; }
21      set { base.Content = value; }
22    }
23
24    public LineageExplorerView() {
25      InitializeComponent();
26      treeMap = new Dictionary<TreeNode, ISymbolicExpressionTree>();
27    }
28
29    protected override void DeregisterContentEvents() {
30      // TODO: Deregister your event handlers here
31      base.DeregisterContentEvents();
32    }
33
34    protected override void RegisterContentEvents() {
35      base.RegisterContentEvents();
36      // TODO: Register your event handlers here
37    }
38
39    #region Event Handlers (Content)
40    // TODO: Put event handlers of the content here
41    #endregion
42
43    protected override void OnContentChanged() {
44      base.OnContentChanged();
45      if (Content == null) {
46      } else {
47        var tuples = Content.Trees.OrderByDescending(x => x.Item2).ToList();
48        scopeListTreeView.Nodes.AddRange(tuples.Select(x => new TreeNode(x.Item2.ToString())).ToArray());
49
50        for (int i = 0; i != tuples.Count; ++i) {
51          treeMap.Add(scopeListTreeView.Nodes[i], tuples[i].Item1);
52          double quality = tuples[i].Item2;
53          int colorIndex = (int)Math.Floor(quality * ColorGradient.Colors.Count);
54          var color = ColorGradient.Colors[colorIndex];
55          scopeListTreeView.Nodes[i].ForeColor = color;
56        }
57      }
58    }
59
60    protected override void SetEnabledStateOfControls() {
61      base.SetEnabledStateOfControls();
62      // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
63    }
64
65    private void scopeListTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
66      var treeNode = scopeListTreeView.SelectedNode;
67      var symbExprTree = treeMap[treeNode];
68      HighlightFragment(symbExprTree);
69
70      var graphNode = Content.GenealogyGraph.GetGraphNodes(symbExprTree).First();
71      lineage = lineage ?? new List<SymbolicExpressionGenealogyGraphNode>();
72      lineage.Clear();
73      lineage.Add(graphNode);
74
75      var gn = graphNode;
76      while (gn.InEdges != null && gn.InEdges.Count != 0) {
77        gn = (SymbolicExpressionGenealogyGraphNode)gn.InEdges[0].Source;
78        lineage.Add(gn);
79      }
80      lineage.Reverse(); // incresing order by generation
81
82      // update the righthand side tree view
83      qualityImprovementTreeView.Nodes.Clear();
84      for (int i = 0; i != lineage.Count; ++i) {
85        var node = new TreeNode(string.Format("Generation {0}", lineage[i].Rank));
86        qualityImprovementTreeView.Nodes.Add(node);
87        var quality = lineage[i].Quality;
88        int colorIndex = (int)Math.Floor(quality * ColorGradient.Colors.Count);
89        var color = ColorGradient.Colors[colorIndex];
90        node.ForeColor = color;
91      }
92    }
93
94    /// <summary>
95    /// Given a symbolic expression tree, find the corresponding vertex in the genealogy graph, and highlight the fragment
96    /// that is saved as a data member in the incoming arc from the parent in the previous generation
97    /// </summary>
98    /// <param name="symbExprTree"></param>
99    private void HighlightFragment(ISymbolicExpressionTree symbExprTree) {
100      // update the symbolic expression tree chart with the new selected tree:
101      symbolicExpressionTreeChart.SuspendRepaint = true;
102      symbolicExpressionTreeChart.Tree = symbExprTree;
103      var matchingNodes = Content.GenealogyGraph.GetGraphNodes(symbExprTree);
104      SymbolicExpressionGenealogyGraphNode graphNode = matchingNodes.First();
105      if (graphNode.InEdges == null) {
106        symbolicExpressionTreeChart.SuspendRepaint = false;
107        symbolicExpressionTreeChart.Repaint();
108        return;
109      }
110      var fragment = (IFragment)graphNode.InEdges[0].Data;
111      if (fragment.Root == null) {
112        symbolicExpressionTreeChart.SuspendRepaint = false;
113        symbolicExpressionTreeChart.Repaint();
114        return;
115      }
116      foreach (var node in fragment.Root.IterateNodesBreadth()) {
117        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
118        visualNode.LineColor = Color.DodgerBlue;
119      }
120      symbolicExpressionTreeChart.SuspendRepaint = false;
121      symbolicExpressionTreeChart.Repaint();
122    }
123
124    private void qualityImprovementTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
125      var tree = lineage[qualityImprovementTreeView.SelectedNode.Index].SymbolicExpressionTree;
126      HighlightFragment(tree);
127    }
128
129    private void calculateFragmentFrequencyButton_Click(object sender, EventArgs e) {
130      // the individual can occur multiple times in the graph if it is(was) elite. we take the first occurence.
131      var graphNode = Content.GenealogyGraph.GetGraphNodes(symbolicExpressionTreeChart.Tree).First();
132      var fragments = graphNode.SymbolicExpressionTree.IterateNodesBreadth().Select(n => new Fragment(n));
133      var trees = Content.Trees.Select(t => t.Item1).ToList();
134      var hashset = new HashSet<ISymbolicExpressionTree>();
135
136      var similarityComparer = new SymbolicExpressionTreeNodeSimilarityComparer {
137        MatchConstantValues = false,
138        MatchVariableNames = true,
139        MatchVariableWeights = false
140      };
141
142      symbolicExpressionTreeChart.SuspendRepaint = true;
143      foreach (var f in fragments) {
144        double freq = 0;
145        foreach (var t in trees) {
146          if (t.Root.ContainsFragment(f, similarityComparer)) ++freq;
147          int index = (int)Math.Floor(freq * ColorGradient.Colors.Count / trees.Count);
148          if (index == ColorGradient.Colors.Count) index--;
149          var color = ColorGradient.Colors[index];
150          foreach (var node in f.Root.IterateNodesBreadth()) {
151            var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
152            visualNode.FillColor = Color.FromArgb(200, color);
153          }
154        }
155      }
156      symbolicExpressionTreeChart.SuspendRepaint = false;
157      symbolicExpressionTreeChart.Repaint();
158    }
159
160    private void symbolicExpressionTreeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
161    }
162
163    private void showGenealogyButton_Click(object sender, EventArgs e) {
164      var tree = symbolicExpressionTreeChart.Tree;
165      if (tree == null) return;
166      var graphNode = Content.GenealogyGraph.GetGraphNodes(tree).First();
167
168      var graph = new SymbolicExpressionTreeGenealogyGraph();
169      var ancestors = graphNode.Ancestors();
170      foreach (var ancestorNode in ancestors) {
171        graph.AddNode(ancestorNode);
172      }
173      using (var dialog = new GenealogyGraphDialog { Graph = graph }) {
174        dialog.ShowDialog(this);
175      }
176    }
177
178    #region Event Handlers (child controls)
179    // TODO: Put event handlers of child controls here.
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.