Esempio n. 1
0
        public void NormalAndPbc3()
        {
            var p = new Problem("normal clauses mixed with large PBCs and conditional PBCs");

            p.AddClause("a", "b", "c", "d", "e", "f", "g", "h", "i");
            p.AddClause("a", "d");
            p.AddClause("d", "c");
            p.AddClause("f", "g");
            p.AddClause("g", Not("c"));
            p.AddClause(Not("j"), "g");
            p.AddClause("k", "c");
            p.AddClause("l", "c");
            p.AddClause(Not("m"), "j");
            p.AddClause("n", "r");
            p.AddClause(1, 1, "e", "f");
            p.AddClause("w");
            p.QuantifyIf("a", 2, 2, "a", "c");
            p.QuantifyIf("r", 2, 2, "r", "b");
            p.AddClause(1, 1, "b", "c");
            int[] flip = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                var m = p.Solve();
                flip[i] = m.Problem.BooleanSolver.SolveFlips;
            }

            int average = flip.Sum() / flip.Length;

            Console.WriteLine(average); // average 5 flips 6 ms w/ propagation, 8 flips 9 ms w/o
        }
Esempio n. 2
0
        public void ConditionalConstraintPropagationTest()
        {
            // This should come out of the initialization process with a valid model
            // So this shouldn't require any flips
            var p = new Problem("normal clauses mixed with large PBCs and conditional PBCs2");
            var lits = new[] { "a", "b", "c" }.Select(s => (Literal)s).ToArray();

            p.QuantifyIf("condition", 2, 3, lits);
            var enabledCount = 0;
            var tries        = 10000;

            for (int i = 0; i < tries; i++)
            {
                var m       = p.Solve();
                var count   = lits.Count(l => m[l]);
                var enabled = m["condition"];
                if (enabled)
                {
                    enabledCount++;
                }
                Assert.AreEqual(0, m.Problem.BooleanSolver.SolveFlips);
                Assert.IsTrue(!enabled || (count >= 2 && count <= 3));
            }
            Assert.IsTrue(enabledCount > 0, "condition is never enabled");
            Assert.IsTrue(enabledCount < tries, "condition is always enabled");
        }
Esempio n. 3
0
        public void SkipPropagationTest()
        {
            var p = new Problem("Skip last test's propagation during initialization");

            p.AddClause("b", "c");
            p.AddClause("a", "d");
            p.AddClause("d", "c");
            p.AddClause("e", "c");
            p.AddClause("f", "g");
            p.AddClause("g", "c");
            p.AddClause("h", "k");
            p.AddClause("i", "c");
            p.AddClause("j", "g");
            p.AddClause("k", "c");
            p.AddClause("l", "c");
            p.AddClause("m", "c");
            p.AddClause("n", "r");
            p.AddClause(1, 1, "e", "f");
            p.AddClause("w");
            p.QuantifyIf("a", 2, 2, "a", "c");
            p.AddClause(1, 1, "b", "c");
            p.PropagateConstraintsDuringInitialization = false;
            int[] flip = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                var m = p.Solve();
                flip[i] = m.Problem.BooleanSolver.SolveFlips;
            }
            int average = flip.Sum() / flip.Length;

            p.Assert(average == 4);
        }
Esempio n. 4
0
        public void ConditionalTest3()
        {
            var p = new Problem("SingleClause");

            p.QuantifyIf("a", 2, 2, "b", "c");
            for (int i = 0; i < 100; i++)
            {
                var m = p.Solve();
                Assert.IsFalse(m.IsTrue("a") & !m.IsTrue("b") & !m.IsTrue("c"));
            }
        }
Esempio n. 5
0
        public void ConditionalTest4()
        {
            var p = new Problem("SingleClause2");

            p.QuantifyIf("a", 1, 1, "b", "c");
            for (int i = 0; i < 100; i++)
            {
                var m = p.Solve();
                Assert.IsFalse(m.IsTrue("a") & !m.IsTrue("b") & !m.IsTrue("c"));
                Assert.IsFalse(m.IsTrue("a") & m.IsTrue("b") & m.IsTrue("c"));
                Assert.IsTrue(m.Model.Equals("{}") | m.Model.Equals("{a}") | m.Model.Equals("{b}") | m.Model.Equals("{c}") | m.Model.Equals("{a, b}") | m.Model.Equals("{a, c}") | m.Model.Equals("{b, c}"));
            }
        }
Esempio n. 6
0
        public void ConditionalTest1()
        {
            var p = new Problem("MultipleCardinality");

            p.QuantifyIf("a", 2, 2, "a", "c");
            p.AddClause(1, 1, "b", "c");
            for (int i = 0; i < 1000; i++)
            {
                var m = p.Solve();
                Assert.IsTrue(m.Model.Equals("{a, c}") | m.Model.Equals("{b}") | m.Model.Equals("{c}"));
                Assert.IsFalse(m.Model.Equals("{b, c}"));
            }
        }
Esempio n. 7
0
        public void ConditionalTest5()
        {
            var p = new Problem("MultipleCardinality2");

            p.AddClause(10, 29, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "ll", "mm", "nn", "oo");
            p.AddClause(20, 29, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "p", "q", "r", "s", "t", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "pp", "qq", "rr", "ss", "tt");
            p.AddClause(10, 29, "a", "b", "c", "d", "e", "f", "g", "h", "i", "t", "u", "v", "w", "x", "y", "z", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo");
            p.AddClause(1, 1, "e");
            p.QuantifyIf("a", 2, 2, "a", "c");
            p.AddClause(1, 1, "b", "c");
            p.AddClause(1, 1, "d");
            for (int i = 0; i < 100; i++)
            {
                var m = p.Solve();
                Assert.IsTrue(m.IsTrue("e"));
                Assert.IsTrue(m.IsTrue("d"));
                Assert.IsFalse(m.Model.Equals("{e, c, b}"));
                Assert.IsFalse(m.Model.Equals("{e, c, b, d}"));
                Assert.IsFalse(m.Model.Equals("{e, a, b}"));
            }
        }
Esempio n. 8
0
        public void NormalAndPbc2()
        {
            var p = new Problem("normal clauses mixed with small PBCs");

            p.AddClause("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "ll", "mm", "nn", "oo");
            p.AddClause("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "p", "q", "r", "s", "t", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "pp", "qq", "rr", "ss", "tt");
            p.AddClause("a", "b", "c", "d", "e", "f", "g", "h", "i", "t", "u", "v", "w", "x", "y", "z", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo");
            p.AddClause(1, 1, "e", "f");
            p.AddClause("w");
            p.QuantifyIf("a", 2, 2, "a", "c");
            p.AddClause(1, 1, "b", "c");
            int[] flip = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                var m = p.Solve();
                flip[i] = m.Problem.BooleanSolver.SolveFlips;
            }

            int average = flip.Sum() / flip.Length;

            Console.WriteLine(average); // average 2 flips 15 ms with propagation, 2 flips 6 ms w/o
        }