Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs @ 17753

Last change on this file since 17753 was 17685, checked in by gkronber, 4 years ago

#2968: simplified code and fixed bugs in infix formatter

File size: 11.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Globalization;
25using System.Linq;
26using System.Text;
27using HEAL.Attic;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using Microsoft.SqlServer.Server;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  public static class BaseInfixExpressionFormatter {
35    public static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder,
36                                          NumberFormatInfo numberFormat, string formatString, List<KeyValuePair<string, double>> constants = null) {
37      if (node.SubtreeCount > 1) {
38        var token = GetToken(node.Symbol);
39        // operators
40        if (token == "+" || token == "-" || token == "OR" || token == "XOR" ||
41            token == "*" || token == "/" || token == "AND" ||
42            token == "^") {
43          strBuilder.Append("(");
44          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);
45
46          foreach (var subtree in node.Subtrees.Skip(1)) {
47            strBuilder.Append(" ").Append(token).Append(" ");
48            FormatRecursively(subtree, strBuilder, numberFormat, formatString, constants);
49          }
50
51          strBuilder.Append(")");
52        } else {
53          // function with multiple arguments
54          strBuilder.Append(token).Append("(");
55          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);
56          foreach (var subtree in node.Subtrees.Skip(1)) {
57            strBuilder.Append(", ");
58            FormatRecursively(subtree, strBuilder, numberFormat, formatString, constants);
59          }
60
61          strBuilder.Append(")");
62        }
63      } else if (node.SubtreeCount == 1) {
64        var token = GetToken(node.Symbol);
65        if (token == "-" || token == "NOT") {
66          strBuilder.Append("(").Append(token).Append("(");
67          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
68          strBuilder.Append("))");
69        } else if (token == "/") {
70          strBuilder.Append("1/");
71          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
72        } else if (token == "+" || token == "*") {
73          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
74        } else {
75          // function with only one argument
76          strBuilder.Append(token).Append("(");
77          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);
78          strBuilder.Append(")");
79        }
80      } else {
81        // no subtrees
82        if (node.Symbol is LaggedVariable) {
83          var varNode = node as LaggedVariableTreeNode;
84          if (!varNode.Weight.IsAlmost(1.0)) {
85            strBuilder.Append("(");
86            AppendConstant(strBuilder, constants, varNode.Weight, formatString, numberFormat);
87            strBuilder.Append("*");
88          }
89
90          strBuilder.Append("LAG(");
91          AppendVariableName(strBuilder, varNode.VariableName);
92          strBuilder.Append(", ")
93                    .AppendFormat(numberFormat, "{0}", varNode.Lag)
94                    .Append(")");
95          if (!varNode.Weight.IsAlmost(1.0)) strBuilder.Append(")");
96        } else if (node.Symbol is Variable) {
97          var varNode = node as VariableTreeNode;
98          if (!varNode.Weight.IsAlmost(1.0)) {
99            strBuilder.Append("(");
100            AppendConstant(strBuilder, constants, varNode.Weight, formatString, numberFormat);
101            strBuilder.Append("*");
102          }
103
104          AppendVariableName(strBuilder, varNode.VariableName);
105
106          if (!varNode.Weight.IsAlmost(1.0)) strBuilder.Append(")");
107        } else if (node.Symbol is FactorVariable) {
108          var factorNode = node as FactorVariableTreeNode;
109          AppendVariableName(strBuilder, factorNode.VariableName);
110
111          strBuilder.Append("[");
112          for (int i = 0; i < factorNode.Weights.Length; i++) {
113            if (i > 0) strBuilder.Append(", ");
114            AppendConstant(strBuilder, constants, factorNode.Weights[i], formatString, numberFormat);
115          }
116          strBuilder.Append("]");
117        } else if (node.Symbol is BinaryFactorVariable) {
118          var factorNode = node as BinaryFactorVariableTreeNode;
119          if (!factorNode.Weight.IsAlmost(1.0)) {
120            strBuilder.Append("(");
121            AppendConstant(strBuilder, constants, factorNode.Weight, formatString, numberFormat);
122
123            strBuilder.Append("*");
124          }
125
126          AppendVariableName(strBuilder, factorNode.VariableName);
127          strBuilder.Append(" = ");
128          AppendVariableName(strBuilder, factorNode.VariableValue);
129
130          if (!factorNode.Weight.IsAlmost(1.0)) strBuilder.Append(")");
131        } else if (node.Symbol is Constant) {
132          var constNode = node as ConstantTreeNode;
133          if(constants==null && constNode.Value < 0) {
134            strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat))
135                      .Append(")"); // (-1
136          } else {
137            AppendConstant(strBuilder, constants, constNode.Value, formatString, numberFormat);
138          }
139        }
140      }
141    }
142
143    private static void AppendConstant(StringBuilder strBuilder, List<KeyValuePair<string, double>> constants, double value, string formatString, NumberFormatInfo numberFormat) {
144      if (constants != null) {
145        string constantKey = $"c_{constants.Count}";
146        strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constantKey);
147        constants.Add(new KeyValuePair<string, double>(constantKey, value));
148      } else {
149        strBuilder.Append(value.ToString(formatString, numberFormat));
150      }
151    }
152
153    private static void AppendVariableName(StringBuilder strBuilder, string name) {
154      if (name.Contains("'"))
155        strBuilder.AppendFormat("\"{0}\"", name);
156      else
157        strBuilder.AppendFormat("'{0}'", name);
158    }
159
160    private static string GetToken(ISymbol symbol) {
161      var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault();
162      if (tok == null)
163        throw new ArgumentException(string.Format("Unknown symbol {0} found.", symbol.Name));
164      return tok;
165    }
166  }
167
168  /// <summary>
169  /// Formats mathematical expressions in infix form. E.g. x1 * (3.0 * x2 + x3)
170  /// </summary>
171  [StorableType("6FE2C83D-A594-4ABF-B101-5AEAEA6D3E3D")]
172  [Item("Infix Symbolic Expression Tree Formatter",
173    "A string formatter that converts symbolic expression trees to infix expressions.")]
174  public sealed class InfixExpressionFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
175    [StorableConstructor]
176    private InfixExpressionFormatter(StorableConstructorFlag _) : base(_) { }
177
178    private InfixExpressionFormatter(InfixExpressionFormatter original, Cloner cloner) : base(original, cloner) { }
179
180    public InfixExpressionFormatter()
181      : base() {
182      Name = ItemName;
183      Description = ItemDescription;
184    }
185
186    public override IDeepCloneable Clone(Cloner cloner) {
187      return new InfixExpressionFormatter(this, cloner);
188    }
189
190    /// <summary>
191    /// Produces an infix expression for a given expression tree.
192    /// </summary>
193    /// <param name="symbolicExpressionTree">The tree representation of the expression.</param>
194    /// <param name="numberFormat">Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param>
195    /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param>
196    /// <returns>Infix expression</returns>
197    public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat,
198                         string formatString = "G") {
199      // skip root and start symbols
200      StringBuilder strBuilder = new StringBuilder();
201      BaseInfixExpressionFormatter.FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0),
202        strBuilder, numberFormat, formatString);
203      return strBuilder.ToString();
204    }
205
206    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
207      return Format(symbolicExpressionTree, NumberFormatInfo.InvariantInfo);
208    }
209  }
210
211  [StorableType("54D917E8-134E-4066-9A60-2737C12D81DC")]
212  [Item("Infix String Formater", "Formatter for symbolic expressions, which produces an infix expression " +
213                                 "as well as a list of all coefficient values")]
214  public sealed class InfixExpressionStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
215    [StorableConstructor]
216    private InfixExpressionStringFormatter(StorableConstructorFlag _) : base(_) { }
217
218    private InfixExpressionStringFormatter(InfixExpressionStringFormatter original, Cloner cloner) : base(original, cloner) { }
219
220    public InfixExpressionStringFormatter() : base() {
221      Name = ItemName;
222      Description = ItemDescription;
223    }
224
225    public override IDeepCloneable Clone(Cloner cloner) {
226      return new InfixExpressionStringFormatter(this, cloner);
227    }
228
229    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
230      StringBuilder strBuilder = new StringBuilder();
231      var constants = new List<KeyValuePair<string, double>>();
232      BaseInfixExpressionFormatter.FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0),
233        strBuilder, NumberFormatInfo.InvariantInfo, "G", constants);
234      strBuilder.Append($"{Environment.NewLine}{Environment.NewLine}");
235
236      int maxDigits = GetDigits(constants.Count);
237      int padding = constants.Max(x => x.Value.ToString("F12", CultureInfo.InvariantCulture).Length);
238      foreach (var constant in constants) {
239        int digits = GetDigits(Int32.Parse(constant.Key.Substring(2)));
240        strBuilder.Append($"{constant.Key}{new String(' ', maxDigits - digits)} = " +
241                          string.Format($"{{0,{padding}:F12}}", constant.Value, CultureInfo.InvariantCulture) +
242                          Environment.NewLine);
243      }
244
245      return strBuilder.ToString();
246    }
247
248    private int GetDigits(int x) {
249      if (x == 0) return 1;
250      return (int)Math.Floor(Math.Log10(x) + 1);
251    }
252  }
253}
Note: See TracBrowser for help on using the repository browser.