mirror of
https://github.com/Z3Prover/z3
synced 2025-07-31 08:23:17 +00:00
Merge branch 'opt' of https://git01.codeplex.com/z3 into opt
This commit is contained in:
commit
3a3f93c4a5
7 changed files with 115 additions and 43 deletions
|
@ -1119,7 +1119,6 @@ void cmd_context::insert_aux_pdecl(pdecl * p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmd_context::reset(bool finalize) {
|
void cmd_context::reset(bool finalize) {
|
||||||
m_check_sat_result = 0;
|
|
||||||
m_logic = symbol::null;
|
m_logic = symbol::null;
|
||||||
m_check_sat_result = 0;
|
m_check_sat_result = 0;
|
||||||
m_numeral_as_real = false;
|
m_numeral_as_real = false;
|
||||||
|
|
|
@ -15,18 +15,11 @@ Author:
|
||||||
Notes:
|
Notes:
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
- integrate with parameters.
|
|
||||||
Parameter infrastructure lets us control setttings, such as
|
|
||||||
timeouts and control which backend optimization approach to
|
|
||||||
use during experiments.
|
|
||||||
|
|
||||||
- Display statistics properly on exit when configured to do so.
|
- Add appropriate statistics tracking to opt::context
|
||||||
Also add appropriate statistics tracking to opt::context
|
|
||||||
|
|
||||||
- Deal with push/pop (later)
|
- Deal with push/pop (later)
|
||||||
|
|
||||||
- Revisit weighted constraints if we want to group them using identifiers.
|
|
||||||
|
|
||||||
--*/
|
--*/
|
||||||
#include "opt_cmds.h"
|
#include "opt_cmds.h"
|
||||||
#include "cmd_context.h"
|
#include "cmd_context.h"
|
||||||
|
@ -34,6 +27,8 @@ Notes:
|
||||||
#include "opt_context.h"
|
#include "opt_context.h"
|
||||||
#include "cancel_eh.h"
|
#include "cancel_eh.h"
|
||||||
#include "scoped_ctrl_c.h"
|
#include "scoped_ctrl_c.h"
|
||||||
|
#include "scoped_timer.h"
|
||||||
|
#include "parametric_cmd.h"
|
||||||
|
|
||||||
|
|
||||||
class opt_context {
|
class opt_context {
|
||||||
|
@ -152,30 +147,49 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class optimize_cmd : public cmd {
|
class optimize_cmd : public parametric_cmd {
|
||||||
opt_context& m_opt_ctx;
|
opt_context& m_opt_ctx;
|
||||||
public:
|
public:
|
||||||
optimize_cmd(opt_context& opt_ctx):
|
optimize_cmd(opt_context& opt_ctx):
|
||||||
cmd("optimize"),
|
parametric_cmd("optimize"),
|
||||||
m_opt_ctx(opt_ctx)
|
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 init_pdescrs(cmd_context & ctx, param_descrs & p) {
|
||||||
virtual void prepare(cmd_context & ctx) {}
|
insert_timeout(p);
|
||||||
|
insert_max_memory(p);
|
||||||
|
p.insert("print_statistics", CPK_BOOL, "(default: false) print statistics.");
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual char const * get_main_descr() const { return "check sat modulo objective function";}
|
||||||
|
virtual char const * get_usage() const { return "(<keyword> <value>)*"; }
|
||||||
|
virtual void prepare(cmd_context & ctx) {
|
||||||
|
parametric_cmd::prepare(ctx);
|
||||||
|
}
|
||||||
virtual void failure_cleanup(cmd_context & ctx) {
|
virtual void failure_cleanup(cmd_context & ctx) {
|
||||||
reset(ctx);
|
reset(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual cmd_arg_kind next_arg_kind(cmd_context & ctx) const {
|
||||||
|
return parametric_cmd::next_arg_kind(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
virtual void execute(cmd_context & ctx) {
|
virtual void execute(cmd_context & ctx) {
|
||||||
|
params_ref p = ctx.params().merge_default_params(ps());
|
||||||
opt::context& opt = m_opt_ctx();
|
opt::context& opt = m_opt_ctx();
|
||||||
|
opt.updt_params(p);
|
||||||
|
unsigned timeout = p.get_uint("timeout", UINT_MAX);
|
||||||
|
|
||||||
ptr_vector<expr>::const_iterator it = ctx.begin_assertions();
|
ptr_vector<expr>::const_iterator it = ctx.begin_assertions();
|
||||||
ptr_vector<expr>::const_iterator end = ctx.end_assertions();
|
ptr_vector<expr>::const_iterator end = ctx.end_assertions();
|
||||||
for (; it != end; ++it) {
|
for (; it != end; ++it) {
|
||||||
opt.add_hard_constraint(*it);
|
opt.add_hard_constraint(*it);
|
||||||
}
|
}
|
||||||
cancel_eh<opt::context> eh(opt);
|
cancel_eh<opt::context> eh(opt);
|
||||||
{
|
{
|
||||||
scoped_ctrl_c ctrlc(eh);
|
scoped_ctrl_c ctrlc(eh);
|
||||||
|
scoped_timer timer(timeout, &eh);
|
||||||
cmd_context::scoped_watch sw(ctx);
|
cmd_context::scoped_watch sw(ctx);
|
||||||
try {
|
try {
|
||||||
opt.optimize();
|
opt.optimize();
|
||||||
|
@ -187,6 +201,21 @@ public:
|
||||||
ctx.regular_stream() << "(error: " << ex.msg() << "\")" << std::endl;
|
ctx.regular_stream() << "(error: " << ex.msg() << "\")" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (p.get_bool("print_statistics", false)) {
|
||||||
|
display_statistics(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
|
||||||
|
void display_statistics(cmd_context& ctx) {
|
||||||
|
statistics stats;
|
||||||
|
unsigned long long max_mem = memory::get_max_used_memory();
|
||||||
|
unsigned long long mem = memory::get_allocation_size();
|
||||||
|
stats.update("time", ctx.get_seconds());
|
||||||
|
stats.update("memory", static_cast<double>(mem)/static_cast<double>(1024*1024));
|
||||||
|
stats.update("max memory", static_cast<double>(max_mem)/static_cast<double>(1024*1024));
|
||||||
|
m_opt_ctx().collect_statistics(stats);
|
||||||
|
stats.display_smt2(ctx.regular_stream());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,6 @@ Notes:
|
||||||
#include "opt_context.h"
|
#include "opt_context.h"
|
||||||
#include "fu_malik.h"
|
#include "fu_malik.h"
|
||||||
#include "weighted_maxsat.h"
|
#include "weighted_maxsat.h"
|
||||||
#include "optimize_objectives.h"
|
|
||||||
#include "ast_pp.h"
|
#include "ast_pp.h"
|
||||||
#include "opt_solver.h"
|
#include "opt_solver.h"
|
||||||
#include "arith_decl_plugin.h"
|
#include "arith_decl_plugin.h"
|
||||||
|
@ -35,16 +34,24 @@ Notes:
|
||||||
|
|
||||||
namespace opt {
|
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_params.set_bool("model", true);
|
||||||
|
m_params.set_bool("unsat_core", true);
|
||||||
|
}
|
||||||
|
|
||||||
void context::optimize() {
|
void context::optimize() {
|
||||||
|
|
||||||
expr_ref_vector const& fmls = m_soft_constraints;
|
expr_ref_vector const& fmls = m_soft_constraints;
|
||||||
|
|
||||||
if (!m_solver) {
|
if (!m_solver) {
|
||||||
symbol logic;
|
symbol logic;
|
||||||
params_ref p;
|
set_solver(alloc(opt_solver, m, m_params, logic));
|
||||||
p.set_bool("model", true);
|
|
||||||
p.set_bool("unsat_core", true);
|
|
||||||
set_solver(alloc(opt_solver, m, p, logic));
|
|
||||||
}
|
}
|
||||||
solver* s = m_solver.get();
|
solver* s = m_solver.get();
|
||||||
|
|
||||||
|
@ -76,8 +83,7 @@ namespace opt {
|
||||||
for (unsigned i = 0; i < fmls_copy.size(); ++i) {
|
for (unsigned i = 0; i < fmls_copy.size(); ++i) {
|
||||||
s->assert_expr(fmls_copy[i].get());
|
s->assert_expr(fmls_copy[i].get());
|
||||||
}
|
}
|
||||||
optimize_objectives obj(m, get_opt_solver(*s)); // TBD: make an attribute
|
is_sat = m_opt_objectives(get_opt_solver(*s), m_objectives, values);
|
||||||
is_sat = obj(m_objectives, values);
|
|
||||||
std::cout << "is-sat: " << is_sat << std::endl;
|
std::cout << "is-sat: " << is_sat << std::endl;
|
||||||
|
|
||||||
if (is_sat != l_true) {
|
if (is_sat != l_true) {
|
||||||
|
@ -119,12 +125,14 @@ namespace opt {
|
||||||
if (m_solver) {
|
if (m_solver) {
|
||||||
m_solver->cancel();
|
m_solver->cancel();
|
||||||
}
|
}
|
||||||
|
m_opt_objectives.set_cancel(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void context::reset_cancel() {
|
void context::reset_cancel() {
|
||||||
if (m_solver) {
|
if (m_solver) {
|
||||||
m_solver->reset_cancel();
|
m_solver->reset_cancel();
|
||||||
}
|
}
|
||||||
|
m_opt_objectives.set_cancel(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void context::add_objective(app* t, bool is_max) {
|
void context::add_objective(app* t, bool is_max) {
|
||||||
|
@ -140,5 +148,19 @@ namespace opt {
|
||||||
m_is_max.push_back(is_max);
|
m_is_max.push_back(is_max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void context::collect_statistics(statistics& stats) {
|
||||||
|
if (m_solver) {
|
||||||
|
m_solver->collect_statistics(stats);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void context::updt_params(params_ref& p) {
|
||||||
|
m_params.append(p);
|
||||||
|
if (m_solver) {
|
||||||
|
m_solver->updt_params(m_params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ Notes:
|
||||||
|
|
||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
#include "solver.h"
|
#include "solver.h"
|
||||||
|
#include "optimize_objectives.h"
|
||||||
|
|
||||||
namespace opt {
|
namespace opt {
|
||||||
|
|
||||||
|
@ -40,14 +41,10 @@ namespace opt {
|
||||||
app_ref_vector m_objectives;
|
app_ref_vector m_objectives;
|
||||||
svector<bool> m_is_max;
|
svector<bool> m_is_max;
|
||||||
ref<solver> m_solver;
|
ref<solver> m_solver;
|
||||||
|
params_ref m_params;
|
||||||
|
optimize_objectives m_opt_objectives;
|
||||||
public:
|
public:
|
||||||
context(ast_manager& m):
|
context(ast_manager& m);
|
||||||
m(m),
|
|
||||||
m_hard_constraints(m),
|
|
||||||
m_soft_constraints(m),
|
|
||||||
m_objectives(m)
|
|
||||||
{}
|
|
||||||
|
|
||||||
void add_soft_constraint(expr* f, rational const& w) {
|
void add_soft_constraint(expr* f, rational const& w) {
|
||||||
m_soft_constraints.push_back(f);
|
m_soft_constraints.push_back(f);
|
||||||
|
@ -69,6 +66,10 @@ namespace opt {
|
||||||
void cancel();
|
void cancel();
|
||||||
void reset_cancel();
|
void reset_cancel();
|
||||||
|
|
||||||
|
void collect_statistics(statistics& stats);
|
||||||
|
|
||||||
|
void updt_params(params_ref& p);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool is_maxsat_problem() const;
|
bool is_maxsat_problem() const;
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,23 @@
|
||||||
|
/*++
|
||||||
|
Copyright (c) 2013 Microsoft Corporation
|
||||||
|
|
||||||
|
Module Name:
|
||||||
|
|
||||||
|
opt_solver.cpp
|
||||||
|
|
||||||
|
Abstract:
|
||||||
|
|
||||||
|
Wraps smt::kernel as a solver for optimization
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
Anh-Dung Phan (t-anphan) 2013-10-16
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
Based directly on smt_solver.
|
||||||
|
|
||||||
|
--*/
|
||||||
#include"reg_decl_plugins.h"
|
#include"reg_decl_plugins.h"
|
||||||
#include"opt_solver.h"
|
#include"opt_solver.h"
|
||||||
#include"smt_context.h"
|
#include"smt_context.h"
|
||||||
|
|
|
@ -67,28 +67,28 @@ namespace opt {
|
||||||
ast_manager& m = objectives.get_manager();
|
ast_manager& m = objectives.get_manager();
|
||||||
arith_util autil(m);
|
arith_util autil(m);
|
||||||
|
|
||||||
s.reset_objectives();
|
s->reset_objectives();
|
||||||
values.reset();
|
values.reset();
|
||||||
// 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_false) {
|
if (is_sat == l_false) {
|
||||||
return is_sat;
|
return is_sat;
|
||||||
}
|
}
|
||||||
|
|
||||||
opt_solver::scoped_push _push(s);
|
opt_solver::scoped_push _push(*s);
|
||||||
|
|
||||||
opt_solver::toggle_objective _t(s, true);
|
opt_solver::toggle_objective _t(*s, true);
|
||||||
|
|
||||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||||
s.add_objective(objectives[i].get());
|
s->add_objective(objectives[i].get());
|
||||||
values.push_back(inf_eps(rational(-1),inf_rational(0)));
|
values.push_back(inf_eps(rational(-1),inf_rational(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
is_sat = s.check_sat(0, 0);
|
is_sat = s->check_sat(0, 0);
|
||||||
|
|
||||||
|
|
||||||
while (is_sat == l_true && !m_cancel) {
|
while (is_sat == l_true && !m_cancel) {
|
||||||
set_max(values, s.get_objective_values());
|
set_max(values, s->get_objective_values());
|
||||||
IF_VERBOSE(1,
|
IF_VERBOSE(1,
|
||||||
for (unsigned i = 0; i < values.size(); ++i) {
|
for (unsigned i = 0; i < values.size(); ++i) {
|
||||||
verbose_stream() << values[i] << " ";
|
verbose_stream() << values[i] << " ";
|
||||||
|
@ -99,11 +99,11 @@ namespace opt {
|
||||||
|
|
||||||
for (unsigned i = 0; i < objectives.size(); ++i) {
|
for (unsigned i = 0; i < objectives.size(); ++i) {
|
||||||
inf_eps const& v = values[i];
|
inf_eps const& v = values[i];
|
||||||
disj.push_back(s.block_lower_bound(i, v));
|
disj.push_back(s->block_lower_bound(i, v));
|
||||||
}
|
}
|
||||||
constraint = m.mk_or(disj.size(), disj.c_ptr());
|
constraint = m.mk_or(disj.size(), disj.c_ptr());
|
||||||
s.assert_expr(constraint);
|
s->assert_expr(constraint);
|
||||||
is_sat = s.check_sat(0, 0);
|
is_sat = s->check_sat(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,7 +117,8 @@ 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()(app_ref_vector& objectives, vector<inf_eps>& values) {
|
lbool optimize_objectives::operator()(opt_solver& solver, app_ref_vector& objectives, vector<inf_eps>& values) {
|
||||||
|
s = &solver;
|
||||||
return basic_opt(objectives, values);
|
return basic_opt(objectives, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,12 +29,12 @@ 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;
|
||||||
public:
|
public:
|
||||||
optimize_objectives(ast_manager& m, opt_solver& s): m(m), s(s), m_cancel(false) {}
|
optimize_objectives(ast_manager& m): m(m), s(0), m_cancel(false) {}
|
||||||
|
|
||||||
lbool operator()(app_ref_vector& objectives, vector<inf_eps>& values);
|
lbool operator()(opt_solver& s, app_ref_vector& objectives, vector<inf_eps>& values);
|
||||||
|
|
||||||
void set_cancel(bool f);
|
void set_cancel(bool f);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue