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 |
|
---|
22 | using System.Text;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
28 |
|
---|
29 | namespace 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("(");
|
---|
62 | stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
|
---|
63 | stringBuilder.Append(")");
|
---|
64 | }
|
---|
65 | } else if (symbol is And) {
|
---|
66 | stringBuilder.Append("(");
|
---|
67 | for (int i = 0; i < node.SubTrees.Count; i++) {
|
---|
68 | if (i > 0) stringBuilder.Append("&");
|
---|
69 | stringBuilder.Append("((");
|
---|
70 | stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
|
---|
71 | stringBuilder.Append(")>=0)");
|
---|
72 | }
|
---|
73 | stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
|
---|
74 | } else if (symbol is Average) {
|
---|
75 | stringBuilder.Append("(1/");
|
---|
76 | stringBuilder.Append(node.SubTrees.Count);
|
---|
77 | stringBuilder.Append(")*(");
|
---|
78 | for (int i = 0; i < node.SubTrees.Count; i++) {
|
---|
79 | if (i > 0) stringBuilder.Append("+");
|
---|
80 | stringBuilder.Append("(");
|
---|
81 | stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
|
---|
82 | stringBuilder.Append(")");
|
---|
83 | }
|
---|
84 | stringBuilder.Append(")");
|
---|
85 | } else if (symbol is Constant) {
|
---|
86 | ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
|
---|
87 | stringBuilder.Append(constantTreeNode.Value);
|
---|
88 | } else if (symbol is Cosine) {
|
---|
89 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
90 | stringBuilder.Append(" cos");
|
---|
91 | } else if (symbol is Division) {
|
---|
92 | if (node.SubTrees.Count == 1) {
|
---|
93 | stringBuilder.Append("1/");
|
---|
94 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
95 | } else {
|
---|
96 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
97 | stringBuilder.Append("/(");
|
---|
98 | for (int i = 1; i < node.SubTrees.Count; i++) {
|
---|
99 | if (i > 1) stringBuilder.Append("*");
|
---|
100 | stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
|
---|
101 | }
|
---|
102 | stringBuilder.Append(")");
|
---|
103 | }
|
---|
104 | } else if (symbol is Exponential) {
|
---|
105 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
106 | stringBuilder.Append(" exp");
|
---|
107 | } else if (symbol is GreaterThan) {
|
---|
108 | stringBuilder.Append("(");
|
---|
109 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
110 | stringBuilder.Append(" > ");
|
---|
111 | stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
|
---|
112 | stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
|
---|
113 | } else if (symbol is IfThenElse) {
|
---|
114 | stringBuilder.Append("(");
|
---|
115 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
116 | stringBuilder.Append(" > 0) ifTrue: [");
|
---|
117 | stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
|
---|
118 | stringBuilder.Append("] ifFalse: [");
|
---|
119 | stringBuilder.Append(FormatRecursively(node.SubTrees[2]));
|
---|
120 | stringBuilder.Append("] )");
|
---|
121 | } else if (symbol is LaggedVariable) {
|
---|
122 | stringBuilder.Append("not implemented");
|
---|
123 | } else if (symbol is LessThan) {
|
---|
124 | stringBuilder.Append("(");
|
---|
125 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
126 | stringBuilder.Append(" < ");
|
---|
127 | stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
|
---|
128 | stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
|
---|
129 | } else if (symbol is Logarithm) {
|
---|
130 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
131 | stringBuilder.Append("ln");
|
---|
132 | } else if (symbol is Multiplication) {
|
---|
133 | for (int i = 0; i < node.SubTrees.Count; i++) {
|
---|
134 | if (i > 0) stringBuilder.Append("*");
|
---|
135 | stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
|
---|
136 | }
|
---|
137 | } else if (symbol is Not) {
|
---|
138 | stringBuilder.Append("-");
|
---|
139 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
140 | } else if (symbol is Or) {
|
---|
141 | stringBuilder.Append("(");
|
---|
142 | for (int i = 0; i < node.SubTrees.Count; i++) {
|
---|
143 | if (i > 0) stringBuilder.Append("|");
|
---|
144 | stringBuilder.Append("((");
|
---|
145 | stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
|
---|
146 | stringBuilder.Append(")>=0)");
|
---|
147 | }
|
---|
148 | stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
|
---|
149 | } else if (symbol is Sine) {
|
---|
150 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
151 | stringBuilder.Append(" sin");
|
---|
152 | } else if (symbol is Subtraction) {
|
---|
153 | if (node.SubTrees.Count == 1) {
|
---|
154 | stringBuilder.Append("-");
|
---|
155 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
156 | } else {
|
---|
157 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
158 | for (int i = 1; i < node.SubTrees.Count; i++) {
|
---|
159 | stringBuilder.Append("-");
|
---|
160 | stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
|
---|
161 | }
|
---|
162 | }
|
---|
163 | } else if (symbol is Tangent) {
|
---|
164 | stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
|
---|
165 | stringBuilder.Append("tan");
|
---|
166 | } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
|
---|
167 | VariableTreeNode variableTreeNode = node as VariableTreeNode;
|
---|
168 | stringBuilder.Append(variableTreeNode.Weight);
|
---|
169 | stringBuilder.Append("*");
|
---|
170 | stringBuilder.Append(variableTreeNode.VariableName);
|
---|
171 | }
|
---|
172 |
|
---|
173 | else {
|
---|
174 | stringBuilder.Append("ERROR");
|
---|
175 | }
|
---|
176 |
|
---|
177 | stringBuilder.Append(")");
|
---|
178 |
|
---|
179 | return stringBuilder.ToString();
|
---|
180 | }
|
---|
181 |
|
---|
182 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
183 | return new SymbolicExpressionTreeSmalltalkStringFormatter(this, cloner);
|
---|
184 | }
|
---|
185 |
|
---|
186 | }
|
---|
187 |
|
---|
188 | }
|
---|