Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14403


Ignore:
Timestamp:
11/17/16 18:41:05 (8 years ago)
Author:
gkronber
Message:

#2650: added support for factor variables to C# formatter

File:
1 edited

Legend:

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

    r14185 r14403  
    110110          var constNode = node as ConstantTreeNode;
    111111          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);
    112118        } else {
    113119          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter.");
    114120        }
    115121      }
     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);
    116131    }
    117132
     
    182197      GenerateAverageSource(strBuilder);
    183198      GenerateIfThenElseSource(strBuilder);
     199      GenerateFactorSource(strBuilder);
     200      GenerateBinaryFactorSource(strBuilder);
    184201      strBuilder.Append(Environment.NewLine + "public static double Evaluate (");
    185202
     
    215232      strBuilder.AppendLine("}");
    216233    }
     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
    217249  }
    218250}
Note: See TracChangeset for help on using the changeset viewer.