Esempio n. 1
0
        public void TestWS1S_SuccDef_GetAutomatonBDD()
        {
            var solver        = new CharSetSolver(BitWidth.BV7);
            var nrOfLabelBits = (int)BitWidth.BV7;
            var x             = new Variable("x", true);
            var y             = new Variable("y", true);
            var z             = new Variable("z", true);
            var xLTy          = new MSOLt <BDD>(x, y);
            var xLTzLTy       = new MSOAnd <BDD>(new MSOLt <BDD>(x, z), new MSOLt <BDD>(z, y));
            var Ez            = new MSOExists <BDD>(z, xLTzLTy);
            var notEz         = new MSONot <BDD>(Ez);
            var xSyDef        = new MSOAnd <BDD>(xLTy, notEz);

            var aut_xSyDef  = xSyDef.GetAutomaton(solver);
            var aut_xLTzLTy = xLTzLTy.GetAutomaton(solver);
            var aut_Ez      = Ez.GetAutomaton(solver).Determinize().Minimize();
            var aut_notEz   = notEz.GetAutomaton(solver);
            var aut_xLTy    = xLTy.GetAutomaton(solver);

            //aut_xSyDef.ShowGraph("aut_xSyDEf");
            //aut_xLTzLTy.ShowGraph("aut_xLTzLTy");
            //aut_Ez.ShowGraph("aut_Ez");
            //aut_notEz.ShowGraph("aut_notEz");

            var xSyPrim     = new MSOSuccN <BDD>(x, y, 1);
            var aut_xSyPrim = xSyPrim.GetAutomaton(solver);
            var equiv       = aut_xSyPrim.IsEquivalentWith(aut_xSyDef);

            Assert.IsTrue(equiv);
        }
Esempio n. 2
0
        public void BooleanAlgebraZ3_test2()
        {
            var ctx         = new Context();
            var sort        = ctx.IntSort;
            var solver      = new Z3BoolAlg(ctx, sort);
            var alg         = new BDDAlgebra <BoolExpr>(solver);
            var x           = new Variable("x", true);
            var y           = new Variable("y", true);
            var z           = new Variable("z", true);
            var xLTy        = new MSOLt <BoolExpr>(x, y);
            var xLTzLTy     = new MSOAnd <BoolExpr>((new MSOLt <BoolExpr>(x, z)), (new MSOLt <BoolExpr>(z, y)));
            var Ez          = new MSOExists <BoolExpr>(z, xLTzLTy);
            var notEz       = new MSONot <BoolExpr>(Ez);
            var xSyDef      = new MSOAnd <BoolExpr>(xLTy, notEz);
            var aut_xSyDef  = xSyDef.GetAutomaton(alg);
            var aut_xLTzLTy = xLTzLTy.GetAutomaton(alg);
            var aut_Ez      = Ez.GetAutomaton(alg);
            var aut_notEz   = notEz.GetAutomaton(alg);
            var aut_xLTy    = xLTy.GetAutomaton(alg);

            //aut_xSyDef.ShowGraph("aut_xSyDEf");
            //aut_xLTzLTy.ShowGraph("aut_xLTzLTy");
            //aut_Ez.ShowGraph("aut_Ez");
            //aut_notEz.ShowGraph("aut_notEz");

            var xSyPrim     = new MSOSuccN <BoolExpr>(x, y, 1);
            var aut_xSyPrim = xSyPrim.GetAutomaton(alg);
            var equiv       = aut_xSyPrim.IsEquivalentWith(aut_xSyDef);

            Assert.IsTrue(equiv);
        }
        public void MSOFirstLastSucc()
        {
            var solver = new CharSetSolver(BitWidth.BV64);  //new solver using ASCII encoding

            List <char> alph = new List <char> {
                'a', 'b'
            };
            HashSet <char> al = new HashSet <char>(alph);

            //ex x. first(x)
            MSOFormula first   = new MSOFirst("x");
            MSOFormula last    = new MSOLast("y");
            MSOFormula succ    = new MSOSucc("x", "y");
            MSOFormula and     = new MSOAnd(new MSOAnd(first, last), succ);
            MSOFormula formula = new MSOExistsFO("x", new MSOExistsFO("y", and));

            Assert.IsTrue(formula.CheckUseOfVars());

            var dfa = formula.getDFA(al, solver);

            var test = solver.Convert(@"^(a|b){2}$");

            Assert.IsTrue(dfa.IsEquivalentWith(test, solver));

            string file = "../../../MSOZ3Test/DotFiles/ab";

            solver.SaveAsDot(dfa, "aut", file);   //extension .dot  is added automatically when missing
        }
