Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HivePerformance/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicExpressionTreeSmalltalkStringFormatter.cs @ 9655

Last change on this file since 9655 was 9655, checked in by ascheibe, 11 years ago

#2030 merged trunk into hive performance branch

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