Changeset 14501


Ignore:
Timestamp:
12/18/16 18:06:42 (3 years ago)
Author:
gkronber
Message:

#2650: better handling of variable names (as identifiers) and fixed some bugs

File:
1 edited

Legend:

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

    r14403 r14501  
    5353      GenerateFooter(strBuilder);
    5454      return strBuilder.ToString();
     55    }
     56
     57    private string VariableName2Identifier(string name) {
     58      return "_" + string.Join("_", Encoding.UTF8.GetBytes(name));
    5559    }
    5660
     
    106110        if (node is VariableTreeNode) {
    107111          var varNode = node as VariableTreeNode;
    108           strBuilder.AppendFormat("{0} * {1}", varNode.VariableName, varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
     112          strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture));
    109113        } else if (node is ConstantTreeNode) {
    110114          var constNode = node as ConstantTreeNode;
     
    123127
    124128    private void FormatFactor(FactorVariableTreeNode node, StringBuilder strBuilder) {
    125       strBuilder.AppendFormat("EvaluateFactor({0}, new [] {{ {1} }}, new [] {{ {2} }})", node.VariableName,
    126         string.Join(",", node.Symbol.GetVariableValues(node.VariableName)), string.Join(",", node.Weights.Select(v => v.ToString(CultureInfo.InvariantCulture))));
     129      strBuilder.AppendFormat("EvaluateFactor({0}, new [] {{ {1} }}, new [] {{ {2} }})", VariableName2Identifier(node.VariableName),
     130        string.Join(",", node.Symbol.GetVariableValues(node.VariableName).Select(name => "\"" + name + "\"")), string.Join(",", node.Weights.Select(v => v.ToString(CultureInfo.InvariantCulture))));
    127131    }
    128132
    129133    private void FormatBinaryFactor(BinaryFactorVariableTreeNode node, StringBuilder strBuilder) {
    130       strBuilder.AppendFormat(CultureInfo.InvariantCulture, "EvaluateBinaryFactor({0}, {1}, {2})", node.VariableName, node.VariableValue, node.Weight);
     134      strBuilder.AppendFormat(CultureInfo.InvariantCulture, "EvaluateBinaryFactor({0}, \"{1}\", {2})", VariableName2Identifier(node.VariableName), node.VariableValue, node.Weight);
    131135    }
    132136
     
    201205      strBuilder.Append(Environment.NewLine + "public static double Evaluate (");
    202206
    203       HashSet<string> varNames = new HashSet<string>();
    204       foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode)) {
    205         varNames.Add(((VariableTreeNode)node).VariableName);
    206       }
    207 
    208       var orderedNames = varNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "double " + n);
     207      // here we don't have access to problemData to determine the type for each variable (double/string) therefore we must distinguish based on the symbol type
     208      HashSet<string> doubleVarNames = new HashSet<string>();
     209      foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is VariableTreeNode || x is VariableConditionTreeNode)) {
     210        doubleVarNames.Add(((IVariableTreeNode)node).VariableName);
     211      }
     212
     213      HashSet<string> stringVarNames = new HashSet<string>();
     214      foreach (var node in symbolicExpressionTree.IterateNodesPostfix().Where(x => x is BinaryFactorVariableTreeNode || x is FactorVariableTreeNode)) {
     215        stringVarNames.Add(((IVariableTreeNode)node).VariableName);
     216      }
     217
     218      var orderedNames = stringVarNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "string " + VariableName2Identifier(n) + " /* " + n + " */");
    209219      strBuilder.Append(string.Join(", ", orderedNames));
     220
     221      orderedNames = doubleVarNames.OrderBy(n => n, new NaturalStringComparer()).Select(n => "double " + VariableName2Identifier(n) + " /* " + n + " */");
     222      strBuilder.Append(string.Join(", ", orderedNames));
     223
    210224
    211225      strBuilder.AppendLine(") {");
Note: See TracChangeset for help on using the changeset viewer.