Esempio n. 4
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;
        }
Esempio n. 5
0
        public void TestWS1S_GetAutomatonBDD_eq_GetAutomaton()
        {
            var solver = new CharSetSolver(BitWidth.BV7);
            //var nrOfLabelBits = (int)BitWidth.BV7;
            var isDigit  = solver.MkCharSetFromRegexCharClass(@"\d");
            var isLetter = solver.MkCharSetFromRegexCharClass(@"(c|C)");
            var x        = new Variable("x", false);
            var y        = new Variable("y", false);
            var z        = new Variable("z", false);
            var X        = new Variable("X", false);
            //there are at least two distinct positions x and y
            var xy = new MSOAnd <BDD>(new MSONot <BDD>(new MSOEq <BDD>(x, y)), new MSOAnd <BDD>(new MSOIsSingleton <BDD>(x), new MSOIsSingleton <BDD>(y)));
            //there is a set X containing x and y and all positions z in X have characters that satisfy isWordLetter
            var x_sub_X    = new MSOSubset <BDD>(x, X);
            var y_sub_X    = new MSOSubset <BDD>(y, X);
            var z_sub_X    = new MSOSubset <BDD>(z, X);
            var isletter_z = new MSOPredicate <BDD>(isLetter, z);
            var psi        = new MSOExists <BDD>(X, (x_sub_X & y_sub_X & ~(new MSOExists <BDD>(z, ~((~((new MSOIsSingleton <BDD>(z)) & z_sub_X)) | isletter_z)))));

            var atLeast2w   = xy & psi;
            var atLeast2wEE = new MSOExists <BDD>(x, (new MSOExists <BDD>(y, atLeast2w)));
            var autBDD      = atLeast2w.GetAutomaton(solver);
            var ca          = new CartesianAlgebraBDD <BDD>(solver);
            var autPROD     = atLeast2w.GetAutomaton(ca);
            //autBDD.ShowGraph("autBDD");
            //autPROD.ShowGraph("autPROD");
            var aut_atLeast2wEE1 = BasicAutomata.Restrict(atLeast2wEE.GetAutomaton(ca));
            var aut_atLeast2wEE2 = atLeast2wEE.GetAutomaton(solver);

            //aut_atLeast2wEE1.ShowGraph("aut_atLeast2wEE1");
            //aut_atLeast2wEE2.ShowGraph("aut_atLeast2wEE2");
            Assert.IsTrue(aut_atLeast2wEE1.IsEquivalentWith(aut_atLeast2wEE2));
        }
