1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Text;
|
---|
23 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Formatters {
|
---|
30 | [Item("SymbolicExpressionTreeGraphvizFormatter", "Formatter for symbolic expression trees for import into GraphViz documents.")]
|
---|
31 | public sealed class SymbolicExpressionTreeGraphvizFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
32 | public bool Indent { get; set; }
|
---|
33 |
|
---|
34 | private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string>() {
|
---|
35 | {"Multiplication", "Mul"},
|
---|
36 | {"Addition", "Add"},
|
---|
37 | {"Division", "Div"},
|
---|
38 | {"Subtraction", "Sub"},
|
---|
39 | {"Average", "Avg"},
|
---|
40 |
|
---|
41 | {"Logarithm", "Log"},
|
---|
42 | {"Exponential", "Exp"},
|
---|
43 |
|
---|
44 | {"Cosine", "Cos"},
|
---|
45 | {"Sine", "Sin"},
|
---|
46 | {"Tangent", "Tan"},
|
---|
47 |
|
---|
48 | {"GreaterThan", ">"},
|
---|
49 | {"LessThan", "<"},
|
---|
50 | {"IfThenElse", "If"},
|
---|
51 |
|
---|
52 | // match Koza style
|
---|
53 | {"ProgramRootSymbol", "Prog"},
|
---|
54 | {"StartSymbol", "RPB"},
|
---|
55 | };
|
---|
56 |
|
---|
57 | public SymbolicExpressionTreeGraphvizFormatter()
|
---|
58 | : base("SymbolicExpressionTreeLatexFormatter", "Formatter for symbolic expression trees for import into LaTeX documents.") {
|
---|
59 | Indent = true;
|
---|
60 | }
|
---|
61 | private SymbolicExpressionTreeGraphvizFormatter(SymbolicExpressionTreeGraphvizFormatter original, Cloner cloner)
|
---|
62 | : base(original, cloner) {
|
---|
63 | this.Indent = original.Indent;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new SymbolicExpressionTreeGraphvizFormatter(this, cloner);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public string Format(SymbolicExpressionTree symbolicExpressionTree) {
|
---|
71 | int nodeCounter = 1;
|
---|
72 | StringBuilder strBuilder = new StringBuilder();
|
---|
73 | strBuilder.AppendLine("graph {");
|
---|
74 | strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root, 0, ref nodeCounter));
|
---|
75 | strBuilder.AppendLine("}");
|
---|
76 | return strBuilder.ToString();
|
---|
77 | }
|
---|
78 |
|
---|
79 | private string FormatRecursively(SymbolicExpressionTreeNode node, int indentLength, ref int nodeId) {
|
---|
80 | // save id of current node
|
---|
81 | int currentNodeId = nodeId;
|
---|
82 | // increment id for next node
|
---|
83 | nodeId++;
|
---|
84 |
|
---|
85 | StringBuilder strBuilder = new StringBuilder();
|
---|
86 | if (Indent) strBuilder.Append(' ', indentLength);
|
---|
87 |
|
---|
88 | // get label for node and map if necessary
|
---|
89 | string nodeLabel = node.ToString();
|
---|
90 | if (symbolNameMap.ContainsKey(nodeLabel)) {
|
---|
91 | nodeLabel = symbolNameMap[nodeLabel];
|
---|
92 | }
|
---|
93 |
|
---|
94 | strBuilder.Append("node" + currentNodeId + "[label=\"" + nodeLabel + "\"");
|
---|
95 | // leaf nodes should have box shape
|
---|
96 | if (node.SubTrees.Count == 0) {
|
---|
97 | strBuilder.AppendLine(", shape=\"box\"];");
|
---|
98 | } else {
|
---|
99 | strBuilder.AppendLine("];");
|
---|
100 | }
|
---|
101 |
|
---|
102 | // internal nodes or leaf nodes?
|
---|
103 | foreach (SymbolicExpressionTreeNode subTree in node.SubTrees) {
|
---|
104 | // add an edge
|
---|
105 | if (Indent) strBuilder.Append(' ', indentLength);
|
---|
106 | strBuilder.AppendLine("node" + currentNodeId + " -- node" + nodeId + ";");
|
---|
107 | // format the whole subtree
|
---|
108 | strBuilder.Append(FormatRecursively(subTree, indentLength + 2, ref nodeId));
|
---|
109 | }
|
---|
110 |
|
---|
111 | return strBuilder.ToString();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|