Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/LayoutEngines/SymbolicExpressionTreeLayoutAdapter.cs @ 10471

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

#2076: Thanks for the feedback:

  • I removed the ILayoutNode Interfaces
  • Fixed the error that was causing it to crash in the textual representation
  • The ancestor is a node on the path between the root and the current node (from the paper), but in the algorithm it's assigned differently
  • Parent is the immediate parent

I also increased the node label text size a bit and did some cosmetic improvements.

File size: 3.8 KB
RevLine 
[9970]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
24
25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
26  // adapter class that provides some conversion methods from symbolic expression trees to layout nodes (preserving the tree structure)
27  public class SymbolicExpressionTreeLayoutAdapter : ILayoutAdapter<ISymbolicExpressionTreeNode> {
[10471]28    // default conversion function between ISymbolicExpressionTreeNode and LayoutNode<ISymbolicExpressionTree>
29    LayoutNode<ISymbolicExpressionTreeNode> defaultConvert(ISymbolicExpressionTreeNode node) {
[9970]30      var layoutNode = new LayoutNode<ISymbolicExpressionTreeNode> { Content = node };
31      layoutNode.Ancestor = layoutNode;
32      return layoutNode;
33    }
34
[10471]35    public IEnumerable<LayoutNode<ISymbolicExpressionTreeNode>> Convert(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, LayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
[9970]36      return Convert(tree.Root, convertFunc);
37    }
38    // translate the symbolic expression tree structure to a layout node tree structure
39    // return an enumerable containing all the layout nodes
[10471]40    public IEnumerable<LayoutNode<ISymbolicExpressionTreeNode>> Convert(ISymbolicExpressionTreeNode root, Func<ISymbolicExpressionTreeNode, LayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
[9970]41      var rootLayoutNode = convertFunc == null ? defaultConvert(root) : convertFunc(root);
42      rootLayoutNode.Ancestor = rootLayoutNode;
43
44      if (root.SubtreeCount > 0) {
[10471]45        rootLayoutNode.Children = new List<LayoutNode<ISymbolicExpressionTreeNode>>(root.SubtreeCount);
[9970]46        Expand(rootLayoutNode, convertFunc);
47      }
48
[10471]49      var list = new List<LayoutNode<ISymbolicExpressionTreeNode>> { rootLayoutNode };
[9970]50      int i = 0;
51      while (i < list.Count) {
52        if (list[i].Children != null)
[10471]53          list.AddRange(list[i].Children);
54        //          foreach (LayoutNode<ISymbolicExpressionTreeNode> child in list[i].Children)
55        //            list.Add(child);
[9970]56        ++i;
57      }
58      return list;
59    }
60
[10471]61    private void Expand(LayoutNode<ISymbolicExpressionTreeNode> layoutNode, Func<ISymbolicExpressionTreeNode, LayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
[9970]62      if (layoutNode.Children == null) return;
63      for (int i = 0; i < layoutNode.Content.SubtreeCount; ++i) {
64        var subtree = layoutNode.Content.GetSubtree(i);
65        var childLayoutNode = convertFunc == null ? defaultConvert(subtree) : convertFunc(subtree);
66        childLayoutNode.Parent = layoutNode;
67        childLayoutNode.Number = i;
68        childLayoutNode.Level = layoutNode.Level + 1;
69        childLayoutNode.Ancestor = childLayoutNode;
[10471]70        childLayoutNode.Children = subtree.SubtreeCount > 0 ? new List<LayoutNode<ISymbolicExpressionTreeNode>>(subtree.SubtreeCount) : null;
[9970]71        layoutNode.Children.Add(childLayoutNode);
72        Expand(childLayoutNode, convertFunc);
73      }
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.