Esempio n. 6
0
        public void TestWS1S_SuccDef_GetAutomaton()
        {
            var solver      = new CharSetSolver(BitWidth.BV7);
            var x           = new Variable("x", true);
            var y           = new Variable("y", true);
            var z           = new Variable("z", true);
            var xLTy        = new MSOLt <BDD>(x, y);
            var xLTzLTy     = new MSOAnd <BDD>(new MSOLt <BDD>(x, z), new MSOLt <BDD>(z, y));
            var Ez          = new MSOExists <BDD>(z, xLTzLTy);
            var notEz       = new MSONot <BDD>(Ez);
            var xSyDef      = new MSOAnd <BDD>(xLTy, notEz);
            var ca          = new CartesianAlgebraBDD <BDD>(solver);
            var aut_xSyDef  = xSyDef.GetAutomaton(ca);
            var aut_xLTzLTy = xLTzLTy.GetAutomaton(ca);
            var aut_Ez      = Ez.GetAutomaton(ca);
            var aut_notEz   = notEz.GetAutomaton(ca);
            var aut_xLTy    = xLTy.GetAutomaton(ca);

            //aut_xSyDef.ShowGraph("aut_xSyDEf");
            //aut_xLTzLTy.ShowGraph("aut_xLTzLTy");
            //aut_Ez.ShowGraph("aut_Ez");
            //aut_notEz.ShowGraph("aut_notEz");

            var xSyPrim     = new MSOSuccN <BDD>(x, y, 1);
            var aut_xSyPrim = xSyPrim.GetAutomaton(ca);
            var equiv       = aut_xSyPrim.IsEquivalentWith(aut_xSyDef);

            Assert.IsTrue(equiv);
        }
        public void MSO2a2b()
        {
            var solver = new CharSetSolver(BitWidth.BV64);  //new solver using ASCII encoding

            List <char> alph = new List <char> {
                'a', 'b'
            };
            HashSet <char> al = new HashSet <char>(alph);

            MSOFormula formula1 = new MSOExistsFO("x1",
                                                  new MSOExistsFO("x2",
                                                                  new MSOAnd(
                                                                      new MSOAnd(
                                                                          new MSOLabel("x1", 'a'),
                                                                          new MSOLabel("x2", 'a')),
                                                                      new MSONot(
                                                                          new MSOEqual("x1", "x2")))));
            MSOFormula formula2 = new MSOExistsFO("x1",
                                                  new MSOExistsFO("x2",
                                                                  new MSOAnd(
                                                                      new MSOAnd(
                                                                          new MSOLabel("x1", 'b'),
                                                                          new MSOLabel("x2", 'b')),
                                                                      new MSONot(
                                                                          new MSOEqual("x1", "x2")))));

            MSOFormula formula = new MSOAnd(formula1, formula2);


            Assert.IsTrue(formula.CheckUseOfVars());

            var WS1S  = formula.ToWS1S(solver);
            var dfa   = WS1S.getDFA(al, solver);
            var timer = new Stopwatch();
            var tt    = 100;
            var acc   = 0L;

            for (int k = 0; k < tt; k++)
            {
                timer.Reset();
                timer.Start();
                dfa = WS1S.getDFA(al, solver);
                timer.Stop();
                acc += timer.ElapsedMilliseconds;
            }
            Console.WriteLine("time: " + acc / tt + " ms");

            //var test = solver.Convert(@"^(a|b)$");

            //Assert.IsTrue(dfa.IsEquivalentWith(test, solver));

            string file = "../../../MSOZ3Test/DotFiles/a2b2";

            solver.SaveAsDot(dfa, "aut", file);   //extension .dot  is added automatically when missing
        }
Esempio n. 8
0
 public void TestMSO_Member()
 {
     var solver           = new CharSetSolver(BitWidth.BV7);
     var ca               = new CartesianAlgebraBDD <BDD>(solver);
     var x                = new Variable("x", false);
     var y                = new Variable("y", false);
     var fo_x             = new MSOIsSingleton <BDD>(x);
     MSOFormula <BDD> xSy = new MSOSubset <BDD>(x, y);
     var mem              = new MSOAnd <BDD>(xSy, fo_x);
     var aut_mem          = mem.GetAutomaton(ca);
     //aut_mem.ShowGraph("aut_mem");
 }
Esempio n. 9
0
 public void TestMSO_Equal()
 {
     var solver = new CharSetSolver(BitWidth.BV7);
     var ca = new CartesianAlgebraBDD<BDD>(solver);
     var x = new Variable("x", false);
     var y = new Variable("y", false);
     var fo_x = new MSOIsSingleton<BDD>(x) ;
     var fo_y = new MSOIsSingleton<BDD>(y);
     MSOFormula<BDD> fo = new MSOAnd<BDD>(fo_x, fo_y);
     MSOFormula<BDD> xSy = new MSOSubset<BDD>(x, y);
     MSOFormula<BDD> ySx = new MSOSubset<BDD>(y, x);
     MSOFormula<BDD> yEQx = new MSOAnd<BDD>(xSy, ySx);
     yEQx = new MSOAnd<BDD>(yEQx, fo);
     var aut_yEQx = yEQx.GetAutomaton(ca);
     //aut_yEQx.ShowGraph("aut_yEQx");
 }
