Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs @ 16723

Last change on this file since 16723 was 16723, checked in by abeham, 6 years ago

#2521: merged changes from r15684 to trunk HEAD (r16716) and resolved all merge conflicts

  • it doesn't build
File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Globalization;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29
30  [Item("Smalltalk String Formatter", "String formatter for string representations of symbolic expression trees in Smalltalk syntax.")]
31  public class SymbolicDataAnalysisExpressionSmalltalkFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
32
33    protected SymbolicDataAnalysisExpressionSmalltalkFormatter(SymbolicDataAnalysisExpressionSmalltalkFormatter original, Cloner cloner) : base(original, cloner) { }
34    public SymbolicDataAnalysisExpressionSmalltalkFormatter()
35      : base() {
36      Name = ItemName;
37      Description = ItemDescription;
38    }
39
40    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
41      return FormatRecursively(symbolicExpressionTree.Root);
42    }
43
44    // returns the smalltalk expression corresponding to the node
45    // smalltalk expressions are always surrounded by parantheses "(<expr>)"
46    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
47
48      ISymbol symbol = node.Symbol;
49
50      if (symbol is ProgramRootSymbol || symbol is StartSymbol)
51        return FormatRecursively(node.GetSubtree(0));
52
53      StringBuilder stringBuilder = new StringBuilder(20);
54
55      stringBuilder.Append("(");
56
57      if (symbol is Addition) {
58        for (int i = 0; i < node.SubtreeCount; i++) {
59          if (i > 0) stringBuilder.Append("+");
60          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
61        }
62      } else if (symbol is And) {
63        stringBuilder.Append("(");
64        for (int i = 0; i < node.SubtreeCount; i++) {
65          if (i > 0) stringBuilder.Append("&");
66          stringBuilder.Append("(");
67          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
68          stringBuilder.Append(" > 0)");
69        }
70        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
71      } else if (symbol is Average) {
72        stringBuilder.Append("(1/");
73        stringBuilder.Append(node.SubtreeCount);
74        stringBuilder.Append(")*(");
75        for (int i = 0; i < node.SubtreeCount; i++) {
76          if (i > 0) stringBuilder.Append("+");
77          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
78        }
79        stringBuilder.Append(")");
80      } else if (symbol is Constant) {
81        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
82        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
83      } else if (symbol is Cosine) {
84        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
85        stringBuilder.Append(" cos");
86      } else if (symbol is Division) {
87        if (node.SubtreeCount == 1) {
88          stringBuilder.Append("1/");
89          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
90        } else {
91          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
92          stringBuilder.Append("/(");
93          for (int i = 1; i < node.SubtreeCount; i++) {
94            if (i > 1) stringBuilder.Append("*");
95            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
96          }
97          stringBuilder.Append(")");
98        }
99      } else if (symbol is Exponential) {
100        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
101        stringBuilder.Append(" exp");
102      } else if (symbol is GreaterThan) {
103        stringBuilder.Append("(");
104        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
105        stringBuilder.Append(" > ");
106        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
107        stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
108      } else if (symbol is IfThenElse) {
109        stringBuilder.Append("(");
110        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
111        stringBuilder.Append(" > 0) ifTrue: [");
112        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
113        stringBuilder.Append("] ifFalse: [");
114        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
115        stringBuilder.Append("]");
116      } else if (symbol is LaggedVariable) {
117        stringBuilder.Append("lagged variables are not supported");
118      } else if (symbol is LessThan) {
119        stringBuilder.Append("(");
120        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
121        stringBuilder.Append(" < ");
122        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
123        stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
124      } else if (symbol is Logarithm) {
125        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
126        stringBuilder.Append("ln");
127      } else if (symbol is Multiplication) {
128        for (int i = 0; i < node.SubtreeCount; i++) {
129          if (i > 0) stringBuilder.Append("*");
130          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
131        }
132      } else if (symbol is Not) {
133        stringBuilder.Append("(");
134        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
135        stringBuilder.Append(">0) ifTrue: [-1] ifFalse: [1.0]");
136      } else if (symbol is Or) {
137        stringBuilder.Append("(");
138        for (int i = 0; i < node.SubtreeCount; i++) {
139          if (i > 0) stringBuilder.Append("|");
140          stringBuilder.Append("(");
141          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
142          stringBuilder.Append(">0)");
143        }
144        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
145      } else if (symbol is Sine) {
146        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
147        stringBuilder.Append(" sin");
148      } else if (symbol is Subtraction) {
149        if (node.SubtreeCount == 1) {
150          stringBuilder.Append("-1*");
151          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
152        } else {
153          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
154          for (int i = 1; i < node.SubtreeCount; i++) {
155            stringBuilder.Append(" - ");
156            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
157          }
158        }
159      } else if (symbol is Tangent) {
160        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
161        stringBuilder.Append(" tan");
162      } else if (symbol is HyperbolicTangent) {
163        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
164        stringBuilder.Append(" tanh");
165      } else if (symbol is Variable) {
166        VariableTreeNode variableTreeNode = node as VariableTreeNode;
167        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
168        stringBuilder.Append("*");
169        stringBuilder.Append(variableTreeNode.VariableName);
170      } else if (symbol is BinaryFactorVariable || symbol is FactorVariable) {
171        stringBuilder.Append("factor variables are not supported");
172      } else {
173        stringBuilder.Append("(");
174        for (int i = 0; i < node.SubtreeCount; i++) {
175          if (i > 0) stringBuilder.Append(", ");
176          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
177        }
178        stringBuilder.AppendFormat(" {0} [Not Supported] )", node.Symbol.Name);
179      }
180
181      stringBuilder.Append(")");
182
183      return stringBuilder.ToString();
184    }
185
186    public override IDeepCloneable Clone(Cloner cloner) {
187      return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner);
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.