Пример #1
0
        Automaton<T> CreateAutomaton3<T>(Func<int, T> f, int bitWidth, IBooleanAlgebra<T> Z)
        {

            Func<int, Variable, MSOPredicate<T>> pred = (i, s) => new MSOPredicate<T>(f(i), s);

            MSOFormula<T> phi = new MSOTrue<T>();

            // x1<x2<x3<x4...
            for (int index = 1; index < bitWidth; index++)
            {
                MSOFormula<T> phi1 = new MSOLt<T>(V1("x" + (index - 1)), V1("x" + index));
                phi = new MSOAnd<T>(phi, phi1);
            }

            // bi(xi)
            for (int index = 0; index < bitWidth; index++)
            {
                MSOFormula<T> phi1 = pred(index, V1("x" + index));
                phi = new MSOAnd<T>(phi, phi1);
            }

            // exists forall...
            for (int index = 0; index < bitWidth; index++)
            {
                if (index % 2 == 0)
                    phi = new MSOExists<T>(V1("x" + index), phi);
                else
                    phi = new MSOForall<T>(V1("x" + index), phi);
            }
            var aut = phi.GetAutomaton(Z);
            return aut;
        }
        public override MSOFormula Contains(FreshGen fg, string v)
        {
            string zi, ziPrev, z0 = "_z0";
            int    i = str.Length - 1;

            MSOFormula phi = new MSOTrue();

            while (i > 0)
            {
                zi     = "_z" + i.ToString();
                ziPrev = "_z" + (i - 1).ToString();
                phi    = new MSOExistsFO(zi, new MSOAnd(phi, new MSOAnd(new MSOLabel(zi, str[i]), new MSOSucc(ziPrev, zi))));
                i--;
            }
            phi = new MSOExistsFO(z0, new MSOAnd(phi, new MSOAnd(new MSOLabel(z0, str[0]), new MSOEqual(z0, v))));

            return(phi);
        }
Пример #3
0
        public void TestCountNoProduct2()
        {
            int       maxvars = 13;
            var       solver  = new CharSetSolver(BitWidth.BV7);
            Stopwatch sw      = new Stopwatch();
            var       tries   = 1;
            var       times   = new long[maxvars - 1];
            var       x       = new Variable("x", true);
            var       y       = new Variable("y", true);

            for (int k = 0; k < tries; k++)
            {
                for (int vars = 8; vars <= maxvars; vars++)
                {
                    MSOFormula <BDD> phi = new MSOTrue <BDD>();
                    for (int i = 1; i < vars; i++)
                    {
                        phi = new MSOAnd <BDD>(phi, new MSOLt <BDD>(new Variable("x" + i, true), new Variable("x" + (i + 1), true)));
                        phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), new Variable("x" + i, true)));
                        phi = new MSOOr <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('k'), new Variable("x" + i, true)));
                    }
                    phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), new Variable("x" + vars, true)));
                    phi = new MSOOr <BDD>(phi,
                                          new MSOExists <BDD>(y,
                                                              new MSOPredicate <BDD>(solver.MkCharConstraint('c'), y)));
                    for (int i = vars; i >= 1; i--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + i, true), phi);
                    }

                    sw.Restart();
                    //var aut1 = phi.GetAutomaton(new CartesianAlgebraBDD<BDD>(solver));
                    var aut1 = phi.GetAutomaton(solver);
                    sw.Stop();
                    times[vars - 2] += sw.ElapsedMilliseconds;
                    //Console.WriteLine("States {0} Trans {1}",aut1.StateCount,aut1.MoveCount);
                    if (k == tries - 1)
                    {
                        TestContext.WriteLine(string.Format("{0} variables; {1} ms", vars, times[vars - 2] / tries));
                    }
                    //solver.ShowGraph(aut1, "a"+vars);
                }
            }
        }
Пример #4
0
        public void TestCount()
        {
            int       maxvars = 10;
            var       solver  = new CharSetSolver(BitWidth.BV16);
            Stopwatch sw      = new Stopwatch();
            var       tries   = 1;
            var       times   = new long[maxvars - 1];

            for (int k = 0; k < tries; k++)
            {
                for (int vars = 8; vars <= maxvars; vars++)
                {
                    MSOFormula <BDD> phi = new MSOTrue <BDD>();
                    for (int i = 1; i < vars; i++)
                    {
                        phi = new MSOAnd <BDD>(phi, new MSOLt <BDD>(V1("x" + i), V1("x" + (i + 1))));
                        phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), V1("x" + i)));
                    }
                    phi = new MSOAnd <BDD>(phi, new MSOPredicate <BDD>(solver.MkCharConstraint('a'), V1("x" + vars)));
                    phi = new MSOOr <BDD>(phi,
                                          new MSOExists <BDD>(V1("y"),
                                                              new MSOPredicate <BDD>(solver.MkCharConstraint('c'), V1("y"))));
                    for (int i = vars; i >= 1; i--)
                    {
                        phi = new MSOExists <BDD>(V1("x" + i), phi);
                    }


                    sw.Restart();
                    //var aut1 = phi.GetAutomaton(new CartesianAlgebraBDD<BDD>(solver));
                    var aut1 = phi.GetAutomaton(solver);
                    //aut1.ShowGraph();
                    sw.Stop();
                    times[vars - 2] += sw.ElapsedMilliseconds;
                    if (k == tries - 1)
                    {
                        TestContext.WriteLine(string.Format("{0} variables; {1} ms", vars, times[vars - 2] / tries));
                    }
                }
            }
        }
