3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-07 06:33:23 +00:00

continued re-factoring

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2013-11-08 09:00:24 -08:00
parent acbeed2e97
commit 33be06c6dc
6 changed files with 98 additions and 88 deletions

View file

@ -33,9 +33,7 @@ namespace opt {
context::context(ast_manager& m): context::context(ast_manager& m):
m(m), m(m),
m_hard_constraints(m), m_hard_constraints(m),
m_soft_constraints(m), m_optsmt(m),
m_objectives(m),
m_opt_objectives(m),
m_maxsmt(m) m_maxsmt(m)
{ {
m_params.set_bool("model", true); m_params.set_bool("model", true);
@ -59,19 +57,15 @@ namespace opt {
lbool is_sat; 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) { expr_ref_vector ans = m_maxsmt.get_assignment();
s->assert_expr(m_soft_constraints[i].get()); for (unsigned i = 0; i < ans.size(); ++i) {
s->assert_expr(ans[i].get());
} }
if (is_sat == l_true) { if (is_sat == l_true) {
is_sat = m_opt_objectives(get_opt_solver(*s), m_objectives); is_sat = m_optsmt(get_opt_solver(*s));
}
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;
} }
} }
@ -87,7 +81,7 @@ namespace opt {
if (m_solver) { if (m_solver) {
m_solver->cancel(); m_solver->cancel();
} }
m_opt_objectives.set_cancel(true); m_optsmt.set_cancel(true);
m_maxsmt.set_cancel(true); m_maxsmt.set_cancel(true);
} }
@ -95,23 +89,10 @@ namespace opt {
if (m_solver) { if (m_solver) {
m_solver->reset_cancel(); m_solver->reset_cancel();
} }
m_opt_objectives.set_cancel(false); m_optsmt.set_cancel(false);
m_maxsmt.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) { void context::collect_statistics(statistics& stats) {
if (m_solver) { if (m_solver) {
m_solver->collect_statistics(stats); m_solver->collect_statistics(stats);
@ -128,7 +109,7 @@ namespace opt {
m_solver->updt_params(m_params); m_solver->updt_params(m_params);
} }
opt_params _p(m_params); opt_params _p(m_params);
m_opt_objectives.set_engine(_p.engine()); m_optsmt.set_engine(_p.engine());
} }

View file

@ -35,43 +35,24 @@ namespace opt {
class opt_solver; class opt_solver;
class context { class context {
ast_manager& m; ast_manager& m;
expr_ref_vector m_hard_constraints; expr_ref_vector m_hard_constraints;
expr_ref_vector m_soft_constraints; ref<solver> m_solver;
vector<rational> m_weights; params_ref m_params;
app_ref_vector m_objectives; optimize_objectives m_optsmt;
svector<bool> m_is_max; maxsmt m_maxsmt;
ref<solver> m_solver;
params_ref m_params;
optimize_objectives m_opt_objectives;
maxsmt m_maxsmt;
public: public:
context(ast_manager& m); context(ast_manager& m);
void add_soft_constraint(expr* f, rational const& w) { void add_soft_constraint(expr* f, rational const& w) { m_maxsmt.add(f, w); }
m_soft_constraints.push_back(f); void add_objective(app* t, bool is_max) { m_optsmt.add(t, is_max); }
m_weights.push_back(w); void add_hard_constraint(expr* f) { m_hard_constraints.push_back(f); }
} void set_solver(solver* s) { m_solver = s; }
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 optimize(); void optimize();
void cancel(); void cancel();
void reset_cancel(); void reset_cancel();
void collect_statistics(statistics& stats); void collect_statistics(statistics& stats);
static void collect_param_descrs(param_descrs & r); static void collect_param_descrs(param_descrs & r);
void updt_params(params_ref& p); void updt_params(params_ref& p);
private: private:

View file

@ -24,29 +24,38 @@ Notes:
namespace opt { 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; lbool is_sat;
m_answer.reset();
if (fmls.empty()) { m_answer.append(m_soft_constraints);
if (m_answer.empty()) {
is_sat = s.check_sat(0, 0); is_sat = s.check_sat(0, 0);
} }
else if (is_maxsat_problem(ws)) { else if (is_maxsat_problem(m_weights)) {
is_sat = opt::fu_malik_maxsat(s, fmls); is_sat = opt::fu_malik_maxsat(s, m_answer);
} }
else { 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. // Infrastructure for displaying and storing solution is TBD.
std::cout << "is-sat: " << is_sat << "\n"; std::cout << "is-sat: " << is_sat << "\n";
if (is_sat == l_true) { if (is_sat == l_true) {
std::cout << "Satisfying soft constraints\n"; std::cout << "Satisfying soft constraints\n";
for (unsigned i = 0; i < fmls.size(); ++i) { display_answer(std::cout);
std::cout << mk_pp(fmls[i].get(), m) << "\n"; }
}
}
return is_sat; 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) { void maxsmt::set_cancel(bool f) {
m_cancel = f; m_cancel = f;

View file

@ -28,19 +28,33 @@ namespace opt {
*/ */
class maxsmt { class maxsmt {
ast_manager& m; ast_manager& m;
opt_solver* s; opt_solver* s;
volatile bool m_cancel; volatile bool m_cancel;
symbol m_engine; expr_ref_vector m_soft_constraints;
expr_ref_vector m_answer;
vector<rational> m_weights;
symbol m_engine;
public: 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 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; } 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: private:
bool is_maxsat_problem(vector<rational> const& ws) const; bool is_maxsat_problem(vector<rational> const& ws) const;

View file

@ -46,6 +46,7 @@ Notes:
#include "theory_arith.h" #include "theory_arith.h"
#include "ast_pp.h" #include "ast_pp.h"
#include "model_pp.h" #include "model_pp.h"
#include "th_rewriter.h"
namespace opt { namespace opt {
@ -204,25 +205,24 @@ namespace opt {
Takes solver with hard constraints added. Takes solver with hard constraints added.
Returns an optimal assignment to objective functions. 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 = &solver;
s->reset_objectives(); s->reset_objectives();
m_lower.reset(); m_lower.reset();
m_upper.reset(); m_upper.reset();
m_objs.reset(); m_vars.reset();
for (unsigned i = 0; i < objectives.size(); ++i) { for (unsigned i = 0; i < m_objs.size(); ++i) {
m_lower.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))); m_upper.push_back(inf_eps(rational(1), inf_rational(0)));
m_objs.push_back(objectives[i]);
} }
// First check_sat call to initialize theories // First check_sat call to initialize theories
lbool is_sat = s->check_sat(0, 0); 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); opt_solver::scoped_push _push(*s);
for (unsigned i = 0; i < objectives.size(); ++i) { for (unsigned i = 0; i < m_objs.size(); ++i) {
m_vars.push_back(s->add_objective(objectives[i])); m_vars.push_back(s->add_objective(m_objs[i].get()));
} }
if (m_engine == symbol("basic")) { if (m_engine == symbol("basic")) {
@ -257,10 +257,32 @@ namespace opt {
void optimize_objectives::display(std::ostream& out) const { void optimize_objectives::display(std::ostream& out) const {
unsigned sz = m_objs.size(); unsigned sz = m_objs.size();
for (unsigned i = 0; i < sz; ++i) { 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);
}
} }

View file

@ -28,18 +28,21 @@ namespace opt {
*/ */
class optimize_objectives { class optimize_objectives {
ast_manager& m; ast_manager& m;
opt_solver* s; opt_solver* s;
volatile bool m_cancel; volatile bool m_cancel;
vector<inf_eps> m_lower; vector<inf_eps> m_lower;
vector<inf_eps> m_upper; vector<inf_eps> m_upper;
app_ref_vector m_objs; app_ref_vector m_objs;
svector<bool> m_is_max;
svector<smt::theory_var> m_vars; svector<smt::theory_var> m_vars;
symbol m_engine; symbol m_engine;
public: public:
optimize_objectives(ast_manager& m): m(m), s(0), m_cancel(false), m_objs(m) {} 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); void set_cancel(bool f);