Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4969 was 4906, checked in by swinkler, 14 years ago

Removed attributes Storable and StorableClass from tree formatters. (#1270)

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