Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs @ 14237

Last change on this file since 14237 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Globalization;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  [Item("Mathematica Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to Mathematica expressions.")]
33  [StorableClass]
34  public sealed class SymbolicDataAnalysisExpressionMathematicaFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
35    [StorableConstructor]
36    private SymbolicDataAnalysisExpressionMathematicaFormatter(bool deserializing) : base(deserializing) { }
37    private SymbolicDataAnalysisExpressionMathematicaFormatter(SymbolicDataAnalysisExpressionMathematicaFormatter original, Cloner cloner) : base(original, cloner) { }
38    public SymbolicDataAnalysisExpressionMathematicaFormatter()
39      : base() {
40      Name = ItemName;
41      Description = ItemDescription;
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new SymbolicDataAnalysisExpressionMathematicaFormatter(this, cloner);
45    }
46
47    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
48      // skip root and start symbols
49      StringBuilder strBuilder = new StringBuilder();
50      FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder);
51      return strBuilder.ToString();
52    }
53
54    private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
55      if (node.Subtrees.Any()) {
56        if (node.Symbol is Addition) {
57          FormatFunction(node, "Plus", strBuilder);
58        } else if (node.Symbol is Average) {
59          FormatAverage(node, strBuilder);
60        } else if (node.Symbol is Multiplication) {
61          FormatFunction(node, "Times", strBuilder);
62        } else if (node.Symbol is Subtraction) {
63          FormatSubtraction(node, strBuilder);
64        } else if (node.Symbol is Division) {
65          FormatDivision(node, strBuilder);
66        } else if (node.Symbol is Sine) {
67          FormatFunction(node, "Sin", strBuilder);
68        } else if (node.Symbol is Cosine) {
69          FormatFunction(node, "Cos", strBuilder);
70        } else if (node.Symbol is Tangent) {
71          FormatFunction(node, "Tan", strBuilder);
72        } else if (node.Symbol is Exponential) {
73          FormatFunction(node, "Exp", strBuilder);
74        } else if (node.Symbol is Logarithm) {
75          FormatFunction(node, "Log", strBuilder);
76        } else if (node.Symbol is IfThenElse) {
77          FormatIf(node, strBuilder);
78        } else if (node.Symbol is GreaterThan) {
79          strBuilder.Append("If[Greater[");
80          FormatRecursively(node.GetSubtree(0), strBuilder);
81          strBuilder.Append(",");
82          FormatRecursively(node.GetSubtree(1), strBuilder);
83          strBuilder.Append("], 1, -1]");
84        } else if (node.Symbol is LessThan) {
85          strBuilder.Append("If[Less[");
86          FormatRecursively(node.GetSubtree(0), strBuilder);
87          strBuilder.Append(",");
88          FormatRecursively(node.GetSubtree(1), strBuilder);
89          strBuilder.Append("], 1, -1]");
90        } else if (node.Symbol is And) {
91          FormatAnd(node, strBuilder);
92        } else if (node.Symbol is Not) {
93          strBuilder.Append("If[Greater[");
94          FormatRecursively(node.GetSubtree(0), strBuilder);
95          strBuilder.Append(", 0], -1, 1]");
96        } else if (node.Symbol is Or) {
97          FormatOr(node, strBuilder);
98        } else if (node.Symbol is Xor) {
99          FormatXor(node, strBuilder);
100        } else if (node.Symbol is Square) {
101          FormatSquare(node, strBuilder);
102        } else if (node.Symbol is SquareRoot) {
103          FormatFunction(node, "Sqrt", strBuilder);
104        } else if (node.Symbol is Power) {
105          FormatFunction(node, "Power", strBuilder);
106        } else if (node.Symbol is Root) {
107          FormatRoot(node, strBuilder);
108        } else {
109          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " is not supported.");
110        }
111      } else {
112        if (node is VariableTreeNode) {
113          var varNode = node as VariableTreeNode;
114          strBuilder.AppendFormat("Times[{0}, {1}]", varNode.VariableName, varNode.Weight.ToString("G17", CultureInfo.InvariantCulture));
115        } else if (node is ConstantTreeNode) {
116          var constNode = node as ConstantTreeNode;
117          strBuilder.Append(constNode.Value.ToString("G17", CultureInfo.InvariantCulture));
118        } else {
119          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " is not supported.");
120        }
121      }
122    }
123
124    private void FormatXor(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
125      strBuilder.Append("If[Xor[");
126      foreach (var t in node.Subtrees) {
127        strBuilder.Append("Greater[");
128        FormatRecursively(t, strBuilder);
129        strBuilder.Append(", 0]");
130        if (t != node.Subtrees.Last()) strBuilder.Append(",");
131      }
132      strBuilder.Append("], 1, -1]");
133    }
134
135    private void FormatOr(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
136      strBuilder.Append("If[Or[");
137      foreach (var t in node.Subtrees) {
138        strBuilder.Append("Greater[");
139        FormatRecursively(t, strBuilder);
140        strBuilder.Append(", 0]");
141        if (t != node.Subtrees.Last()) strBuilder.Append(",");
142      }
143      strBuilder.Append("], 1, -1]");
144    }
145
146    private void FormatAnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
147      strBuilder.Append("If[And[");
148      foreach (var t in node.Subtrees) {
149        strBuilder.Append("Greater[");
150        FormatRecursively(t, strBuilder);
151        strBuilder.Append(", 0]");
152        if (t != node.Subtrees.Last()) strBuilder.Append(",");
153      }
154      strBuilder.Append("], 1, -1]");
155    }
156
157    private void FormatIf(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
158      strBuilder.Append("If[Greater[");
159      FormatRecursively(node.GetSubtree(0), strBuilder);
160      strBuilder.Append(", 0], ");
161      FormatRecursively(node.GetSubtree(1), strBuilder);
162      strBuilder.Append(", ");
163      FormatRecursively(node.GetSubtree(2), strBuilder);
164      strBuilder.Append("]");
165    }
166
167    private void FormatAverage(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
168      // mean function needs a list of values
169      strBuilder.Append("Mean[{");
170      FormatRecursively(node.GetSubtree(0), strBuilder);
171      for (int i = 1; i < node.SubtreeCount; i++) {
172        strBuilder.Append(",");
173        FormatRecursively(node.GetSubtree(i), strBuilder);
174      }
175      strBuilder.Append("}]");
176    }
177
178    private void FormatSubtraction(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
179      strBuilder.Append("Subtract[");
180      FormatRecursively(node.GetSubtree(0), strBuilder);
181      strBuilder.Append(", Times[-1");
182      foreach (var t in node.Subtrees) {
183        strBuilder.Append(",");
184        FormatRecursively(t, strBuilder);
185      }
186      strBuilder.Append("]]");
187    }
188
189    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
190      strBuilder.Append("Power[");
191      FormatRecursively(node.GetSubtree(0), strBuilder);
192      strBuilder.Append(", 2]");
193    }
194
195    private void FormatRoot(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
196      strBuilder.Append("Power[");
197      FormatRecursively(node.GetSubtree(0), strBuilder);
198      strBuilder.Append(", Divide[1,");
199      FormatRecursively(node.GetSubtree(1), strBuilder);
200      strBuilder.Append("]]");
201    }
202
203    private void FormatDivision(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
204      if (node.SubtreeCount == 1) {
205        strBuilder.Append("Divide[1, ");
206        FormatRecursively(node.GetSubtree(0), strBuilder);
207        strBuilder.Append("]");
208      } else {
209        strBuilder.Append("Divide[");
210        FormatRecursively(node.GetSubtree(0), strBuilder);
211        strBuilder.Append(", Times[");
212        FormatRecursively(node.GetSubtree(1), strBuilder);
213        for (int i = 2; i < node.SubtreeCount; i++) {
214          strBuilder.Append(",");
215          FormatRecursively(node.GetSubtree(i), strBuilder);
216        }
217        strBuilder.Append("]]");
218      }
219    }
220
221    private void FormatFunction(ISymbolicExpressionTreeNode node, string function, StringBuilder strBuilder) {
222      strBuilder.Append(function + "[");
223      foreach (var child in node.Subtrees) {
224        FormatRecursively(child, strBuilder);
225        if (child != node.Subtrees.Last())
226          strBuilder.Append(", ");
227      }
228      strBuilder.Append("]");
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.