Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/Symbolic/SymbolicExpressionTreeGraphvizFormatter.cs @ 7908

Last change on this file since 7908 was 4327, checked in by gkronber, 14 years ago

Added views to export symbolic expression trees as latex and graphviz expressions. #1142

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