Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Made some progress with the BottomUpDistanceCalculator, still not entirely correct.

File size: 4.7 KB
RevLine 
[10496]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;
[10649]24using System.Drawing;
[10496]25using System.Globalization;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30
[10520]31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
[10496]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"},
[10655]37      {"StartSymbol","RPB"},
38      {"Multiplication", "$\\times$"},
39      {"Division", "$\\div$"},
40      {"Addition", "$+$"},
41      {"Subtraction", "$-$"},
42      {"Exponential", "$\\exp$"},
43      {"Logarithm", "$\\log$"}
[10496]44    };
45
[10649]46    private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
47
[10496]48    public SymbolicExpressionTreeLatexFormatter()
49      : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") {
[10649]50      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
[10520]51        HorizontalSpacing = 2,
52        VerticalSpacing = 2,
53        NodeWidth = 8,
54        NodeHeight = 4
55      };
[10496]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) {
[11015]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>();
[10520]74      var root = symbolicExpressionTree.Root;
75      var actualRoot = root.SubtreeCount == 0 ? root.GetSubtree(0) : root;
[10649]76      var nodeCoordinates = layoutEngine.CalculateLayout(actualRoot).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
[10496]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 +
[11015]87                "\\def\\hs{0.7}" + nl +
88                "\\def\\offs{" + offset + "}" + nl);
[10496]89
90      var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
91      for (int i = 0; i < nodes.Count; ++i) {
92        var node = nodes[i];
[11015]93        var id = Guid.NewGuid().ToString();
94        nodeIds[node] = id;
[10496]95        var coord = nodeCoordinates[node];
96        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
[11016]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)));
[10496]98      }
99
100      for (int i = 0; i < nodes.Count; ++i) {
[11015]101        var n = nodes[i];
[10496]102        foreach (var s in nodes[i].Subtrees) {
[11015]103          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", nodeIds[n], nodeIds[s]));
[10496]104        }
105      }
106
107      sb.Append("\\end{tikzpicture}" + nl +
108                "\\end{document}" + nl);
[11015]109      str = sb.ToString();
110      return nodeIds;
[10496]111    }
112
113    private static string EscapeLatexString(string s) {
114      return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.