Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/FragmentGraphView.cs @ 11638

Last change on this file since 11638 was 11638, checked in by bburlacu, 9 years ago

#1772: Merged trunk changes. Updated PhenotypicSimilarityCalculator, updated FragmentGraphView.

File size: 8.7 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;
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.Problems.DataAnalysis.Symbolic.Views {
34  [View("FragmentGraphView")]
35  [Content(typeof(FragmentGraph), IsDefaultView = true)]
36  public sealed partial class FragmentGraphView : ItemView {
37    private const int PreferredHorizontalSpacing = 10;
38    private const int PreferredVerticalSpacing = 25;
39
40    private readonly ReingoldTilfordLayoutEngine<TileLayoutNode> layoutEngine;
41    private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> symbolicExpressionEngine;
42
43    private readonly Dictionary<FragmentNode, TileLayoutNode> nodeToTileMap;
44
45    private SymbolicExpressionTreeTile Root { get; set; }
46
47    public new FragmentGraph Content {
48      get { return (FragmentGraph)base.Content; }
49      set { base.Content = value; }
50    }
51
52    public FragmentGraphView() {
53      InitializeComponent();
54
55      layoutEngine = new ReingoldTilfordLayoutEngine<TileLayoutNode>(n => n.Children) {
56        HorizontalSpacing = PreferredHorizontalSpacing,
57        VerticalSpacing = PreferredVerticalSpacing,
58      };
59      symbolicExpressionEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
60        HorizontalSpacing = PreferredHorizontalSpacing,
61        VerticalSpacing = PreferredVerticalSpacing,
62        NodeWidth = 80,
63        NodeHeight = 40
64      };
65      nodeToTileMap = new Dictionary<FragmentNode, TileLayoutNode>();
66    }
67
68    private void MakeTiles() {
69      var chart = symbolicExpressionChartControl.Chart;
70      nodeToTileMap.Clear();
71      foreach (var node in Content.Vertices) {
72        var tile = new SymbolicExpressionTreeTile(chart);
73        tile.LayoutEngine = symbolicExpressionEngine;
74        tile.Label = "Generation " + node.Rank + Environment.NewLine +
75                     "Quality " + String.Format("{0:0.000}", node.Quality);
76        tile.Root = node.Data.Root;
77        var tileNode = new TileLayoutNode { Tile = tile };
78        nodeToTileMap.Add(node, tileNode);
79      }
80      foreach (var node in Content.Vertices.Where(n => n.OutArcs.Any())) {
81        var layoutNode = nodeToTileMap[node];
82        layoutNode.Children = new List<TileLayoutNode>(node.OutArcs.Select(a => nodeToTileMap[(FragmentNode)a.Target]));
83      }
84    }
85
86    private void Draw() {
87      var chart = symbolicExpressionChartControl.Chart;
88      var nodes = Content.Vertices.ToList();
89      var root = nodes[0];
90      var fragmentRoot = nodeToTileMap[root];
91      int maxTileWidth = 0, maxTileHeight = 0;
92      var tiles = nodes.Select(x => nodeToTileMap[x].Tile).ToList();
93
94      foreach (var tile in tiles) {
95        var size = tile.Size;
96        if (maxTileWidth < size.Width) maxTileWidth = size.Width;
97        if (maxTileHeight < size.Height) maxTileHeight = size.Height;
98      }
99      layoutEngine.NodeWidth = maxTileWidth;
100      layoutEngine.NodeHeight = maxTileHeight;
101      layoutEngine.HorizontalSpacing = PreferredHorizontalSpacing;
102      layoutEngine.VerticalSpacing = PreferredVerticalSpacing;
103
104      var visualNodes = layoutEngine.CalculateLayout(fragmentRoot);
105
106      symbolicExpressionChartControl.UpdateEnabled = false;
107      foreach (var visualNode in visualNodes) {
108        var tile = visualNode.Content.Tile;
109        tile.Position = new Point(visualNode.X, visualNode.Y);
110        symbolicExpressionChartControl.Add(tile);
111      }
112
113      // add connections between the tiles
114      foreach (var node in nodes) {
115        var aTile = nodeToTileMap[node].Tile;
116        var aSize = aTile.Size;
117        var aPos = aTile.Position;
118        var tree = node.Data;
119
120        if (node.SubtreeIndex > 0) {
121          var subtree = tree.Root.NodeAt(node.SubtreeIndex);
122          foreach (var s in subtree.IterateNodesPrefix()) {
123            var primitive = aTile.GetPrimitive(s);
124            if (primitive != null) {
125              var rpb = primitive as RectangularPrimitiveBase;
126              if (rpb != null) {
127                rpb.Pen = new Pen(Color.Black);
128              }
129            }
130          }
131        }
132        if (node.FragmentIndex > 0) {
133          var subtree = tree.Root.NodeAt(node.FragmentIndex);
134          foreach (var s in subtree.IterateNodesPrefix()) {
135            var primitive = aTile.GetPrimitive(s);
136            if (primitive != null) {
137              var rpb = primitive as RectangularPrimitiveBase;
138              if (rpb != null) {
139                rpb.Brush = new SolidBrush(Color.LightGray);
140              }
141            }
142          }
143        }
144
145        if (node.InArcs.Any()) {
146          var parent = (FragmentNode)node.InArcs.First().Source;
147          if (parent.OutArcs.First().Target == node) {
148            var index = node.SubtreeIndex + (parent.FragmentIndex - parent.SubtreeIndex);
149            // some mutations create discontinuities which invalidate the index
150            if (index >= 0 && index < tree.Length) {
151              var subtree = tree.NodeAt(index);
152              var primitive = aTile.GetPrimitive(subtree);
153              primitive.Brush = new SolidBrush(Color.LightCoral);
154            }
155          }
156        }
157
158        foreach (var child in node.OutArcs.Select(x => (FragmentNode)x.Target)) {
159          var bTile = nodeToTileMap[child].Tile;
160          var bSize = bTile.Size;
161          var bPos = bTile.Position;
162
163          var line = new Line(chart, new PointD(aPos.X + aSize.Width / 2.0, aPos.Y + aSize.Height), new PointD(bPos.X + bSize.Width / 2.0, bPos.Y)) {
164            Pen = Pens.DimGray
165          };
166          symbolicExpressionChartControl.Add(line);
167        }
168      }
169      // center display on the root of the fragment graph
170      symbolicExpressionChartControl.Chart.Move(nodeToTileMap[root].Tile.Position.X, nodeToTileMap[root].Tile.Position.Y);
171      symbolicExpressionChartControl.UpdateEnabled = true;
172      symbolicExpressionChartControl.EnforceUpdate();
173    }
174
175    protected override void DeregisterContentEvents() {
176      // TODO: Deregister your event handlers here
177      base.DeregisterContentEvents();
178    }
179
180    protected override void RegisterContentEvents() {
181      base.RegisterContentEvents();
182      // TODO: Register your event handlers here
183    }
184
185    #region Event Handlers (Content)
186    // TODO: Put event handlers of the content here
187    protected override void OnContentChanged() {
188      base.OnContentChanged();
189      if (Content != null) {
190        MakeTiles();
191        Draw();
192      }
193    }
194    #endregion
195
196    protected override void SetEnabledStateOfControls() {
197      base.SetEnabledStateOfControls();
198      // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
199    }
200
201    private void FragmentGraphView_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
202      if (e.KeyChar == 'i') {
203        // highlight node impacts
204      }
205    }
206
207    #region Event Handlers (child controls)
208
209    // TODO: Put event handlers of child controls here.
210
211    #endregion
212  }
213
214  internal static class Util {
215    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) {
216      return NodeAt(tree.Root, position);
217    }
218    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) {
219      return root.IterateNodesPrefix().ElementAt(position);
220    }
221  }
222
223  internal class TileLayoutNode {
224    public SymbolicExpressionTreeTile Tile { get; set; }
225
226    private List<TileLayoutNode> children;
227    public IEnumerable<TileLayoutNode> Children {
228      get { return children ?? Enumerable.Empty<TileLayoutNode>(); }
229      set { children = value.ToList(); }
230    }
231  }
232}
Note: See TracBrowser for help on using the repository browser.