Esempio n. 10
0
        //Automaton<T> Restrict<T>(Automaton<IMonadicPredicate<BDD, T>> autom)
        //{
        //    List<Move<T>> moves = new List<Move<T>>();
        //    foreach (var move in autom.GetMoves())
        //        moves.Add(new Move<T>(move.SourceState, move.TargetState, move.Label.ProjectSecond()));
        //    var res = Automaton<T>.Create(autom.InitialState, autom.GetFinalStates(), moves);
        //    return res;
        //}

        public void TestWS1S_UseOfCharRangePreds <T>(IBooleanAlgebra <T> solver, T isDigit, T isWordLetter, IRegexConverter <T> regexConverter)
        {
            var ca = new CartesianAlgebraBDD <T>(solver);
            var x  = new Variable("x", false);
            var y  = new Variable("y", false);
            var z  = new Variable("z", false);
            var X  = new Variable("X", false);
            //there are at least two distinct positions x and y
            var xy = new MSOAnd <T>(new MSOAnd <T>(new MSONot <T>(new MSOEq <T>(x, y)), new MSOIsSingleton <T>(x)), new MSOIsSingleton <T>(y));
            //there is a set X containing x and y and all positions z in X have characters that satisfy isWordLetter
            var phi = new MSOExists <T>(X, new MSOAnd <T>(
                                            new MSOAnd <T>(new MSOSubset <T>(x, X), new MSOSubset <T>(y, X)),
                                            new MSONot <T>(new MSOExists <T>(z, new MSONot <T>(
                                                                                 new MSOOr <T>(new MSONot <T>(new MSOAnd <T>(new MSOIsSingleton <T>(z), new MSOSubset <T>(z, X))),
                                                                                               new MSOPredicate <T>(isWordLetter, z)))))));

            var psi2        = new MSOAnd <T>(xy, phi);
            var atLeast2wEE = new MSOExists <T>(x, new MSOExists <T>(y, psi2));
            var psi1        = new MSOAnd <T>(new MSOIsSingleton <T>(x), new MSOPredicate <T>(isDigit, x));
            var aut_psi1    = psi1.GetAutomaton(ca);
            //aut_psi1.ShowGraph("SFA(psi1)");
            var atLeast1d = new MSOExists <T>(x, psi1);
            var psi       = new MSOAnd <T>(atLeast2wEE, atLeast1d);
            var aut1      = psi.GetAutomaton(ca);
            //var aut_atLeast1d = atLeast1d.GetAutomaton(solver);

            var aut2 = regexConverter.Convert(@"\w.*\w", System.Text.RegularExpressions.RegexOptions.Singleline).Intersect(regexConverter.Convert(@"\d"));

            //aut1.ShowGraph("aut1");
            //aut2.ShowGraph("aut2");

            bool equiv = aut2.IsEquivalentWith(BasicAutomata.Restrict(aut1));

            Assert.IsTrue(equiv);

            //solver.ShowGraph(aut_atLeast1d, "aut_atLeast1d");

            var aut_psi2 = psi2.GetAutomaton(ca);
            // var aut_atLeast2wEE = atLeast2wEE.GetAutomaton(ca, "x", "y");
            // var aut_atLeast2wEE2 = atLeast2wEE.GetAutomaton(solver);
            //aut_psi2.ShowGraph("SFA(psi2)");
            //aut_atLeast2wEE.ShowGraph("aut_atLeast2wEE");
            //aut_atLeast2wEE2.ShowGraph("aut_atLeast2wEE2");
            //solver.ShowGraph(aut_atLeast2wEE2, "aut_atLeast2wEE2");
        }
Esempio n. 11
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);
                }
            }
        }
Esempio n. 12
0
 public void TestWS1S_NotLabel()
 {
     var solver = new CharSetSolver(BitWidth.BV7);
     //var x1 = new Variable("x1", false);
     var x    = new Variable("x", false);
     var pred = new MSOPredicate <BDD>(solver.MkCharConstraint('c'), x);
     var fo_x = new MSOIsSingleton <BDD>(x);
     var ca   = new CartesianAlgebraBDD <BDD>(solver);
     var lab  = new MSOAnd <BDD>(pred, fo_x);
     MSOFormula <BDD> not_lab = new MSONot <BDD>(lab);
     var not_lab_actual       = new MSOAnd <BDD>(not_lab, fo_x);
     var aut_not_lab          = not_lab_actual.GetAutomaton(ca);
     var aut_not_lab_prelim   = not_lab.GetAutomaton(ca);
     var c_aut_lab            = lab.GetAutomaton(ca).Complement().Minimize();
     //c_aut_lab.ShowGraph("c_aut_lab");
     //aut_not_lab.ShowGraph("aut_not_lab");
     //aut_not_lab_prelim.ShowGraph("aut_not_lab_prelim");
     //TBD: equivalence
 }
