Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationSymbolicExpressionTreeStringFormatter.cs @ 13656

Last change on this file since 13656 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
36  public class ExternalEvaluationSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
37
38    public bool Indent { get; set; }
39
40    [StorableConstructor]
41    protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(bool deserializing) : base(deserializing) { }
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.Subtrees.Count() > 0) { // internal node
61        strBuilder.Append("(");
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        }
97        // each subtree expression on a new line
98        // and closing ')' also on new line
99        foreach (var subtree in node.Subtrees) {
100          strBuilder.AppendLine(FormatRecursively(subtree, indentLength + 2));
101        }
102        if (Indent) strBuilder.Append(' ', indentLength);
103        strBuilder.Append(")");
104      } else {
105        if (node is VariableTreeNode) {
106          var varNode = node as VariableTreeNode;
107          strBuilder.AppendFormat("(* {0} {1})", varNode.VariableName, varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
108        } else if (node is ConstantTreeNode) {
109          var constNode = node as ConstantTreeNode;
110          strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture));
111        } else {
112          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for external evaluation.");
113        }
114      }
115      return strBuilder.ToString();
116    }
117
118    public override IDeepCloneable Clone(Cloner cloner) {
119      return new ExternalEvaluationSymbolicExpressionTreeStringFormatter(this, cloner);
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.