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); }
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 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); }
/// <summary> /// If t is a variable name then returns t as a f-o variable and sets psi=null. /// Else returns a fresh f-o variable x and outputs psi(x) s.t. psi(x) iff x=t. /// </summary> Variable ConvertTerm1(MonaExpr t, MapStack <string, MonaParam> locals, out MSOFormula <BDD> psi) { switch (t.symbol.Kind) { case Tokens.NAME: { MonaParam p; if (locals.TryGetValue(t.symbol.text, out p)) { if (p.kind == MonaParamKind.var1) { psi = null; return(new Variable(t.symbol.text, true)); } else { throw new NotImplementedException(t.ToString()); } } else { MonaDecl d; if (globals.TryGetValue(t.symbol.text, out d)) { if (d.kind == MonaDeclKind.constant) { int n = ((MonaConstDecl)d).def.ToInt(globals); Variable x = MkNewVar1(); var pred = new MSOeqN <BDD>(x, n); psi = pred; return(x); } else { throw new NotImplementedException(t.ToString()); } } else { throw new NotImplementedException(t.ToString()); } } } case Tokens.PLUS: { Variable y = MkNewVar1(); if (t[0].symbol.Kind == Tokens.NAME) { int n = t[1].ToInt(globals); Variable x = new Variable(t[0].symbol.text, true); psi = new MSOSuccN <BDD>(x, y, n); // y = x + n } else { int n = t.ToInt(globals); psi = new MSOeqN <BDD>(y, n); // y = n } return(y); } case Tokens.MIN: { MSOFormula <BDD> X_psi; Variable X = ConvertTerm2(t[0], locals, out X_psi); Variable x = MkNewVar1(); MSOFormula <BDD> min = new MSOMin <BDD>(x, X); if (X_psi != null) { min = new MSOExists <BDD>(X, new MSOAnd <BDD>(X_psi, min)); } psi = min; return(x); } case Tokens.MAX: { MSOFormula <BDD> X_psi; Variable X = ConvertTerm2(t[0], locals, out X_psi); Variable x = MkNewVar1(); MSOFormula <BDD> max = new MSOMax <BDD>(x, X); if (X_psi != null) { max = new MSOExists <BDD>(X, new MSOAnd <BDD>(X_psi, max)); } psi = max; return(x); } default: throw new NotImplementedException(t.ToString()); } }
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); } } }
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; }
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); }
/// <summary> /// If t is a variable name then returns t as a f-o variable and sets psi=null. /// Else returns a fresh f-o variable x and outputs psi(x) s.t. psi(x) iff x=t. /// </summary> Variable ConvertTerm1(MonaExpr t, MapStack<string, MonaParam> locals, out MSOFormula<BDD> psi) { switch (t.symbol.Kind) { case Tokens.NAME: { MonaParam p; if (locals.TryGetValue(t.symbol.text, out p)) { if (p.kind == MonaParamKind.var1) { psi = null; return new Variable(t.symbol.text, true); } else throw new NotImplementedException(t.ToString()); } else { MonaDecl d; if (globals.TryGetValue(t.symbol.text, out d)) { if (d.kind == MonaDeclKind.constant) { int n = ((MonaConstDecl)d).def.ToInt(globals); Variable x = MkNewVar1(); var pred = new MSOeqN<BDD>(x, n); psi = pred; return x; } else if (d.kind == MonaDeclKind.var1) { psi = null; return new Variable(t.symbol.text, true); } else throw new NotImplementedException(t.ToString()); } else throw new NotImplementedException(t.ToString()); } } case Tokens.PLUS: { Variable y = MkNewVar1(); if (t[0].symbol.Kind == Tokens.NAME) { int n = t[1].ToInt(globals); Variable x = new Variable(t[0].symbol.text, true); psi = new MSOSuccN<BDD>(x, y, n); // y = x + n } else { int n = t.ToInt(globals); psi = new MSOeqN<BDD>(y, n); // y = n } return y; } case Tokens.MIN: { MSOFormula<BDD> X_psi; Variable X = ConvertTerm2(t[0], locals, out X_psi); Variable x = MkNewVar1(); MSOFormula<BDD> min = new MSOMin<BDD>(x, X); if (X_psi != null) min = new MSOExists<BDD>(X, new MSOAnd<BDD>(X_psi, min)); psi = min; return x; } case Tokens.MAX: { MSOFormula<BDD> X_psi; Variable X = ConvertTerm2(t[0], locals, out X_psi); Variable x = MkNewVar1(); MSOFormula<BDD> max = new MSOMax<BDD>(x, X); if (X_psi != null) max = new MSOExists<BDD>(X, new MSOAnd<BDD>(X_psi, max)); psi = max; return x; } case Tokens.NUMBER: { Variable x = MkNewVar1(); int num = t.symbol.ToInt(); psi = new MSOeqN<BDD>(x,num); return x; } default: throw new NotImplementedException(t.ToString()); } }