#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.LayoutEngines; namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { // adapter class that provides some conversion methods from symbolic expression trees to layout nodes (preserving the tree structure) public class SymbolicExpressionTreeLayoutAdapter : ILayoutAdapter { // default conversion function between ISymbolicExpressionTreeNode and ILayoutNode ILayoutNode defaultConvert(ISymbolicExpressionTreeNode node) { var layoutNode = new LayoutNode { Content = node }; layoutNode.Ancestor = layoutNode; return layoutNode; } public IEnumerable> Convert(ISymbolicExpressionTree tree, Func> convertFunc = null) { return Convert(tree.Root, convertFunc); } // translate the symbolic expression tree structure to a layout node tree structure // return an enumerable containing all the layout nodes public IEnumerable> Convert(ISymbolicExpressionTreeNode root, Func> convertFunc = null) { var rootLayoutNode = convertFunc == null ? defaultConvert(root) : convertFunc(root); rootLayoutNode.Ancestor = rootLayoutNode; if (root.SubtreeCount > 0) { rootLayoutNode.Children = new List>(root.SubtreeCount); Expand(rootLayoutNode, convertFunc); } var list = new List> { rootLayoutNode }; int i = 0; while (i < list.Count) { if (list[i].Children != null) foreach (ILayoutNode child in list[i].Children) list.Add(child); ++i; } return list; } private void Expand(ILayoutNode layoutNode, Func> convertFunc = null) { if (layoutNode.Children == null) return; for (int i = 0; i < layoutNode.Content.SubtreeCount; ++i) { var subtree = layoutNode.Content.GetSubtree(i); var childLayoutNode = convertFunc == null ? defaultConvert(subtree) : convertFunc(subtree); childLayoutNode.Parent = layoutNode; childLayoutNode.Number = i; childLayoutNode.Level = layoutNode.Level + 1; childLayoutNode.Ancestor = childLayoutNode; childLayoutNode.Children = subtree.SubtreeCount > 0 ? new List>(subtree.SubtreeCount) : null; layoutNode.Children.Add(childLayoutNode); Expand(childLayoutNode, convertFunc); } } } }