[4327] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4327] | 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 |
|
---|
[9821] | 22 | using System.Collections.Generic;
|
---|
[4327] | 23 | using System.Text;
|
---|
[9821] | 24 | using HeuristicLab.Common;
|
---|
[4900] | 25 | using HeuristicLab.Core;
|
---|
[4327] | 26 |
|
---|
[9821] | 27 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[4968] | 28 | [Item("GraphViz String Formatter", "Formatter for symbolic expression trees for visualization with GraphViz.")]
|
---|
[4900] | 29 | public sealed class SymbolicExpressionTreeGraphvizFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
[4327] | 30 | public bool Indent { get; set; }
|
---|
| 31 |
|
---|
| 32 | private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string>() {
|
---|
| 33 | // match Koza style
|
---|
| 34 | {"ProgramRootSymbol", "Prog"},
|
---|
[16802] | 35 | {"StartSymbol", "RPB"},
|
---|
| 36 |
|
---|
| 37 | // short form
|
---|
| 38 | {"Subtraction", "-" },
|
---|
| 39 | {"Addition", "+" },
|
---|
| 40 | {"Multiplication", "*" },
|
---|
| 41 | {"Division", "/" },
|
---|
| 42 | {"Absolute", "abs" },
|
---|
| 43 | {"AnalyticQuotient", "AQ" },
|
---|
| 44 | {"Sine", "sin" },
|
---|
| 45 | {"Cosine", "cos" },
|
---|
| 46 | {"Tanget", "tan" },
|
---|
| 47 | {"HyperbolicTangent", "tanh" },
|
---|
| 48 | {"Exponential", "exp" },
|
---|
| 49 | {"Logarithm", "log" },
|
---|
| 50 | {"SquareRoot", "sqrt" },
|
---|
| 51 | {"Square", "sqr" },
|
---|
| 52 | {"CubeRoot", "cbrt" },
|
---|
| 53 | {"Cube", "cube" },
|
---|
| 54 | {"GreaterThan", ">" },
|
---|
| 55 | {"LessThan", "<" },
|
---|
[4327] | 56 | };
|
---|
| 57 |
|
---|
| 58 | public SymbolicExpressionTreeGraphvizFormatter()
|
---|
[4968] | 59 | : base("GraphViz String Formatter", "Formatter for symbolic expression trees for visualization with GraphViz.") {
|
---|
[4327] | 60 | Indent = true;
|
---|
| 61 | }
|
---|
[4900] | 62 | private SymbolicExpressionTreeGraphvizFormatter(SymbolicExpressionTreeGraphvizFormatter original, Cloner cloner)
|
---|
| 63 | : base(original, cloner) {
|
---|
| 64 | this.Indent = original.Indent;
|
---|
| 65 | }
|
---|
[4327] | 66 |
|
---|
[4900] | 67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 68 | return new SymbolicExpressionTreeGraphvizFormatter(this, cloner);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[9649] | 71 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
[4327] | 72 | int nodeCounter = 1;
|
---|
| 73 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 74 | strBuilder.AppendLine("graph {");
|
---|
| 75 | strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root, 0, ref nodeCounter));
|
---|
| 76 | strBuilder.AppendLine("}");
|
---|
| 77 | return strBuilder.ToString();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[9649] | 80 | private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength, ref int nodeId) {
|
---|
[4327] | 81 | // save id of current node
|
---|
| 82 | int currentNodeId = nodeId;
|
---|
| 83 | // increment id for next node
|
---|
| 84 | nodeId++;
|
---|
| 85 |
|
---|
| 86 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 87 | if (Indent) strBuilder.Append(' ', indentLength);
|
---|
| 88 |
|
---|
| 89 | // get label for node and map if necessary
|
---|
| 90 | string nodeLabel = node.ToString();
|
---|
| 91 | if (symbolNameMap.ContainsKey(nodeLabel)) {
|
---|
| 92 | nodeLabel = symbolNameMap[nodeLabel];
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | strBuilder.Append("node" + currentNodeId + "[label=\"" + nodeLabel + "\"");
|
---|
| 96 | // leaf nodes should have box shape
|
---|
[9649] | 97 | if (node.SubtreeCount == 0) {
|
---|
[4327] | 98 | strBuilder.AppendLine(", shape=\"box\"];");
|
---|
| 99 | } else {
|
---|
| 100 | strBuilder.AppendLine("];");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | // internal nodes or leaf nodes?
|
---|
[9649] | 104 | foreach (ISymbolicExpressionTreeNode subTree in node.Subtrees) {
|
---|
[4327] | 105 | // add an edge
|
---|
| 106 | if (Indent) strBuilder.Append(' ', indentLength);
|
---|
| 107 | strBuilder.AppendLine("node" + currentNodeId + " -- node" + nodeId + ";");
|
---|
| 108 | // format the whole subtree
|
---|
| 109 | strBuilder.Append(FormatRecursively(subTree, indentLength + 2, ref nodeId));
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | return strBuilder.ToString();
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|