mirror of
https://github.com/Z3Prover/z3
synced 2025-04-24 01:25:31 +00:00
preparing for inf extension of arithmetic
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
acc7aa1636
commit
0b65aa83e8
6 changed files with 83 additions and 34 deletions
|
@ -43,9 +43,11 @@ Notes:
|
|||
#include "optimize_objectives.h"
|
||||
#include "opt_solver.h"
|
||||
#include "arith_decl_plugin.h"
|
||||
#include "smt_context.h"
|
||||
|
||||
namespace opt {
|
||||
|
||||
|
||||
void optimize_objectives::set_cancel(bool f) {
|
||||
m_cancel = true;
|
||||
}
|
||||
|
@ -62,50 +64,29 @@ namespace opt {
|
|||
/*
|
||||
Enumerate locally optimal assignments until fixedpoint.
|
||||
*/
|
||||
lbool optimize_objectives::basic_opt(app_ref_vector& objectives, vector<inf_eps>& values)
|
||||
{
|
||||
ast_manager& m = objectives.get_manager();
|
||||
lbool optimize_objectives::basic_opt(app_ref_vector& objectives) {
|
||||
arith_util autil(m);
|
||||
|
||||
s->reset_objectives();
|
||||
values.reset();
|
||||
m_lower.reset();
|
||||
m_upper.reset();
|
||||
// First check_sat call to initialize theories
|
||||
lbool is_sat = s->check_sat(0, 0);
|
||||
if (is_sat == l_false) {
|
||||
if (is_sat != l_true) {
|
||||
return is_sat;
|
||||
}
|
||||
|
||||
opt_solver::scoped_push _push(*s);
|
||||
|
||||
opt_solver::toggle_objective _t(*s, true);
|
||||
|
||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||
s->add_objective(objectives[i].get());
|
||||
values.push_back(inf_eps(rational(-1),inf_rational(0)));
|
||||
m_lower.push_back(inf_eps(rational(-1),inf_rational(0)));
|
||||
m_upper.push_back(inf_eps(rational(1), inf_rational(0)));
|
||||
}
|
||||
|
||||
is_sat = s->check_sat(0, 0);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
is_sat = update_lower();
|
||||
}
|
||||
|
||||
|
||||
if (m_cancel || is_sat == l_undef) {
|
||||
return l_undef;
|
||||
|
@ -113,13 +94,40 @@ namespace opt {
|
|||
return l_true;
|
||||
}
|
||||
|
||||
lbool optimize_objectives::update_lower() {
|
||||
lbool is_sat = s->check_sat(0, 0);
|
||||
set_max(m_lower, s->get_objective_values());
|
||||
IF_VERBOSE(1,
|
||||
for (unsigned i = 0; i < m_lower.size(); ++i) {
|
||||
verbose_stream() << m_lower[i] << " ";
|
||||
}
|
||||
verbose_stream() << "\n";);
|
||||
expr_ref_vector disj(m);
|
||||
expr_ref constraint(m);
|
||||
|
||||
for (unsigned i = 0; i < m_lower.size(); ++i) {
|
||||
inf_eps const& v = m_lower[i];
|
||||
disj.push_back(s->block_lower_bound(i, v));
|
||||
}
|
||||
constraint = m.mk_or(disj.size(), disj.c_ptr());
|
||||
s->assert_expr(constraint);
|
||||
return is_sat;
|
||||
}
|
||||
|
||||
lbool optimize_objectives::update_upper() {
|
||||
return l_undef;
|
||||
}
|
||||
|
||||
/**
|
||||
Takes solver with hard constraints added.
|
||||
Returns an optimal assignment to objective functions.
|
||||
*/
|
||||
lbool optimize_objectives::operator()(opt_solver& solver, app_ref_vector& objectives, vector<inf_eps>& values) {
|
||||
s = &solver;
|
||||
return basic_opt(objectives, values);
|
||||
lbool result = basic_opt(objectives);
|
||||
values.reset();
|
||||
values.append(m_lower);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,6 +31,8 @@ namespace opt {
|
|||
ast_manager& m;
|
||||
opt_solver* s;
|
||||
volatile bool m_cancel;
|
||||
vector<inf_eps> m_lower;
|
||||
vector<inf_eps> m_upper;
|
||||
public:
|
||||
optimize_objectives(ast_manager& m): m(m), s(0), m_cancel(false) {}
|
||||
|
||||
|
@ -40,10 +42,14 @@ namespace opt {
|
|||
|
||||
private:
|
||||
|
||||
lbool basic_opt(app_ref_vector& objectives, vector<inf_eps>& values);
|
||||
lbool basic_opt(app_ref_vector& objectives);
|
||||
|
||||
void set_max(vector<inf_eps>& dst, vector<inf_eps> const& src);
|
||||
|
||||
lbool update_lower();
|
||||
|
||||
lbool update_upper();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue