mirror of
https://github.com/Z3Prover/z3
synced 2025-08-14 14:55:25 +00:00
Create placeholders to optimization methods
This commit is contained in:
parent
3da47a280e
commit
f4e2b23238
9 changed files with 363 additions and 68 deletions
|
@ -18,37 +18,18 @@ Notes:
|
|||
#include "opt_cmds.h"
|
||||
#include "cmd_context.h"
|
||||
#include "ast_pp.h"
|
||||
#include "smt_solver.h"
|
||||
#include "fu_malik.h"
|
||||
|
||||
class opt_context {
|
||||
ast_manager& m;
|
||||
expr_ref_vector m_formulas;
|
||||
vector<rational> m_weights;
|
||||
#include "opt_context.h"
|
||||
|
||||
public:
|
||||
opt_context(ast_manager& m):
|
||||
m(m),
|
||||
m_formulas(m)
|
||||
{}
|
||||
|
||||
void add_formula(expr* f, rational const& w) {
|
||||
m_formulas.push_back(f);
|
||||
m_weights.push_back(w);
|
||||
}
|
||||
|
||||
expr_ref_vector const & formulas() const { return m_formulas; }
|
||||
vector<rational> const& weights() const { return m_weights; }
|
||||
};
|
||||
|
||||
class assert_weighted_cmd : public cmd {
|
||||
opt_context* m_opt_ctx;
|
||||
opt::context* m_opt_ctx;
|
||||
unsigned m_idx;
|
||||
expr_ref m_formula;
|
||||
rational m_weight;
|
||||
|
||||
public:
|
||||
assert_weighted_cmd(cmd_context& ctx, opt_context* opt_ctx):
|
||||
assert_weighted_cmd(cmd_context& ctx, opt::context* opt_ctx):
|
||||
cmd("assert-weighted"),
|
||||
m_opt_ctx(opt_ctx),
|
||||
m_idx(0),
|
||||
|
@ -95,12 +76,11 @@ public:
|
|||
}
|
||||
|
||||
virtual void execute(cmd_context & ctx) {
|
||||
m_opt_ctx->add_formula(m_formula, m_weight);
|
||||
m_opt_ctx->add_soft_constraint(m_formula, m_weight);
|
||||
reset(ctx);
|
||||
}
|
||||
|
||||
virtual void finalize(cmd_context & ctx) {
|
||||
std::cout << "FINALIZE\n";
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -111,19 +91,16 @@ public:
|
|||
// to do the feasibility check.
|
||||
class min_maximize_cmd : public cmd {
|
||||
bool m_is_max;
|
||||
expr_ref m_term;
|
||||
opt_context* m_opt_ctx;
|
||||
opt::context* m_opt_ctx;
|
||||
|
||||
public:
|
||||
min_maximize_cmd(cmd_context& ctx, opt_context* opt_ctx, bool is_max):
|
||||
min_maximize_cmd(cmd_context& ctx, opt::context* opt_ctx, bool is_max):
|
||||
cmd(is_max?"maximize":"minimize"),
|
||||
m_is_max(is_max),
|
||||
m_term(ctx.m()),
|
||||
m_opt_ctx(opt_ctx)
|
||||
{}
|
||||
|
||||
virtual void reset(cmd_context & ctx) {
|
||||
m_term = 0;
|
||||
}
|
||||
|
||||
virtual char const * get_usage() const { return "<term>"; }
|
||||
|
@ -134,7 +111,9 @@ public:
|
|||
virtual cmd_arg_kind next_arg_kind(cmd_context & ctx) const { return CPK_EXPR; }
|
||||
|
||||
virtual void set_next_arg(cmd_context & ctx, expr * t) {
|
||||
m_term = t;
|
||||
// TODO: type check objective term. It should pass basic sanity being
|
||||
// integer, real (, bit-vector) or other supported objective function type.
|
||||
m_opt_ctx->add_objective(t, m_is_max);
|
||||
}
|
||||
|
||||
virtual void failure_cleanup(cmd_context & ctx) {
|
||||
|
@ -142,58 +121,45 @@ public:
|
|||
}
|
||||
|
||||
virtual void execute(cmd_context & ctx) {
|
||||
ast_manager& m = m_term.get_manager();
|
||||
std::cout << "TODO: " << mk_pp(m_term, ctx.m()) << "\n";
|
||||
// Here is how to retrieve the soft constraints
|
||||
expr_ref_vector const& fmls = m_opt_ctx->formulas();
|
||||
vector<rational> const& ws = m_opt_ctx->weights();
|
||||
}
|
||||
|
||||
// TODO: move most functionaltiy to separate module, because it is going to grow..
|
||||
ref<solver> s;
|
||||
symbol logic;
|
||||
params_ref p;
|
||||
p.set_bool("model", true);
|
||||
p.set_bool("unsat_core", true);
|
||||
s = mk_smt_solver(m, p, logic);
|
||||
|
||||
};
|
||||
|
||||
class optimize_cmd : public cmd {
|
||||
opt::context* m_opt_ctx;
|
||||
public:
|
||||
optimize_cmd(opt::context* opt_ctx):
|
||||
cmd("optimize"),
|
||||
m_opt_ctx(opt_ctx)
|
||||
{}
|
||||
virtual char const * get_descr(cmd_context & ctx) const { return "check sat modulo objective function";}
|
||||
virtual unsigned get_arity() const { return 0; }
|
||||
virtual void prepare(cmd_context & ctx) {}
|
||||
virtual void failure_cleanup(cmd_context & ctx) {
|
||||
reset(ctx);
|
||||
}
|
||||
|
||||
virtual void execute(cmd_context & ctx) {
|
||||
|
||||
ptr_vector<expr>::const_iterator it = ctx.begin_assertions();
|
||||
ptr_vector<expr>::const_iterator end = ctx.end_assertions();
|
||||
for (; it != end; ++it) {
|
||||
s->assert_expr(*it);
|
||||
}
|
||||
expr_ref_vector fmls_copy(fmls);
|
||||
if (is_maxsat_problem(ws)) {
|
||||
lbool is_sat = opt::fu_malik_maxsat(*s, fmls_copy);
|
||||
std::cout << "is-sat: " << is_sat << "\n";
|
||||
if (is_sat == l_true) {
|
||||
for (unsigned i = 0; i < fmls_copy.size(); ++i) {
|
||||
std::cout << mk_pp(fmls_copy[i].get(), m) << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
NOT_IMPLEMENTED_YET();
|
||||
m_opt_ctx->add_hard_constraint(*it);
|
||||
}
|
||||
m_opt_ctx->optimize();
|
||||
|
||||
// handle optimization criterion.
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
bool is_maxsat_problem(vector<rational> const& ws) const {
|
||||
for (unsigned i = 0; i < ws.size(); ++i) {
|
||||
if (!ws[i].is_one()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void install_opt_cmds(cmd_context & ctx) {
|
||||
opt_context* opt_ctx = alloc(opt_context, ctx.m());
|
||||
opt::context* opt_ctx = alloc(opt::context, ctx.m());
|
||||
ctx.insert(alloc(assert_weighted_cmd, ctx, opt_ctx));
|
||||
ctx.insert(alloc(min_maximize_cmd, ctx, opt_ctx, true));
|
||||
ctx.insert(alloc(min_maximize_cmd, ctx, opt_ctx, false));
|
||||
ctx.insert(alloc(optimize_cmd, opt_ctx));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue