Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeGraphvizFormatter.cs @ 16802

Last change on this file since 16802 was 16802, checked in by gkronber, 5 years ago

#2915 added support for abs(), aq(), cube(), cuberoot() to formatters. Skipped the TSQLFormatter.

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Collections.Generic;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
28  [Item("GraphViz String Formatter", "Formatter for symbolic expression trees for visualization with GraphViz.")]
29  public sealed class SymbolicExpressionTreeGraphvizFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
30    public bool Indent { get; set; }
31
32    private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string>() {
33      // match Koza style
34      {"ProgramRootSymbol", "Prog"},
35      {"StartSymbol", "RPB"},
36
37      // short form
38      {"Subtraction", "-" },
39      {"Addition", "+" },
40      {"Multiplication", "*" },
41      {"Division", "/" },
42      {"Absolute", "abs" },
43      {"AnalyticQuotient", "AQ" },
44      {"Sine", "sin" },
45      {"Cosine", "cos" },
46      {"Tanget", "tan" },
47      {"HyperbolicTangent", "tanh" },
48      {"Exponential", "exp" },
49      {"Logarithm", "log" },
50      {"SquareRoot", "sqrt" },
51      {"Square", "sqr" },
52      {"CubeRoot", "cbrt" },
53      {"Cube", "cube" },
54      {"GreaterThan", ">" },
55      {"LessThan", "<" },
56    };
57
58    public SymbolicExpressionTreeGraphvizFormatter()
59      : base("GraphViz String Formatter", "Formatter for symbolic expression trees for visualization with GraphViz.") {
60      Indent = true;
61    }
62    private SymbolicExpressionTreeGraphvizFormatter(SymbolicExpressionTreeGraphvizFormatter original, Cloner cloner)
63      : base(original, cloner) {
64      this.Indent = original.Indent;
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new SymbolicExpressionTreeGraphvizFormatter(this, cloner);
69    }
70
71    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
72      int nodeCounter = 1;
73      StringBuilder strBuilder = new StringBuilder();
74      strBuilder.AppendLine("graph {");
75      strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root, 0, ref nodeCounter));
76      strBuilder.AppendLine("}");
77      return strBuilder.ToString();
78    }
79
80    private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength, ref int nodeId) {
81      // save id of current node
82      int currentNodeId = nodeId;
83      // increment id for next node
84      nodeId++;
85
86      StringBuilder strBuilder = new StringBuilder();
87      if (Indent) strBuilder.Append(' ', indentLength);
88
89      // get label for node and map if necessary
90      string nodeLabel = node.ToString();
91      if (symbolNameMap.ContainsKey(nodeLabel)) {
92        nodeLabel = symbolNameMap[nodeLabel];
93      }
94
95      strBuilder.Append("node" + currentNodeId + "[label=\"" + nodeLabel + "\"");
96      // leaf nodes should have box shape
97      if (node.SubtreeCount == 0) {
98        strBuilder.AppendLine(", shape=\"box\"];");
99      } else {
100        strBuilder.AppendLine("];");
101      }
102
103      // internal nodes or leaf nodes?
104      foreach (ISymbolicExpressionTreeNode subTree in node.Subtrees) {
105        // add an edge
106        if (Indent) strBuilder.Append(' ', indentLength);
107        strBuilder.AppendLine("node" + currentNodeId + " -- node" + nodeId + ";");
108        // format the whole subtree
109        strBuilder.Append(FormatRecursively(subTree, indentLength + 2, ref nodeId));
110      }
111
112      return strBuilder.ToString();
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.