示例#1
0
        public void ZeroDetection()
        {
            StringDistribution dist1 = StringDistribution.OneOf(1.0, StringDistribution.Zero(), 0.0, StringDistribution.Any());

            Assert.True(dist1.IsZero());
            StringInferenceTestUtilities.TestProbability(dist1, 0.0, string.Empty, "a", "bc");

            StringDistribution dist2 = StringDistribution.Capitalized(2, 4).Product(StringDistribution.Any(minLength: 5, maxLength: 7));

            Assert.True(dist2.IsZero());
            StringInferenceTestUtilities.TestProbability(dist2, 0.0, string.Empty, "Abc", "Abcdef");

            StringDistribution dist3 = StringDistribution.Digits(minLength: 3, maxLength: 3).Product(StringDistribution.String("12a"));

            Assert.True(dist3.IsZero());
            StringInferenceTestUtilities.TestProbability(dist3, 0.0, string.Empty, "12a", "1", "2", "666");

            StringDistribution dist4 = StringDistribution.Any(minLength: 1, maxLength: 2).Product(StringDistribution.Any(minLength: 2, maxLength: 3).Product(StringDistribution.Any(minLength: 3, maxLength: 4)));

            Assert.True(dist4.IsZero());
            StringInferenceTestUtilities.TestProbability(dist4, 0.0, string.Empty, "a", "ab", "abc", "abcd");

            StringDistribution dist5 = StringDistribution.Any().Append(StringDistribution.Zero());

            Assert.True(dist5.IsZero());
            StringInferenceTestUtilities.TestProbability(dist5, 0.0, string.Empty, "a", "bc");

            StringDistribution dist6 = StringDistribution.Zero().Append(StringDistribution.OneOf("abc", "def"));

            Assert.True(dist6.IsZero());
            StringInferenceTestUtilities.TestProbability(dist6, 0.0, string.Empty, "a", "bc");
        }
示例#2
0
        public void Capitalized()
        {
            int lowercaseCharacterCount = DiscreteChar.Lower().GetProbs().Count(p => p > 0);
            int uppercaseCharacterCount = DiscreteChar.Upper().GetProbs().Count(p => p > 0);

            var capitalizedAutomaton1 = StringDistribution.Capitalized(minLength: 3, maxLength: 5);

            Assert.True(capitalizedAutomaton1.IsProper());
            StringInferenceTestUtilities.TestProbability(
                capitalizedAutomaton1,
                StringInferenceTestUtilities.StringUniformProbability(2, 4, lowercaseCharacterCount) / uppercaseCharacterCount,
                "Abc",
                "Bcde",
                "Abcde");
            StringInferenceTestUtilities.TestProbability(capitalizedAutomaton1, 0.0, "A", "abc", "Ab", "Abcdef", string.Empty);

            var capitalizedAutomaton2 = StringDistribution.Capitalized(minLength: 3);

            Assert.False(capitalizedAutomaton2.IsProper());
            StringInferenceTestUtilities.TestProbability(capitalizedAutomaton2, 1.0, "Abc", "Bcde", "Abcde", "Abfjrhfjlrl");
            StringInferenceTestUtilities.TestProbability(capitalizedAutomaton2, 0.0, "A", "abc", "Ab", string.Empty);
        }
