mirror of
https://github.com/Z3Prover/z3
synced 2025-04-15 05:18:44 +00:00
continued re-factoring
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
acbeed2e97
commit
33be06c6dc
|
@ -33,9 +33,7 @@ namespace opt {
|
|||
context::context(ast_manager& m):
|
||||
m(m),
|
||||
m_hard_constraints(m),
|
||||
m_soft_constraints(m),
|
||||
m_objectives(m),
|
||||
m_opt_objectives(m),
|
||||
m_optsmt(m),
|
||||
m_maxsmt(m)
|
||||
{
|
||||
m_params.set_bool("model", true);
|
||||
|
@ -59,19 +57,15 @@ namespace opt {
|
|||
|
||||
lbool is_sat;
|
||||
|
||||
is_sat = m_maxsmt(get_opt_solver(*s), m_soft_constraints, m_weights);
|
||||
is_sat = m_maxsmt(get_opt_solver(*s));
|
||||
|
||||
for (unsigned i = 0; i < m_soft_constraints.size(); ++i) {
|
||||
s->assert_expr(m_soft_constraints[i].get());
|
||||
expr_ref_vector ans = m_maxsmt.get_assignment();
|
||||
for (unsigned i = 0; i < ans.size(); ++i) {
|
||||
s->assert_expr(ans[i].get());
|
||||
}
|
||||
|
||||
if (is_sat == l_true) {
|
||||
is_sat = m_opt_objectives(get_opt_solver(*s), m_objectives);
|
||||
}
|
||||
|
||||
if (m_objectives.empty() && m_soft_constraints.empty()) {
|
||||
is_sat = s->check_sat(0,0);
|
||||
std::cout << "nothing to optimize: is-sat " << is_sat << std::endl;
|
||||
is_sat = m_optsmt(get_opt_solver(*s));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,7 +81,7 @@ namespace opt {
|
|||
if (m_solver) {
|
||||
m_solver->cancel();
|
||||
}
|
||||
m_opt_objectives.set_cancel(true);
|
||||
m_optsmt.set_cancel(true);
|
||||
m_maxsmt.set_cancel(true);
|
||||
}
|
||||
|
||||
|
@ -95,23 +89,10 @@ namespace opt {
|
|||
if (m_solver) {
|
||||
m_solver->reset_cancel();
|
||||
}
|
||||
m_opt_objectives.set_cancel(false);
|
||||
m_optsmt.set_cancel(false);
|
||||
m_maxsmt.set_cancel(false);
|
||||
}
|
||||
|
||||
void context::add_objective(app* t, bool is_max) {
|
||||
expr_ref t1(t, m), t2(m);
|
||||
th_rewriter rw(m);
|
||||
if (!is_max) {
|
||||
arith_util a(m);
|
||||
t1 = a.mk_uminus(t);
|
||||
}
|
||||
rw(t1, t2);
|
||||
SASSERT(is_app(t2));
|
||||
m_objectives.push_back(to_app(t2));
|
||||
m_is_max.push_back(is_max);
|
||||
}
|
||||
|
||||
void context::collect_statistics(statistics& stats) {
|
||||
if (m_solver) {
|
||||
m_solver->collect_statistics(stats);
|
||||
|
@ -128,7 +109,7 @@ namespace opt {
|
|||
m_solver->updt_params(m_params);
|
||||
}
|
||||
opt_params _p(m_params);
|
||||
m_opt_objectives.set_engine(_p.engine());
|
||||
m_optsmt.set_engine(_p.engine());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -35,43 +35,24 @@ namespace opt {
|
|||
class opt_solver;
|
||||
|
||||
class context {
|
||||
ast_manager& m;
|
||||
expr_ref_vector m_hard_constraints;
|
||||
expr_ref_vector m_soft_constraints;
|
||||
vector<rational> m_weights;
|
||||
app_ref_vector m_objectives;
|
||||
svector<bool> m_is_max;
|
||||
ref<solver> m_solver;
|
||||
params_ref m_params;
|
||||
optimize_objectives m_opt_objectives;
|
||||
maxsmt m_maxsmt;
|
||||
ast_manager& m;
|
||||
expr_ref_vector m_hard_constraints;
|
||||
ref<solver> m_solver;
|
||||
params_ref m_params;
|
||||
optimize_objectives m_optsmt;
|
||||
maxsmt m_maxsmt;
|
||||
public:
|
||||
context(ast_manager& m);
|
||||
|
||||
void add_soft_constraint(expr* f, rational const& w) {
|
||||
m_soft_constraints.push_back(f);
|
||||
m_weights.push_back(w);
|
||||
}
|
||||
|
||||
void add_objective(app* t, bool is_max);
|
||||
|
||||
void add_hard_constraint(expr* f) {
|
||||
m_hard_constraints.push_back(f);
|
||||
}
|
||||
|
||||
void set_solver(solver* s) {
|
||||
m_solver = s;
|
||||
}
|
||||
|
||||
void add_soft_constraint(expr* f, rational const& w) { m_maxsmt.add(f, w); }
|
||||
void add_objective(app* t, bool is_max) { m_optsmt.add(t, is_max); }
|
||||
void add_hard_constraint(expr* f) { m_hard_constraints.push_back(f); }
|
||||
void set_solver(solver* s) { m_solver = s; }
|
||||
void optimize();
|
||||
|
||||
void cancel();
|
||||
void reset_cancel();
|
||||
|
||||
void collect_statistics(statistics& stats);
|
||||
|
||||
static void collect_param_descrs(param_descrs & r);
|
||||
|
||||
void updt_params(params_ref& p);
|
||||
|
||||
private:
|
||||
|
|
|
@ -24,29 +24,38 @@ Notes:
|
|||
|
||||
namespace opt {
|
||||
|
||||
lbool maxsmt::operator()(opt_solver& s, expr_ref_vector& fmls, vector<rational> const& ws) {
|
||||
lbool maxsmt::operator()(opt_solver& s) {
|
||||
lbool is_sat;
|
||||
|
||||
if (fmls.empty()) {
|
||||
m_answer.reset();
|
||||
m_answer.append(m_soft_constraints);
|
||||
if (m_answer.empty()) {
|
||||
is_sat = s.check_sat(0, 0);
|
||||
}
|
||||
else if (is_maxsat_problem(ws)) {
|
||||
is_sat = opt::fu_malik_maxsat(s, fmls);
|
||||
else if (is_maxsat_problem(m_weights)) {
|
||||
is_sat = opt::fu_malik_maxsat(s, m_answer);
|
||||
}
|
||||
else {
|
||||
is_sat = weighted_maxsat(s, fmls, ws);
|
||||
is_sat = weighted_maxsat(s, m_answer, m_weights);
|
||||
}
|
||||
|
||||
// Infrastructure for displaying and storing solution is TBD.
|
||||
std::cout << "is-sat: " << is_sat << "\n";
|
||||
if (is_sat == l_true) {
|
||||
std::cout << "Satisfying soft constraints\n";
|
||||
for (unsigned i = 0; i < fmls.size(); ++i) {
|
||||
std::cout << mk_pp(fmls[i].get(), m) << "\n";
|
||||
}
|
||||
}
|
||||
display_answer(std::cout);
|
||||
}
|
||||
return is_sat;
|
||||
}
|
||||
|
||||
expr_ref_vector maxsmt::get_assignment() const {
|
||||
return m_answer;
|
||||
}
|
||||
|
||||
void maxsmt::display_answer(std::ostream& out) const {
|
||||
for (unsigned i = 0; i < m_answer.size(); ++i) {
|
||||
out << mk_pp(m_answer[i], m) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void maxsmt::set_cancel(bool f) {
|
||||
m_cancel = f;
|
||||
|
|
|
@ -28,19 +28,33 @@ namespace opt {
|
|||
*/
|
||||
|
||||
class maxsmt {
|
||||
ast_manager& m;
|
||||
opt_solver* s;
|
||||
volatile bool m_cancel;
|
||||
symbol m_engine;
|
||||
ast_manager& m;
|
||||
opt_solver* s;
|
||||
volatile bool m_cancel;
|
||||
expr_ref_vector m_soft_constraints;
|
||||
expr_ref_vector m_answer;
|
||||
vector<rational> m_weights;
|
||||
symbol m_engine;
|
||||
public:
|
||||
maxsmt(ast_manager& m): m(m), s(0), m_cancel(false) {}
|
||||
maxsmt(ast_manager& m): m(m), s(0), m_cancel(false), m_soft_constraints(m), m_answer(m) {}
|
||||
|
||||
lbool operator()(opt_solver& s, expr_ref_vector& soft, vector<rational> const& weights);
|
||||
lbool operator()(opt_solver& s);
|
||||
|
||||
void set_cancel(bool f);
|
||||
|
||||
void add(expr* f, rational const& w) {
|
||||
m_soft_constraints.push_back(f);
|
||||
m_weights.push_back(w);
|
||||
}
|
||||
|
||||
void set_engine(symbol const& e) { m_engine = e; }
|
||||
|
||||
// TBD: rational get_value() const;
|
||||
|
||||
expr_ref_vector get_assignment() const;
|
||||
|
||||
void display_answer(std::ostream& out) const;
|
||||
|
||||
private:
|
||||
|
||||
bool is_maxsat_problem(vector<rational> const& ws) const;
|
||||
|
|
|
@ -46,6 +46,7 @@ Notes:
|
|||
#include "theory_arith.h"
|
||||
#include "ast_pp.h"
|
||||
#include "model_pp.h"
|
||||
#include "th_rewriter.h"
|
||||
|
||||
namespace opt {
|
||||
|
||||
|
@ -204,25 +205,24 @@ namespace opt {
|
|||
Takes solver with hard constraints added.
|
||||
Returns an optimal assignment to objective functions.
|
||||
*/
|
||||
lbool optimize_objectives::operator()(opt_solver& solver, app_ref_vector const& objectives) {
|
||||
lbool optimize_objectives::operator()(opt_solver& solver) {
|
||||
s = &solver;
|
||||
s->reset_objectives();
|
||||
m_lower.reset();
|
||||
m_upper.reset();
|
||||
m_objs.reset();
|
||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||
m_vars.reset();
|
||||
for (unsigned i = 0; i < m_objs.size(); ++i) {
|
||||
m_lower.push_back(inf_eps(rational(-1),inf_rational(0)));
|
||||
m_upper.push_back(inf_eps(rational(1), inf_rational(0)));
|
||||
m_objs.push_back(objectives[i]);
|
||||
}
|
||||
|
||||
// First check_sat call to initialize theories
|
||||
lbool is_sat = s->check_sat(0, 0);
|
||||
if (is_sat == l_true && !objectives.empty()) {
|
||||
if (is_sat == l_true && !m_objs.empty()) {
|
||||
opt_solver::scoped_push _push(*s);
|
||||
|
||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||
m_vars.push_back(s->add_objective(objectives[i]));
|
||||
for (unsigned i = 0; i < m_objs.size(); ++i) {
|
||||
m_vars.push_back(s->add_objective(m_objs[i].get()));
|
||||
}
|
||||
|
||||
if (m_engine == symbol("basic")) {
|
||||
|
@ -257,10 +257,32 @@ namespace opt {
|
|||
void optimize_objectives::display(std::ostream& out) const {
|
||||
unsigned sz = m_objs.size();
|
||||
for (unsigned i = 0; i < sz; ++i) {
|
||||
out << "objective value: " << mk_pp(m_objs[i], m) << " -> " << get_value(true, i).to_string() << std::endl;
|
||||
bool is_max = m_is_max[i];
|
||||
inf_eps val = get_value(is_max, i);
|
||||
expr_ref obj(m_objs[i], m);
|
||||
if (!is_max) {
|
||||
arith_util a(m);
|
||||
th_rewriter rw(m);
|
||||
obj = a.mk_uminus(obj);
|
||||
rw(obj, obj);
|
||||
}
|
||||
out << "objective value: " << obj << " |-> " << val << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void optimize_objectives::add(app* t, bool is_max) {
|
||||
expr_ref t1(t, m), t2(m);
|
||||
th_rewriter rw(m);
|
||||
if (!is_max) {
|
||||
arith_util a(m);
|
||||
t1 = a.mk_uminus(t);
|
||||
}
|
||||
rw(t1, t2);
|
||||
SASSERT(is_app(t2));
|
||||
m_objs.push_back(to_app(t2));
|
||||
m_is_max.push_back(is_max);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -28,18 +28,21 @@ namespace opt {
|
|||
*/
|
||||
|
||||
class optimize_objectives {
|
||||
ast_manager& m;
|
||||
opt_solver* s;
|
||||
volatile bool m_cancel;
|
||||
vector<inf_eps> m_lower;
|
||||
vector<inf_eps> m_upper;
|
||||
app_ref_vector m_objs;
|
||||
ast_manager& m;
|
||||
opt_solver* s;
|
||||
volatile bool m_cancel;
|
||||
vector<inf_eps> m_lower;
|
||||
vector<inf_eps> m_upper;
|
||||
app_ref_vector m_objs;
|
||||
svector<bool> m_is_max;
|
||||
svector<smt::theory_var> m_vars;
|
||||
symbol m_engine;
|
||||
public:
|
||||
optimize_objectives(ast_manager& m): m(m), s(0), m_cancel(false), m_objs(m) {}
|
||||
|
||||
lbool operator()(opt_solver& s, app_ref_vector const& objectives);
|
||||
lbool operator()(opt_solver& s);
|
||||
|
||||
void add(app* t, bool is_max);
|
||||
|
||||
void set_cancel(bool f);
|
||||
|
||||
|
|
Loading…
Reference in a new issue