Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/12/17 18:14:19 (8 years ago)
Author:
gkronber
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r14259 r14560  
    2828using HeuristicLab.Core;
    2929using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    30 using HeuristicLab.Optimization.Operators;
    3130using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3231
     
    5251      while (dividend > 0) {
    5352        int modulo = (dividend - 1) % 26;
    54         columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
     53        columnName = Convert.ToChar(65 + modulo) + columnName;
    5554        dividend = (int)((dividend - modulo) / 26);
    5655      }
     
    6160    private readonly Dictionary<string, string> variableNameMapping = new Dictionary<string, string>();
    6261    private int currentVariableIndex = 0;
    63     private string GetColumnToVariableName(string variabelName) {
    64       if (!variableNameMapping.ContainsKey(variabelName)) {
     62    private string GetColumnToVariableName(string varName) {
     63      if (!variableNameMapping.ContainsKey(varName)) {
    6564        currentVariableIndex++;
    66         variableNameMapping.Add(variabelName, GetExcelColumnName(currentVariableIndex));
    67       }
    68       return string.Format("${0}1", variableNameMapping[variabelName]);
     65        variableNameMapping.Add(varName, GetExcelColumnName(currentVariableIndex));
     66      }
     67      return string.Format("${0}1", variableNameMapping[varName]);
    6968    }
    7069    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
     
    7574    public string Format(ISymbolicExpressionTree symbolicExpressionTree, IDataset dataset) {
    7675      var stringBuilder = new StringBuilder();
    77       if (dataset != null) CalculateVariableMapping(symbolicExpressionTree, dataset);
    78 
     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     
    7986      stringBuilder.Append("=");
    8087      stringBuilder.Append(FormatRecursively(symbolicExpressionTree.Root));
    8188
    82       foreach (var variable in variableNameMapping) {
     89      foreach (var variable in this.variableNameMapping) {
    8390        stringBuilder.AppendLine();
    8491        stringBuilder.Append(variable.Key + " = " + variable.Value);
     
    8794    }
    8895
    89     private void CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
    90       int columnIndex = 0;
     96    private Dictionary<string,string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
     97      var mapping = new Dictionary<string,string>();
    9198      int inputIndex = 0;
    92       var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct();
     99      var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct().ToArray();
    93100      foreach (var variable in dataset.VariableNames) {
    94         columnIndex++;
    95101        if (!usedVariables.Contains(variable)) continue;
    96102        inputIndex++;
    97         variableNameMapping[variable] = GetExcelColumnName(inputIndex);
    98       }
     103        mapping[variable] = GetExcelColumnName(inputIndex);
     104      }
     105      return mapping;
    99106    }
    100107
     
    192199        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    193200        stringBuilder.Append("*");
    194         stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName)); // + LagToString(currentLag));
     201        stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));
    195202      } else if (symbol is BinaryFactorVariable) {
    196203        var binFactorNode = node as BinaryFactorVariableTreeNode;
Note: See TracChangeset for help on using the changeset viewer.