mirror of
https://github.com/Z3Prover/z3
synced 2025-04-07 18:05:21 +00:00
expose bounds as vector expressions instead of containing ad-hoc expressions. Issue #911
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
e744d0f271
commit
e02160c674
|
@ -218,6 +218,34 @@ extern "C" {
|
|||
Z3_CATCH_RETURN(0);
|
||||
}
|
||||
|
||||
// get lower value or current approximation
|
||||
Z3_ast_vector Z3_API Z3_optimize_get_lower_as_vector(Z3_context c, Z3_optimize o, unsigned idx) {
|
||||
Z3_TRY;
|
||||
LOG_Z3_optimize_get_lower_as_vector(c, o, idx);
|
||||
RESET_ERROR_CODE();
|
||||
expr_ref_vector es(mk_c(c)->m());
|
||||
to_optimize_ptr(o)->get_lower(idx, es);
|
||||
Z3_ast_vector_ref * v = alloc(Z3_ast_vector_ref, *mk_c(c), mk_c(c)->m());
|
||||
mk_c(c)->save_object(v);
|
||||
v->m_ast_vector.append(es.size(), (ast*const*)es.c_ptr());
|
||||
RETURN_Z3(of_ast_vector(v));
|
||||
Z3_CATCH_RETURN(0);
|
||||
}
|
||||
|
||||
// get upper or current approximation
|
||||
Z3_ast_vector Z3_API Z3_optimize_get_upper_as_vector(Z3_context c, Z3_optimize o, unsigned idx) {
|
||||
Z3_TRY;
|
||||
LOG_Z3_optimize_get_upper_as_vector(c, o, idx);
|
||||
RESET_ERROR_CODE();
|
||||
expr_ref_vector es(mk_c(c)->m());
|
||||
to_optimize_ptr(o)->get_upper(idx, es);
|
||||
Z3_ast_vector_ref * v = alloc(Z3_ast_vector_ref, *mk_c(c), mk_c(c)->m());
|
||||
mk_c(c)->save_object(v);
|
||||
v->m_ast_vector.append(es.size(), (ast*const*)es.c_ptr());
|
||||
RETURN_Z3(of_ast_vector(v));
|
||||
Z3_CATCH_RETURN(0);
|
||||
}
|
||||
|
||||
Z3_string Z3_API Z3_optimize_to_string(Z3_context c, Z3_optimize o) {
|
||||
Z3_TRY;
|
||||
LOG_Z3_optimize_to_string(c, o);
|
||||
|
|
|
@ -144,6 +144,23 @@ namespace Microsoft.Z3
|
|||
{
|
||||
get { return Lower; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve a lower bound for the objective handle.
|
||||
/// </summary>
|
||||
public ArithExpr[] LowerAsVector
|
||||
{
|
||||
get { return opt.GetLowerAsVector(handle); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an upper bound for the objective handle.
|
||||
/// </summary>
|
||||
public ArithExpr[] UpperAsVector
|
||||
{
|
||||
get { return opt.GetUpperAsVector(handle); }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -255,6 +272,25 @@ namespace Microsoft.Z3
|
|||
return (ArithExpr)Expr.Create(Context, Native.Z3_optimize_get_upper(Context.nCtx, NativeObject, index));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve a lower bound for the objective handle.
|
||||
/// </summary>
|
||||
private ArithExpr[] GetLowerAsVector(uint index)
|
||||
{
|
||||
ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_lower_as_vector(Context.nCtx, NativeObject, index));
|
||||
return v.ToArithExprArray();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve an upper bound for the objective handle.
|
||||
/// </summary>
|
||||
private ArithExpr[] GetUpperAsVector(uint index)
|
||||
{
|
||||
ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_upper_as_vector(Context.nCtx, NativeObject, index));
|
||||
return v.ToArithExprArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a string the describes why the last to check returned unknown
|
||||
/// </summary>
|
||||
|
|
|
@ -6719,12 +6719,24 @@ class OptimizeObjective:
|
|||
opt = self._opt
|
||||
return _to_expr_ref(Z3_optimize_get_upper(opt.ctx.ref(), opt.optimize, self._value), opt.ctx)
|
||||
|
||||
def lower_values(self):
|
||||
opt = self._opt
|
||||
return AstVector(Z3_optimize_get_lower_as_vector(opt.ctx.ref(), opt.optimize, self._value), opt.ctx)
|
||||
|
||||
def upper_values(self):
|
||||
opt = self._opt
|
||||
return AstVector(Z3_optimize_get_upper_as_vector(opt.ctx.ref(), opt.optimize, self._value), opt.ctx)
|
||||
|
||||
def value(self):
|
||||
if self._is_max:
|
||||
return self.upper()
|
||||
else:
|
||||
return self.lower()
|
||||
|
||||
def __str__(self):
|
||||
return "%s:%s" % (self._value, self._is_max)
|
||||
|
||||
|
||||
class Optimize(Z3PPObject):
|
||||
"""Optimize API provides methods for solving using objective functions and weighted soft constraints"""
|
||||
|
||||
|
@ -6829,6 +6841,16 @@ class Optimize(Z3PPObject):
|
|||
raise Z3Exception("Expecting objective handle returned by maximize/minimize")
|
||||
return obj.upper()
|
||||
|
||||
def lower_values(self, obj):
|
||||
if not isinstance(obj, OptimizeObjective):
|
||||
raise Z3Exception("Expecting objective handle returned by maximize/minimize")
|
||||
return obj.lower_values()
|
||||
|
||||
def upper_values(self, obj):
|
||||
if not isinstance(obj, OptimizeObjective):
|
||||
raise Z3Exception("Expecting objective handle returned by maximize/minimize")
|
||||
return obj.upper_values()
|
||||
|
||||
def from_file(self, filename):
|
||||
"""Parse assertions and objectives from a file"""
|
||||
Z3_optimize_from_file(self.ctx.ref(), self.optimize, filename)
|
||||
|
|
|
@ -186,6 +186,33 @@ extern "C" {
|
|||
*/
|
||||
Z3_ast Z3_API Z3_optimize_get_upper(Z3_context c, Z3_optimize o, unsigned idx);
|
||||
|
||||
|
||||
/**
|
||||
\brief Retrieve lower bound value or approximation for the i'th optimization objective.
|
||||
The returned vector is of length 3. It always contains numerals.
|
||||
The three numerals are coefficients a, b, c and encode the result of \c Z3_optimize_get_lower
|
||||
a * infinity + b + c * epsilon.
|
||||
|
||||
\param c - context
|
||||
\param o - optimization context
|
||||
\param idx - index of optimization objective
|
||||
|
||||
def_API('Z3_optimize_get_lower_as_vector', AST_VECTOR, (_in(CONTEXT), _in(OPTIMIZE), _in(UINT)))
|
||||
*/
|
||||
Z3_ast_vector Z3_API Z3_optimize_get_lower_as_vector(Z3_context c, Z3_optimize o, unsigned idx);
|
||||
|
||||
/**
|
||||
\brief Retrieve upper bound value or approximation for the i'th optimization objective.
|
||||
|
||||
\param c - context
|
||||
\param o - optimization context
|
||||
\param idx - index of optimization objective
|
||||
|
||||
def_API('Z3_optimize_get_upper_as_vector', AST_VECTOR, (_in(CONTEXT), _in(OPTIMIZE), _in(UINT)))
|
||||
*/
|
||||
Z3_ast_vector Z3_API Z3_optimize_get_upper_as_vector(Z3_context c, Z3_optimize o, unsigned idx);
|
||||
|
||||
|
||||
/**
|
||||
\brief Print the current context as a string.
|
||||
\param c - context.
|
||||
|
|
|
@ -223,7 +223,7 @@ public:
|
|||
cmd_context::scoped_watch sw(ctx);
|
||||
lbool r = l_undef;
|
||||
try {
|
||||
r = check_sat(t, g, md, result->labels, pr, core, reason_unknown);
|
||||
r = check_sat(t, g, md, result->labels, pr, core, reason_unknown);
|
||||
ctx.display_sat_result(r);
|
||||
result->set_status(r);
|
||||
if (r == l_undef) {
|
||||
|
|
|
@ -1290,6 +1290,15 @@ namespace opt {
|
|||
return to_expr(get_upper_as_num(idx));
|
||||
}
|
||||
|
||||
void context::to_exprs(inf_eps const& n, expr_ref_vector& es) {
|
||||
rational inf = n.get_infinity();
|
||||
rational r = n.get_rational();
|
||||
rational eps = n.get_infinitesimal();
|
||||
es.push_back(m_arith.mk_numeral(inf, inf.is_int()));
|
||||
es.push_back(m_arith.mk_numeral(r, r.is_int()));
|
||||
es.push_back(m_arith.mk_numeral(eps, eps.is_int()));
|
||||
}
|
||||
|
||||
expr_ref context::to_expr(inf_eps const& n) {
|
||||
rational inf = n.get_infinity();
|
||||
rational r = n.get_rational();
|
||||
|
@ -1455,9 +1464,10 @@ namespace opt {
|
|||
|
||||
void context::validate_maxsat(symbol const& id) {
|
||||
maxsmt& ms = *m_maxsmts.find(id);
|
||||
TRACE("opt", tout << "Validate: " << id << "\n";);
|
||||
for (unsigned i = 0; i < m_objectives.size(); ++i) {
|
||||
objective const& obj = m_objectives[i];
|
||||
if (obj.m_id == id) {
|
||||
if (obj.m_id == id && obj.m_type == O_MAXSMT) {
|
||||
SASSERT(obj.m_type == O_MAXSMT);
|
||||
rational value(0);
|
||||
expr_ref val(m);
|
||||
|
|
|
@ -207,6 +207,9 @@ namespace opt {
|
|||
expr_ref get_lower(unsigned idx);
|
||||
expr_ref get_upper(unsigned idx);
|
||||
|
||||
void get_lower(unsigned idx, expr_ref_vector& es) { to_exprs(get_lower_as_num(idx), es); }
|
||||
void get_upper(unsigned idx, expr_ref_vector& es) { to_exprs(get_upper_as_num(idx), es); }
|
||||
|
||||
std::string to_string() const;
|
||||
|
||||
|
||||
|
@ -238,6 +241,7 @@ namespace opt {
|
|||
lbool adjust_unknown(lbool r);
|
||||
bool scoped_lex();
|
||||
expr_ref to_expr(inf_eps const& n);
|
||||
void to_exprs(inf_eps const& n, expr_ref_vector& es);
|
||||
|
||||
void reset_maxsmts();
|
||||
void import_scoped_state();
|
||||
|
|
|
@ -25,6 +25,9 @@ Notes:
|
|||
#include"filter_model_converter.h"
|
||||
#include"ast_util.h"
|
||||
#include"solver2tactic.h"
|
||||
#include"smt_solver.h"
|
||||
#include"solver.h"
|
||||
#include"mus.h"
|
||||
|
||||
typedef obj_map<expr, expr *> expr2expr_map;
|
||||
|
||||
|
@ -159,6 +162,8 @@ public:
|
|||
ref<filter_model_converter> fmc;
|
||||
if (in->unsat_core_enabled()) {
|
||||
extract_clauses_and_dependencies(in, clauses, assumptions, bool2dep, fmc);
|
||||
TRACE("mus", in->display_with_dependencies(tout);
|
||||
tout << clauses << "\n";);
|
||||
if (in->proofs_enabled() && !assumptions.empty())
|
||||
throw tactic_exception("smt tactic does not support simultaneous generation of proofs and unsat cores");
|
||||
for (unsigned i = 0; i < clauses.size(); ++i) {
|
||||
|
@ -224,8 +229,26 @@ public:
|
|||
pr = m_ctx->get_proof();
|
||||
if (in->unsat_core_enabled()) {
|
||||
unsigned sz = m_ctx->get_unsat_core_size();
|
||||
expr_ref_vector _core(m);
|
||||
for (unsigned i = 0; i < sz; ++i) {
|
||||
_core.push_back(m_ctx->get_unsat_core_expr(i));
|
||||
}
|
||||
if (sz > 0 && smt_params_helper(m_params_ref).core_minimize()) {
|
||||
std::cout << "size1 " << sz << " " << clauses << "\n";
|
||||
ref<solver> slv = mk_smt_solver(m, m_params_ref, m_logic);
|
||||
slv->assert_expr(clauses);
|
||||
mus mus(*slv.get());
|
||||
mus.add_soft(_core.size(), _core.c_ptr());
|
||||
lbool got_core = mus.get_mus(_core);
|
||||
sz = _core.size();
|
||||
std::cout << "size2 " << sz << "\n";
|
||||
if (got_core != l_true) {
|
||||
r = l_undef;
|
||||
goto undef_case;
|
||||
}
|
||||
}
|
||||
for (unsigned i = 0; i < sz; i++) {
|
||||
expr * b = m_ctx->get_unsat_core_expr(i);
|
||||
expr * b = _core[i].get();
|
||||
SASSERT(is_uninterp_const(b) && m.is_bool(b));
|
||||
expr * d = bool2dep.find(b);
|
||||
lcore = m.mk_join(lcore, m.mk_leaf(d));
|
||||
|
@ -236,6 +259,7 @@ public:
|
|||
return;
|
||||
}
|
||||
case l_undef:
|
||||
undef_case:
|
||||
if (m_ctx->canceled()) {
|
||||
throw tactic_exception(Z3_CANCELED_MSG);
|
||||
}
|
||||
|
|
|
@ -89,6 +89,7 @@ struct mus::imp {
|
|||
lbool get_mus1(expr_ref_vector& mus) {
|
||||
ptr_vector<expr> unknown(m_lit2expr.size(), m_lit2expr.c_ptr());
|
||||
ptr_vector<expr> core_exprs;
|
||||
TRACE("mus", m_solver.display(tout););
|
||||
while (!unknown.empty()) {
|
||||
IF_VERBOSE(12, verbose_stream() << "(mus reducing core: " << unknown.size() << " new core: " << mus.size() << ")\n";);
|
||||
TRACE("mus", display_vec(tout << "core: ", unknown); display_vec(tout << "mus: ", mus););
|
||||
|
|
|
@ -290,3 +290,5 @@ solver_factory * mk_tactic2solver_factory(tactic * t) {
|
|||
solver_factory * mk_tactic_factory2solver_factory(tactic_factory * f) {
|
||||
return alloc(tactic_factory2solver_factory, f);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -23,7 +23,6 @@ Notes:
|
|||
#include"model_v2_pp.h"
|
||||
|
||||
|
||||
|
||||
struct tactic_report::imp {
|
||||
char const * m_id;
|
||||
goal const & m_goal;
|
||||
|
@ -175,6 +174,7 @@ void exec(tactic & t, goal_ref const & in, goal_ref_buffer & result, model_conve
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
lbool check_sat(tactic & t, goal_ref & g, model_ref & md, labels_vec & labels, proof_ref & pr, expr_dependency_ref & core, std::string & reason_unknown) {
|
||||
bool models_enabled = g->models_enabled();
|
||||
bool proofs_enabled = g->proofs_enabled();
|
||||
|
|
Loading…
Reference in a new issue