[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.LayoutEngines;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 27 | // adapter class that provides some conversion methods from symbolic expression trees to layout nodes (preserving the tree structure)
|
---|
| 28 | public class SymbolicExpressionTreeLayoutAdapter : ILayoutAdapter<ISymbolicExpressionTreeNode> {
|
---|
| 29 | // default conversion function between ISymbolicExpressionTreeNode and ILayoutNode<ISymbolicExpressionTree>
|
---|
| 30 | ILayoutNode<ISymbolicExpressionTreeNode> defaultConvert(ISymbolicExpressionTreeNode node) {
|
---|
| 31 | var layoutNode = new LayoutNode<ISymbolicExpressionTreeNode> { Content = node };
|
---|
| 32 | layoutNode.Ancestor = layoutNode;
|
---|
| 33 | return layoutNode;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public IEnumerable<ILayoutNode<ISymbolicExpressionTreeNode>> Convert(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, ILayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
|
---|
| 37 | return Convert(tree.Root, convertFunc);
|
---|
| 38 | }
|
---|
| 39 | // translate the symbolic expression tree structure to a layout node tree structure
|
---|
| 40 | // return an enumerable containing all the layout nodes
|
---|
| 41 | public IEnumerable<ILayoutNode<ISymbolicExpressionTreeNode>> Convert(ISymbolicExpressionTreeNode root, Func<ISymbolicExpressionTreeNode, ILayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
|
---|
| 42 | var rootLayoutNode = convertFunc == null ? defaultConvert(root) : convertFunc(root);
|
---|
| 43 | rootLayoutNode.Ancestor = rootLayoutNode;
|
---|
| 44 |
|
---|
| 45 | if (root.SubtreeCount > 0) {
|
---|
| 46 | rootLayoutNode.Children = new List<ILayoutNode<ISymbolicExpressionTreeNode>>(root.SubtreeCount);
|
---|
| 47 | Expand(rootLayoutNode, convertFunc);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | var list = new List<ILayoutNode<ISymbolicExpressionTreeNode>> { rootLayoutNode };
|
---|
| 51 | int i = 0;
|
---|
| 52 | while (i < list.Count) {
|
---|
| 53 | if (list[i].Children != null)
|
---|
| 54 | foreach (ILayoutNode<ISymbolicExpressionTreeNode> child in list[i].Children)
|
---|
| 55 | list.Add(child);
|
---|
| 56 | ++i;
|
---|
| 57 | }
|
---|
| 58 | return list;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private void Expand(ILayoutNode<ISymbolicExpressionTreeNode> layoutNode, Func<ISymbolicExpressionTreeNode, ILayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
|
---|
| 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;
|
---|
| 70 | childLayoutNode.Children = subtree.SubtreeCount > 0 ? new List<ILayoutNode<ISymbolicExpressionTreeNode>>(subtree.SubtreeCount) : null;
|
---|
| 71 | layoutNode.Children.Add(childLayoutNode);
|
---|
| 72 | Expand(childLayoutNode, convertFunc);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|