1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 System.Drawing;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
32 | [Item("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.")]
|
---|
33 | public class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
34 | private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string>
|
---|
35 | {
|
---|
36 | {"ProgramRootSymbol", "Prog"},
|
---|
37 | {"StartSymbol","RPB"},
|
---|
38 | {"Multiplication", "$\\times$"},
|
---|
39 | {"Division", "$\\div$"},
|
---|
40 | {"Addition", "$+$"},
|
---|
41 | {"Subtraction", "$-$"},
|
---|
42 | {"Exponential", "$\\exp$"},
|
---|
43 | {"Logarithm", "$\\log$"}
|
---|
44 | };
|
---|
45 |
|
---|
46 | private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
|
---|
47 |
|
---|
48 | public SymbolicExpressionTreeLatexFormatter()
|
---|
49 | : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") {
|
---|
50 | layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
|
---|
51 | HorizontalSpacing = 2,
|
---|
52 | VerticalSpacing = 2,
|
---|
53 | NodeWidth = 8,
|
---|
54 | NodeHeight = 4
|
---|
55 | };
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner)
|
---|
59 | : base(original, cloner) {
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
63 | return new SymbolicExpressionTreeLatexFormatter(this, cloner);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
67 | string s;
|
---|
68 | Format(symbolicExpressionTree, out s);
|
---|
69 | return s;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public Dictionary<ISymbolicExpressionTreeNode, string> Format(ISymbolicExpressionTree symbolicExpressionTree, out string str, int offset = 0) {
|
---|
73 | var nodeIds = new Dictionary<ISymbolicExpressionTreeNode, string>();
|
---|
74 | var root = symbolicExpressionTree.Root;
|
---|
75 | var actualRoot = root.SubtreeCount == 0 ? root.GetSubtree(0) : root;
|
---|
76 | var nodeCoordinates = layoutEngine.CalculateLayout(actualRoot).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
|
---|
77 | var sb = new StringBuilder();
|
---|
78 | var nl = Environment.NewLine;
|
---|
79 | double ws = 1;
|
---|
80 | double hs = 0.7;
|
---|
81 |
|
---|
82 | sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
|
---|
83 | "\\usepackage{tikz}" + nl +
|
---|
84 | "\\begin{document}" + nl +
|
---|
85 | "\\begin{tikzpicture}" + nl +
|
---|
86 | "\\def\\ws{1}" + nl +
|
---|
87 | "\\def\\hs{0.7}" + nl +
|
---|
88 | "\\def\\offs{" + offset + "}" + nl);
|
---|
89 |
|
---|
90 | var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
|
---|
91 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
92 | var node = nodes[i];
|
---|
93 | var id = Guid.NewGuid().ToString();
|
---|
94 | nodeIds[node] = id;
|
---|
95 | var coord = nodeCoordinates[node];
|
---|
96 | var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
|
---|
97 | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1} + \\offs,\\hs*{2}) {{{3}}};", nodeIds[node], ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
|
---|
98 | }
|
---|
99 |
|
---|
100 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
101 | var n = nodes[i];
|
---|
102 | foreach (var s in nodes[i].Subtrees) {
|
---|
103 | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", nodeIds[n], nodeIds[s]));
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | sb.Append("\\end{tikzpicture}" + nl +
|
---|
108 | "\\end{document}" + nl);
|
---|
109 | str = sb.ToString();
|
---|
110 | return nodeIds;
|
---|
111 | }
|
---|
112 |
|
---|
113 | private static string EscapeLatexString(string s) {
|
---|
114 | return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|