Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs @ 17034

Last change on this file since 17034 was 17034, checked in by mkommend, 5 years ago

#2435: Updated branch with most recent trunk changes.

File size: 18.6 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 Absolute) {
129        stringBuilder.Append($"abs({FormatRecursively(node.GetSubtree(0))})");
130      } else if (symbol is AnalyticQuotient) {
131        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / sqrt(1 + ({FormatRecursively(node.GetSubtree(1))}).^2)");
132      } else if (symbol is And) {
133        stringBuilder.Append("((");
134        for (int i = 0; i < node.SubtreeCount; i++) {
135          if (i > 0) stringBuilder.Append("&");
136          stringBuilder.Append("((");
137          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
138          stringBuilder.Append(")>0)");
139        }
140        stringBuilder.Append(")-0.5)*2");
141        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
142      } else if (symbol is Average) {
143        stringBuilder.Append("(1/");
144        stringBuilder.Append(node.SubtreeCount);
145        stringBuilder.Append(")*(");
146        for (int i = 0; i < node.SubtreeCount; i++) {
147          if (i > 0) stringBuilder.Append("+");
148          stringBuilder.Append("(");
149          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
150          stringBuilder.Append(")");
151        }
152        stringBuilder.Append(")");
153      } else if (symbol is Constant) {
154        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
155        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
156      } else if (symbol is Cosine) {
157        stringBuilder.Append("cos(");
158        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
159        stringBuilder.Append(")");
160      } else if (symbol is Division) {
161        if (node.SubtreeCount == 1) {
162          stringBuilder.Append("1/");
163          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
164        } else {
165          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
166          stringBuilder.Append("/(");
167          for (int i = 1; i < node.SubtreeCount; i++) {
168            if (i > 1) stringBuilder.Append("*");
169            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
170          }
171          stringBuilder.Append(")");
172        }
173      } else if (symbol is Exponential) {
174        stringBuilder.Append("exp(");
175        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
176        stringBuilder.Append(")");
177      } else if (symbol is Square) {
178        stringBuilder.Append("(");
179        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
180        stringBuilder.Append(").^2");
181      } else if (symbol is SquareRoot) {
182        stringBuilder.Append("sqrt(");
183        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
184        stringBuilder.Append(")");
185      } else if (symbol is Cube) {
186        stringBuilder.Append("(");
187        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
188        stringBuilder.Append(").^3");
189      } else if (symbol is CubeRoot) {
190        stringBuilder.Append("NTHROOT(");
191        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
192        stringBuilder.Append(", 3)");
193      } else if (symbol is GreaterThan) {
194        stringBuilder.Append("((");
195        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
196        stringBuilder.Append(">");
197        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
198        stringBuilder.Append(")-0.5)*2");
199        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
200      } else if (symbol is IfThenElse) {
201        stringBuilder.Append("(");
202        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
203        stringBuilder.Append(">0)*");
204        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
205        stringBuilder.Append("+");
206        stringBuilder.Append("(");
207        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
208        stringBuilder.Append("<=0)*");
209        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
210      } else if (symbol is LaggedVariable) {
211        // this if must be checked before if(symbol is LaggedVariable)
212        LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode;
213        stringBuilder.Append(laggedVariableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
214        stringBuilder.Append("*");
215        stringBuilder.Append(laggedVariableTreeNode.VariableName +
216                             LagToString(currentLag + laggedVariableTreeNode.Lag));
217      } else if (symbol is LessThan) {
218        stringBuilder.Append("((");
219        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
220        stringBuilder.Append("<");
221        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
222        stringBuilder.Append(")-0.5)*2");
223        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
224      } else if (symbol is Logarithm) {
225        stringBuilder.Append("log_(");
226        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
227        stringBuilder.Append(")");
228      } else if (symbol is Multiplication) {
229        for (int i = 0; i < node.SubtreeCount; i++) {
230          if (i > 0) stringBuilder.Append("*");
231          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
232        }
233      } else if (symbol is Not) {
234        stringBuilder.Append("~(");
235        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
236        stringBuilder.Append(" > 0 )");
237      } else if (symbol is Or) {
238        stringBuilder.Append("((");
239        for (int i = 0; i < node.SubtreeCount; i++) {
240          if (i > 0) stringBuilder.Append("|");
241          stringBuilder.Append("((");
242          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
243          stringBuilder.Append(")>0)");
244        }
245        stringBuilder.Append(")-0.5)*2");
246        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
247      } else if (symbol is Sine) {
248        stringBuilder.Append("sin(");
249        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
250        stringBuilder.Append(")");
251      } else if (symbol is Subtraction) {
252        stringBuilder.Append("(");
253        if (node.SubtreeCount == 1) {
254          stringBuilder.Append("-");
255          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
256        } else {
257          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
258          for (int i = 1; i < node.SubtreeCount; i++) {
259            stringBuilder.Append("-");
260            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
261          }
262        }
263        stringBuilder.Append(")");
264      } else if (symbol is Tangent) {
265        stringBuilder.Append("tan(");
266        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
267        stringBuilder.Append(")");
268      } else if (symbol is HyperbolicTangent) {
269        stringBuilder.Append("tanh(");
270        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
271        stringBuilder.Append(")");
272      } else if (node.Symbol is AiryA) {
273        stringBuilder.Append("airy(");
274        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
275        stringBuilder.Append(")");
276      } else if (node.Symbol is AiryB) {
277        stringBuilder.Append("airy(2, ");
278        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
279        stringBuilder.Append(")");
280      } else if (node.Symbol is Bessel) {
281        stringBuilder.Append("besseli(0.0,");
282        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
283        stringBuilder.Append(")");
284      } else if (node.Symbol is CosineIntegral) {
285        stringBuilder.Append("cosint(");
286        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
287        stringBuilder.Append(")");
288      } else if (node.Symbol is Dawson) {
289        stringBuilder.Append("dawson(");
290        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
291        stringBuilder.Append(")");
292      } else if (node.Symbol is Erf) {
293        stringBuilder.Append("erf(");
294        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
295        stringBuilder.Append(")");
296      } else if (node.Symbol is ExponentialIntegralEi) {
297        stringBuilder.Append("expint(");
298        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
299        stringBuilder.Append(")");
300      } else if (node.Symbol is FresnelCosineIntegral) {
301        stringBuilder.Append("FresnelC(");
302        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
303        stringBuilder.Append(")");
304      } else if (node.Symbol is FresnelSineIntegral) {
305        stringBuilder.Append("FresnelS(");
306        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
307        stringBuilder.Append(")");
308      } else if (node.Symbol is Gamma) {
309        stringBuilder.Append("gamma(");
310        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
311        stringBuilder.Append(")");
312      } else if (node.Symbol is HyperbolicCosineIntegral) {
313        stringBuilder.Append("Chi(");
314        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
315        stringBuilder.Append(")");
316      } else if (node.Symbol is HyperbolicSineIntegral) {
317        stringBuilder.Append("Shi(");
318        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
319        stringBuilder.Append(")");
320      } else if (node.Symbol is Norm) {
321        stringBuilder.Append("normpdf(");
322        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
323        stringBuilder.Append(")");
324      } else if (node.Symbol is Psi) {
325        stringBuilder.Append("psi(");
326        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
327        stringBuilder.Append(")");
328      } else if (node.Symbol is SineIntegral) {
329        stringBuilder.Append("sinint(");
330        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
331        stringBuilder.Append(")");
332      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
333        VariableTreeNode variableTreeNode = node as VariableTreeNode;
334        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
335        stringBuilder.Append("*");
336        stringBuilder.Append(variableTreeNode.VariableName + LagToString(currentLag));
337      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.FactorVariable) {
338        var factorNode = node as FactorVariableTreeNode;
339        var weights = string.Join(" ", factorNode.Weights.Select(w => w.ToString("G17", CultureInfo.InvariantCulture)));
340        stringBuilder.AppendFormat("switch_{0}(\"{1}\",[{2}])",
341          factorNode.VariableName, factorNode.VariableName, weights)
342          .AppendLine();
343      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.BinaryFactorVariable) {
344        var factorNode = node as BinaryFactorVariableTreeNode;
345        stringBuilder.AppendFormat(CultureInfo.InvariantCulture,
346          "((strcmp({0},\"{1}\")==1) * {2:G17})", factorNode.VariableName, factorNode.VariableValue, factorNode.Weight);
347      } else if (symbol is Power) {
348        stringBuilder.Append("(");
349        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
350        stringBuilder.Append(")^round(");
351        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
352        stringBuilder.Append(")");
353      } else if (symbol is Root) {
354        stringBuilder.Append("(");
355        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
356        stringBuilder.Append(")^(1 / round(");
357        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
358        stringBuilder.Append("))");
359      } else if (symbol is Derivative) {
360        stringBuilder.Append("fivePoint(");
361        // f0
362        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
363        stringBuilder.Append(", ");
364        // f1
365        currentLag--;
366        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
367        stringBuilder.Append(", ");
368        // f3
369        currentLag -= 2;
370        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
371        stringBuilder.Append(", ");
372        currentLag--;
373        // f4
374        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
375        stringBuilder.Append(")");
376        currentLag += 4;
377      } else if (symbol is Integral) {
378        var laggedNode = node as LaggedTreeNode;
379        string prevCounterVariable = CurrentIndexVariable;
380        string counterVariable = AllocateIndexVariable();
381        stringBuilder.AppendLine(" sum (map(@(" + counterVariable + ") " + FormatRecursively(node.GetSubtree(0)) +
382                                 ", (" + prevCounterVariable + "+" + laggedNode.Lag + "):" + prevCounterVariable +
383                                 "))");
384        ReleaseIndexVariable();
385      } else if (symbol is TimeLag) {
386        var laggedNode = node as LaggedTreeNode;
387        currentLag += laggedNode.Lag;
388        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
389        currentLag -= laggedNode.Lag;
390      } else {
391        stringBuilder.Append("ERROR");
392      }
393      return stringBuilder.ToString();
394    }
395
396
397    private string LagToString(int lag) {
398      if (lag < 0) {
399        return "(" + CurrentIndexVariable + "" + lag + ")";
400      } else if (lag > 0) {
401        return "(" + CurrentIndexVariable + "+" + lag + ")";
402      } else return "(" + CurrentIndexVariable + ")";
403    }
404
405  }
406}
Note: See TracBrowser for help on using the repository browser.