Changeset 14403
- Timestamp:
- 11/17/16 18:41:05 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r14185 r14403 110 110 var constNode = node as ConstantTreeNode; 111 111 strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 112 } else if (node.Symbol is FactorVariable) { 113 var factorNode = node as FactorVariableTreeNode; 114 FormatFactor(factorNode, strBuilder); 115 } else if (node.Symbol is BinaryFactorVariable) { 116 var binFactorNode = node as BinaryFactorVariableTreeNode; 117 FormatBinaryFactor(binFactorNode, strBuilder); 112 118 } else { 113 119 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter."); 114 120 } 115 121 } 122 } 123 124 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)))); 127 } 128 129 private void FormatBinaryFactor(BinaryFactorVariableTreeNode node, StringBuilder strBuilder) { 130 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "EvaluateBinaryFactor({0}, {1}, {2})", node.VariableName, node.VariableValue, node.Weight); 116 131 } 117 132 … … 182 197 GenerateAverageSource(strBuilder); 183 198 GenerateIfThenElseSource(strBuilder); 199 GenerateFactorSource(strBuilder); 200 GenerateBinaryFactorSource(strBuilder); 184 201 strBuilder.Append(Environment.NewLine + "public static double Evaluate ("); 185 202 … … 215 232 strBuilder.AppendLine("}"); 216 233 } 234 235 private void GenerateFactorSource(StringBuilder strBuilder) { 236 strBuilder.AppendLine("private static double EvaluateFactor(string factorValue, string[] factorValues, double[] constants) {"); 237 strBuilder.AppendLine(" for(int i=0;i<factorValues.Length;i++) " + 238 " if(factorValues[i] == factorValue) return constants[i];" + 239 " throw new ArgumentException();"); 240 strBuilder.AppendLine("}"); 241 } 242 243 private void GenerateBinaryFactorSource(StringBuilder strBuilder) { 244 strBuilder.AppendLine("private static double EvaluateBinaryFactor(string factorValue, string targetValue, double weight) {"); 245 strBuilder.AppendLine(" return factorValue == targetValue ? weight : 0.0;"); 246 strBuilder.AppendLine("}"); 247 } 248 217 249 } 218 250 }
Note: See TracChangeset
for help on using the changeset viewer.