/// <summary> /// A helper function for evidence messages when the output of the factor is observed. /// </summary> /// <param name="str">The observed value of <c>str</c>.</param> /// <param name="format">The message from <c>format</c>.</param> /// <param name="args">The message from <c>args</c>.</param> /// <param name="expectedLogEvidence"> /// The expected log-evidence if the format string is required to contain placeholders for all arguments. /// </param> /// <param name="expectedLogEvidenceWithMissingPlaceholders"> /// The expected log-evidence if the format string may not contain placeholders for some arguments. /// </param> private static void TestEvidence( string str, StringDistribution format, StringDistribution[] args, double expectedLogEvidence, double expectedLogEvidenceWithMissingPlaceholders) { const double LogEvidenceEps = 1e-6; string[] argNames = GetDefaultArgumentNames(args.Length); Assert.Equal( expectedLogEvidence, StringFormatOp_RequireEveryPlaceholder_NoArgumentNames.LogEvidenceRatio(str, format, args), LogEvidenceEps); Assert.Equal( expectedLogEvidence, StringFormatOp_RequireEveryPlaceholder.LogEvidenceRatio(str, format, args, argNames), LogEvidenceEps); Assert.Equal( expectedLogEvidenceWithMissingPlaceholders, StringFormatOp_AllowMissingPlaceholders_NoArgumentNames.LogEvidenceRatio(str, format, args), LogEvidenceEps); Assert.Equal( expectedLogEvidenceWithMissingPlaceholders, StringFormatOp_AllowMissingPlaceholders.LogEvidenceRatio(str, format, args, argNames), LogEvidenceEps); }
/// <summary> /// A helper function for evidence messages when the output of the factor is not observed. /// </summary> /// <param name="str">The message to <c>str</c>.</param>. private static void TestEvidence(StringDistribution str) { Assert.Equal(0, StringFormatOp_RequireEveryPlaceholder_NoArgumentNames.LogEvidenceRatio(str)); Assert.Equal(0, StringFormatOp_RequireEveryPlaceholder.LogEvidenceRatio(str)); Assert.Equal(0, StringFormatOp_AllowMissingPlaceholders_NoArgumentNames.LogEvidenceRatio(str)); Assert.Equal(0, StringFormatOp_AllowMissingPlaceholders.LogEvidenceRatio(str)); }
public void InefficientPermutationRepresentation() { StringDistribution placeholder = StringDistribution.Char('{') + StringDistribution.Char(DiscreteChar.Digit()) + StringDistribution.Char('}'); StringDistribution formatPrior = placeholder + StringDistribution.String(" "); formatPrior = StringDistribution.ZeroOrMore(formatPrior) + placeholder; const int ArgCount = 6; const int ArgLength = 3; StringDistribution[] args = Util.ArrayInit(ArgCount, i => StringDistribution.String(new string((char)('0' + i), ArgLength))); StringDistribution str = StringFormatOp_AllowMissingPlaceholders_NoArgumentNames.StrAverageConditional(formatPrior, args); Console.WriteLine(str.GetWorkspaceOrPoint().States.Count); StringInferenceTestUtilities.TestIfIncludes(str, "111 333 222", "111 222", "222 111", "111", "222", "333"); StringInferenceTestUtilities.TestIfExcludes(str, string.Empty, "111 111", "222 222 111", "333 111 222 333", "112", "1"); }
/// <summary> /// A helper function for testing messages to <c>format</c>. /// </summary> /// <param name="str">The message from <c>str</c>.</param> /// <param name="args">The message from <c>args</c>.</param> /// <param name="expectedFormatRequireEveryPlaceholder"> /// The expected message to <c>format</c> if the format string is required to contain placeholders for all arguments. /// </param> /// <param name="expectedFormatAllowMissingPlaceholders"> /// The expected message to <c>format</c> if the format string may not contain placeholders for some arguments. /// </param> private static void TestMessageToFormat( StringDistribution str, StringDistribution[] args, StringDistribution expectedFormatRequireEveryPlaceholder, StringDistribution expectedFormatAllowMissingPlaceholders) { string[] argNames = GetDefaultArgumentNames(args.Length); Assert.Equal( expectedFormatRequireEveryPlaceholder, StringFormatOp_RequireEveryPlaceholder_NoArgumentNames.FormatAverageConditional(str, args)); Assert.Equal( expectedFormatRequireEveryPlaceholder, StringFormatOp_RequireEveryPlaceholder.FormatAverageConditional(str, args, argNames)); Assert.Equal( expectedFormatAllowMissingPlaceholders, StringFormatOp_AllowMissingPlaceholders_NoArgumentNames.FormatAverageConditional(str, args)); Assert.Equal( expectedFormatAllowMissingPlaceholders, StringFormatOp_AllowMissingPlaceholders.FormatAverageConditional(str, args, argNames)); }
/// <summary> /// A helper function for testing messages to <c>args</c>. /// </summary> /// <param name="str">The message from <c>str</c>.</param> /// <param name="format">The message from <c>format</c>.</param> /// <param name="args">The message from <c>args</c>.</param> /// <param name="expectedArgsRequireEveryPlaceholder"> /// The expected message to <c>args</c> if the format string is required to contain placeholders for all arguments. /// </param> /// <param name="expectedArgsAllowMissingPlaceholders"> /// The expected message to <c>args</c> if the format string may not contain placeholders for some arguments. /// </param> private static void TestMessageToArgs( StringDistribution str, StringDistribution format, StringDistribution[] args, StringDistribution[] expectedArgsRequireEveryPlaceholder, StringDistribution[] expectedArgsAllowMissingPlaceholders) { string[] argNames = GetDefaultArgumentNames(args.Length); var result = new StringDistribution[args.Length]; Assert.Equal( expectedArgsRequireEveryPlaceholder, StringFormatOp_RequireEveryPlaceholder_NoArgumentNames.ArgsAverageConditional(str, format, args, result)); Assert.Equal( expectedArgsRequireEveryPlaceholder, StringFormatOp_RequireEveryPlaceholder.ArgsAverageConditional(str, format, args, argNames, result)); Assert.Equal( expectedArgsAllowMissingPlaceholders, StringFormatOp_AllowMissingPlaceholders_NoArgumentNames.ArgsAverageConditional(str, format, args, result)); var actualArgs = StringFormatOp_AllowMissingPlaceholders.ArgsAverageConditional(str, format, args, argNames, result); Assert.Equal(expectedArgsAllowMissingPlaceholders, actualArgs); }