[4803] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 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 |
|
---|
| 22 | using System.Text;
|
---|
[5499] | 23 | using System.Linq;
|
---|
[4820] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[17097] | 26 | using HEAL.Attic;
|
---|
[5750] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 29 | using System;
|
---|
[6460] | 30 | using System.Globalization;
|
---|
[4803] | 31 |
|
---|
[5750] | 32 | namespace HeuristicLab.Problems.ExternalEvaluation.GP {
|
---|
[4820] | 33 |
|
---|
[5750] | 34 | [Item("ExternalEvaluationSymbolicExpressionTreeStringFormatter", "A string formatter for symbolic expression trees for external evaluation.")]
|
---|
[17097] | 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]
|
---|
[17097] | 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);
|
---|
[15131] | 60 | if (node.Subtrees.Any()) { // internal node
|
---|
[6460] | 61 | strBuilder.Append("(");
|
---|
[5750] | 62 | if (node.Symbol is Addition) {
|
---|
| 63 | strBuilder.AppendLine("+");
|
---|
| 64 | } else if (node.Symbol is And) {
|
---|
| 65 | strBuilder.AppendLine("&&");
|
---|
| 66 | } else if (node.Symbol is Average) {
|
---|
| 67 | strBuilder.AppendLine("avg");
|
---|
| 68 | } else if (node.Symbol is Cosine) {
|
---|
| 69 | strBuilder.AppendLine("cos");
|
---|
| 70 | } else if (node.Symbol is Division) {
|
---|
| 71 | strBuilder.AppendLine("/");
|
---|
| 72 | } else if (node.Symbol is Exponential) {
|
---|
| 73 | strBuilder.AppendLine("exp");
|
---|
| 74 | } else if (node.Symbol is GreaterThan) {
|
---|
| 75 | strBuilder.AppendLine(">");
|
---|
| 76 | } else if (node.Symbol is IfThenElse) {
|
---|
| 77 | strBuilder.AppendLine("if");
|
---|
| 78 | } else if (node.Symbol is LessThan) {
|
---|
| 79 | strBuilder.AppendLine("<");
|
---|
| 80 | } else if (node.Symbol is Logarithm) {
|
---|
| 81 | strBuilder.AppendLine("ln");
|
---|
| 82 | } else if (node.Symbol is Multiplication) {
|
---|
| 83 | strBuilder.AppendLine("*");
|
---|
| 84 | } else if (node.Symbol is Not) {
|
---|
| 85 | strBuilder.AppendLine("!");
|
---|
| 86 | } else if (node.Symbol is Or) {
|
---|
| 87 | strBuilder.AppendLine("||");
|
---|
| 88 | } else if (node.Symbol is Sine) {
|
---|
| 89 | strBuilder.AppendLine("sin");
|
---|
| 90 | } else if (node.Symbol is Subtraction) {
|
---|
| 91 | strBuilder.AppendLine("-");
|
---|
| 92 | } else if (node.Symbol is Tangent) {
|
---|
| 93 | strBuilder.AppendLine("tan");
|
---|
| 94 | } else {
|
---|
| 95 | throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
|
---|
| 96 | }
|
---|
[4803] | 97 | // each subtree expression on a new line
|
---|
| 98 | // and closing ')' also on new line
|
---|
[5733] | 99 | foreach (var subtree in node.Subtrees) {
|
---|
[4803] | 100 | strBuilder.AppendLine(FormatRecursively(subtree, indentLength + 2));
|
---|
| 101 | }
|
---|
| 102 | if (Indent) strBuilder.Append(' ', indentLength);
|
---|
| 103 | strBuilder.Append(")");
|
---|
| 104 | } else {
|
---|
[15131] | 105 | if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
|
---|
[5750] | 106 | var varNode = node as VariableTreeNode;
|
---|
[6460] | 107 | strBuilder.AppendFormat("(* {0} {1})", varNode.VariableName, varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
|
---|
[15131] | 108 | } else if (node.Symbol is Constant) {
|
---|
[5750] | 109 | var constNode = node as ConstantTreeNode;
|
---|
[6460] | 110 | strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
|
---|
[5750] | 111 | } else {
|
---|
| 112 | throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
|
---|
| 113 | }
|
---|
[4803] | 114 | }
|
---|
| 115 | return strBuilder.ToString();
|
---|
| 116 | }
|
---|
[4820] | 117 |
|
---|
| 118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5750] | 119 | return new ExternalEvaluationSymbolicExpressionTreeStringFormatter(this, cloner);
|
---|
[4820] | 120 | }
|
---|
[4803] | 121 | }
|
---|
| 122 | }
|
---|