Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation.GP/3.4/ExternalEvaluationSymbolicExpressionTreeStringFormatter.cs @ 5809

Last change on this file since 5809 was 5809, checked in by mkommend, 12 years ago

#1418: Reintegrated branch into trunk.

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
30
31namespace HeuristicLab.Problems.ExternalEvaluation.GP {
32
33  [Item("ExternalEvaluationSymbolicExpressionTreeStringFormatter", "A string formatter for symbolic expression trees for external evaluation.")]
34  [StorableClass]
35  public class ExternalEvaluationSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
36
37    public bool Indent { get; set; }
38
39    [StorableConstructor]
40    protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(bool deserializing) : base(deserializing) { }
41    protected ExternalEvaluationSymbolicExpressionTreeStringFormatter(ExternalEvaluationSymbolicExpressionTreeStringFormatter original, Cloner cloner)
42      : base(original, cloner) {
43      Indent = original.Indent;
44    }
45    public ExternalEvaluationSymbolicExpressionTreeStringFormatter()
46      : base() {
47      Name = "External Evaluation Symbolic Expression Tree Formatter";
48      Indent = true;
49    }
50
51    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
52      // skip root and start symbols
53      return FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), 0);
54    }
55
56    private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength) {
57      StringBuilder strBuilder = new StringBuilder();
58      if (Indent) strBuilder.Append(' ', indentLength);
59      strBuilder.Append("(");
60      // internal nodes or leaf nodes?
61      if (node.Subtrees.Count() > 0) {
62        // symbol on same line as '('
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        }
98        // each subtree expression on a new line
99        // and closing ')' also on new line
100
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 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        }
120      }
121      return strBuilder.ToString();
122    }
123
124    public override IDeepCloneable Clone(Cloner cloner) {
125      return new ExternalEvaluationSymbolicExpressionTreeStringFormatter(this, cloner);
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.