Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10888 was 10888, checked in by bburlacu, 10 years ago

#1772: Introduced separate class for FragmentNodes and adjusted tracing code. Fixed small bug creating the genealogy graph.

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