示例#3
0
        [Trait("Category", "OpenBug")] // Test failing with AutomatonTooLarge due to determinization added to SetToProduct in change 47614.  Increasing max states to 1M does not fix the issue
        public void PropertyInferencePerformanceTest()
        {
            Rand.Restart(777);

            var namesData     = new[] { "Alice", "Bob", "Charlie", "Eve", "Boris", "John" };
            var valueData     = new[] { "sender", "receiver", "attacker", "eavesdropper", "developer", "researcher" };
            var templatesData = new[] { "{0} is {1}", "{0} is known as {1}", "{1} is a role of {0}", "{0} -- {1}", "{0} aka {1}" };

            var textsData = new string[10];

            for (int i = 0; i < textsData.Length; ++i)
            {
                int entityIndex   = Rand.Int(namesData.Length);
                int templateIndex = Rand.Int(templatesData.Length);
                textsData[i] = string.Format(templatesData[templateIndex], namesData[entityIndex], valueData[entityIndex]);
            }

            var entity   = new Range(namesData.Length).Named("entity");
            var template = new Range(templatesData.Length).Named("template");
            var text     = new Range(textsData.Length).Named("text");

            var entityNames = Variable.Array <string>(entity).Named("entityNames");

            entityNames[entity] = Variable.Random(StringDistribution.Capitalized()).ForEach(entity);
            var entityValues = Variable.Array <string>(entity).Named("entityValues");

            entityValues[entity] = Variable.Random(StringDistribution.Lower()).ForEach(entity);

            StringDistribution templatePriorMiddle = StringDistribution.ZeroOrMore(DiscreteChar.OneOf('{', '}').Complement());
            StringDistribution templatePrior       =
                StringDistribution.OneOf(
                    StringDistribution.String("{0} ") + templatePriorMiddle + StringDistribution.String(" {1}"),
                    StringDistribution.String("{1} ") + templatePriorMiddle + StringDistribution.String(" {0}"));
            var templates = Variable.Array <string>(template).Named("templates");

            templates[template] = Variable.Random(templatePrior).ForEach(template);

            var texts = Variable.Array <string>(text).Named("texts");

            using (Variable.ForEach(text))
            {
                var entityIndex   = Variable.DiscreteUniform(entity).Named("entityIndex");
                var templateIndex = Variable.DiscreteUniform(template).Named("templateIndex");
                using (Variable.Switch(entityIndex))
                    using (Variable.Switch(templateIndex))
                    {
                        texts[text] = Variable.StringFormat(templates[templateIndex], entityNames[entityIndex], entityValues[entityIndex]);
                    }
            }

            texts.ObservedValue = textsData;

            var engine = new InferenceEngine();

            engine.ShowProgress                = false;
            engine.OptimiseForVariables        = new[] { entityNames, entityValues };
            engine.Compiler.RecommendedQuality = QualityBand.Experimental;
            // TODO: get this test to work with parallel for loops.
            engine.Compiler.UseParallelForLoops = false;
            engine.NumberOfIterations           = 1;

            ProfileAction(
                () =>
            {
                Console.WriteLine(engine.Infer <StringDistribution[]>(entityNames)[0]);
                Console.WriteLine(engine.Infer <StringDistribution[]>(entityValues)[0]);
            },
                1);
        }
 /// <summary>EP message to <c>str</c>.</summary>
 /// <param name="minLength">Constant value for <c>minLength</c>.</param>
 /// <param name="maxLength">Constant value for <c>maxLength</c>.</param>
 /// <returns>The outgoing EP message to the <c>str</c> argument.</returns>
 /// <remarks>
 ///   <para>The outgoing message is the factor viewed as a function of <c>str</c> conditioned on the given values.</para>
 /// </remarks>
 public static StringDistribution StrAverageConditional(int minLength, int maxLength)
 {
     return(StringDistribution.Capitalized(minLength, maxLength));
 }
