Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.Extensions/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeSmalltalkStringFormatter.cs @ 4869

Last change on this file since 4869 was 4869, checked in by swinkler, 13 years ago

Fixed bugs in Smalltalk formatter. (#1270)

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