Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs @ 16144

Last change on this file since 16144 was 15973, checked in by gkronber, 6 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 Variable) {
163        VariableTreeNode variableTreeNode = node as VariableTreeNode;
164        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
165        stringBuilder.Append("*");
166        stringBuilder.Append(variableTreeNode.VariableName);
167      } else if (symbol is BinaryFactorVariable || symbol is FactorVariable) {
168        stringBuilder.Append("factor variables are not supported");
169      } else {
170        stringBuilder.Append("(");
171        for (int i = 0; i < node.SubtreeCount; i++) {
172          if (i > 0) stringBuilder.Append(", ");
173          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
174        }
175        stringBuilder.AppendFormat(" {0} [Not Supported] )", node.Symbol.Name);
176      }
177
178      stringBuilder.Append(")");
179
180      return stringBuilder.ToString();
181    }
182
183    public override IDeepCloneable Clone(Cloner cloner) {
184      return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner);
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.