[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 |
|
---|
| 25 | namespace 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) {
|
---|
[10487] | 52 | if (list[i].Children != null) list.AddRange(list[i].Children);
|
---|
[9970] | 53 | ++i;
|
---|
| 54 | }
|
---|
| 55 | return list;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[10471] | 58 | private void Expand(LayoutNode<ISymbolicExpressionTreeNode> layoutNode, Func<ISymbolicExpressionTreeNode, LayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
|
---|
[9970] | 59 | if (layoutNode.Children == null) return;
|
---|
| 60 | for (int i = 0; i < layoutNode.Content.SubtreeCount; ++i) {
|
---|
| 61 | var subtree = layoutNode.Content.GetSubtree(i);
|
---|
| 62 | var childLayoutNode = convertFunc == null ? defaultConvert(subtree) : convertFunc(subtree);
|
---|
| 63 | childLayoutNode.Parent = layoutNode;
|
---|
| 64 | childLayoutNode.Number = i;
|
---|
| 65 | childLayoutNode.Level = layoutNode.Level + 1;
|
---|
| 66 | childLayoutNode.Ancestor = childLayoutNode;
|
---|
[10471] | 67 | childLayoutNode.Children = subtree.SubtreeCount > 0 ? new List<LayoutNode<ISymbolicExpressionTreeNode>>(subtree.SubtreeCount) : null;
|
---|
[9970] | 68 | layoutNode.Children.Add(childLayoutNode);
|
---|
| 69 | Expand(childLayoutNode, convertFunc);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|