mirror of
https://github.com/Z3Prover/z3
synced 2025-04-24 01:25:31 +00:00
push blocking code to optimizer context
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
b0fddd8e60
commit
bc44bcad10
11 changed files with 152 additions and 49 deletions
|
@ -76,7 +76,8 @@ namespace opt {
|
|||
for (unsigned i = 0; i < fmls_copy.size(); ++i) {
|
||||
s->assert_expr(fmls_copy[i].get());
|
||||
}
|
||||
is_sat = optimize_objectives(get_opt_solver(*s), m_objectives, values);
|
||||
optimize_objectives obj(m, get_opt_solver(*s)); // TBD: make an attribute
|
||||
is_sat = obj(m_objectives, values);
|
||||
std::cout << "is-sat: " << is_sat << std::endl;
|
||||
|
||||
if (is_sat != l_true) {
|
||||
|
|
|
@ -6,10 +6,11 @@
|
|||
|
||||
namespace opt {
|
||||
|
||||
opt_solver::opt_solver(ast_manager & m, params_ref const & p, symbol const & l):
|
||||
solver_na2as(m),
|
||||
opt_solver::opt_solver(ast_manager & mgr, params_ref const & p, symbol const & l):
|
||||
solver_na2as(mgr),
|
||||
m_params(p),
|
||||
m_context(m, m_params),
|
||||
m_context(mgr, m_params),
|
||||
m(mgr),
|
||||
m_objective_enabled(false) {
|
||||
m_logic = l;
|
||||
if (m_logic != symbol::null)
|
||||
|
@ -139,9 +140,21 @@ namespace opt {
|
|||
return m_objective_vars.back();
|
||||
}
|
||||
|
||||
vector<opt_solver::inf_value> const& opt_solver::get_objective_values() {
|
||||
vector<inf_eps> const& opt_solver::get_objective_values() {
|
||||
return m_objective_values;
|
||||
}
|
||||
|
||||
expr_ref opt_solver::block_lower_bound(unsigned var, inf_eps const& val) {
|
||||
if (val.get_infinity().is_pos()) {
|
||||
return expr_ref(m.mk_false(), m);
|
||||
}
|
||||
else if (val.get_infinity().is_neg()) {
|
||||
return expr_ref(m.mk_true(), m);
|
||||
}
|
||||
else {
|
||||
return expr_ref(get_optimizer().block_lower_bound(m_objective_vars[var], val.get_numeral()), m);
|
||||
}
|
||||
}
|
||||
|
||||
void opt_solver::reset_objectives() {
|
||||
m_objective_vars.reset();
|
||||
|
|
|
@ -33,17 +33,19 @@ Notes:
|
|||
|
||||
namespace opt {
|
||||
|
||||
typedef inf_eps_rational<inf_rational> inf_eps;
|
||||
|
||||
|
||||
class opt_solver : public solver_na2as {
|
||||
public:
|
||||
typedef inf_eps_rational<inf_rational> inf_value;
|
||||
private:
|
||||
smt_params m_params;
|
||||
smt::kernel m_context;
|
||||
ast_manager& m;
|
||||
progress_callback * m_callback;
|
||||
symbol m_logic;
|
||||
bool m_objective_enabled;
|
||||
svector<smt::theory_var> m_objective_vars;
|
||||
vector<inf_value> m_objective_values;
|
||||
vector<inf_eps> m_objective_values;
|
||||
public:
|
||||
opt_solver(ast_manager & m, params_ref const & p, symbol const & l);
|
||||
virtual ~opt_solver();
|
||||
|
@ -69,7 +71,8 @@ namespace opt {
|
|||
smt::theory_var add_objective(app* term);
|
||||
void reset_objectives();
|
||||
|
||||
vector<inf_value> const& get_objective_values();
|
||||
vector<inf_eps> const& get_objective_values();
|
||||
expr_ref block_lower_bound(unsigned obj_index, inf_eps const& val);
|
||||
|
||||
class toggle_objective {
|
||||
opt_solver& s;
|
||||
|
|
|
@ -46,83 +46,92 @@ Notes:
|
|||
|
||||
namespace opt {
|
||||
|
||||
class scoped_push {
|
||||
opt_solver& s;
|
||||
public:
|
||||
scoped_push(opt_solver& s):s(s) {
|
||||
s.push();
|
||||
}
|
||||
~scoped_push() {
|
||||
s.pop(1);
|
||||
}
|
||||
};
|
||||
|
||||
void optimize_objectives::set_cancel(bool f) {
|
||||
m_cancel = true;
|
||||
}
|
||||
|
||||
void optimize_objectives::set_max(vector<inf_eps>& dst, vector<inf_eps> const& src) {
|
||||
for (unsigned i = 0; i < src.size(); ++i) {
|
||||
if (src[i] > dst[i]) {
|
||||
dst[i] = src[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Enumerate locally optimal assignments until fixedpoint.
|
||||
*/
|
||||
lbool mathsat_style_opt(
|
||||
opt_solver& s,
|
||||
app_ref_vector const& objectives,
|
||||
vector<inf_eps_rational<inf_rational> >& values)
|
||||
lbool optimize_objectives::basic_opt(app_ref_vector& objectives, vector<inf_eps>& values)
|
||||
{
|
||||
ast_manager& m = objectives.get_manager();
|
||||
arith_util autil(m);
|
||||
|
||||
s.reset_objectives();
|
||||
values.reset();
|
||||
// First check_sat call to initialize theories
|
||||
lbool is_sat = s.check_sat(0, 0);
|
||||
if (is_sat == l_false) {
|
||||
return is_sat;
|
||||
}
|
||||
|
||||
s.push();
|
||||
scoped_push _push(s);
|
||||
|
||||
opt_solver::toggle_objective _t(s, true);
|
||||
|
||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||
s.add_objective(objectives[i]);
|
||||
s.add_objective(objectives[i].get());
|
||||
values.push_back(inf_eps(rational(-1),inf_rational(0)));
|
||||
}
|
||||
|
||||
is_sat = s.check_sat(0, 0);
|
||||
|
||||
|
||||
while (is_sat == l_true) {
|
||||
// Extract values for objectives
|
||||
values.reset();
|
||||
values.append(s.get_objective_values());
|
||||
while (is_sat == l_true && !m_cancel) {
|
||||
set_max(values, s.get_objective_values());
|
||||
IF_VERBOSE(1,
|
||||
for (unsigned i = 0; i < values.size(); ++i) {
|
||||
verbose_stream() << values[i] << " ";
|
||||
}
|
||||
verbose_stream() << "\n";);
|
||||
expr_ref_vector disj(m);
|
||||
expr_ref constraint(m), num(m);
|
||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||
expr_ref constraint(m);
|
||||
|
||||
if (!values[i].get_infinity().is_zero()) {
|
||||
continue;
|
||||
}
|
||||
num = autil.mk_numeral(values[i].get_rational(), m.get_sort(objectives[i]));
|
||||
|
||||
SASSERT(values[i].get_infinitesimal().is_nonpos());
|
||||
if (values[i].get_infinitesimal().is_neg()) {
|
||||
disj.push_back(autil.mk_ge(objectives[i], num));
|
||||
}
|
||||
else {
|
||||
disj.push_back(autil.mk_gt(objectives[i], num));
|
||||
}
|
||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||
inf_eps const& v = values[i];
|
||||
disj.push_back(s.block_lower_bound(i, v));
|
||||
}
|
||||
constraint = m.mk_or(disj.size(), disj.c_ptr());
|
||||
s.assert_expr(constraint);
|
||||
is_sat = s.check_sat(0, 0);
|
||||
}
|
||||
|
||||
s.pop(1);
|
||||
|
||||
if (is_sat == l_undef) {
|
||||
return is_sat;
|
||||
|
||||
if (m_cancel || is_sat == l_undef) {
|
||||
return l_undef;
|
||||
}
|
||||
return l_true;
|
||||
return l_true;
|
||||
}
|
||||
|
||||
/**
|
||||
Takes solver with hard constraints added.
|
||||
Returns an optimal assignment to objective functions.
|
||||
*/
|
||||
|
||||
lbool optimize_objectives(opt_solver& s,
|
||||
app_ref_vector& objectives,
|
||||
vector<inf_eps_rational<inf_rational> >& values) {
|
||||
return mathsat_style_opt(s, objectives, values);
|
||||
lbool optimize_objectives::operator()(app_ref_vector& objectives, vector<inf_eps>& values) {
|
||||
return basic_opt(objectives, values);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -26,10 +26,26 @@ namespace opt {
|
|||
Takes solver with hard constraints added.
|
||||
Returns an optimal assignment to objective functions.
|
||||
*/
|
||||
|
||||
lbool optimize_objectives(opt_solver& s,
|
||||
app_ref_vector& objectives,
|
||||
vector<inf_eps_rational<inf_rational> >& values);
|
||||
|
||||
class optimize_objectives {
|
||||
ast_manager& m;
|
||||
opt_solver& s;
|
||||
volatile bool m_cancel;
|
||||
public:
|
||||
optimize_objectives(ast_manager& m, opt_solver& s): m(m), s(s), m_cancel(false) {}
|
||||
|
||||
lbool operator()(app_ref_vector& objectives, vector<inf_eps>& values);
|
||||
|
||||
void set_cancel(bool f);
|
||||
|
||||
private:
|
||||
|
||||
lbool basic_opt(app_ref_vector& objectives, vector<inf_eps>& values);
|
||||
|
||||
void set_max(vector<inf_eps>& dst, vector<inf_eps> const& src);
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue