Ignore:
Timestamp:
04/04/17 17:52:44 (6 months ago)
Author:
gkronber
Message:

#2650: merged the factors branch into trunk

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r14400 r14826  
    5151      while (dividend > 0) {
    5252        int modulo = (dividend - 1) % 26;
    53         columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
     53        columnName = Convert.ToChar(65 + modulo) + columnName;
    5454        dividend = (int)((dividend - modulo) / 26);
    5555      }
     
    6060    private readonly Dictionary<string, string> variableNameMapping = new Dictionary<string, string>();
    6161    private int currentVariableIndex = 0;
    62     private string GetColumnToVariableName(string variabelName) {
    63       if (!variableNameMapping.ContainsKey(variabelName)) {
     62    private string GetColumnToVariableName(string varName) {
     63      if (!variableNameMapping.ContainsKey(varName)) {
    6464        currentVariableIndex++;
    65         variableNameMapping.Add(variabelName, GetExcelColumnName(currentVariableIndex));
    66       }
    67       return string.Format("${0}1", variableNameMapping[variabelName]);
     65        variableNameMapping.Add(varName, GetExcelColumnName(currentVariableIndex));
     66      }
     67      return string.Format("${0}1", variableNameMapping[varName]);
    6868    }
    6969    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
     
    7171    }
    7272
     73
    7374    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);
    7483      var stringBuilder = new StringBuilder();
    75       if (dataset != null) CalculateVariableMapping(symbolicExpressionTree, dataset);
    76 
     84     
    7785      stringBuilder.Append("=");
    7886      stringBuilder.Append(FormatRecursively(symbolicExpressionTree.Root));
    7987
    80       foreach (var variable in variableNameMapping) {
     88      foreach (var variable in this.variableNameMapping) {
    8189        stringBuilder.AppendLine();
    8290        stringBuilder.Append(variable.Key + " = " + variable.Value);
     
    8593    }
    8694
    87     private void CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
    88       int columnIndex = 0;
     95    private Dictionary<string,string> CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
     96      var mapping = new Dictionary<string,string>();
    8997      int inputIndex = 0;
    90       var usedVariables = tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(v => v.VariableName).Distinct();
     98      var usedVariables = tree.IterateNodesPrefix().OfType<IVariableTreeNode>().Select(v => v.VariableName).Distinct().ToArray();
    9199      foreach (var variable in dataset.VariableNames) {
    92         columnIndex++;
    93100        if (!usedVariables.Contains(variable)) continue;
    94101        inputIndex++;
    95         variableNameMapping[variable] = GetExcelColumnName(inputIndex);
    96       }
     102        mapping[variable] = GetExcelColumnName(inputIndex);
     103      }
     104      return mapping;
    97105    }
    98106
     
    190198        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    191199        stringBuilder.Append("*");
    192         stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));// + LagToString(currentLag));
     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
    193221      } else if (symbol is Power) {
    194222        stringBuilder.Append("POWER(");
Note: See TracChangeset for help on using the changeset viewer.