Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs @ 15301

Last change on this file since 15301 was 14878, checked in by bburlacu, 8 years ago

#1772: Merge trunk changes.

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