Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/Formatters/SymbolicExpressionTreeLatexFormatter.cs @ 10655

Last change on this file since 10655 was 10655, checked in by bburlacu, 10 years ago

#1772: Made some progress towards the visualization of building block trajectories. Added the FragmentGraphView.

File size: 4.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Globalization;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30
31namespace 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      var root = symbolicExpressionTree.Root;
68      var actualRoot = root.SubtreeCount == 0 ? root.GetSubtree(0) : root;
69      var nodeCoordinates = layoutEngine.CalculateLayout(actualRoot).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
70      var sb = new StringBuilder();
71      var nl = Environment.NewLine;
72      double ws = 1;
73      double hs = 0.7;
74
75      sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
76                "\\usepackage{tikz}" + nl +
77                "\\begin{document}" + nl +
78                "\\begin{tikzpicture}" + nl +
79                "\\def\\ws{1}" + nl +
80                "\\def\\hs{0.7}" + nl);
81
82      var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();
83      var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
84      for (int i = 0; i < nodes.Count; ++i) {
85        var node = nodes[i];
86        nodeIndices.Add(node, i);
87        var coord = nodeCoordinates[node];
88        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
89        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
90      }
91
92      for (int i = 0; i < nodes.Count; ++i) {
93        foreach (var s in nodes[i].Subtrees) {
94          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", i, nodeIndices[s]));
95        }
96      }
97
98      sb.Append("\\end{tikzpicture}" + nl +
99                "\\end{document}" + nl);
100      return sb.ToString();
101    }
102
103    private static string EscapeLatexString(string s) {
104      return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.