[4803] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5750] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 29 | using System;
|
---|
[4803] | 30 |
|
---|
[5750] | 31 | namespace HeuristicLab.Problems.ExternalEvaluation.GP {
|
---|
[4820] | 32 |
|
---|
[5750] | 33 | [Item("ExternalEvaluationSymbolicExpressionTreeStringFormatter", "A string formatter for symbolic expression trees for external evaluation.")]
|
---|
[4820] | 34 | [StorableClass]
|
---|
[5750] | 35 | public class ExternalEvaluationSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
[4820] | 36 |
|
---|
[4803] | 37 | public bool Indent { get; set; }
|
---|
[4820] | 38 |
|
---|
| 39 | [StorableConstructor]
|
---|
[5750] | 40 | protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(bool deserializing) : base(deserializing) { }
|
---|
| 41 | protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(ExternalEvaluationSymbolicExpressionTreeStringFormatter original, Cloner cloner)
|
---|
[4840] | 42 | : base(original, cloner) {
|
---|
| 43 | Indent = original.Indent;
|
---|
| 44 | }
|
---|
[5750] | 45 | public ExternalEvaluationSymbolicExpressionTreeStringFormatter()
|
---|
[4803] | 46 | : base() {
|
---|
[5750] | 47 | Name = "External Evaluation Symbolic Expression Tree Formatter";
|
---|
[4803] | 48 | Indent = true;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[5510] | 51 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
[5750] | 52 | // skip root and start symbols
|
---|
| 53 | return FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), 0);
|
---|
[4803] | 54 | }
|
---|
| 55 |
|
---|
[5499] | 56 | private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength) {
|
---|
[4803] | 57 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 58 | if (Indent) strBuilder.Append(' ', indentLength);
|
---|
| 59 | strBuilder.Append("(");
|
---|
| 60 | // internal nodes or leaf nodes?
|
---|
[5733] | 61 | if (node.Subtrees.Count() > 0) {
|
---|
[4803] | 62 | // symbol on same line as '('
|
---|
[5750] | 63 | if (node.Symbol is Addition) {
|
---|
| 64 | strBuilder.AppendLine("+");
|
---|
| 65 | } else if (node.Symbol is And) {
|
---|
| 66 | strBuilder.AppendLine("&&");
|
---|
| 67 | } else if (node.Symbol is Average) {
|
---|
| 68 | strBuilder.AppendLine("avg");
|
---|
| 69 | } else if (node.Symbol is Cosine) {
|
---|
| 70 | strBuilder.AppendLine("cos");
|
---|
| 71 | } else if (node.Symbol is Division) {
|
---|
| 72 | strBuilder.AppendLine("/");
|
---|
| 73 | } else if (node.Symbol is Exponential) {
|
---|
| 74 | strBuilder.AppendLine("exp");
|
---|
| 75 | } else if (node.Symbol is GreaterThan) {
|
---|
| 76 | strBuilder.AppendLine(">");
|
---|
| 77 | } else if (node.Symbol is IfThenElse) {
|
---|
| 78 | strBuilder.AppendLine("if");
|
---|
| 79 | } else if (node.Symbol is LessThan) {
|
---|
| 80 | strBuilder.AppendLine("<");
|
---|
| 81 | } else if (node.Symbol is Logarithm) {
|
---|
| 82 | strBuilder.AppendLine("ln");
|
---|
| 83 | } else if (node.Symbol is Multiplication) {
|
---|
| 84 | strBuilder.AppendLine("*");
|
---|
| 85 | } else if (node.Symbol is Not) {
|
---|
| 86 | strBuilder.AppendLine("!");
|
---|
| 87 | } else if (node.Symbol is Or) {
|
---|
| 88 | strBuilder.AppendLine("||");
|
---|
| 89 | } else if (node.Symbol is Sine) {
|
---|
| 90 | strBuilder.AppendLine("sin");
|
---|
| 91 | } else if (node.Symbol is Subtraction) {
|
---|
| 92 | strBuilder.AppendLine("-");
|
---|
| 93 | } else if (node.Symbol is Tangent) {
|
---|
| 94 | strBuilder.AppendLine("tan");
|
---|
| 95 | } else {
|
---|
| 96 | throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
|
---|
| 97 | }
|
---|
[4803] | 98 | // each subtree expression on a new line
|
---|
| 99 | // and closing ')' also on new line
|
---|
[5750] | 100 |
|
---|
[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 {
|
---|
[5750] | 107 | if (node is VariableTreeNode) {
|
---|
| 108 | var varNode = node as VariableTreeNode;
|
---|
| 109 | // symbol in the same line with as '(' and ')'
|
---|
| 110 | strBuilder.Append(";" + varNode.VariableName + ";" + varNode.Weight.ToString("E4"));
|
---|
| 111 | strBuilder.Append(")");
|
---|
| 112 | } else if (node is ConstantTreeNode) {
|
---|
| 113 | var constNode = node as ConstantTreeNode;
|
---|
| 114 | // symbol in the same line with as '(' and ')'
|
---|
| 115 | strBuilder.Append(";" + constNode.Value.ToString("E4"));
|
---|
| 116 | strBuilder.Append(")");
|
---|
| 117 | } else {
|
---|
| 118 | throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
|
---|
| 119 | }
|
---|
[4803] | 120 | }
|
---|
| 121 | return strBuilder.ToString();
|
---|
| 122 | }
|
---|
[4820] | 123 |
|
---|
| 124 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5750] | 125 | return new ExternalEvaluationSymbolicExpressionTreeStringFormatter(this, cloner);
|
---|
[4820] | 126 | }
|
---|
[4803] | 127 | }
|
---|
| 128 | }
|
---|