Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs @ 17869

Last change on this file since 17869 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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