Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs @ 16654

Last change on this file since 16654 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 14.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
35  public sealed class SymbolicDataAnalysisExpressionExcelFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
36    [StorableConstructor]
37    private SymbolicDataAnalysisExpressionExcelFormatter(bool deserializing) : base(deserializing) { }
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        } else {
144          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
145          stringBuilder.Append("/(");
146          for (int i = 1; i < node.SubtreeCount; i++) {
147            if (i > 1) stringBuilder.Append("*");
148            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
149          }
150          stringBuilder.Append(")");
151        }
152      } else if (symbol is Exponential) {
153        stringBuilder.Append("EXP(");
154        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
155        stringBuilder.Append(")");
156      } else if (symbol is Square) {
157        stringBuilder.Append("POWER(");
158        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
159        stringBuilder.Append(",2)");
160      } else if (symbol is SquareRoot) {
161        stringBuilder.Append("SQRT(");
162        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
163        stringBuilder.Append(")");
164      } else if (symbol is Logarithm) {
165        stringBuilder.Append("LN(");
166        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
167        stringBuilder.Append(")");
168      } else if (symbol is Multiplication) {
169        for (int i = 0; i < node.SubtreeCount; i++) {
170          if (i > 0) stringBuilder.Append("*");
171          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
172        }
173      } else if (symbol is Sine) {
174        stringBuilder.Append("SIN(");
175        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
176        stringBuilder.Append(")");
177      } else if (symbol is Subtraction) {
178        stringBuilder.Append("(");
179        if (node.SubtreeCount == 1) {
180          stringBuilder.Append("-");
181          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
182        } else {
183          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
184          for (int i = 1; i < node.SubtreeCount; i++) {
185            stringBuilder.Append("-");
186            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
187          }
188        }
189        stringBuilder.Append(")");
190      } else if (symbol is Tangent) {
191        stringBuilder.Append("TAN(");
192        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
193        stringBuilder.Append(")");
194
195      } else if (symbol is Variable) {
196        VariableTreeNode variableTreeNode = node as VariableTreeNode;
197        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
198        stringBuilder.Append("*");
199        stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));
200      } else if (symbol is BinaryFactorVariable) {
201        var binFactorNode = node as BinaryFactorVariableTreeNode;
202        stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, 0)",
203          GetColumnToVariableName(binFactorNode.VariableName),
204          binFactorNode.VariableValue,
205          binFactorNode.Weight.ToString(CultureInfo.InvariantCulture)
206          );
207      } else if (symbol is FactorVariable) {
208        var factorNode = node as FactorVariableTreeNode;
209        var values = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray();
210        var w = factorNode.Weights;
211        // create nested if
212        for (int i = 0; i < values.Length; i++) {
213          stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, ",
214            GetColumnToVariableName(factorNode.VariableName),
215            values[i],
216            w[i].ToString(CultureInfo.InvariantCulture));
217        }
218        stringBuilder.Append("\"\""); // return empty string on unknown value
219        stringBuilder.Append(')', values.Length); // add closing parenthesis
220      } else if (symbol is Power) {
221        stringBuilder.Append("POWER(");
222        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
223        stringBuilder.Append(",ROUND(");
224        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
225        stringBuilder.Append(",0))");
226      } else if (symbol is Root) {
227        stringBuilder.Append("(");
228        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
229        stringBuilder.Append(")^(1 / ROUND(");
230        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
231        stringBuilder.Append(",0))");
232      } else if (symbol is IfThenElse) {
233        stringBuilder.Append("IF(");
234        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + " ) > 0");
235        stringBuilder.Append(",");
236        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
237        stringBuilder.Append(",");
238        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
239        stringBuilder.Append(")");
240      } else if (symbol is VariableCondition) {
241        VariableConditionTreeNode variableConditionTreeNode = node as VariableConditionTreeNode;
242        if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
243          double threshold = variableConditionTreeNode.Threshold;
244          double slope = variableConditionTreeNode.Slope;
245          string p = "(1 / (1 + EXP(-" + slope.ToString(CultureInfo.InvariantCulture) + " * (" +
246                     GetColumnToVariableName(variableConditionTreeNode.VariableName) + "-" +
247                     threshold.ToString(CultureInfo.InvariantCulture) + "))))";
248          stringBuilder.Append("((");
249          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
250          stringBuilder.Append("*");
251          stringBuilder.Append(p);
252          stringBuilder.Append(") + (");
253          stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
254          stringBuilder.Append("*(");
255          stringBuilder.Append("1 - " + p + ")");
256          stringBuilder.Append("))");
257        } else {
258          stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "(IF({0} <= {1}, {2}, {3}))",
259            GetColumnToVariableName(variableConditionTreeNode.VariableName),
260            variableConditionTreeNode.Threshold,
261            FormatRecursively(node.GetSubtree(0)),
262            FormatRecursively(node.GetSubtree(1))
263            );
264        }
265      } else if (symbol is Xor) {
266        stringBuilder.Append("IF(");
267        stringBuilder.Append("XOR(");
268        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
269        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
270        stringBuilder.Append("), 1.0, -1.0)");
271      } else if (symbol is Or) {
272        stringBuilder.Append("IF(");
273        stringBuilder.Append("OR(");
274        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
275        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
276        stringBuilder.Append("), 1.0, -1.0)");
277      } else if (symbol is And) {
278        stringBuilder.Append("IF(");
279        stringBuilder.Append("AND(");
280        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
281        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
282        stringBuilder.Append("), 1.0, -1.0)");
283      } else if (symbol is Not) {
284        stringBuilder.Append("IF(");
285        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
286        stringBuilder.Append(" > 0, -1.0, 1.0)");
287      } else if (symbol is GreaterThan) {
288        stringBuilder.Append("IF((");
289        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
290        stringBuilder.Append(") > (");
291        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
292        stringBuilder.Append("), 1.0, -1.0)");
293      } else if (symbol is LessThan) {
294        stringBuilder.Append("IF((");
295        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
296        stringBuilder.Append(") < (");
297        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
298        stringBuilder.Append("), 1.0, -1.0)");
299      } else {
300        throw new NotImplementedException("Excel export of " + node.Symbol + " is not implemented.");
301      }
302      return stringBuilder.ToString();
303    }
304  }
305}
Note: See TracBrowser for help on using the repository browser.