Changeset 14501
- Timestamp:
- 12/18/16 18:06:42 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r14403 r14501 53 53 GenerateFooter(strBuilder); 54 54 return strBuilder.ToString(); 55 } 56 57 private string VariableName2Identifier(string name) { 58 return "_" + string.Join("_", Encoding.UTF8.GetBytes(name)); 55 59 } 56 60 … … 106 110 if (node is VariableTreeNode) { 107 111 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)); 109 113 } else if (node is ConstantTreeNode) { 110 114 var constNode = node as ConstantTreeNode; … … 123 127 124 128 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)))); 127 131 } 128 132 129 133 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); 131 135 } 132 136 … … 201 205 strBuilder.Append(Environment.NewLine + "public static double Evaluate ("); 202 206 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 + " */"); 209 219 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 210 224 211 225 strBuilder.AppendLine(") {");
Note: See TracChangeset
for help on using the changeset viewer.