Esempio n. 13
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));
                    }
                }
            }
        }
Esempio n. 14
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);
        }
Esempio n. 15
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);
        }
Esempio n. 16
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);
                }
            }
        }
Esempio n. 17
0
        private static Pair<MSOFormula<BoolExpr>, List<BoolExpr>> GenerateMSOFormula(int maxVarIndex)
        {
            int randomNumber = random.Next(0, 8);
            size--;
            if (size <= 0)
            {
                int variable = random.Next(0, maxVarIndex-1);
                BoolExpr b = GeneratePredicateOut(200);
                List<BoolExpr> l = new List<BoolExpr>();
                l.Add(b);
                return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(new MSOPredicate<BoolExpr>(b, new Variable("x"+variable, true)), l);
            }
            switch (randomNumber)
            {
                case 0:
                    {
                        Pair<MSOFormula<BoolExpr>, List<BoolExpr>> phi1 = GenerateMSOFormula(maxVarIndex + 1);
                        MSOFormula<BoolExpr> phi = new MSOExists<BoolExpr>(new Variable("x"+maxVarIndex, true), phi1.First);
                        return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(phi, phi1.Second);
                    }
                case 1:
                    {
                        Pair<MSOFormula<BoolExpr>, List<BoolExpr>> phi1 = GenerateMSOFormula(maxVarIndex + 1);
                        MSOFormula<BoolExpr> phi = new MSOForall<BoolExpr>(new Variable("x" + maxVarIndex, true), phi1.First);
                        return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(phi, phi1.Second);
                    }
                case 2:
                case 3:
                    {
                        Pair<MSOFormula<BoolExpr>, List<BoolExpr>> phi1 = GenerateMSOFormula(maxVarIndex);
                        Pair<MSOFormula<BoolExpr>, List<BoolExpr>> phi2 = GenerateMSOFormula(maxVarIndex);
                        MSOFormula<BoolExpr> phi = new MSOAnd<BoolExpr>(phi1.First, phi2.First);
                        return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(phi, new List<BoolExpr>(phi1.Second.Union(phi2.Second)));
                    }
                case 4:
                case 5:
                    {
                        Pair<MSOFormula<BoolExpr>, List<BoolExpr>> phi1 = GenerateMSOFormula(maxVarIndex);
                        Pair<MSOFormula<BoolExpr>, List<BoolExpr>> phi2 = GenerateMSOFormula(maxVarIndex);
                        MSOFormula<BoolExpr> phi = new MSOOr<BoolExpr>(phi1.First, phi2.First);
                        return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(phi, new List<BoolExpr>(phi1.Second.Union(phi2.Second)));
                    }
                case 6:
                    {
                        Pair<MSOFormula<BoolExpr>, List<BoolExpr>> phi1 = GenerateMSOFormula(maxVarIndex);
                        MSOFormula<BoolExpr> phi = new MSONot<BoolExpr>(phi1.First);
                        return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(phi, phi1.Second);
                    }
                case 7:
                    {
                        if (maxVarIndex > 1)
                        {
                            int variable1 = random.Next(0, maxVarIndex - 1);
                            int variable2 = random.Next(0, maxVarIndex - 1);
                            if (variable1 == variable2)
                            {
                                if (variable1 == maxVarIndex - 1)
                                    variable1 = variable1 - 1;
                                else
                                    variable2 = variable2 + 1;
                            }

                            //Successor
                            MSOFormula<BoolExpr> phi = new MSOSuccN<BoolExpr>(varOf(variable1),varOf(variable2),random.Next(1,4));
                            return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(phi, new List<BoolExpr>());
                        }
                        else
                        {
                            int variable = random.Next(0, maxVarIndex - 1);
                            BoolExpr b = GeneratePredicate();
                            List<BoolExpr> l = new List<BoolExpr>();
                            l.Add(b);
                            return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(new MSOPredicate<BoolExpr>(b, new Variable("x" + variable, true)), l);
                        }

                    }
                case 8:
                    {
                        int variable1 = random.Next(0, maxVarIndex - 1);
                        int variable2 = random.Next(0, maxVarIndex - 1);

                        //less than
                        MSOFormula<BoolExpr> phi = new MSOLe<BoolExpr>(varOf(variable1), varOf(variable2));
                        return new Pair<MSOFormula<BoolExpr>, List<BoolExpr>>(phi, new List<BoolExpr>());
                    }
            }
            return null;
        }
