Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS(old)/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/Formatters/SymbolicExpressionTreeLatexFormatter.cs @ 12081

Last change on this file since 12081 was 12018, checked in by pfleck, 10 years ago

#2269

  • merged trunk after 3.3.11 release
  • updated copyright and plugin version in ALPS plugin
  • removed old ALPS samples based on an userdefined alg
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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    };
39
40    private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
41
42    public SymbolicExpressionTreeLatexFormatter()
43      : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") {
44      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
45        HorizontalSpacing = 2,
46        VerticalSpacing = 2,
47        NodeWidth = 8,
48        NodeHeight = 4
49      };
50    }
51
52    protected SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner)
53      : base(original, cloner) {
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new SymbolicExpressionTreeLatexFormatter(this, cloner);
58    }
59
60    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
61      var root = symbolicExpressionTree.Root;
62      var actualRoot = root.SubtreeCount == 0 ? root.GetSubtree(0) : root;
63      var nodeCoordinates = layoutEngine.CalculateLayout(actualRoot).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
64      var sb = new StringBuilder();
65      var nl = Environment.NewLine;
66      double ws = 1;
67      double hs = 0.7;
68
69      sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
70                "\\usepackage{tikz}" + nl +
71                "\\begin{document}" + nl +
72                "\\begin{tikzpicture}" + nl +
73                "\\def\\ws{1}" + nl +
74                "\\def\\hs{0.7}" + nl);
75
76      var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();
77      var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
78      for (int i = 0; i < nodes.Count; ++i) {
79        var node = nodes[i];
80        nodeIndices.Add(node, i);
81        var coord = nodeCoordinates[node];
82        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
83        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
84      }
85
86      for (int i = 0; i < nodes.Count; ++i) {
87        foreach (var s in nodes[i].Subtrees) {
88          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", i, nodeIndices[s]));
89        }
90      }
91
92      sb.Append("\\end{tikzpicture}" + nl +
93                "\\end{document}" + nl);
94      return sb.ToString();
95    }
96
97    private static string EscapeLatexString(string s) {
98      return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.