示例#5
0
        public void MessageOperatorsTest()
        {
            //// Messages to concatenation results

            StringDistribution concat1 = StringConcatOp.ConcatAverageConditional(StringDistribution.String("ab"), StringDistribution.String("cd"));

            Assert.True(concat1.IsPointMass && concat1.Point == "abcd");

            StringDistribution concat2 = StringConcatOp.ConcatAverageConditional(StringDistribution.Upper(), StringDistribution.String("cd"));

            StringInferenceTestUtilities.TestIfIncludes(concat2, "Acd", "ABcd");
            StringInferenceTestUtilities.TestIfExcludes(concat2, "cd", "Abcd", "ABc");

            StringDistribution concat3 = StringConcatOp.ConcatAverageConditional(StringDistribution.OneOf("a", "ab"), StringDistribution.OneOf("b", string.Empty));

            StringInferenceTestUtilities.TestProbability(concat3, 0.5, "ab");
            StringInferenceTestUtilities.TestProbability(concat3, 0.25, "a", "abb");

            //// Messages to the first argument

            StringDistribution str11 = StringConcatOp.Str1AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "bc"));

            StringInferenceTestUtilities.TestProbability(str11, 0.5, string.Empty, "a");

            StringDistribution str12 = StringConcatOp.Str1AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "bc", "a", "b"));

            StringInferenceTestUtilities.TestProbability(str12, 0.5, string.Empty, "a");

            StringDistribution str13 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("aa", "aaa"), StringDistribution.OneOf("a", "aa"));

            StringInferenceTestUtilities.TestProbability(str13, 0.5, "a");
            StringInferenceTestUtilities.TestProbability(str13, 0.25, string.Empty, "aa");

            StringDistribution str14 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "d"));

            StringInferenceTestUtilities.TestProbability(str14, 0.5, "a", "ab");

            StringDistribution str15 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "bd"));

            Assert.True(str15.IsPointMass && str15.Point == "a");

            //// Messages to the second argument

            StringDistribution str21 = StringConcatOp.Str2AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "ab"));

            StringInferenceTestUtilities.TestProbability(str21, 0.5, string.Empty, "c");

            StringDistribution str22 = StringConcatOp.Str2AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "ab", "c", "b"));

            StringInferenceTestUtilities.TestProbability(str22, 0.5, string.Empty, "c");

            StringDistribution str23 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("aa", "aaa"), StringDistribution.OneOf("a", "aa"));

            StringInferenceTestUtilities.TestProbability(str23, 0.5, "a");
            StringInferenceTestUtilities.TestProbability(str23, 0.25, string.Empty, "aa");

            StringDistribution str24 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "dbc"), StringDistribution.OneOf("ab", "d"));

            StringInferenceTestUtilities.TestProbability(str24, 0.5, "c", "bc");

            StringDistribution str25 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "dbc"), StringDistribution.OneOf("ab", "db"));

            Assert.True(str25.IsPointMass && str25.Point == "c");

            //// Evidence messages

            const double EvidenceEps = 1e-6;

            Assert.Equal(0.0, StringConcatOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 5), StringDistribution.Capitalized(minLength: 2, maxLength: 2), StringDistribution.Upper()), EvidenceEps);
            Assert.Equal(
                Math.Log(1.0 / 3.0),
                StringConcatOp.LogEvidenceRatio("aaba", StringDistribution.OneOf("a", "aa"), StringDistribution.OneOf("a", "ba", "aba")),
                EvidenceEps);
            Assert.True(double.IsNegativeInfinity(
                            StringConcatOp.LogEvidenceRatio("aaba", StringDistribution.OneOf("a", "aa"), StringDistribution.OneOf("a", "bd", "abd"))));

            //// Incompatible message parameters

            Assert.True(StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("ab", "b")).IsZero());
            Assert.True(StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "b")).IsZero());
        }
        public void MessageOperatorsTest()
        {
            //// Messages to substring

            StringDistribution substr1 = SubstringOp.SubAverageConditional("abc", 1, 1);

            Assert.True(substr1.IsPointMass && substr1.Point == "b");

            StringDistribution substr2 = SubstringOp.SubAverageConditional("abc", 0, 3);

            Assert.True(substr2.IsPointMass && substr2.Point == "abc");

            StringDistribution substr3 = SubstringOp.SubAverageConditional(StringDistribution.String("abc"), 1, 1);

            Assert.True(substr3.IsPointMass && substr3.Point == "b");

            StringDistribution substr4 = SubstringOp.SubAverageConditional(StringDistribution.String("abc"), 0, 3);

            Assert.True(substr4.IsPointMass && substr4.Point == "abc");

            StringDistribution substr5 = SubstringOp.SubAverageConditional(StringDistribution.Any(), 0, 2);

            StringInferenceTestUtilities.TestIfIncludes(substr5, "ab", "  ", "17");
            StringInferenceTestUtilities.TestIfExcludes(substr5, "abb", " ", "177", string.Empty);

            StringDistribution substr6 = SubstringOp.SubAverageConditional(StringDistribution.OneOf("abc", "abd", "de"), 0, 2);

            StringInferenceTestUtilities.TestProbability(substr6, 2.0 / 3.0, "ab");
            StringInferenceTestUtilities.TestProbability(substr6, 1.0 / 3.0, "de");

            StringDistribution substr7 = SubstringOp.SubAverageConditional(StringDistribution.OneOf("abc", "abd", "de", "d", "c", string.Empty), 0, 2);

            StringInferenceTestUtilities.TestProbability(substr7, 2.0 / 3.0, "ab");
            StringInferenceTestUtilities.TestProbability(substr7, 1.0 / 3.0, "de");

            //// Messages to string

            StringDistribution str1 = SubstringOp.StrAverageConditional("sss", 1, 3);

            StringInferenceTestUtilities.TestIfIncludes(str1, "asss", "asssa", "bsssa");
            StringInferenceTestUtilities.TestIfExcludes(str1, "sss", "assa", "basssa", string.Empty);

            StringDistribution str2 = SubstringOp.StrAverageConditional("sss", 0, 3);

            StringInferenceTestUtilities.TestIfIncludes(str2, "sss", "sssa", "sssab");
            StringInferenceTestUtilities.TestIfExcludes(str2, "asss", "basssa", "ssa", string.Empty);

            StringDistribution str3 = SubstringOp.StrAverageConditional(StringDistribution.String("sss"), 1, 3);

            StringInferenceTestUtilities.TestIfIncludes(str3, "asss", "asssa", "bsssa");
            StringInferenceTestUtilities.TestIfExcludes(str3, "sss", "assa", "basssa", string.Empty);

            StringDistribution str4 = SubstringOp.StrAverageConditional(StringDistribution.String("sss"), 0, 3);

            StringInferenceTestUtilities.TestIfIncludes(str4, "sss", "sssa", "sssab");
            StringInferenceTestUtilities.TestIfExcludes(str4, "asss", "basssa", "ssa", string.Empty);

            StringDistribution str5 = SubstringOp.StrAverageConditional(StringDistribution.Capitalized(minLength: 3, maxLength: 3), 0, 3);

            StringInferenceTestUtilities.TestIfIncludes(str5, "Bbb", "Baba", "Bbb ab");
            StringInferenceTestUtilities.TestIfExcludes(str5, "BAba", "aaaB", "ABab", "Bb ab", string.Empty);

            StringDistribution str6 = SubstringOp.StrAverageConditional(StringDistribution.Upper(minLength: 0, maxLength: 5), 0, 2);

            StringInferenceTestUtilities.TestIfIncludes(str6, "BBb", "BAba", "BBB ab");
            StringInferenceTestUtilities.TestIfExcludes(str6, "Baba", "aaaB", "aBab", "bb ab", string.Empty);

            //// Evidence messages

            const double EvidenceEps = 1e-6;

            Assert.Equal(0.0, SubstringOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 5), StringDistribution.Capitalized(2, 2), 1, 2), EvidenceEps);
            Assert.Equal(Math.Log(2.0 / 3.0), SubstringOp.LogEvidenceRatio(StringDistribution.OneOf("baa", "bab", "baab"), "aa", 1, 2), EvidenceEps);
            Assert.True(double.IsNegativeInfinity(SubstringOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 5), "string", 1, 3)));
            Assert.True(double.IsNegativeInfinity(SubstringOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 2), "str", 1, 3)));

            //// Incompatible message parameters

            Assert.True(SubstringOp.SubAverageConditional("abc", 1, 3).IsZero());
            Assert.True(SubstringOp.SubAverageConditional(StringDistribution.Any(minLength: 1, maxLength: 2), 1, 3).IsZero());
            Assert.True(SubstringOp.StrAverageConditional("abc", 1, 4).IsZero());
            Assert.True(SubstringOp.StrAverageConditional(StringDistribution.Any(minLength: 1, maxLength: 2), 1, 3).IsZero());
        }