Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/21/14 18:20:47 (11 years ago)
Author:
bburlacu
Message:

#1772: Merged trunk changes and added missing frame files (for HeuristicLab.EvolutionTracking and HeuristicLab.EvolutionTracking.Views.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding

  • 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
     22using System;
    223using System.Collections.Generic;
    324using System.Globalization;
     
    829
    930namespace 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.")]
    1132  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>
    1334    {
    1435      {"ProgramRootSymbol", "Prog"},
    15       {"StartSymbol","RPB"},
    16       {"Addition", "$+$"},
    17       {"Subtraction", "$-$"},
    18       {"Multiplication", "$\\times$"},
    19       {"Division", "$\\div$"},
    20       {"Logarithm", "$\\log$"},
    21       {"Exponential", "$\\exp$"}
     36      {"StartSymbol","RPB"}
    2237    };
    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();
    2540
    26     public SymbolicExpressionTreeLatexFormatter() {
     41    public SymbolicExpressionTreeLatexFormatter()
     42      : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") {
    2743      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
    28       layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
    2944    }
    3045
    3146    protected SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner)
    3247      : base(original, cloner) {
    33       layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
    34       layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
    3548    }
    3649
     
    4053
    4154    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
     55      layoutEngine.Reset();
    4256      var layoutNodes = layoutAdapter.Convert(symbolicExpressionTree).ToList();
    43       layoutEngine.Reset();
    4457      layoutEngine.Root = layoutNodes[0];
    45       foreach (var ln in layoutNodes)
    46         layoutEngine.AddNode(ln.Content, ln);
     58      layoutEngine.AddNodes(layoutNodes);
    4759      layoutEngine.CalculateLayout();
    4860      var nodeCoordinates = layoutEngine.GetNodeCoordinates();
    4961      var sb = new StringBuilder();
    5062      var nl = Environment.NewLine;
     63      double ws = 1;
     64      double hs = 0.7;
     65
    5166      sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
    5267                "\\usepackage{tikz}" + nl +
     
    5671                "\\def\\hs{0.7}" + nl);
    5772
    58       const double ws = 1.0, hs = 0.7; // some scaling factors useful for fine-tuning document appearance before latex compilation
     73      var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();
    5974      var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
    60       var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();
    6175      for (int i = 0; i < nodes.Count; ++i) {
    6276        var node = nodes[i];
    6377        nodeIndices.Add(node, i);
    64         var symbolName = node.Symbol.Name;
    65         var nodeName = SymbolNamesMap.ContainsKey(symbolName) ? SymbolNamesMap[symbolName] : symbolName;
    6678        var coord = nodeCoordinates[node];
    67 
     79        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
    6880        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
    6981      }
     
    7587      }
    7688
    77       sb.AppendLine("\\end{tikzpicture}" + nl + "\\end{document}" + nl);
    78 
     89      sb.Append("\\end{tikzpicture}" + nl +
     90                "\\end{document}" + nl);
    7991      return sb.ToString();
    8092    }
Note: See TracChangeset for help on using the changeset viewer.