Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs @ 16773

Last change on this file since 16773 was 16764, checked in by gkronber, 6 years ago

#3001: fixed a bug in the Excel formatter for 1/x (division with a single argument)

File size: 14.3 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;
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;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [Item("Excel String Formatter", "String formatter for string representations of symbolic data analysis expressions in Excel syntax.")]
34  [StorableType("46C46897-9C92-4CF1-81C9-700732700DD3")]
35  public sealed class SymbolicDataAnalysisExpressionExcelFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
36    [StorableConstructor]
37    private SymbolicDataAnalysisExpressionExcelFormatter(StorableConstructorFlag _) : base(_) { }
38    private SymbolicDataAnalysisExpressionExcelFormatter(SymbolicDataAnalysisExpressionExcelFormatter original, Cloner cloner) : base(original, cloner) { }
39    public SymbolicDataAnalysisExpressionExcelFormatter()
40      : base() {
41      Name = ItemName;
42      Description = ItemDescription;
43    }
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new SymbolicDataAnalysisExpressionExcelFormatter(this, cloner);
46    }
47    private string GetExcelColumnName(int columnNumber) {
48      int dividend = columnNumber;
49      string columnName = String.Empty;
50
51      while (dividend > 0) {
52        int modulo = (dividend - 1) % 26;
53        columnName = System.Convert.ToChar(65 + modulo) + columnName;
54        dividend = (int)((dividend - modulo) / 26);
55      }
56
57      return columnName;
58    }
59
60    private readonly Dictionary<string, string> variableNameMapping = new Dictionary<string, string>();
61    private int currentVariableIndex = 0;
62    private string GetColumnToVariableName(string varName) {
63      if (!variableNameMapping.ContainsKey(varName)) {
64        currentVariableIndex++;
65        variableNameMapping.Add(varName, GetExcelColumnName(currentVariableIndex));
66      }
67      return string.Format("${0}1", variableNameMapping[varName]);
68    }
69    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
70      return Format(symbolicExpressionTree, null);
71    }
72
73
74    public string Format(ISymbolicExpressionTree symbolicExpressionTree, IDataset dataset) {
75      if (dataset != null)
76        return FormatWithMapping(symbolicExpressionTree, CalculateVariableMapping(symbolicExpressionTree, dataset));
77      else return FormatWithMapping(symbolicExpressionTree, new Dictionary<string, string>());
78    }
79
80    public string FormatWithMapping(ISymbolicExpressionTree symbolicExpressionTree, Dictionary<string, string> variableNameMapping) {
81      foreach (var kvp in variableNameMapping) this.variableNameMapping.Add(kvp.Key, kvp.Value);
82      var stringBuilder = new StringBuilder();
83
84      stringBuilder.Append("=");
85      stringBuilder.Append(FormatRecursively(symbolicExpressionTree.Root));
86
87      foreach (var variable in this.variableNameMapping) {
88        stringBuilder.AppendLine();
89        stringBuilder.Append(variable.Key + " = " + variable.Value);
90      }
91      return stringBuilder.ToString();
92    }
93
94    private Dictionary<string, string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
95      var mapping = new Dictionary<string, string>();
96      int inputIndex = 0;
97      var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct().ToArray();
98      foreach (var variable in dataset.VariableNames) {
99        if (!usedVariables.Contains(variable)) continue;
100        inputIndex++;
101        mapping[variable] = GetExcelColumnName(inputIndex);
102      }
103      return mapping;
104    }
105
106    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
107      ISymbol symbol = node.Symbol;
108      StringBuilder stringBuilder = new StringBuilder();
109
110      if (symbol is ProgramRootSymbol) {
111        stringBuilder.AppendLine(FormatRecursively(node.GetSubtree(0)));
112      } else if (symbol is StartSymbol)
113        return FormatRecursively(node.GetSubtree(0));
114      else if (symbol is Addition) {
115        stringBuilder.Append("(");
116        for (int i = 0; i < node.SubtreeCount; i++) {
117          if (i > 0) stringBuilder.Append("+");
118          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
119        }
120        stringBuilder.Append(")");
121      } else if (symbol is Average) {
122        stringBuilder.Append("(1/(");
123        stringBuilder.Append(node.SubtreeCount);
124        stringBuilder.Append(")*(");
125        for (int i = 0; i < node.SubtreeCount; i++) {
126          if (i > 0) stringBuilder.Append("+");
127          stringBuilder.Append("(");
128          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
129          stringBuilder.Append(")");
130        }
131        stringBuilder.Append(")");
132      } else if (symbol is Constant) {
133        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
134        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
135      } else if (symbol is Cosine) {
136        stringBuilder.Append("COS(");
137        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
138        stringBuilder.Append(")");
139      } else if (symbol is Division) {
140        if (node.SubtreeCount == 1) {
141          stringBuilder.Append("1/(");
142          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
143          stringBuilder.Append(")");
144        } else {
145          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
146          stringBuilder.Append("/(");
147          for (int i = 1; i < node.SubtreeCount; i++) {
148            if (i > 1) stringBuilder.Append("*");
149            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
150          }
151          stringBuilder.Append(")");
152        }
153      } else if (symbol is Exponential) {
154        stringBuilder.Append("EXP(");
155        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
156        stringBuilder.Append(")");
157      } else if (symbol is Square) {
158        stringBuilder.Append("POWER(");
159        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
160        stringBuilder.Append(",2)");
161      } else if (symbol is SquareRoot) {
162        stringBuilder.Append("SQRT(");
163        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
164        stringBuilder.Append(")");
165      } else if (symbol is Logarithm) {
166        stringBuilder.Append("LN(");
167        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
168        stringBuilder.Append(")");
169      } else if (symbol is Multiplication) {
170        for (int i = 0; i < node.SubtreeCount; i++) {
171          if (i > 0) stringBuilder.Append("*");
172          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
173        }
174      } else if (symbol is Sine) {
175        stringBuilder.Append("SIN(");
176        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
177        stringBuilder.Append(")");
178      } else if (symbol is Subtraction) {
179        stringBuilder.Append("(");
180        if (node.SubtreeCount == 1) {
181          stringBuilder.Append("-");
182          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
183        } else {
184          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
185          for (int i = 1; i < node.SubtreeCount; i++) {
186            stringBuilder.Append("-");
187            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
188          }
189        }
190        stringBuilder.Append(")");
191      } else if (symbol is Tangent) {
192        stringBuilder.Append("TAN(");
193        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
194        stringBuilder.Append(")");
195      } else if (symbol is HyperbolicTangent) {
196        stringBuilder.Append("TANH(");
197        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
198        stringBuilder.Append(")");
199      } else if (symbol is Variable) {
200        VariableTreeNode variableTreeNode = node as VariableTreeNode;
201        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
202        stringBuilder.Append("*");
203        stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));
204      } else if (symbol is BinaryFactorVariable) {
205        var binFactorNode = node as BinaryFactorVariableTreeNode;
206        stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, 0)",
207          GetColumnToVariableName(binFactorNode.VariableName),
208          binFactorNode.VariableValue,
209          binFactorNode.Weight.ToString(CultureInfo.InvariantCulture)
210          );
211      } else if (symbol is FactorVariable) {
212        var factorNode = node as FactorVariableTreeNode;
213        var values = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray();
214        var w = factorNode.Weights;
215        // create nested if
216        for (int i = 0; i < values.Length; i++) {
217          stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, ",
218            GetColumnToVariableName(factorNode.VariableName),
219            values[i],
220            w[i].ToString(CultureInfo.InvariantCulture));
221        }
222        stringBuilder.Append("\"\""); // return empty string on unknown value
223        stringBuilder.Append(')', values.Length); // add closing parenthesis
224      } else if (symbol is Power) {
225        stringBuilder.Append("POWER(");
226        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
227        stringBuilder.Append(",ROUND(");
228        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
229        stringBuilder.Append(",0))");
230      } else if (symbol is Root) {
231        stringBuilder.Append("(");
232        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
233        stringBuilder.Append(")^(1 / ROUND(");
234        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
235        stringBuilder.Append(",0))");
236      } else if (symbol is IfThenElse) {
237        stringBuilder.Append("IF(");
238        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + " ) > 0");
239        stringBuilder.Append(",");
240        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
241        stringBuilder.Append(",");
242        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
243        stringBuilder.Append(")");
244      } else if (symbol is VariableCondition) {
245        VariableConditionTreeNode variableConditionTreeNode = node as VariableConditionTreeNode;
246        if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
247          double threshold = variableConditionTreeNode.Threshold;
248          double slope = variableConditionTreeNode.Slope;
249          string p = "(1 / (1 + EXP(-" + slope.ToString(CultureInfo.InvariantCulture) + " * (" +
250                     GetColumnToVariableName(variableConditionTreeNode.VariableName) + "-" +
251                     threshold.ToString(CultureInfo.InvariantCulture) + "))))";
252          stringBuilder.Append("((");
253          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
254          stringBuilder.Append("*");
255          stringBuilder.Append(p);
256          stringBuilder.Append(") + (");
257          stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
258          stringBuilder.Append("*(");
259          stringBuilder.Append("1 - " + p + ")");
260          stringBuilder.Append("))");
261        } else {
262          stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "(IF({0} <= {1}, {2}, {3}))",
263            GetColumnToVariableName(variableConditionTreeNode.VariableName),
264            variableConditionTreeNode.Threshold,
265            FormatRecursively(node.GetSubtree(0)),
266            FormatRecursively(node.GetSubtree(1))
267            );
268        }
269      } else if (symbol is Xor) {
270        stringBuilder.Append("IF(");
271        stringBuilder.Append("XOR(");
272        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
273        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
274        stringBuilder.Append("), 1.0, -1.0)");
275      } else if (symbol is Or) {
276        stringBuilder.Append("IF(");
277        stringBuilder.Append("OR(");
278        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
279        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
280        stringBuilder.Append("), 1.0, -1.0)");
281      } else if (symbol is And) {
282        stringBuilder.Append("IF(");
283        stringBuilder.Append("AND(");
284        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
285        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
286        stringBuilder.Append("), 1.0, -1.0)");
287      } else if (symbol is Not) {
288        stringBuilder.Append("IF(");
289        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
290        stringBuilder.Append(" > 0, -1.0, 1.0)");
291      } else if (symbol is GreaterThan) {
292        stringBuilder.Append("IF((");
293        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
294        stringBuilder.Append(") > (");
295        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
296        stringBuilder.Append("), 1.0, -1.0)");
297      } else if (symbol is LessThan) {
298        stringBuilder.Append("IF((");
299        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
300        stringBuilder.Append(") < (");
301        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
302        stringBuilder.Append("), 1.0, -1.0)");
303      } else {
304        throw new NotImplementedException("Excel export of " + node.Symbol + " is not implemented.");
305      }
306      return stringBuilder.ToString();
307    }
308  }
309}
Note: See TracBrowser for help on using the repository browser.