Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs @ 16702

Last change on this file since 16702 was 16702, checked in by bburlacu, 5 years ago

#2866: Add tanh to all formatters except the TSQLExpressionFormatter, since TSQL does not have a TANH function (could be approximated but ugly).

File size: 17.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Globalization;
23using System.Linq;
24using System.Text;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
31
32  [Item("MATLAB String Formatter", "String formatter for string representations of symbolic data analysis expressions in MATLAB syntax.")]
33  [StorableType("ADFB0A37-412D-4DD4-A174-F0103ADD7972")]
34  public sealed class SymbolicDataAnalysisExpressionMATLABFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
35    private int currentLag;
36
37    [StorableConstructor]
38    private SymbolicDataAnalysisExpressionMATLABFormatter(StorableConstructorFlag _) : base(_) { }
39    private SymbolicDataAnalysisExpressionMATLABFormatter(SymbolicDataAnalysisExpressionMATLABFormatter original, Cloner cloner) : base(original, cloner) { }
40    public SymbolicDataAnalysisExpressionMATLABFormatter()
41      : base() {
42      Name = ItemName;
43      Description = ItemDescription;
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new SymbolicDataAnalysisExpressionMATLABFormatter(this, cloner);
47    }
48    private int currentIndexNumber;
49    public string CurrentIndexVariable {
50      get {
51        return "i" + currentIndexNumber;
52      }
53    }
54    private void ReleaseIndexVariable() {
55      currentIndexNumber--;
56    }
57
58    private string AllocateIndexVariable() {
59      currentIndexNumber++;
60      return CurrentIndexVariable;
61    }
62
63    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
64      currentLag = 0;
65      currentIndexNumber = 0;
66
67      var stringBuilder = new StringBuilder();
68      stringBuilder.AppendLine("rows = ???");
69      stringBuilder.AppendLine(FormatOnlyExpression(symbolicExpressionTree.Root) + ";");
70      stringBuilder.AppendLine();
71      stringBuilder.AppendLine("function y = log_(x)");
72      stringBuilder.AppendLine("  if(x<=0) y = NaN;");
73      stringBuilder.AppendLine("  else     y = log(x);");
74      stringBuilder.AppendLine("  end");
75      stringBuilder.AppendLine("end");
76      stringBuilder.AppendLine();
77      stringBuilder.AppendLine("function y = fivePoint(f0, f1, f3, f4)");
78      stringBuilder.AppendLine("  y = (f0 + 2*f1 - 2*f3 - f4) / 8;");
79      stringBuilder.AppendLine("end");
80
81      var factorVariableNames =
82        symbolicExpressionTree.IterateNodesPostfix()
83          .OfType<FactorVariableTreeNode>()
84          .Select(n => n.VariableName)
85          .Distinct();
86
87      foreach (var factorVarName in factorVariableNames) {
88        var factorSymb = symbolicExpressionTree.IterateNodesPostfix()
89          .OfType<FactorVariableTreeNode>()
90          .First(n => n.VariableName == factorVarName)
91          .Symbol;
92        stringBuilder.AppendFormat("function y = switch_{0}(val, v)", factorVarName).AppendLine();
93        var values = factorSymb.GetVariableValues(factorVarName).ToArray();
94        stringBuilder.AppendLine("switch val");
95        for (int i = 0; i < values.Length; i++) {
96          stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "  case \"{0}\" y = v({1})", values[i], i).AppendLine();
97        }
98        stringBuilder.AppendLine("end");
99        stringBuilder.AppendLine();
100      }
101
102      return stringBuilder.ToString();
103    }
104
105    public string FormatOnlyExpression(ISymbolicExpressionTreeNode expressionNode) {
106      var stringBuilder = new StringBuilder();
107      stringBuilder.AppendLine("  for " + CurrentIndexVariable + " = 1:1:rows");
108      stringBuilder.AppendLine("    estimated(" + CurrentIndexVariable + ") = " + FormatRecursively(expressionNode.GetSubtree(0)) + ";");
109      stringBuilder.AppendLine("  end;");
110      return stringBuilder.ToString();
111    }
112
113    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
114      ISymbol symbol = node.Symbol;
115      StringBuilder stringBuilder = new StringBuilder();
116
117      if (symbol is ProgramRootSymbol) {
118        stringBuilder.AppendLine(FormatRecursively(node.GetSubtree(0)));
119      } else if (symbol is StartSymbol)
120        return FormatRecursively(node.GetSubtree(0));
121      else if (symbol is Addition) {
122        stringBuilder.Append("(");
123        for (int i = 0; i < node.SubtreeCount; i++) {
124          if (i > 0) stringBuilder.Append("+");
125          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
126        }
127        stringBuilder.Append(")");
128      } else if (symbol is And) {
129        stringBuilder.Append("((");
130        for (int i = 0; i < node.SubtreeCount; i++) {
131          if (i > 0) stringBuilder.Append("&");
132          stringBuilder.Append("((");
133          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
134          stringBuilder.Append(")>0)");
135        }
136        stringBuilder.Append(")-0.5)*2");
137        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
138      } else if (symbol is Average) {
139        stringBuilder.Append("(1/");
140        stringBuilder.Append(node.SubtreeCount);
141        stringBuilder.Append(")*(");
142        for (int i = 0; i < node.SubtreeCount; i++) {
143          if (i > 0) stringBuilder.Append("+");
144          stringBuilder.Append("(");
145          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
146          stringBuilder.Append(")");
147        }
148        stringBuilder.Append(")");
149      } else if (symbol is Constant) {
150        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
151        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
152      } else if (symbol is Cosine) {
153        stringBuilder.Append("cos(");
154        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
155        stringBuilder.Append(")");
156      } else if (symbol is Division) {
157        if (node.SubtreeCount == 1) {
158          stringBuilder.Append("1/");
159          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
160        } else {
161          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
162          stringBuilder.Append("/(");
163          for (int i = 1; i < node.SubtreeCount; i++) {
164            if (i > 1) stringBuilder.Append("*");
165            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
166          }
167          stringBuilder.Append(")");
168        }
169      } else if (symbol is Exponential) {
170        stringBuilder.Append("exp(");
171        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
172        stringBuilder.Append(")");
173      } else if (symbol is Square) {
174        stringBuilder.Append("(");
175        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
176        stringBuilder.Append(").^2");
177      } else if (symbol is SquareRoot) {
178        stringBuilder.Append("sqrt(");
179        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
180        stringBuilder.Append(")");
181      } else if (symbol is GreaterThan) {
182        stringBuilder.Append("((");
183        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
184        stringBuilder.Append(">");
185        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
186        stringBuilder.Append(")-0.5)*2");
187        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
188      } else if (symbol is IfThenElse) {
189        stringBuilder.Append("(");
190        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
191        stringBuilder.Append(">0)*");
192        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
193        stringBuilder.Append("+");
194        stringBuilder.Append("(");
195        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
196        stringBuilder.Append("<=0)*");
197        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
198      } else if (symbol is LaggedVariable) {
199        // this if must be checked before if(symbol is LaggedVariable)
200        LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode;
201        stringBuilder.Append(laggedVariableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
202        stringBuilder.Append("*");
203        stringBuilder.Append(laggedVariableTreeNode.VariableName +
204                             LagToString(currentLag + laggedVariableTreeNode.Lag));
205      } else if (symbol is LessThan) {
206        stringBuilder.Append("((");
207        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
208        stringBuilder.Append("<");
209        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
210        stringBuilder.Append(")-0.5)*2");
211        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
212      } else if (symbol is Logarithm) {
213        stringBuilder.Append("log_(");
214        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
215        stringBuilder.Append(")");
216      } else if (symbol is Multiplication) {
217        for (int i = 0; i < node.SubtreeCount; i++) {
218          if (i > 0) stringBuilder.Append("*");
219          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
220        }
221      } else if (symbol is Not) {
222        stringBuilder.Append("~(");
223        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
224        stringBuilder.Append(" > 0 )");
225      } else if (symbol is Or) {
226        stringBuilder.Append("((");
227        for (int i = 0; i < node.SubtreeCount; i++) {
228          if (i > 0) stringBuilder.Append("|");
229          stringBuilder.Append("((");
230          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
231          stringBuilder.Append(")>0)");
232        }
233        stringBuilder.Append(")-0.5)*2");
234        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
235      } else if (symbol is Sine) {
236        stringBuilder.Append("sin(");
237        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
238        stringBuilder.Append(")");
239      } else if (symbol is Subtraction) {
240        stringBuilder.Append("(");
241        if (node.SubtreeCount == 1) {
242          stringBuilder.Append("-");
243          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
244        } else {
245          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
246          for (int i = 1; i < node.SubtreeCount; i++) {
247            stringBuilder.Append("-");
248            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
249          }
250        }
251        stringBuilder.Append(")");
252      } else if (symbol is Tangent) {
253        stringBuilder.Append("tan(");
254        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
255        stringBuilder.Append(")");
256      } else if (symbol is HyperbolicTangent) {
257        stringBuilder.Append("tanh(");
258        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
259        stringBuilder.Append(")");
260      } else if (node.Symbol is AiryA) {
261        stringBuilder.Append("airy(");
262        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
263        stringBuilder.Append(")");
264      } else if (node.Symbol is AiryB) {
265        stringBuilder.Append("airy(2, ");
266        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
267        stringBuilder.Append(")");
268      } else if (node.Symbol is Bessel) {
269        stringBuilder.Append("besseli(0.0,");
270        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
271        stringBuilder.Append(")");
272      } else if (node.Symbol is CosineIntegral) {
273        stringBuilder.Append("cosint(");
274        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
275        stringBuilder.Append(")");
276      } else if (node.Symbol is Dawson) {
277        stringBuilder.Append("dawson(");
278        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
279        stringBuilder.Append(")");
280      } else if (node.Symbol is Erf) {
281        stringBuilder.Append("erf(");
282        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
283        stringBuilder.Append(")");
284      } else if (node.Symbol is ExponentialIntegralEi) {
285        stringBuilder.Append("expint(");
286        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
287        stringBuilder.Append(")");
288      } else if (node.Symbol is FresnelCosineIntegral) {
289        stringBuilder.Append("FresnelC(");
290        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
291        stringBuilder.Append(")");
292      } else if (node.Symbol is FresnelSineIntegral) {
293        stringBuilder.Append("FresnelS(");
294        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
295        stringBuilder.Append(")");
296      } else if (node.Symbol is Gamma) {
297        stringBuilder.Append("gamma(");
298        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
299        stringBuilder.Append(")");
300      } else if (node.Symbol is HyperbolicCosineIntegral) {
301        stringBuilder.Append("Chi(");
302        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
303        stringBuilder.Append(")");
304      } else if (node.Symbol is HyperbolicSineIntegral) {
305        stringBuilder.Append("Shi(");
306        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
307        stringBuilder.Append(")");
308      } else if (node.Symbol is Norm) {
309        stringBuilder.Append("normpdf(");
310        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
311        stringBuilder.Append(")");
312      } else if (node.Symbol is Psi) {
313        stringBuilder.Append("psi(");
314        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
315        stringBuilder.Append(")");
316      } else if (node.Symbol is SineIntegral) {
317        stringBuilder.Append("sinint(");
318        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
319        stringBuilder.Append(")");
320      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
321        VariableTreeNode variableTreeNode = node as VariableTreeNode;
322        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
323        stringBuilder.Append("*");
324        stringBuilder.Append(variableTreeNode.VariableName + LagToString(currentLag));
325      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.FactorVariable) {
326        var factorNode = node as FactorVariableTreeNode;
327        var weights = string.Join(" ", factorNode.Weights.Select(w => w.ToString("G17", CultureInfo.InvariantCulture)));
328        stringBuilder.AppendFormat("switch_{0}(\"{1}\",[{2}])",
329          factorNode.VariableName, factorNode.VariableName, weights)
330          .AppendLine();
331      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.BinaryFactorVariable) {
332        var factorNode = node as BinaryFactorVariableTreeNode;
333        stringBuilder.AppendFormat(CultureInfo.InvariantCulture,
334          "((strcmp({0},\"{1}\")==1) * {2:G17})", factorNode.VariableName, factorNode.VariableValue, factorNode.Weight);
335      } else if (symbol is Power) {
336        stringBuilder.Append("(");
337        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
338        stringBuilder.Append(")^round(");
339        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
340        stringBuilder.Append(")");
341      } else if (symbol is Root) {
342        stringBuilder.Append("(");
343        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
344        stringBuilder.Append(")^(1 / round(");
345        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
346        stringBuilder.Append("))");
347      } else if (symbol is Derivative) {
348        stringBuilder.Append("fivePoint(");
349        // f0
350        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
351        stringBuilder.Append(", ");
352        // f1
353        currentLag--;
354        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
355        stringBuilder.Append(", ");
356        // f3
357        currentLag -= 2;
358        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
359        stringBuilder.Append(", ");
360        currentLag--;
361        // f4
362        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
363        stringBuilder.Append(")");
364        currentLag += 4;
365      } else if (symbol is Integral) {
366        var laggedNode = node as LaggedTreeNode;
367        string prevCounterVariable = CurrentIndexVariable;
368        string counterVariable = AllocateIndexVariable();
369        stringBuilder.AppendLine(" sum (map(@(" + counterVariable + ") " + FormatRecursively(node.GetSubtree(0)) +
370                                 ", (" + prevCounterVariable + "+" + laggedNode.Lag + "):" + prevCounterVariable +
371                                 "))");
372        ReleaseIndexVariable();
373      } else if (symbol is TimeLag) {
374        var laggedNode = node as LaggedTreeNode;
375        currentLag += laggedNode.Lag;
376        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
377        currentLag -= laggedNode.Lag;
378      } else {
379        stringBuilder.Append("ERROR");
380      }
381      return stringBuilder.ToString();
382    }
383
384
385    private string LagToString(int lag) {
386      if (lag < 0) {
387        return "(" + CurrentIndexVariable + "" + lag + ")";
388      } else if (lag > 0) {
389        return "(" + CurrentIndexVariable + "+" + lag + ")";
390      } else return "(" + CurrentIndexVariable + ")";
391    }
392
393  }
394}
Note: See TracBrowser for help on using the repository browser.