Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Added license headers where they were missing. Introduced an id map to the DirectedGraph to get graph vertices based on the id injected in the scopes by the genealogy analyzer.

File size: 8.7 KB
RevLine 
[10884]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;
[10833]23using System.Collections.Generic;
[10685]24using System.Drawing;
[10655]25using System.Linq;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
29using HeuristicLab.EvolutionTracking;
30using HeuristicLab.MainForm;
[10730]31using HeuristicLab.Visualization;
[10655]32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
34  [View("FragmentGraphView")]
35  [Content(typeof(IGenealogyGraph<IFragment<ISymbolicExpressionTreeNode>>), IsDefaultView = true)]
36  public sealed partial class FragmentGraphView : ItemView {
[10746]37    private const int PreferredHorizontalSpacing = 10;
38    private const int PreferredVerticalSpacing = 25;
[10685]39
[10655]40    private ReingoldTilfordLayoutEngine<TileLayoutNode> layoutEngine;
41    private ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> symbolicExpressionEngine;
42
43    private Dictionary<IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>, TileLayoutNode> tileDictionary;
44
45    private SymbolicExpressionTreeTile Root { get; set; }
46
47    public new IGenealogyGraph<IFragment<ISymbolicExpressionTreeNode>> Content {
48      get { return (IGenealogyGraph<IFragment<ISymbolicExpressionTreeNode>>)base.Content; }
[10677]49      set { base.Content = value; }
[10655]50    }
51
52    public FragmentGraphView() {
53      InitializeComponent();
54
[10729]55      layoutEngine = new ReingoldTilfordLayoutEngine<TileLayoutNode>(n => n.Children) {
[10746]56        HorizontalSpacing = PreferredHorizontalSpacing,
57        VerticalSpacing = PreferredVerticalSpacing,
[10729]58      };
59      symbolicExpressionEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
[10746]60        HorizontalSpacing = PreferredHorizontalSpacing,
61        VerticalSpacing = PreferredVerticalSpacing,
[10729]62        NodeWidth = 80,
63        NodeHeight = 40
64      };
[10656]65      tileDictionary = new Dictionary<IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>, TileLayoutNode>();
[10655]66    }
67
68    private void MakeTiles() {
69      var chart = symbolicExpressionChartControl.Chart;
[10656]70      tileDictionary.Clear();
[10677]71      foreach (var node in Content.Nodes) {
[10656]72        var tile = new SymbolicExpressionTreeTile(chart);
73        tile.LayoutEngine = symbolicExpressionEngine;
[10833]74        tile.Label = "Generation " + node.Rank + Environment.NewLine +
75                     "Quality " + String.Format("{0:0.000}", node.Quality);
[10656]76        tile.Root = node.Content.Root;
[10685]77        var tileNode = new TileLayoutNode { Tile = tile };
[10655]78        tileDictionary.Add(node, tileNode);
79      }
[10677]80      foreach (var node in Content.Nodes.Where(n => n.Children.Any())) {
81        var layoutNode = tileDictionary[node];
[10685]82        layoutNode.Children = new List<TileLayoutNode>(node.Children.Select(x => tileDictionary[x]));
[10655]83      }
84    }
85
86    private void Draw() {
[10730]87      var chart = symbolicExpressionChartControl.Chart;
88      var nodes = Content.Nodes.ToList();
89      var root = nodes[0];
[10677]90      var fragmentRoot = tileDictionary[root];
[10728]91      int maxTileWidth = 0, maxTileHeight = 0;
[10730]92      var tiles = nodes.Select(x => tileDictionary[x].Tile).ToList();
93
94      foreach (var tile in tiles) {
[10728]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;
[10746]101      layoutEngine.HorizontalSpacing = PreferredHorizontalSpacing;
102      layoutEngine.VerticalSpacing = PreferredVerticalSpacing;
[10728]103
[10677]104      var visualNodes = layoutEngine.CalculateLayout(fragmentRoot);
105
[10656]106      symbolicExpressionChartControl.UpdateEnabled = false;
107      foreach (var visualNode in visualNodes) {
108        var tile = visualNode.Content.Tile;
[10685]109        tile.Position = new Point(visualNode.X, visualNode.Y);
[10656]110        symbolicExpressionChartControl.Add(tile);
111      }
[10730]112
113      // add connections between the tiles
114      foreach (var node in nodes) {
115        var aTile = tileDictionary[node].Tile;
116        var aSize = aTile.Size;
117        var aPos = aTile.Position;
118
[10838]119        var fragment = node.Content;
120        if (fragment.Index1 > 0) {
121          var subtree = fragment.Root.NodeAt(fragment.Index1);
[10797]122          foreach (var s in subtree.IterateNodesPrefix()) {
[10746]123            var primitive = aTile.GetPrimitive(s);
124            if (primitive != null) {
125              var rpb = primitive as RectangularPrimitiveBase;
126              if (rpb != null) {
[10838]127                rpb.Pen = new Pen(Color.Black);
[10746]128              }
129            }
130          }
131        }
[10838]132        if (fragment.Index2 > 0) {
133          var subtree = fragment.Root.NodeAt(fragment.Index2);
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        }
[10730]144
[10838]145        if (node.Parents.Any() && node == node.Parents.First().Children.First()) {
146          var parent = node.Parents.First();
147          var index = fragment.Index1 + (parent.Content.Index2 - parent.Content.Index1);
[10840]148          // some mutations create discontinuities which invalidate the index
149          if (index >= 0 && index < fragment.Root.GetLength()) {
150            var subtree = fragment.Root.NodeAt(index);
151            var primitive = aTile.GetPrimitive(subtree);
152            primitive.Brush = new SolidBrush(Color.LightCoral);
153          }
[10838]154        }
155
[10730]156        foreach (var child in node.Children) {
157          var bTile = tileDictionary[child].Tile;
158          var bSize = bTile.Size;
159          var bPos = bTile.Position;
160
[10838]161          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)) {
162            Pen = Pens.DimGray
163          };
[10730]164          symbolicExpressionChartControl.Add(line);
165        }
166      }
[10833]167      // center display on the root of the fragment graph
168      symbolicExpressionChartControl.Chart.Move(tileDictionary[root].Tile.Position.X, tileDictionary[root].Tile.Position.Y);
[10656]169      symbolicExpressionChartControl.UpdateEnabled = true;
170      symbolicExpressionChartControl.EnforceUpdate();
[10655]171    }
172
173    protected override void DeregisterContentEvents() {
174      // TODO: Deregister your event handlers here
175      base.DeregisterContentEvents();
176    }
177
178    protected override void RegisterContentEvents() {
179      base.RegisterContentEvents();
180      // TODO: Register your event handlers here
181    }
182
183    #region Event Handlers (Content)
184    // TODO: Put event handlers of the content here
185    protected override void OnContentChanged() {
186      base.OnContentChanged();
[10677]187      if (Content != null) {
188        MakeTiles();
189        Draw();
[10655]190      }
191    }
[10677]192    #endregion
[10655]193
194    protected override void SetEnabledStateOfControls() {
195      base.SetEnabledStateOfControls();
196      // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
197    }
198
199    #region Event Handlers (child controls)
200
201    // TODO: Put event handlers of child controls here.
202
203    #endregion
204  }
205
[10752]206  internal static class Util {
207    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) {
208      return NodeAt(tree.Root, position);
209    }
210    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) {
211      return root.IterateNodesPrefix().ElementAt(position);
212    }
213  }
214
[10655]215  internal class TileLayoutNode {
216    public SymbolicExpressionTreeTile Tile { get; set; }
[10685]217
218    private List<TileLayoutNode> children;
219    public IEnumerable<TileLayoutNode> Children {
220      get { return children ?? Enumerable.Empty<TileLayoutNode>(); }
221      set { children = value.ToList(); }
222    }
[10655]223  }
224}
Note: See TracBrowser for help on using the repository browser.