Esempio n. 18
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);
        }
Esempio n. 19
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);
        }
Esempio n. 20
0
        private static Pair <MSOFormula <BoolExpr>, List <BoolExpr> > GenerateMSOFormula(int maxVarIndex)
        {
            int randomNumber = random.Next(0, 8);

            size--;
            if (size <= 0)
            {
                int             variable = random.Next(0, maxVarIndex - 1);
                BoolExpr        b        = GeneratePredicateOut(200);
                List <BoolExpr> l        = new List <BoolExpr>();
                l.Add(b);
                return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(new MSOPredicate <BoolExpr>(b, new Variable("x" + variable, true)), l));
            }
            switch (randomNumber)
            {
            case 0:
            {
                Pair <MSOFormula <BoolExpr>, List <BoolExpr> > phi1 = GenerateMSOFormula(maxVarIndex + 1);
                MSOFormula <BoolExpr> phi = new MSOExists <BoolExpr>(new Variable("x" + maxVarIndex, true), phi1.First);
                return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(phi, phi1.Second));
            }

            case 1:
            {
                Pair <MSOFormula <BoolExpr>, List <BoolExpr> > phi1 = GenerateMSOFormula(maxVarIndex + 1);
                MSOFormula <BoolExpr> phi = new MSOForall <BoolExpr>(new Variable("x" + maxVarIndex, true), phi1.First);
                return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(phi, phi1.Second));
            }

            case 2:
            case 3:
            {
                Pair <MSOFormula <BoolExpr>, List <BoolExpr> > phi1 = GenerateMSOFormula(maxVarIndex);
                Pair <MSOFormula <BoolExpr>, List <BoolExpr> > phi2 = GenerateMSOFormula(maxVarIndex);
                MSOFormula <BoolExpr> phi = new MSOAnd <BoolExpr>(phi1.First, phi2.First);
                return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(phi, new List <BoolExpr>(phi1.Second.Union(phi2.Second))));
            }

            case 4:
            case 5:
            {
                Pair <MSOFormula <BoolExpr>, List <BoolExpr> > phi1 = GenerateMSOFormula(maxVarIndex);
                Pair <MSOFormula <BoolExpr>, List <BoolExpr> > phi2 = GenerateMSOFormula(maxVarIndex);
                MSOFormula <BoolExpr> phi = new MSOOr <BoolExpr>(phi1.First, phi2.First);
                return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(phi, new List <BoolExpr>(phi1.Second.Union(phi2.Second))));
            }

            case 6:
            {
                Pair <MSOFormula <BoolExpr>, List <BoolExpr> > phi1 = GenerateMSOFormula(maxVarIndex);
                MSOFormula <BoolExpr> phi = new MSONot <BoolExpr>(phi1.First);
                return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(phi, phi1.Second));
            }

            case 7:
            {
                if (maxVarIndex > 1)
                {
                    int variable1 = random.Next(0, maxVarIndex - 1);
                    int variable2 = random.Next(0, maxVarIndex - 1);
                    if (variable1 == variable2)
                    {
                        if (variable1 == maxVarIndex - 1)
                        {
                            variable1 = variable1 - 1;
                        }
                        else
                        {
                            variable2 = variable2 + 1;
                        }
                    }

                    //Successor
                    MSOFormula <BoolExpr> phi = new MSOSuccN <BoolExpr>(varOf(variable1), varOf(variable2), random.Next(1, 4));
                    return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(phi, new List <BoolExpr>()));
                }
                else
                {
                    int             variable = random.Next(0, maxVarIndex - 1);
                    BoolExpr        b        = GeneratePredicate();
                    List <BoolExpr> l        = new List <BoolExpr>();
                    l.Add(b);
                    return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(new MSOPredicate <BoolExpr>(b, new Variable("x" + variable, true)), l));
                }
            }

            case 8:
            {
                int variable1 = random.Next(0, maxVarIndex - 1);
                int variable2 = random.Next(0, maxVarIndex - 1);

                //less than
                MSOFormula <BoolExpr> phi = new MSOLe <BoolExpr>(varOf(variable1), varOf(variable2));
                return(new Pair <MSOFormula <BoolExpr>, List <BoolExpr> >(phi, new List <BoolExpr>()));
            }
            }
            return(null);
        }