def test_lia_qe_requiring_modulus(self): x = Symbol("x", INT) y = Symbol("y", INT) f = Exists([x], Equals(y, Times(x, Int(2)))) with self.assertRaises(ConvertExpressionError): qelim(f) try: qelim(f) except ConvertExpressionError as ex: # The modulus operator must be there self.assertIn("%2", str(ex.expression))
def test_examples_solving(self): for example in get_example_formulae(): if example.logic != pysmt.logics.BOOL: continue fv = example.expr.get_free_variables() f = Exists(fv, example.expr) g = qelim(f, solver_name="shannon").simplify() if example.is_sat: self.assertTrue(g.is_true()) else: self.assertTrue(g.is_false()) f = ForAll(fv, example.expr) g = qelim(f, solver_name="shannon").simplify() if example.is_valid: self.assertTrue(g.is_true()) else: self.assertTrue(g.is_false())
def test_w_theory(self): for example in get_example_formulae(): f = example.expr if example.logic.quantifier_free: continue try: res = qelim(f, solver_name="shannon") self.assertIsNotNone(res, f) except NoSolverAvailableError: self.assertTrue(example.logic > pysmt.logics.BOOL, example) except InternalSolverError: self.assertTrue(example.logic > pysmt.logics.BOOL, example)
from pysmt.shortcuts import Symbol, Or, ForAll, GE, LT, Real, Plus from pysmt.shortcuts import qelim, is_sat from pysmt.typing import REAL x, y, z = [Symbol(s, REAL) for s in "xyz"] f = ForAll([x], Or(LT(x, Real(5.0)), GE(Plus(x, y, z), Real((17, 2))))) print("f := %s" % f) qf_f = qelim(f, solver_name='z3') print("Quantifier-Free equivalent: %s" % qf_f) #Quantifier-Free equivalent: (7/2 <= (z + y)) res = is_sat(qf_f, solver_name="msat") print("SAT check using MathSAT: %s" % res) #SAT check using MathSAT: True
def test_multiple(self): f = ForAll([self.x, self.y], Or(self.x, self.y)) g = qelim(f, solver_name="shannon") g = g.simplify() self.assertEqual(g, FALSE())
def test_nested(self): f = Exists([self.x], ForAll([self.y], Or(self.x, self.y))) g = qelim(f, solver_name="shannon") g = g.simplify() self.assertEqual(g, TRUE())
# coordinate (0,0), the top-right has coordinate (5, 10). # rect = (x >= 0.0) & (x <= 5.0) & \ (y >= 0.0) & (y <= 10.0) # The first expression that we build asks if for any value of x we can # define a value of y that would satisfy the expression above. f1 = ForAll([x], Exists([y], rect)) # This is false, because we know that if we pick x=11, the formula # above cannot be satisfied. Eliminating all the symbols in an # expression is the same as solving the expression. # # The function to perform quantifier elimination is called qelim: qf_f1 = qelim(f1) print(qf_f1) # We can restrict the values that x can adopt by imposing them as # precondition. # # If we perform quantifier elimination on this expression we obtain an # unsurprising result: # f2 = ForAll([x], Exists([y], ((x >= 0.0) & (x <= 4.0)).Implies(rect))) qf_f2 = qelim(f2) print(qf_f2) # The power of quantifier elimination lies in the possibility of # representing sets of solutions. Lets introduce a new symbol z, that # represents the grid distance (aka Manhattan distance) of (x,y) from
def test_forall(self): f = ForAll([self.x], And(self.x, self.y)) g = qelim(f, solver_name="shannon") g = g.simplify() self.assertEqual(g, FALSE())
def test_exists(self): f = Exists([self.x], And(self.x, self.y)) for qe in self.qe: g = qelim(f, solver_name=qe) g = g.simplify() self.assertEqual(g, self.y, qe)
def test_exists(self): f = Exists([self.x], And(self.x, self.y)) g = qelim(f, solver_name="shannon") g = g.simplify() self.assertEqual(g, self.y)
def test_forall(self): f = ForAll([self.x], And(self.x, self.y)) for qe in self.qe: g = qelim(f, solver_name=qe) g = g.simplify() self.assertEqual(g, FALSE(), qe)
def test_quantifier_eliminator(self): f = Exists([self.x], And(self.x, self.y)) g = qelim(f, solver_name="bdd") self.assertEqual(g, self.y)
def test_nested(self): f = Exists([self.x], ForAll([self.y], Or(self.x, self.y))) for qe in self.qe: g = qelim(f, solver_name=qe) g = g.simplify() self.assertEqual(g, TRUE(), qe)
def test_multiple(self): f = ForAll([self.x, self.y], Or(self.x, self.y)) for qe in self.qe: g = qelim(f, solver_name=qe) g = g.simplify() self.assertEqual(g, FALSE(), qe)
# This example requires Z3 and MathSAT to be installed (but you can # replace MathSAT with any other solver for QF_LRA) # # This examples shows how to: # 1. Define Real valued constants using floats and fractions # 2. Perform quantifier elimination # 3. Pass results from one solver to another # from pysmt.shortcuts import Symbol, Or, ForAll, GE, LT, Real, Plus from pysmt.shortcuts import qelim, is_sat from pysmt.typing import REAL x, y, z = [Symbol(s, REAL) for s in "xyz"] f = ForAll([x], Or(LT(x, Real(5.0)), GE(Plus(x, y, z), Real((17,2))))) # (17,2) ~> 17/2 print("f := %s" % f) #f := (forall x . ((x < 5.0) | (17/2 <= (x + y + z)))) qf_f = qelim(f, solver_name="z3") print("Quantifier-Free equivalent: %s" % qf_f) #Quantifier-Free equivalent: (7/2 <= (z + y)) res = is_sat(qf_f, solver_name="msat") print("SAT check using MathSAT: %s" % res) #SAT check using MathSAT: True