Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3136_Structural_GP/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationSymbolicExpressionTreeStringFormatter.cs @ 18157

Last change on this file since 18157 was 18157, checked in by dpiringe, 2 years ago

#3136

  • adapted formatters to support SubFunctionSymbol
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Text;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Common;
26using HEAL.Attic;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using System;
30using System.Globalization;
31
32namespace HeuristicLab.Problems.ExternalEvaluation.GP {
33
34  [Item("ExternalEvaluationSymbolicExpressionTreeStringFormatter", "A string formatter for symbolic expression trees for external evaluation.")]
35  [StorableType("6621B850-D3F2-4824-B6A8-19F4969AFB44")]
36  public class ExternalEvaluationSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
37
38    public bool Indent { get; set; }
39
40    [StorableConstructor]
41    protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(StorableConstructorFlag _) : base(_) { }
42    protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(ExternalEvaluationSymbolicExpressionTreeStringFormatter original, Cloner cloner)
43      : base(original, cloner) {
44      Indent = original.Indent;
45    }
46    public ExternalEvaluationSymbolicExpressionTreeStringFormatter()
47      : base() {
48      Name = "External Evaluation Symbolic Expression Tree Formatter";
49      Indent = true;
50    }
51
52    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
53      // skip root and start symbols
54      return FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), 0);
55    }
56
57    private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength) {
58      StringBuilder strBuilder = new StringBuilder();
59      if (Indent) strBuilder.Append(' ', indentLength);
60      if(node.Symbol is SubFunctionSymbol) {
61        return FormatRecursively(node.GetSubtree(0), indentLength);
62      } else if (node.Subtrees.Any()) { // internal node
63        strBuilder.Append("(");
64        if (node.Symbol is Addition) {
65          strBuilder.AppendLine("+");
66        } else if (node.Symbol is And) {
67          strBuilder.AppendLine("&&");
68        } else if (node.Symbol is Average) {
69          strBuilder.AppendLine("avg");
70        } else if (node.Symbol is Cosine) {
71          strBuilder.AppendLine("cos");
72        } else if (node.Symbol is Division) {
73          strBuilder.AppendLine("/");
74        } else if (node.Symbol is Exponential) {
75          strBuilder.AppendLine("exp");
76        } else if (node.Symbol is GreaterThan) {
77          strBuilder.AppendLine(">");
78        } else if (node.Symbol is IfThenElse) {
79          strBuilder.AppendLine("if");
80        } else if (node.Symbol is LessThan) {
81          strBuilder.AppendLine("<");
82        } else if (node.Symbol is Logarithm) {
83          strBuilder.AppendLine("ln");
84        } else if (node.Symbol is Multiplication) {
85          strBuilder.AppendLine("*");
86        } else if (node.Symbol is Not) {
87          strBuilder.AppendLine("!");
88        } else if (node.Symbol is Or) {
89          strBuilder.AppendLine("||");
90        } else if (node.Symbol is Sine) {
91          strBuilder.AppendLine("sin");
92        } else if (node.Symbol is Subtraction) {
93          strBuilder.AppendLine("-");
94        } else if (node.Symbol is Tangent) {
95          strBuilder.AppendLine("tan");
96        } else {
97          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
98        }
99        // each subtree expression on a new line
100        // and closing ')' also on new line
101        foreach (var subtree in node.Subtrees) {
102          strBuilder.AppendLine(FormatRecursively(subtree, indentLength + 2));
103        }
104        if (Indent) strBuilder.Append(' ', indentLength);
105        strBuilder.Append(")");
106      } else {
107        if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
108          var varNode = node as VariableTreeNode;
109          strBuilder.AppendFormat("(* {0} {1})", varNode.VariableName, varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
110        } else if (node.Symbol is INumericSymbol) {
111          var numericNode = node as INumericTreeNode;
112          strBuilder.Append(numericNode.Value.ToString("g17", CultureInfo.InvariantCulture));
113        } else {
114          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
115        }
116      }
117      return strBuilder.ToString();
118    }
119
120    public override IDeepCloneable Clone(Cloner cloner) {
121      return new ExternalEvaluationSymbolicExpressionTreeStringFormatter(this, cloner);
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.