Changeset 10456 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Timestamp:
- 02/15/14 13:50:37 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
/branches/HLScript/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding (added) merged: 10358 /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding merged: 10346,10359
- Property svn:mergeinfo changed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/MultiSymbolicExpressionTreeArchitectureManipulator.cs
r9456 r10456 106 106 Operators = checkedItemList.AsReadOnly(); 107 107 Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems)); 108 109 SelectedOperatorParameter.ActualName = "SelectedManipulationOperator"; 108 110 } 109 111 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeLatexFormatter.cs
r10302 r10456 1 using System.Collections.Generic; 1 using System; 2 using System.Collections.Generic; 3 using System.Globalization; 2 4 using System.Linq; 3 5 using System.Text; … … 8 10 [Item("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex.")] 9 11 public class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 10 private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string>12 private readonly static Dictionary<string, string> SymbolNamesMap = new Dictionary<string, string> 11 13 { 12 14 {"ProgramRootSymbol", "Prog"}, 13 {"StartSymbol","RPB"} 15 {"StartSymbol","RPB"}, 16 {"Addition", "$+$"}, 17 {"Subtraction", "$-$"}, 18 {"Multiplication", "$\\times$"}, 19 {"Division", "$\\div$"} 14 20 }; 15 21 private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine; 16 22 private readonly SymbolicExpressionTreeLayoutAdapter layoutAdapter; 17 23 18 19 24 public SymbolicExpressionTreeLatexFormatter() { 20 25 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(); 26 layoutAdapter = new SymbolicExpressionTreeLayoutAdapter(); 21 27 } 22 28 … … 37 43 foreach (var ln in layoutNodes) 38 44 layoutEngine.AddNode(ln.Content, ln); 45 layoutEngine.CalculateLayout(); 39 46 var nodeCoordinates = layoutEngine.GetNodeCoordinates(); 40 47 var sb = new StringBuilder(); 41 sb.AppendLine("\\begin{figure}"); 42 sb.AppendLine("\\begin{tikzpicture}"); 48 var nl = Environment.NewLine; 49 sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl + 50 "\\usepackage{tikz}" + nl + 51 "\\begin{document}" + nl + 52 "\\begin{tikzpicture}" + nl + 53 "\\def\\ws{1}" + nl + 54 "\\def\\hs{0.7}" + nl); 43 55 44 foreach (var node in symbolicExpressionTree.IterateNodesBreadth()) { 56 const double ws = 1.0, hs = 0.7; // some scaling factors useful for fine-tuning document appearance before latex compilation 57 var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList(); 58 var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>(); 59 for (int i = 0; i < nodes.Count; ++i) { 60 var node = nodes[i]; 61 nodeIndices.Add(node, i); 45 62 var symbolName = node.Symbol.Name; 46 var nodeName = symbolNameMap.ContainsKey(symbolName) ? symbolNameMap[symbolName] : symbolName;63 var nodeName = SymbolNamesMap.ContainsKey(symbolName) ? SymbolNamesMap[symbolName] : symbolName; 47 64 var coord = nodeCoordinates[node]; 48 65 49 sb.AppendLine(string.Format( "\\draw node ({0},{1}) {{{2}}};", coord.X, coord.Y, nodeName));66 sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName))); 50 67 } 51 68 52 sb.AppendLine("\\end{figure}"); 53 sb.AppendLine("\\end{tikzpicture}"); 69 for (int i = 0; i < nodes.Count; ++i) { 70 foreach (var s in nodes[i].Subtrees) { 71 sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", i, nodeIndices[s])); 72 } 73 } 74 75 sb.AppendLine("\\end{tikzpicture}" + nl + "\\end{document}" + nl); 54 76 55 77 return sb.ToString(); 56 78 } 79 80 private static string EscapeLatexString(string s) { 81 return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_"); 82 } 57 83 } 58 84 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/LayoutEngines/SymbolicExpressionTreeLayoutAdapter.cs
r10269 r10456 28 28 public class SymbolicExpressionTreeLayoutAdapter : ILayoutAdapter<ISymbolicExpressionTreeNode> { 29 29 // default conversion function between ISymbolicExpressionTreeNode and ILayoutNode<ISymbolicExpressionTree> 30 ILayoutNode<ISymbolicExpressionTreeNode> defaultConvert(ISymbolicExpressionTreeNode node) {30 static ILayoutNode<ISymbolicExpressionTreeNode> DefaultConvert(ISymbolicExpressionTreeNode node) { 31 31 var layoutNode = new LayoutNode<ISymbolicExpressionTreeNode> { Content = node }; 32 32 layoutNode.Ancestor = layoutNode; … … 40 40 // return an enumerable containing all the layout nodes 41 41 public IEnumerable<ILayoutNode<ISymbolicExpressionTreeNode>> Convert(ISymbolicExpressionTreeNode root, Func<ISymbolicExpressionTreeNode, ILayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) { 42 var rootLayoutNode = convertFunc == null ? defaultConvert(root) : convertFunc(root);42 var rootLayoutNode = convertFunc == null ? DefaultConvert(root) : convertFunc(root); 43 43 rootLayoutNode.Ancestor = rootLayoutNode; 44 44 … … 63 63 for (int i = 0; i < layoutNode.Content.SubtreeCount; ++i) { 64 64 var subtree = layoutNode.Content.GetSubtree(i); 65 var childLayoutNode = convertFunc == null ? defaultConvert(subtree) : convertFunc(subtree);65 var childLayoutNode = convertFunc == null ? DefaultConvert(subtree) : convertFunc(subtree); 66 66 childLayoutNode.Parent = layoutNode; 67 67 childLayoutNode.Number = i; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/MultiSymbolicExpressionTreeManipulator.cs
r9456 r10456 83 83 Operators = checkedItemList.AsReadOnly(); 84 84 Operators_ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<ISymbolicExpressionTreeManipulator>>(Operators.CheckedItems)); 85 86 SelectedOperatorParameter.ActualName = "SelectedManipulationOperator"; 85 87 } 86 88
Note: See TracChangeset
for help on using the changeset viewer.