def test_from_formula(self): x, y = Symbol("x"), Symbol("y") f = And(x, Or(y, x)) script = smtlibscript_from_formula(f) self.assertIsNotNone(script) outstream = cStringIO() script.serialize(outstream) output = outstream.getvalue() self.assertIn("(set-logic ", output) self.assertIn("(declare-fun x () Bool)", output) self.assertIn("(declare-fun y () Bool)", output) self.assertIn("(check-sat)", output) # Use custom logic (as str) script2 = smtlibscript_from_formula(f, logic="BOOL") outstream = cStringIO() script2.serialize(outstream) output = outstream.getvalue() self.assertIn("(set-logic BOOL)", output) # Use custom logic (as Logic obj) script3 = smtlibscript_from_formula(f, logic=QF_UFLIRA) outstream = cStringIO() script3.serialize(outstream) output = outstream.getvalue() self.assertIn("(set-logic QF_UFLIRA)", output) # Custom logic must be a Logic or Str with self.assertRaises(UndefinedLogicError): smtlibscript_from_formula(f, logic=4)
def test_dumped_logic(self): # Dumped logic matches the logic in the example. # # There are a few cases where we use a logic # that does not exist in SMT-LIB, and the SMT-LIB # serialization logic will find a logic that # is more expressive. We need to adjust the test # for those cases (see rewrite dict below). rewrite = { logics.QF_BOOL: logics.QF_UF, logics.BOOL: logics.LRA, logics.QF_NIRA: logics.AUFNIRA, } fs = get_example_formulae() for (f_out, _, _, logic) in fs: buf_out = StringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = StringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) for cmd in script_in: if cmd.name == "set-logic": logic_in = cmd.args[0] self.assertEqual(logic_in, rewrite.get(logic, logic)) break else: # Loops exited normally print("-"*40) print(script_in)
def test_dumped_logic(self): # Dumped logic matches the logic in the example fs = get_example_formulae() for (f_out, _, _, logic) in fs: buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) for cmd in script_in: if cmd.name == "set-logic": logic_in = cmd.args[0] if logic == logics.QF_BOOL: self.assertEqual(logic_in, logics.QF_UF) elif logic == logics.BOOL: self.assertEqual(logic_in, logics.LRA) else: self.assertEqual(logic_in, logic, script_in) break else: # Loops exited normally print("-"*40) print(script_in)
def test_dumped_logic(self): # Dumped logic matches the logic in the example fs = get_example_formulae() for (f_out, _, _, logic) in fs: buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) for cmd in script_in: if cmd.name == "set-logic": logic_in = cmd.args[0] if logic == logics.QF_BOOL: self.assertEqual(logic_in, logics.QF_UF) elif logic == logics.BOOL: self.assertEqual(logic_in, logics.LRA) else: self.assertEqual(logic_in, logic, script_in) break else: # Loops exited normally print("-" * 40) print(script_in)
def test_from_formula(self): x, y = Symbol("x"), Symbol("y") f = And(x, Or(y, x)) script = smtlibscript_from_formula(f) self.assertIsNotNone(script) outstream = cStringIO() script.serialize(outstream) output = outstream.getvalue() self.assertIn("(set-logic ", output) self.assertIn("(declare-fun x () Bool)", output) self.assertIn("(declare-fun y () Bool)", output) self.assertIn("(check-sat)", output)
def test_parse_examples_daggified(self): fs = get_example_formulae() for (f_out, _, _, _) in fs: buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out, daggify=True) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) f_in = script_in.get_last_formula() self.assertEqual(f_in, f_out)
def test_parse_examples_daggified_bv(self): fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic != logics.QF_BV: # See test_parse_examples_daggified continue buf_out = StringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out, daggify=True) buf_in = StringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) f_in = script_in.get_last_formula() self.assertValid(Iff(f_in, f_out), f_in.serialize())
def test_parse_examples_daggified_bv(self): fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic != logics.QF_BV: # See test_parse_examples_daggified continue buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out, daggify=True) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) f_in = script_in.get_last_formula() self.assertValid(Iff(f_in, f_out), f_in.serialize())
def test_parse_examples(self): fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic == logics.QF_BV: # See test_parse_examples_bv continue buf = StringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf) buf.seek(0) parser = SmtLibParser() script_in = parser.get_script(buf) f_in = script_in.get_last_formula() self.assertEqual(f_in.simplify(), f_out.simplify())
def test_parse_examples(self): fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic == logics.QF_BV: # See test_parse_examples_bv continue buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) f_in = script_in.get_last_formula() self.assertEqual(f_in.simplify(), f_out.simplify())
def test_parse_examples_bv(self): """For BV we represent a superset of the operators defined in SMT-LIB. We verify the correctness of the serialization process by checking the equivalence of the original and serialized expression. """ fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic != logics.QF_BV: continue buf_out = StringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = StringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) f_in = script_in.get_last_formula() self.assertValid(Iff(f_in, f_out))
def test_parse_examples_bv(self): """For BV we represent a superset of the operators defined in SMT-LIB. We verify the correctness of the serialization process by checking the equivalence of the original and serialized expression. """ fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic != logics.QF_BV: continue buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) f_in = script_in.get_last_formula() self.assertValid(Iff(f_in, f_out))
p.add_argument('max_oa', help='Over-approximate max in ReLUs') p.add_argument('simplex_friendly', help='Simplex-friendly encoding') opts = p.parse_args() logger.debug('Filename is ' + opts.nnet) logger.debug('Violation file is ' + opts.violation) overapproximate_max = bool(int(opts.max_oa)) logger.debug('Using Max overapproximation ' + str(overapproximate_max)) simplex_friendly = bool(int(opts.simplex_friendly)) logger.debug('Using Simplex-friendly encoding' + str(simplex_friendly)) nnet2smt = Nnet2Smt(opts.nnet, opts.violation) #nnet2smt.print_nnet_info() nnet2smt.convert(True) if overapproximate_max: if simplex_friendly: nnet2smt.add_relu_simplex_friendly_OA() else: nnet2smt.add_relu_maxOA_constraint() else: if simplex_friendly: nnet2smt.add_relu_simplex_friendly_eager() else: nnet2smt.add_relu_eager_constraint() f = nnet2smt.get_smt_formula() script_out = smtlibscript_from_formula(f) script_out.serialize(outstream=sys.stdout, daggify=False)