Changeset 10501 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters
- Timestamp:
- 02/21/14 18:20:47 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeLatexFormatter.cs
r10459 r10501 1 using System; 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 22 using System; 2 23 using System.Collections.Generic; 3 24 using System.Globalization; … … 8 29 9 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 10 [Item("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex .")]31 [Item("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.")] 11 32 public class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 12 private readonly static Dictionary<string, string> SymbolNamesMap = new Dictionary<string, string>33 private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string> 13 34 { 14 35 {"ProgramRootSymbol", "Prog"}, 15 {"StartSymbol","RPB"}, 16 {"Addition", "$+$"}, 17 {"Subtraction", "$-$"}, 18 {"Multiplication", "$\\times$"}, 19 {"Division", "$\\div$"}, 20 {"Logarithm", "$\\log$"}, 21 {"Exponential", "$\\exp$"} 36 {"StartSymbol","RPB"} 22 37 }; 23 private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine ;24 private readonly SymbolicExpressionTreeLayoutAdapter layoutAdapter ;38 private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(); 39 private readonly SymbolicExpressionTreeLayoutAdapter layoutAdapter = new SymbolicExpressionTreeLayoutAdapter(); 25 40 26 public SymbolicExpressionTreeLatexFormatter() { 41 public SymbolicExpressionTreeLatexFormatter() 42 : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") { 27 43 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(); 28 layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();29 44 } 30 45 31 46 protected SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner) 32 47 : base(original, cloner) { 33 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();34 layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();35 48 } 36 49 … … 40 53 41 54 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 55 layoutEngine.Reset(); 42 56 var layoutNodes = layoutAdapter.Convert(symbolicExpressionTree).ToList(); 43 layoutEngine.Reset();44 57 layoutEngine.Root = layoutNodes[0]; 45 foreach (var ln in layoutNodes) 46 layoutEngine.AddNode(ln.Content, ln); 58 layoutEngine.AddNodes(layoutNodes); 47 59 layoutEngine.CalculateLayout(); 48 60 var nodeCoordinates = layoutEngine.GetNodeCoordinates(); 49 61 var sb = new StringBuilder(); 50 62 var nl = Environment.NewLine; 63 double ws = 1; 64 double hs = 0.7; 65 51 66 sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl + 52 67 "\\usepackage{tikz}" + nl + … … 56 71 "\\def\\hs{0.7}" + nl); 57 72 58 const double ws = 1.0, hs = 0.7; // some scaling factors useful for fine-tuning document appearance before latex compilation73 var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>(); 59 74 var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList(); 60 var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();61 75 for (int i = 0; i < nodes.Count; ++i) { 62 76 var node = nodes[i]; 63 77 nodeIndices.Add(node, i); 64 var symbolName = node.Symbol.Name;65 var nodeName = SymbolNamesMap.ContainsKey(symbolName) ? SymbolNamesMap[symbolName] : symbolName;66 78 var coord = nodeCoordinates[node]; 67 79 var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString(); 68 80 sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName))); 69 81 } … … 75 87 } 76 88 77 sb.Append Line("\\end{tikzpicture}" + nl + "\\end{document}" + nl);78 89 sb.Append("\\end{tikzpicture}" + nl + 90 "\\end{document}" + nl); 79 91 return sb.ToString(); 80 92 }
Note: See TracChangeset
for help on using the changeset viewer.