Пример #5
0
        Automaton <T> CreateAutomaton3 <T>(Func <int, T> f, int bitWidth, IBooleanAlgebra <T> Z)
        {
            Func <int, string, MSOPredicate <T> > pred = (i, s) => new MSOPredicate <T>(f(i), s);

            MSOFormula <T> phi = new MSOTrue <T>();

            // x1<x2<x3<x4...
            for (int index = 1; index < bitWidth; index++)
            {
                MSOFormula <T> phi1 = new MSOLt <T>("x" + (index - 1), "x" + index);
                phi = new MSOAnd <T>(phi, phi1);
            }

            // bi(xi)
            for (int index = 0; index < bitWidth; index++)
            {
                MSOFormula <T> phi1 = pred(index, "x" + index);
                phi = new MSOAnd <T>(phi, phi1);
            }

            // exists forall...
            for (int index = 0; index < bitWidth; index++)
            {
                if (index % 2 == 0)
                {
                    phi = new MSOExistsFo <T>("x" + index, phi);
                }
                else
                {
                    phi = new MSOForallFo <T>("x" + index, phi);
                }
            }

            Assert.IsTrue(phi.IsWellFormedFormula());
            var aut = phi.GetAutomaton(Z);

            return(aut);
        }
Пример #6
0
        static Automaton <T> CreateAutomaton3 <T>(Func <int, T> f, int bitWidth, IBooleanAlgebra <T> Z)
        {
            Func <int, string, MSOPredicate <T> > pred = (i, s) => new MSOPredicate <T>(f(i), new Variable(s, true));

            MSOFormula <T> phi = new MSOTrue <T>();

            // x1<x2<x3<x4...
            for (int index = 1; index < bitWidth; index++)
            {
                MSOFormula <T> phi1 = new MSOLt <T>(new Variable("x" + (index - 1), true), new Variable("x" + index, true));
                phi = new MSOAnd <T>(phi, phi1);
            }

            // bi(xi)
            for (int index = 0; index < bitWidth; index++)
            {
                MSOFormula <T> phi1 = pred(index, "x" + index);
                phi = new MSOAnd <T>(phi, phi1);
            }

            // exists forall...
            for (int index = 0; index < bitWidth; index++)
            {
                if (index % 2 == 0)
                {
                    phi = new MSOExists <T>(new Variable("x" + index, true), phi);
                }
                else
                {
                    phi = new MSOForall <T>(new Variable("x" + index, true), phi);
                }
            }

            var aut = phi.GetAutomaton(Z);

            return(aut);
        }
Пример #7
0
        public static void Run()
        {
            var sw = new Stopwatch();

            //ex x1 x2... a(x1) /\ a(x2).../\ x1<x2...
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"..\mso-minterm-p1.csv"))
            {
                file.WriteLine("k, old, cartesian, trie, minterm");
                for (int size = 2; size < kminterm; size++)
                {
                    var solver           = new CharSetSolver(BitWidth.BV64);
                    MSOFormula <BDD> phi = new MSOTrue <BDD>();

                    //x1<x2 /\...
                    for (int k = 1; k < size; k++)
                    {
                        var leq = new MSOSuccN <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true), 1);
                        phi = new MSOAnd <BDD>(phi, leq);
                    }

                    //ai(xi) /\ ...
                    for (int k = 0; k < size; k++)
                    {
                        var axk = new MSOPredicate <BDD>(solver.MkBitTrue(k), new Variable("x" + k, true));
                        phi = new MSOAnd <BDD>(phi, axk);
                    }
                    phi = new MSOAnd <BDD>(new MSOeqN <BDD>(new Variable("x" + 0, true), 0), phi);
                    for (int k = size - 1; k >= 0; k--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                    }

                    //Old
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(solver);
                    }

                    var told = sw.ElapsedMilliseconds;

                    //BDD
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(new CartesianAlgebraBDD <BDD>(solver));
                    }
                    sw.Stop();

                    var t1 = sw.ElapsedMilliseconds;

                    //Trie
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(new BDDAlgebra <BDD>(solver), false);
                    }

                    var t2 = sw.ElapsedMilliseconds;

                    //Tminterm
                    solver = new CharSetSolver(BitWidth.BV64);
                    BDD[] predicates = new BDD[size];
                    solver.GenerateMinterms();
                    for (int k = 0; k < size; k++)
                    {
                        predicates[k] = solver.MkBitTrue(k);
                    }

                    var t3 = 60000L * numTests;
                    if (size <= maxmint)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            var mint = solver.GenerateMinterms(predicates).ToList();
                        }
                        sw.Stop();
                        t3 = sw.ElapsedMilliseconds;
                    }

                    file.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                    Console.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                }
            }

            //ex x1 x2... a(x1) /\ a(x2)...
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"..\mso-minterm-p2.csv"))
            {
                file.WriteLine("k, old, cartesian, trie, minterm");
                for (int size = 2; size < 10; size++)
                {
                    // Tsolve force
                    var solver = new CharSetSolver();

                    MSOFormula <BDD> phi = new MSOTrue <BDD>();

                    for (int k = 0; k < size; k++)
                    {
                        var axk = new MSOPredicate <BDD>(solver.MkBitTrue(k), new Variable("x" + k, true));
                        phi = new MSOAnd <BDD>(phi, axk);
                    }
                    for (int k = size - 1; k >= 0; k--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                    }

                    //Old
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(solver);
                    }

                    var told = sw.ElapsedMilliseconds;

                    //Cartesian
                    var t1 = 60000L * numTests;
                    if (size <= 7)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            phi.GetAutomaton(new CartesianAlgebraBDD <BDD>(solver));
                        }
                        sw.Stop();

                        t1 = sw.ElapsedMilliseconds;
                    }


                    //Trie
                    var t2 = 60000L * numTests;
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        phi.GetAutomaton(new BDDAlgebra <BDD>(solver), false);
                    }
                    sw.Stop();

                    t2 = sw.ElapsedMilliseconds;

                    //Tminterm
                    solver = new CharSetSolver(BitWidth.BV64);
                    BDD[] predicates = new BDD[size];
                    solver.GenerateMinterms();
                    for (int k = 0; k < size; k++)
                    {
                        predicates[k] = solver.MkBitTrue(k);
                    }

                    var t3 = 60000L * numTests;
                    if (size <= maxmint)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            var mint = solver.GenerateMinterms(predicates).ToList();
                        }
                        sw.Stop();
                        t3 = sw.ElapsedMilliseconds;
                    }

                    file.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                    Console.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                }
            }
        }
