def check_covered(dim, set1, list_of_covers): em = ExprManager() s_cover = SmtEngine(em) real_type = em.realType() x = [em.mkVar("x_[%s]" % (j + 1), real_type) for j in range(dim)] list_of_constraints = [] #x \in set1 list_of_constraints.append(get_point_membership_constraint(em, x, set1)) #x is not in any of the covers for cover in list_of_covers: list_of_constraints.append( em.mkExpr(CVC4.NOT, get_point_membership_constraint(em, x, cover))) s_cover.assertFormula(get_cvc4_and(em, list_of_constraints)) chk = s_cover.checkSat() if (chk == 1): return False else: return True
def findCounterexample(self, asserts, query, timeout=None): """Tries to find a counterexample to the query while asserts remains valid.""" self.em = ExprManager() self.solver = SmtEngine(self.em) if timeout is not None: self.options['tlimit-per'] = timeout * 1000 for name, value in CVCWrapper.options.items(): self.solver.setOption(name, SExpr(str(value))) self.solver.setLogic(CVCWrapper.logic) self.query = query self.asserts = asserts result, model = self._findModel() return result, model
def findCounterexample(self, asserts, query): """Tries to find a counterexample to the query while asserts remains valid.""" self.em = ExprManager() self.solver = SmtEngine(self.em) for name, value in CVCWrapper.options.items(): self.solver.setOption(name, SExpr(str(value))) self.solver.setLogic(CVCWrapper.logic) self.query = query self.asserts = self._coneOfInfluence(asserts, query) result = self._findModel() log.debug("Query -- %s" % self.query) log.debug("Asserts -- %s" % asserts) log.debug("Cone -- %s" % self.asserts) log.debug("Result -- %s" % result) return result
def findCounterexample(self, asserts, query, timeout=10**10): starttime = time.process_time() Z3Str2Wrapper.options['tlimit-per'] = timeout * 1000 self.solvetimeout = timeout self.em = ExprManager() self.solver = SmtEngine(self.em) for name, value in Z3Str2Wrapper.options.items(): self.solver.setOption(name, SExpr(str(value))) self.solver.setLogic(Z3Str2Wrapper.logic) self.query = query self.asserts = asserts result, model = self._findModel() endtime = time.process_time() log.debug("Timeout -- %s" % timeout) log.debug("Result -- %s" % result) log.debug("Model -- %s" % model) log.debug("Solver time: {0:.2f} seconds".format(timeout)) solvertime = endtime - starttime return result, model, solvertime
def findCounterexample(self, asserts, query, timeout=None): """Tries to find a counterexample to the query while asserts remains valid.""" startime = time.process_time() self.em = ExprManager() self.solver = SmtEngine(self.em) if timeout is not None: self.options['tlimit-per'] = timeout * 1000 for name, value in CVCWrapper.options.items(): self.solver.setOption(name, SExpr(str(value))) self.solver.setLogic(CVCWrapper.logic) self.query = query self.asserts = asserts result, model = self._findModel() endtime = time.process_time() log.debug("Timeout -- %s" % timeout) log.debug("Result -- %s" % result) log.debug("Model -- %s" % model) log.debug("Solver time: {0:.2f} seconds".format(endtime - startime)) solvertime = endtime - startime return result, model, solvertime
def main(): em = ExprManager() smt = SmtEngine(em) # Prove that for integers x and y: # x > 0 AND y > 0 => 2x + y >= 3 integer = em.integerType() x = em.mkVar("x", integer) y = em.mkVar("y", integer) zero = em.mkConst(Rational(0)) x_positive = em.mkExpr(CVC4.GT, x, zero) y_positive = em.mkExpr(CVC4.GT, y, zero) two = em.mkConst(Rational(2)) twox = em.mkExpr(CVC4.MULT, two, x) twox_plus_y = em.mkExpr(CVC4.PLUS, twox, y) three = em.mkConst(Rational(3)) twox_plus_y_geq_3 = em.mkExpr(CVC4.GEQ, twox_plus_y, three) formula = Expr(em.mkExpr(CVC4.AND, x_positive, y_positive)).impExpr(Expr(twox_plus_y_geq_3)) print "Checking validity of formula " + formula.toString() + " with CVC4." print "CVC4 should report VALID." print "Result from CVC4 is: " + smt.query(formula).toString() return 0
def get_controller_cvc4(Theta, initial_size, A, B, u_dim, u_poly, target, avoid_list, avoid_list_dynamic, safe, num_steps, Q_multiplier): #set safe to be None if you want to run the avoid_list version. Else set avoid_list to be None P, radius_dim, _, lam, G = get_overapproximate_rectangles( A, B, Q_multiplier, num_steps) radius_list = radius_without_r0(P, radius_dim, num_steps, lam) K = G * (-1) x_dim = len(radius_list[0]) sqrt_dim_inverse = 10000.0 / (math.floor(math.sqrt(x_dim * 100000000.0))) em = ExprManager() s = SmtEngine(em) s.setOption("produce-models", SExpr("true")) real_type = em.realType() x = [[ em.mkVar("x_ref_%s[%s]" % (i, j + 1), real_type) for j in range(x_dim) ] for i in range(num_steps + 1)] u = [[ em.mkVar("u_ref_%s[%s]" % (i, j + 1), real_type) for j in range(u_dim) ] for i in range(num_steps)] r = em.mkVar("r0", real_type) initial_rectangle = [] for i in range(x_dim): rl_mult_r = em.mkExpr(CVC4.MULT, get_cvc4_const(em, radius_list[0][i]), r) lo = em.mkExpr(CVC4.MINUS, x[0][i], rl_mult_r) hi = em.mkExpr(CVC4.PLUS, x[0][i], rl_mult_r) initial_rectangle.append((lo, hi)) if print_detail: print("Adding constraints ... ") if safe is None: add_constraints(s, em, x, u, r, Theta, radius_list, x_dim, A, u_dim, B, u_poly, target, avoid_list, avoid_list_dynamic, initial_rectangle, num_steps) else: add_constraints_safety(s, em, x, u, r, Theta, radius_list, x_dim, A, u_dim, B, u_poly, target, safe, initial_rectangle, num_steps) if print_detail: print("Done adding main constraints ... ") first_trajectory_found = False s.push() if print_detail: print("Now adding Theta constraints ... ") add_Theta_constraint(s, em, Theta, x[0], True) s.push() if print_detail: print("Done adding Theta constraints ... ") print("Now adding radius constraints ... ") add_radius_constraint(s, em, r, get_cvc4_const(em, initial_size)) if print_detail: print("Done adding radius constraints ... ") Theta_has_been_covered = False number_of_steps_initial_size_has_been_halved = 0 num_iters = 0 covered_list = [] trajectory_radius_controller_list = [ ] #List of tuples of the form (trajectory, radius_list) if print_detail: print("Starting iterations") while ((not Theta_has_been_covered) and number_of_steps_initial_size_has_been_halved < max_decrease_steps and num_iters < max_num_iters): num_iters = num_iters + 1 s_check = s.checkSat() if print_detail: print("checked") if (s_check.isSat() == 1): # trajectory_radius_controller_list = [[]] trajectory = [[ s.getValue(x[i][j]).getConstRational().getDouble() for j in range(x_dim) ] for i in range(num_steps + 1)] controller = [[ s.getValue(u[i][j]).getConstRational().getDouble() for j in range(u_dim) ] for i in range(num_steps)] rad = s.getValue(r).getConstRational().getDouble() init_point = trajectory[0] init_radius = [ r_i * rad * sqrt_dim_inverse for r_i in radius_list[0] ] cover = [] for i in range(len(init_point)): x_i = init_point[i] r_i = init_radius[i] cover.append((x_i - r_i, x_i + r_i)) covered_list.append((True, cover)) Theta_has_been_covered = check_covered(x_dim, Theta, covered_list) trajectory_radius_controller_list.append( (trajectory, [[rad * rad_dim for rad_dim in rad_step] for rad_step in radius_list], controller)) if print_detail: print("Found trajectory from:", trajectory[0], "with radius = ", init_radius[0]) if not first_trajectory_found: first_trajectory_found = True s.pop() s.pop() add_Theta_constraint(s, em, Theta, x[0], False) else: s.pop() add_cover_constraint(s, em, (True, cover), x[0], initial_rectangle) s.push() add_radius_constraint(s, em, r, get_cvc4_const(em, initial_size)) else: if print_detail: print("Fails for:", initial_size) initial_size = initial_size * multiplicative_factor_for_radius number_of_steps_initial_size_has_been_halved = number_of_steps_initial_size_has_been_halved + 1 s.pop() s.push() add_radius_constraint(s, em, r, get_cvc4_const(em, initial_size)) print("Number of iterations: " + str(num_iters)) return (K, trajectory_radius_controller_list, covered_list, num_iters)
def __compute_models(self, model, num, blocking_manager, constraints=None, shared_objects=None): opts = Options() opts.setInputLanguage(CVC4.INPUT_LANG_CVC4) exit_with_unknown = False if shared_objects is None: shared_objects = [] exprmgr = ExprManager(opts) smt = SmtEngine(exprmgr) smt.setOption("produce-models", SExpr(True)) smt.setOption("fmf-bound", SExpr(True)) smt.setOption("macros-quant", SExpr(True)) smt.setOption("finite-model-find", SExpr(True)) # smt.setOption("repeat-simp", SExpr(True)) # smt.setOption("check-models", SExpr(True)) # smt.setOption("full-saturate-quant", SExpr(True)) smt.setOption("incremental", SExpr(True)) ind = 0 assertions = blocking_manager.compute_from_sharedobjs(shared_objects) model = model + assertions if constraints: model += "\n%s;" % (constraints) parserbuilder = ParserBuilder(exprmgr, "", opts) parserbuilder.withStringInput(model) parser = parserbuilder.build() symboltable = parser.getSymbolTable() blocking_manager.exprmgr = exprmgr blocking_manager.symboltable = symboltable while True: cmd = parser.nextCommand() if not cmd: break cmd.invoke(smt) while True: checksat = CheckSatCommand() checksat.invoke(smt) sat = checksat.getResult().isSat() == 1 unk = checksat.getResult().isUnknown() uns = (not sat) and (not unk) Logger.log("sat: %s, uns: %s, unk: %s" % (sat, uns, unk), 2) exitcond = (not sat) if exit_with_unknown else uns if exitcond: return (shared_objects, 0) (bclauses, shared_obj) = blocking_manager.compute_from_smt(smt) Logger.log("%s" % str(shared_obj), 2) if shared_obj not in shared_objects: shared_objects.append(shared_obj) else: if constraints is not None: return (shared_objects, 2) for bclause in bclauses: assertion = AssertCommand(bclause) assertion.invoke(smt) Logger.msg(".", 0, constraints is None, 0) ind += 1 if (num != -1) and (ind >= num): return (shared_objects, 1) return (None, -1)