Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs @ 14536

Last change on this file since 14536 was 14312, checked in by bburlacu, 8 years ago

#1772: Merge trunk changes. Delete unnecessary files (sliding window).

File size: 20.4 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.Collections.Generic;
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("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.")]
33  [StorableClass]
34  public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
35    private readonly List<double> constants;
36    private int targetCount;
37    private int currentLag;
38    private string targetVariable;
39    private bool containsTimeSeriesSymbol;
40
41    [StorableConstructor]
42    private SymbolicDataAnalysisExpressionLatexFormatter(bool deserializing) : base(deserializing) { }
43    private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner)
44      : base(original, cloner) {
45      constants = new List<double>(original.constants);
46    }
47    public SymbolicDataAnalysisExpressionLatexFormatter()
48      : base() {
49      Name = ItemName;
50      Description = ItemDescription;
51      constants = new List<double>();
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new SymbolicDataAnalysisExpressionLatexFormatter(this, cloner);
56    }
57
58    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
59      return Format(symbolicExpressionTree, null);
60    }
61    public string Format(ISymbolicExpressionTree symbolicExpressionTree, string targetVariable) {
62      try {
63        StringBuilder strBuilder = new StringBuilder();
64        constants.Clear();
65        this.targetVariable = targetVariable;
66        containsTimeSeriesSymbol = symbolicExpressionTree.IterateNodesBreadth().Any(n => IsTimeSeriesSymbol(n.Symbol));
67        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
68        return strBuilder.ToString();
69      }
70      catch (NotImplementedException ex) {
71        return ex.Message + Environment.NewLine + ex.StackTrace;
72      }
73    }
74    static bool IsTimeSeriesSymbol(ISymbol s) {
75      return s is TimeLag || s is Integral || s is Derivative || s is LaggedVariable;
76    }
77
78    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
79      StringBuilder strBuilder = new StringBuilder();
80      currentLag = 0;
81      FormatBegin(node, strBuilder);
82
83      if (node.SubtreeCount > 0) {
84        strBuilder.Append(FormatRecursively(node.GetSubtree(0)));
85      }
86      int i = 1;
87      foreach (SymbolicExpressionTreeNode subTree in node.Subtrees.Skip(1)) {
88        FormatSep(node, strBuilder, i);
89        // format the whole subtree
90        strBuilder.Append(FormatRecursively(subTree));
91        i++;
92      }
93
94      FormatEnd(node, strBuilder);
95
96      return strBuilder.ToString();
97    }
98
99    private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
100      if (node.Symbol is Addition) {
101        strBuilder.Append(@" \left( ");
102      } else if (node.Symbol is Subtraction) {
103        if (node.SubtreeCount == 1) {
104          strBuilder.Append(@"- \left( ");
105        } else {
106          strBuilder.Append(@" \left( ");
107        }
108      } else if (node.Symbol is Multiplication) {
109      } else if (node.Symbol is Division) {
110        if (node.SubtreeCount == 1) {
111          strBuilder.Append(@" \cfrac{1");
112        } else {
113          strBuilder.Append(@" \cfrac{ ");
114        }
115      } else if (node.Symbol is Average) {
116        // skip output of (1/1) if only one subtree
117        if (node.SubtreeCount > 1) {
118          strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}");
119        }
120        strBuilder.Append(@" \left( ");
121      } else if (node.Symbol is Logarithm) {
122        strBuilder.Append(@"\log \left( ");
123      } else if (node.Symbol is Exponential) {
124        strBuilder.Append(@"\exp \left( ");
125      } else if (node.Symbol is Square) {
126        strBuilder.Append(@"\left(");
127      } else if (node.Symbol is SquareRoot) {
128        strBuilder.Append(@"\sqrt{");
129      } else if (node.Symbol is Sine) {
130        strBuilder.Append(@"\sin \left( ");
131      } else if (node.Symbol is Cosine) {
132        strBuilder.Append(@"\cos \left( ");
133      } else if (node.Symbol is Tangent) {
134        strBuilder.Append(@"\tan \left( ");
135      } else if (node.Symbol is AiryA) {
136        strBuilder.Append(@"\operatorname{airy}_a \left( ");
137      } else if (node.Symbol is AiryB) {
138        strBuilder.Append(@"\operatorname{airy}_b \left( ");
139      } else if (node.Symbol is Bessel) {
140        strBuilder.Append(@"\operatorname{bessel}_1 \left( ");
141      } else if (node.Symbol is CosineIntegral) {
142        strBuilder.Append(@"\operatorname{cosInt} \left( ");
143      } else if (node.Symbol is Dawson) {
144        strBuilder.Append(@"\operatorname{dawson} \left( ");
145      } else if (node.Symbol is Erf) {
146        strBuilder.Append(@"\operatorname{erf} \left( ");
147      } else if (node.Symbol is ExponentialIntegralEi) {
148        strBuilder.Append(@"\operatorname{expInt}_i \left( ");
149      } else if (node.Symbol is FresnelCosineIntegral) {
150        strBuilder.Append(@"\operatorname{fresnel}_\operatorname{cosInt} \left( ");
151      } else if (node.Symbol is FresnelSineIntegral) {
152        strBuilder.Append(@"\operatorname{fresnel}_\operatorname{sinInt} \left( ");
153      } else if (node.Symbol is Gamma) {
154        strBuilder.Append(@"\Gamma \left( ");
155      } else if (node.Symbol is HyperbolicCosineIntegral) {
156        strBuilder.Append(@"\operatorname{hypCosInt} \left( ");
157      } else if (node.Symbol is HyperbolicSineIntegral) {
158        strBuilder.Append(@"\operatorname{hypSinInt} \left( ");
159      } else if (node.Symbol is Norm) {
160        strBuilder.Append(@"\operatorname{norm} \left( ");
161      } else if (node.Symbol is Psi) {
162        strBuilder.Append(@"\operatorname{digamma} \left( ");
163      } else if (node.Symbol is SineIntegral) {
164        strBuilder.Append(@"\operatorname{sinInt} \left( ");
165      } else if (node.Symbol is GreaterThan) {
166        strBuilder.Append(@"  \left( ");
167      } else if (node.Symbol is LessThan) {
168        strBuilder.Append(@"  \left( ");
169      } else if (node.Symbol is And) {
170        strBuilder.Append(@"  \left( \left( ");
171      } else if (node.Symbol is Or) {
172        strBuilder.Append(@"   \left( \left( ");
173      } else if (node.Symbol is Not) {
174        strBuilder.Append(@" \neg \left( ");
175      } else if (node.Symbol is IfThenElse) {
176        strBuilder.Append(@" \operatorname{if}  \left( ");
177      } else if (node.Symbol is Constant) {
178        strBuilder.Append("c_{" + constants.Count + "} ");
179        var constNode = node as ConstantTreeNode;
180        constants.Add(constNode.Value);
181      } else if (node.Symbol is LaggedVariable) {
182        var laggedVarNode = node as LaggedVariableTreeNode;
183        if (!laggedVarNode.Weight.IsAlmost(1.0)) {
184          strBuilder.Append("c_{" + constants.Count + "} \\cdot ");
185          constants.Add(laggedVarNode.Weight);
186        }
187        strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName));
188        strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag));
189
190      } else if (node.Symbol is Variable) {
191        var varNode = node as VariableTreeNode;
192        if (!varNode.Weight.IsAlmost((1.0))) {
193          strBuilder.Append("c_{" + constants.Count + "} \\cdot ");
194          constants.Add(varNode.Weight);
195        }
196        strBuilder.Append(EscapeLatexString(varNode.VariableName));
197        strBuilder.Append(LagToString(currentLag));
198      } else if (node.Symbol is ProgramRootSymbol) {
199        strBuilder
200          .AppendLine("\\begin{align*}")
201          .AppendLine("\\nonumber");
202      } else if (node.Symbol is Defun) {
203        var defunNode = node as DefunTreeNode;
204        strBuilder.Append(defunNode.FunctionName + " & = ");
205      } else if (node.Symbol is InvokeFunction) {
206        var invokeNode = node as InvokeFunctionTreeNode;
207        strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
208      } else if (node.Symbol is StartSymbol) {
209        FormatStartSymbol(strBuilder);
210      } else if (node.Symbol is Argument) {
211        var argSym = node.Symbol as Argument;
212        strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
213      } else if (node.Symbol is Derivative) {
214        strBuilder.Append(@" \cfrac{d \left( ");
215      } else if (node.Symbol is TimeLag) {
216        var laggedNode = node as ILaggedTreeNode;
217        currentLag += laggedNode.Lag;
218      } else if (node.Symbol is Power) {
219        strBuilder.Append(@" \left( ");
220      } else if (node.Symbol is Root) {
221        strBuilder.Append(@" \left( ");
222      } else if (node.Symbol is Integral) {
223        // actually a new variable for t is needed in all subtrees (TODO)
224        var laggedTreeNode = node as ILaggedTreeNode;
225        strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( ");
226      } else if (node.Symbol is VariableCondition) {
227        var conditionTreeNode = node as VariableConditionTreeNode;
228        string p = @"1 /  1 + \exp  - c_{" + constants.Count + "} ";
229        constants.Add(conditionTreeNode.Slope);
230        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"}   ";
231        constants.Add(conditionTreeNode.Threshold);
232        strBuilder.Append(@" \left( " + p + @"\cdot ");
233      } else {
234        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
235      }
236    }
237
238    private void FormatSep(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, int step) {
239      if (node.Symbol is Addition) {
240        strBuilder.Append(" + ");
241      } else if (node.Symbol is Subtraction) {
242        strBuilder.Append(" - ");
243      } else if (node.Symbol is Multiplication) {
244        strBuilder.Append(@" \cdot ");
245      } else if (node.Symbol is Division) {
246        if (step + 1 == node.SubtreeCount)
247          strBuilder.Append(@"}{");
248        else
249          strBuilder.Append(@" }{ \cfrac{ ");
250      } else if (node.Symbol is Average) {
251        strBuilder.Append(@" + ");
252      } else if (node.Symbol is Logarithm) {
253        throw new InvalidOperationException();
254      } else if (node.Symbol is Exponential) {
255        throw new InvalidOperationException();
256      } else if (node.Symbol is Square) {
257        throw new InvalidOperationException();
258      } else if (node.Symbol is SquareRoot) {
259        throw new InvalidOperationException();
260      } else if (node.Symbol is Sine) {
261        throw new InvalidOperationException();
262      } else if (node.Symbol is Cosine) {
263        throw new InvalidOperationException();
264      } else if (node.Symbol is Tangent) {
265        throw new InvalidOperationException();
266      } else if (node.Symbol is AiryA) {
267        throw new InvalidOperationException();
268      } else if (node.Symbol is AiryB) {
269        throw new InvalidOperationException();
270      } else if (node.Symbol is Bessel) {
271        throw new InvalidOperationException();
272      } else if (node.Symbol is CosineIntegral) {
273        throw new InvalidOperationException();
274      } else if (node.Symbol is Dawson) {
275        throw new InvalidOperationException();
276      } else if (node.Symbol is Erf) {
277        throw new InvalidOperationException();
278      } else if (node.Symbol is ExponentialIntegralEi) {
279        throw new InvalidOperationException();
280      } else if (node.Symbol is FresnelCosineIntegral) {
281        throw new InvalidOperationException();
282      } else if (node.Symbol is FresnelSineIntegral) {
283        throw new InvalidOperationException();
284      } else if (node.Symbol is Gamma) {
285        throw new InvalidOperationException();
286      } else if (node.Symbol is HyperbolicCosineIntegral) {
287        throw new InvalidOperationException();
288      } else if (node.Symbol is HyperbolicSineIntegral) {
289        throw new InvalidOperationException();
290      } else if (node.Symbol is Norm) {
291        throw new InvalidOperationException();
292      } else if (node.Symbol is Psi) {
293        throw new InvalidOperationException();
294      } else if (node.Symbol is SineIntegral) {
295        throw new InvalidOperationException();
296      } else if (node.Symbol is GreaterThan) {
297        strBuilder.Append(@" > ");
298      } else if (node.Symbol is LessThan) {
299        strBuilder.Append(@" < ");
300      } else if (node.Symbol is And) {
301        strBuilder.Append(@" > 0  \right) \land \left(");
302      } else if (node.Symbol is Or) {
303        strBuilder.Append(@" > 0  \right) \lor \left(");
304      } else if (node.Symbol is Not) {
305        throw new InvalidOperationException();
306      } else if (node.Symbol is IfThenElse) {
307        strBuilder.Append(@" , ");
308      } else if (node.Symbol is ProgramRootSymbol) {
309        strBuilder.Append(@"\\" + Environment.NewLine);
310      } else if (node.Symbol is Defun) {
311      } else if (node.Symbol is InvokeFunction) {
312        strBuilder.Append(" , ");
313      } else if (node.Symbol is StartSymbol) {
314        strBuilder.Append(@"\\" + Environment.NewLine);
315        FormatStartSymbol(strBuilder);
316      } else if (node.Symbol is Power) {
317        strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
318      } else if (node.Symbol is Root) {
319        strBuilder.Append(@"\right) ^ {  \cfrac{1}{ \operatorname{round} \left(");
320      } else if (node.Symbol is VariableCondition) {
321        var conditionTreeNode = node as VariableConditionTreeNode;
322        string p = @"1 / \left( 1 + \exp \left( - c_{" + constants.Count + "} ";
323        constants.Add(conditionTreeNode.Slope);
324        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right)   ";
325        constants.Add(conditionTreeNode.Threshold);
326        strBuilder.Append(@" +  \left( 1 - " + p + @" \right) \cdot ");
327      } else {
328        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
329      }
330    }
331
332    private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
333      if (node.Symbol is Addition) {
334        strBuilder.Append(@" \right) ");
335      } else if (node.Symbol is Subtraction) {
336        strBuilder.Append(@" \right) ");
337      } else if (node.Symbol is Multiplication) {
338      } else if (node.Symbol is Division) {
339        strBuilder.Append(" } ");
340        for (int i = 2; i < node.SubtreeCount; i++)
341          strBuilder.Append(" } ");
342      } else if (node.Symbol is Average) {
343        strBuilder.Append(@" \right) ");
344      } else if (node.Symbol is Logarithm) {
345        strBuilder.Append(@" \right) ");
346      } else if (node.Symbol is Exponential) {
347        strBuilder.Append(@" \right) ");
348      } else if (node.Symbol is Square) {
349        strBuilder.Append(@"\right)^2");
350      } else if (node.Symbol is SquareRoot) {
351        strBuilder.Append(@"}");
352      } else if (node.Symbol is Sine) {
353        strBuilder.Append(@" \right) ");
354      } else if (node.Symbol is Cosine) {
355        strBuilder.Append(@" \right) ");
356      } else if (node.Symbol is Tangent) {
357        strBuilder.Append(@" \right) ");
358      } else if (node.Symbol is AiryA) {
359        strBuilder.Append(@" \right) ");
360      } else if (node.Symbol is AiryB) {
361        strBuilder.Append(@" \right) ");
362      } else if (node.Symbol is Bessel) {
363        strBuilder.Append(@" \right) ");
364      } else if (node.Symbol is CosineIntegral) {
365        strBuilder.Append(@" \right) ");
366      } else if (node.Symbol is Dawson) {
367        strBuilder.Append(@" \right) ");
368      } else if (node.Symbol is Erf) {
369        strBuilder.Append(@" \right) ");
370      } else if (node.Symbol is ExponentialIntegralEi) {
371        strBuilder.Append(@" \right) ");
372      } else if (node.Symbol is FresnelCosineIntegral) {
373        strBuilder.Append(@" \right) ");
374      } else if (node.Symbol is FresnelSineIntegral) {
375        strBuilder.Append(@" \right) ");
376      } else if (node.Symbol is Gamma) {
377        strBuilder.Append(@" \right) ");
378      } else if (node.Symbol is HyperbolicCosineIntegral) {
379        strBuilder.Append(@" \right) ");
380      } else if (node.Symbol is HyperbolicSineIntegral) {
381        strBuilder.Append(@" \right) ");
382      } else if (node.Symbol is Norm) {
383        strBuilder.Append(@" \right) ");
384      } else if (node.Symbol is Psi) {
385        strBuilder.Append(@" \right) ");
386      } else if (node.Symbol is SineIntegral) {
387        strBuilder.Append(@" \right) ");
388      } else if (node.Symbol is GreaterThan) {
389        strBuilder.Append(@" \right) ");
390      } else if (node.Symbol is LessThan) {
391        strBuilder.Append(@" \right) ");
392      } else if (node.Symbol is And) {
393        strBuilder.Append(@" > 0 \right) \right) ");
394      } else if (node.Symbol is Or) {
395        strBuilder.Append(@" > 0 \right) \right) ");
396      } else if (node.Symbol is Not) {
397        strBuilder.Append(@" \right) ");
398      } else if (node.Symbol is IfThenElse) {
399        strBuilder.Append(@" \right) ");
400      } else if (node.Symbol is Constant) {
401      } else if (node.Symbol is LaggedVariable) {
402      } else if (node.Symbol is Variable) {
403      } else if (node.Symbol is ProgramRootSymbol) {
404        strBuilder
405          .AppendLine("\\end{align*}")
406          .AppendLine("\\begin{align*}")
407          .AppendLine("\\nonumber");
408        // output all constant values
409        if (constants.Count > 0) {
410          int i = 0;
411          foreach (var constant in constants) {
412            // replace "." with ".&" to align decimal points
413            var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant);
414            if (!constStr.Contains(".")) constStr = constStr + ".0";
415            constStr = constStr.Replace(".", "&.");  // fix problem in rendering of aligned expressions
416            strBuilder.Append("c_{" + i + "}& = & " + constStr);
417            strBuilder.Append(@"\\");
418            i++;
419          }
420        }
421        strBuilder.AppendLine("\\end{align*}");
422      } else if (node.Symbol is Defun) {
423      } else if (node.Symbol is InvokeFunction) {
424        strBuilder.Append(@" \right) ");
425      } else if (node.Symbol is StartSymbol) {
426      } else if (node.Symbol is Argument) {
427      } else if (node.Symbol is Derivative) {
428        strBuilder.Append(@" \right) }{dt} ");
429      } else if (node.Symbol is TimeLag) {
430        var laggedNode = node as ILaggedTreeNode;
431        currentLag -= laggedNode.Lag;
432      } else if (node.Symbol is Power) {
433        strBuilder.Append(@" \right) } ");
434      } else if (node.Symbol is Root) {
435        strBuilder.Append(@" \right) } } ");
436      } else if (node.Symbol is Integral) {
437        strBuilder.Append(@" \right) ");
438      } else if (node.Symbol is VariableCondition) {
439        strBuilder.Append(@"\right) ");
440      } else {
441        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
442      }
443    }
444
445    private void FormatStartSymbol(StringBuilder strBuilder) {
446      strBuilder.Append(targetVariable ?? "target_" + (targetCount++));
447      if (containsTimeSeriesSymbol)
448        strBuilder.Append("(t)");
449      strBuilder.Append(" & = ");
450    }
451
452    private string LagToString(int lag) {
453      if (lag < 0) {
454        return "(t" + lag + ")";
455      } else if (lag > 0) {
456        return "(t+" + lag + ")";
457      } else return "";
458    }
459
460    private string EscapeLatexString(string s) {
461      return "\\text{" +
462        s
463         .Replace("\\", "\\\\")
464         .Replace("{", "\\{")
465         .Replace("}", "\\}")
466        + "}";
467    }
468  }
469}
Note: See TracBrowser for help on using the repository browser.