Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs @ 14720

Last change on this file since 14720 was 14560, checked in by gkronber, 8 years ago

#2650: added a method for exporting models as Excel expressions with given variable mapping (for convenience)

File size: 13.6 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.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 = 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      var stringBuilder = new StringBuilder();
76      if (dataset != null)
77        return FormatWithMapping(symbolicExpressionTree, CalculateVariableMapping(symbolicExpressionTree, dataset));
78      else return FormatWithMapping(symbolicExpressionTree, new Dictionary<string, string>());
79
80    }
81    public string FormatWithMapping(ISymbolicExpressionTree symbolicExpressionTree, Dictionary<string,string> variableNameMapping)
82    {
83      foreach(var kvp in variableNameMapping) this.variableNameMapping.Add(kvp.Key,kvp.Value);
84      var stringBuilder = new StringBuilder();
85     
86      stringBuilder.Append("=");
87      stringBuilder.Append(FormatRecursively(symbolicExpressionTree.Root));
88
89      foreach (var variable in this.variableNameMapping) {
90        stringBuilder.AppendLine();
91        stringBuilder.Append(variable.Key + " = " + variable.Value);
92      }
93      return stringBuilder.ToString();
94    }
95
96    private Dictionary<string,string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
97      var mapping = new Dictionary<string,string>();
98      int inputIndex = 0;
99      var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct().ToArray();
100      foreach (var variable in dataset.VariableNames) {
101        if (!usedVariables.Contains(variable)) continue;
102        inputIndex++;
103        mapping[variable] = GetExcelColumnName(inputIndex);
104      }
105      return mapping;
106    }
107
108    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
109      ISymbol symbol = node.Symbol;
110      StringBuilder stringBuilder = new StringBuilder();
111
112      if (symbol is ProgramRootSymbol) {
113        stringBuilder.AppendLine(FormatRecursively(node.GetSubtree(0)));
114      } else if (symbol is StartSymbol)
115        return FormatRecursively(node.GetSubtree(0));
116      else if (symbol is Addition) {
117        stringBuilder.Append("(");
118        for (int i = 0; i < node.SubtreeCount; i++) {
119          if (i > 0) stringBuilder.Append("+");
120          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
121        }
122        stringBuilder.Append(")");
123      } else if (symbol is Average) {
124        stringBuilder.Append("(1/");
125        stringBuilder.Append(node.SubtreeCount);
126        stringBuilder.Append(")*(");
127        for (int i = 0; i < node.SubtreeCount; i++) {
128          if (i > 0) stringBuilder.Append("+");
129          stringBuilder.Append("(");
130          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
131          stringBuilder.Append(")");
132        }
133        stringBuilder.Append(")");
134      } else if (symbol is Constant) {
135        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
136        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
137      } else if (symbol is Cosine) {
138        stringBuilder.Append("COS(");
139        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
140        stringBuilder.Append(")");
141      } else if (symbol is Division) {
142        if (node.SubtreeCount == 1) {
143          stringBuilder.Append("1/");
144          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
145        } else {
146          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
147          stringBuilder.Append("/(");
148          for (int i = 1; i < node.SubtreeCount; i++) {
149            if (i > 1) stringBuilder.Append("*");
150            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
151          }
152          stringBuilder.Append(")");
153        }
154      } else if (symbol is Exponential) {
155        stringBuilder.Append("EXP(");
156        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
157        stringBuilder.Append(")");
158      } else if (symbol is Square) {
159        stringBuilder.Append("POWER(");
160        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
161        stringBuilder.Append(",2)");
162      } else if (symbol is SquareRoot) {
163        stringBuilder.Append("SQRT(");
164        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
165        stringBuilder.Append(")");
166      } else if (symbol is Logarithm) {
167        stringBuilder.Append("LN(");
168        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
169        stringBuilder.Append(")");
170      } else if (symbol is Multiplication) {
171        for (int i = 0; i < node.SubtreeCount; i++) {
172          if (i > 0) stringBuilder.Append("*");
173          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
174        }
175      } else if (symbol is Sine) {
176        stringBuilder.Append("SIN(");
177        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
178        stringBuilder.Append(")");
179      } else if (symbol is Subtraction) {
180        stringBuilder.Append("(");
181        if (node.SubtreeCount == 1) {
182          stringBuilder.Append("-");
183          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
184        } else {
185          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
186          for (int i = 1; i < node.SubtreeCount; i++) {
187            stringBuilder.Append("-");
188            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
189          }
190        }
191        stringBuilder.Append(")");
192      } else if (symbol is Tangent) {
193        stringBuilder.Append("TAN(");
194        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
195        stringBuilder.Append(")");
196
197      } else if (symbol is Variable) {
198        VariableTreeNode variableTreeNode = node as VariableTreeNode;
199        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
200        stringBuilder.Append("*");
201        stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));
202      } else if (symbol is BinaryFactorVariable) {
203        var binFactorNode = node as BinaryFactorVariableTreeNode;
204        stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, 0)",
205          GetColumnToVariableName(binFactorNode.VariableName),
206          binFactorNode.VariableValue,
207          binFactorNode.Weight.ToString(CultureInfo.InvariantCulture)
208          );
209      } else if (symbol is FactorVariable) {
210        var factorNode = node as FactorVariableTreeNode;
211        var values = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray();
212        var w = factorNode.Weights;
213        // create nested if
214        for (int i = 0; i < values.Length; i++) {
215          stringBuilder.AppendFormat("IF({0}=\"{1}\", {2}, ",
216            GetColumnToVariableName(factorNode.VariableName),
217            values[i],
218            w[i].ToString(CultureInfo.InvariantCulture));
219        }
220        stringBuilder.Append("\"\""); // return empty string on unknown value
221        stringBuilder.Append(')', values.Length); // add closing parenthesis
222      } else if (symbol is Power) {
223        stringBuilder.Append("POWER(");
224        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
225        stringBuilder.Append(",ROUND(");
226        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
227        stringBuilder.Append(",0))");
228      } else if (symbol is Root) {
229        stringBuilder.Append("(");
230        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
231        stringBuilder.Append(")^(1 / ROUND(");
232        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
233        stringBuilder.Append(",0))");
234      } else if (symbol is IfThenElse) {
235        stringBuilder.Append("IF(");
236        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + " ) > 0");
237        stringBuilder.Append(",");
238        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
239        stringBuilder.Append(",");
240        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
241        stringBuilder.Append(")");
242      } else if (symbol is VariableCondition) {
243        VariableConditionTreeNode variableConditionTreeNode = node as VariableConditionTreeNode;
244        double threshold = variableConditionTreeNode.Threshold;
245        double slope = variableConditionTreeNode.Slope;
246        string p = "(1 / (1 + EXP(-" + slope.ToString(CultureInfo.InvariantCulture) + " * (" + GetColumnToVariableName(variableConditionTreeNode.VariableName) + "-" + threshold.ToString(CultureInfo.InvariantCulture) + "))))";
247        stringBuilder.Append("((");
248        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
249        stringBuilder.Append("*");
250        stringBuilder.Append(p);
251        stringBuilder.Append(") + (");
252        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
253        stringBuilder.Append("*(");
254        stringBuilder.Append("1 - " + p + ")");
255        stringBuilder.Append("))");
256      } else if (symbol is Xor) {
257        stringBuilder.Append("IF(");
258        stringBuilder.Append("XOR(");
259        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
260        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
261        stringBuilder.Append("), 1.0, -1.0)");
262      } else if (symbol is Or) {
263        stringBuilder.Append("IF(");
264        stringBuilder.Append("OR(");
265        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
266        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
267        stringBuilder.Append("), 1.0, -1.0)");
268      } else if (symbol is And) {
269        stringBuilder.Append("IF(");
270        stringBuilder.Append("AND(");
271        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(0)) + ") > 0,");
272        stringBuilder.Append("(" + FormatRecursively(node.GetSubtree(1)) + ") > 0");
273        stringBuilder.Append("), 1.0, -1.0)");
274      } else if (symbol is Not) {
275        stringBuilder.Append("IF(");
276        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
277        stringBuilder.Append(" > 0, -1.0, 1.0)");
278      } else if (symbol is GreaterThan) {
279        stringBuilder.Append("IF((");
280        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
281        stringBuilder.Append(") > (");
282        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
283        stringBuilder.Append("), 1.0, -1.0)");
284      } else if (symbol is LessThan) {
285        stringBuilder.Append("IF((");
286        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
287        stringBuilder.Append(") < (");
288        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
289        stringBuilder.Append("), 1.0, -1.0)");
290      } else {
291        throw new NotImplementedException("Excel export of " + node.Symbol + " is not implemented.");
292      }
293      return stringBuilder.ToString();
294    }
295  }
296}
Note: See TracBrowser for help on using the repository browser.