Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/Formatters/SymbolicExpressionTreeLatexFormatter.cs

Last change on this file was 17434, checked in by bburlacu, 5 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

File size: 4.6 KB
RevLine 
[10496]1#region License Information
2/* HeuristicLab
[17434]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10496]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"},
[17434]38      // short form
39      {"Subtraction", "-" },
40      {"Addition", "+" },
41      {"Multiplication", "*" },
42      {"Division", "/" },
43      {"Absolute", "abs" },
44      {"AnalyticQuotient", "AQ" },
45      {"Sine", "sin" },
46      {"Cosine", "cos" },
47      {"Tanget", "tan" },
48      {"HyperbolicTangent", "tanh" },
49      {"Exponential", "exp" },
50      {"Logarithm", "log" },
51      {"SquareRoot", "sqrt" },
52      {"Square", "sqr" },
53      {"CubeRoot", "cbrt" },
54      {"Cube", "cube" },
55      {"GreaterThan", ">" },
56      {"LessThan", "<" },
[10496]57    };
58
[10649]59    private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
60
[10496]61    public SymbolicExpressionTreeLatexFormatter()
62      : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") {
[10649]63      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
[10520]64        HorizontalSpacing = 2,
65        VerticalSpacing = 2,
66        NodeWidth = 8,
67        NodeHeight = 4
68      };
[10496]69    }
70
71    protected SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner)
72      : base(original, cloner) {
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new SymbolicExpressionTreeLatexFormatter(this, cloner);
77    }
78
79    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
[10520]80      var root = symbolicExpressionTree.Root;
81      var actualRoot = root.SubtreeCount == 0 ? root.GetSubtree(0) : root;
[10649]82      var nodeCoordinates = layoutEngine.CalculateLayout(actualRoot).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
[10496]83      var sb = new StringBuilder();
84      var nl = Environment.NewLine;
85      double ws = 1;
86      double hs = 0.7;
87
88      sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
89                "\\usepackage{tikz}" + nl +
90                "\\begin{document}" + nl +
91                "\\begin{tikzpicture}" + nl +
92                "\\def\\ws{1}" + nl +
[17434]93                "\\def\\hs{0.7}" + nl);
[10496]94
[17434]95      var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();
[10496]96      var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
97      for (int i = 0; i < nodes.Count; ++i) {
98        var node = nodes[i];
[17434]99        nodeIndices.Add(node, i);
[10496]100        var coord = nodeCoordinates[node];
101        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
[17434]102        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
[10496]103      }
104
105      for (int i = 0; i < nodes.Count; ++i) {
106        foreach (var s in nodes[i].Subtrees) {
[17434]107          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", i, nodeIndices[s]));
[10496]108        }
109      }
110
111      sb.Append("\\end{tikzpicture}" + nl +
112                "\\end{document}" + nl);
[17434]113      return sb.ToString();
[10496]114    }
115
116    private static string EscapeLatexString(string s) {
117      return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.