Пример #8
0
        public static void RunPOPLTests()
        {
            //// all x1...xn. xi<xi+1
            List <Pair <MSOFormula <BDD>, CharSetSolver> > phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();

            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver();
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, leq);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }

                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"..\popl14-1.csv"), phis);


            // all x1...xn. xi<xi+1 and a(xi)
            phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();
            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver(BitWidth.BV64);
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, leq);
                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate <BDD>(
                        solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, axk);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }
                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }
            RunTest(new StreamWriter(@"..\popl14-2.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi)) and ex y. c(y)
            phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();
            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver(BitWidth.BV64);
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, leq);
                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate <BDD>(solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, axk);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }

                var exycy = new MSOExists <BDD>(new Variable("y", true), new MSOPredicate <BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd <BDD>(phi, exycy);

                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"..\popl14-3.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi) \/ c(xi))

            phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();
            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver(BitWidth.BV64);
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq   = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    var axk   = new MSOPredicate <BDD>(solver.MkCharConstraint('a', false), new Variable("x" + (k - 1), true));
                    var cxk   = new MSOPredicate <BDD>(solver.MkCharConstraint('c', false), new Variable("x" + (k - 1), true));
                    var inter = new MSOOr <BDD>(new MSOAnd <BDD>(leq, axk), cxk);
                    phi = new MSOAnd <BDD>(phi, inter);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }

                MSOFormula <BDD> exycy = new MSOExists <BDD>(new Variable("y", true), new MSOPredicate <BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd <BDD>(phi, exycy);

                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"..\popl14-4.csv"), phis, 10, 10, 11);
        }
Пример #9
0
        public static void RunPOPLTests()
        {
            //// all x1...xn. xi<xi+1
            List<Pair<MSOFormula<BDD>, CharSetSolver>> phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();

            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver();
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, leq);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }

                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"popl14-1.csv"), phis);

            // all x1...xn. xi<xi+1 and a(xi)
            phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();
            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver(BitWidth.BV64);
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, leq);

                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate<BDD>(
                        solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, axk);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }
                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }
            RunTest(new StreamWriter(@"popl14-2.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi)) and ex y. c(y)
            phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();
            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver(BitWidth.BV64);
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, leq);

                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate<BDD>(solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, axk);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }

                var exycy = new MSOExists<BDD>(new Variable("y", true), new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd<BDD>(phi, exycy);

                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"popl14-3.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi) \/ c(xi))

            phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();
            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver(BitWidth.BV64);
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    var axk = new MSOPredicate<BDD>(solver.MkCharConstraint('a', false), new Variable("x" + (k - 1), true));
                    var cxk = new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("x" + (k - 1), true));
                    var inter = new MSOOr<BDD>(new MSOAnd<BDD>(leq, axk), cxk);
                    phi = new MSOAnd<BDD>(phi, inter);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }

                MSOFormula<BDD> exycy = new MSOExists<BDD>(new Variable("y", true), new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd<BDD>(phi, exycy);

                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"popl14-4.csv"), phis, 11, 11, 11);
        }