Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationSymbolicExpressionTreeStringFormatter.cs @ 18242

Last change on this file since 18242 was 18220, checked in by gkronber, 3 years ago

#3136: reintegrated structure-template GP branch into trunk

File size: 5.3 KB
RevLine 
[4803]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4803]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;
[5499]23using System.Linq;
[4820]24using HeuristicLab.Core;
25using HeuristicLab.Common;
[16565]26using HEAL.Attic;
[5750]27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using System;
[6460]30using System.Globalization;
[4803]31
[5750]32namespace HeuristicLab.Problems.ExternalEvaluation.GP {
[4820]33
[5750]34  [Item("ExternalEvaluationSymbolicExpressionTreeStringFormatter", "A string formatter for symbolic expression trees for external evaluation.")]
[16565]35  [StorableType("6621B850-D3F2-4824-B6A8-19F4969AFB44")]
[5750]36  public class ExternalEvaluationSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
[4820]37
[4803]38    public bool Indent { get; set; }
[4820]39
40    [StorableConstructor]
[16565]41    protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(StorableConstructorFlag _) : base(_) { }
[5750]42    protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(ExternalEvaluationSymbolicExpressionTreeStringFormatter original, Cloner cloner)
[4840]43      : base(original, cloner) {
44      Indent = original.Indent;
45    }
[5750]46    public ExternalEvaluationSymbolicExpressionTreeStringFormatter()
[4803]47      : base() {
[5750]48      Name = "External Evaluation Symbolic Expression Tree Formatter";
[4803]49      Indent = true;
50    }
51
[5510]52    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
[5750]53      // skip root and start symbols
54      return FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), 0);
[4803]55    }
56
[5499]57    private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength) {
[4803]58      StringBuilder strBuilder = new StringBuilder();
59      if (Indent) strBuilder.Append(' ', indentLength);
[18220]60      if(node.Symbol is SubFunctionSymbol) {
61        return FormatRecursively(node.GetSubtree(0), indentLength);
62      } else if (node.Subtrees.Any()) { // internal node
[6460]63        strBuilder.Append("(");
[5750]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        }
[4803]99        // each subtree expression on a new line
100        // and closing ')' also on new line
[5733]101        foreach (var subtree in node.Subtrees) {
[4803]102          strBuilder.AppendLine(FormatRecursively(subtree, indentLength + 2));
103        }
104        if (Indent) strBuilder.Append(' ', indentLength);
105        strBuilder.Append(")");
106      } else {
[14826]107        if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
[5750]108          var varNode = node as VariableTreeNode;
[6460]109          strBuilder.AppendFormat("(* {0} {1})", varNode.VariableName, varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
[18132]110        } else if (node.Symbol is INumericSymbol) {
111          var numericNode = node as INumericTreeNode;
112          strBuilder.Append(numericNode.Value.ToString("g17", CultureInfo.InvariantCulture));
[5750]113        } else {
114          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
115        }
[4803]116      }
117      return strBuilder.ToString();
118    }
[4820]119
120    public override IDeepCloneable Clone(Cloner cloner) {
[5750]121      return new ExternalEvaluationSymbolicExpressionTreeStringFormatter(this, cloner);
[4820]122    }
[4803]123  }
124}
Note: See TracBrowser for help on using the repository browser.