mirror of
https://github.com/Z3Prover/z3
synced 2025-04-28 19:35:50 +00:00
separate out file for objectives
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
759d80dfe3
commit
401fced400
10 changed files with 199 additions and 61 deletions
|
@ -21,8 +21,6 @@ Notes:
|
|||
--*/
|
||||
|
||||
#include "opt_context.h"
|
||||
#include "fu_malik.h"
|
||||
#include "weighted_maxsat.h"
|
||||
#include "ast_pp.h"
|
||||
#include "opt_solver.h"
|
||||
#include "arith_decl_plugin.h"
|
||||
|
@ -37,7 +35,8 @@ namespace opt {
|
|||
m_hard_constraints(m),
|
||||
m_soft_constraints(m),
|
||||
m_objectives(m),
|
||||
m_opt_objectives(m)
|
||||
m_opt_objectives(m),
|
||||
m_maxsmt(m)
|
||||
{
|
||||
m_params.set_bool("model", true);
|
||||
m_params.set_bool("unsat_core", true);
|
||||
|
@ -45,57 +44,30 @@ namespace opt {
|
|||
|
||||
void context::optimize() {
|
||||
|
||||
expr_ref_vector const& fmls = m_soft_constraints;
|
||||
|
||||
if (!m_solver) {
|
||||
symbol logic;
|
||||
set_solver(alloc(opt_solver, m, m_params, logic));
|
||||
}
|
||||
solver* s = m_solver.get();
|
||||
|
||||
// really just works for opt_solver now.
|
||||
solver* s = m_solver.get();
|
||||
opt_solver::scoped_push _sp(get_opt_solver(*s));
|
||||
|
||||
for (unsigned i = 0; i < m_hard_constraints.size(); ++i) {
|
||||
s->assert_expr(m_hard_constraints[i].get());
|
||||
}
|
||||
|
||||
expr_ref_vector fmls_copy(fmls);
|
||||
|
||||
lbool is_sat;
|
||||
if (!fmls.empty()) {
|
||||
// TBD: bug when cancel flag is set, fu_malik returns is_sat == l_true instead of l_undef
|
||||
if (is_maxsat_problem()) {
|
||||
is_sat = opt::fu_malik_maxsat(*s, fmls_copy);
|
||||
}
|
||||
else {
|
||||
is_sat = weighted_maxsat(get_opt_solver(*s), fmls_copy, m_weights);
|
||||
}
|
||||
std::cout << "is-sat: " << is_sat << "\n";
|
||||
if (is_sat != l_true) {
|
||||
return;
|
||||
}
|
||||
std::cout << "Satisfying soft constraints\n";
|
||||
for (unsigned i = 0; i < fmls_copy.size(); ++i) {
|
||||
std::cout << mk_pp(fmls_copy[i].get(), m) << "\n";
|
||||
}
|
||||
|
||||
is_sat = m_maxsmt(get_opt_solver(*s), m_soft_constraints, m_weights);
|
||||
|
||||
for (unsigned i = 0; i < m_soft_constraints.size(); ++i) {
|
||||
s->assert_expr(m_soft_constraints[i].get());
|
||||
}
|
||||
|
||||
if (!m_objectives.empty()) {
|
||||
vector<inf_eps_rational<inf_rational> > values;
|
||||
for (unsigned i = 0; i < fmls_copy.size(); ++i) {
|
||||
s->assert_expr(fmls_copy[i].get());
|
||||
}
|
||||
is_sat = m_opt_objectives(get_opt_solver(*s), m_objectives, values);
|
||||
std::cout << "is-sat: " << is_sat << std::endl;
|
||||
|
||||
if (is_sat != l_true) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < values.size(); ++i) {
|
||||
if (!m_is_max[i]) {
|
||||
values[i].neg();
|
||||
}
|
||||
std::cout << "objective value: " << mk_pp(m_objectives[i].get(), m) << " -> " << values[i].to_string() << std::endl;
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
@ -103,15 +75,6 @@ namespace opt {
|
|||
}
|
||||
}
|
||||
|
||||
bool context::is_maxsat_problem() const {
|
||||
vector<rational> const& ws = m_weights;
|
||||
for (unsigned i = 0; i < ws.size(); ++i) {
|
||||
if (!ws[i].is_one()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
opt_solver& context::get_opt_solver(solver& s) {
|
||||
if (typeid(opt_solver) != typeid(s)) {
|
||||
|
@ -125,6 +88,7 @@ namespace opt {
|
|||
m_solver->cancel();
|
||||
}
|
||||
m_opt_objectives.set_cancel(true);
|
||||
m_maxsmt.set_cancel(true);
|
||||
}
|
||||
|
||||
void context::reset_cancel() {
|
||||
|
@ -132,6 +96,7 @@ namespace opt {
|
|||
m_solver->reset_cancel();
|
||||
}
|
||||
m_opt_objectives.set_cancel(false);
|
||||
m_maxsmt.set_cancel(false);
|
||||
}
|
||||
|
||||
void context::add_objective(app* t, bool is_max) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue