3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-24 01:25:31 +00:00

working on incremtal PB theory

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2014-01-13 10:12:45 -08:00
parent 1f7c994e43
commit 236b2d2ff3
12 changed files with 1419 additions and 912 deletions

View file

@ -27,6 +27,7 @@ Notes:
#include "tactic.h"
#include "lia2card_tactic.h"
#include "elim01_tactic.h"
#include "solve_eqs_tactic.h"
#include "simplify_tactic.h"
#include "tactical.h"
#include "model_smt2_pp.h"
@ -299,15 +300,15 @@ namespace opt {
g->assert_expr(fmls[i].get());
}
tactic_ref tac0 = mk_simplify_tactic(m);
tactic_ref tac1 = mk_elim01_tactic(m);
tactic_ref tac2 = mk_lia2card_tactic(m);
tactic_ref tac2 = mk_elim01_tactic(m);
tactic_ref tac3 = mk_lia2card_tactic(m);
tactic_ref tac;
opt_params optp(m_params);
if (optp.elim_01()) {
tac = and_then(tac0.get(), tac1.get(), tac2.get());
tac = and_then(tac0.get(), tac2.get(), tac3.get());
}
else {
tac = tac0;
tac = tac0.get();
}
proof_converter_ref pc;
expr_dependency_ref core(m);

View file

@ -38,20 +38,26 @@ namespace smt {
stats() { reset(); }
};
opt::opt_solver& s;
app_ref_vector m_vars; // Auxiliary variables per soft clause
expr_ref_vector m_fmls; // Formulas per soft clause
opt::opt_solver& s;
mutable unsynch_mpz_manager m_mpz;
app_ref_vector m_vars; // Auxiliary variables per soft clause
expr_ref_vector m_fmls; // Formulas per soft clause
app_ref m_min_cost_atom; // atom tracking modified lower bound
app_ref_vector m_min_cost_atoms;
bool_var m_min_cost_bv; // max cost Boolean variable
vector<rational> m_weights; // weights of theory variables.
svector<theory_var> m_costs; // set of asserted theory variables
svector<theory_var> m_cost_save; // set of asserted theory variables
rational m_cost; // current sum of asserted costs
rational m_min_cost; // current maximal cost assignment.
u_map<theory_var> m_bool2var; // bool_var -> theory_var
svector<bool_var> m_var2bool; // theory_var -> bool_var
bool_var m_min_cost_bv; // max cost Boolean variable
vector<rational> m_rweights; // weights of theory variables.
scoped_mpz_vector m_zweights;
svector<theory_var> m_costs; // set of asserted theory variables
svector<theory_var> m_cost_save; // set of asserted theory variables
rational m_rcost; // current sum of asserted costs
rational m_rmin_cost; // current maximal cost assignment.
scoped_mpz m_zcost; // current sum of asserted costs
scoped_mpz m_zmin_cost; // current maximal cost assignment.
u_map<theory_var> m_bool2var; // bool_var -> theory_var
svector<bool_var> m_var2bool; // theory_var -> bool_var
bool m_propagate;
bool m_normalize;
rational m_den; // lcm of denominators for rational weights.
svector<bool> m_assigned;
stats m_stats;
@ -63,9 +69,15 @@ namespace smt {
m_fmls(m),
m_min_cost_atom(m),
m_min_cost_atoms(m),
m_propagate(false)
m_zweights(m_mpz),
m_zcost(m_mpz),
m_zmin_cost(m_mpz),
m_propagate(false),
m_normalize(false)
{}
virtual ~theory_weighted_maxsat() { }
/**
\brief return the complement of variables that are currently assigned.
*/
@ -100,14 +112,7 @@ namespace smt {
}
virtual void init_search_eh() {
context & ctx = get_context();
ast_manager& m = get_manager();
bool initialized = !m_var2bool.empty();
m_propagate = true;
for (unsigned i = 0; i < m_min_cost_atoms.size(); ++i) {
app* var = m_min_cost_atoms[i].get();
}
}
void assert_weighted(expr* fml, rational const& w) {
@ -118,12 +123,12 @@ namespace smt {
s.mc().insert(var->get_decl());
wfml = m.mk_or(var, fml);
ctx.assert_expr(wfml);
m_weights.push_back(w);
m_rweights.push_back(w);
m_vars.push_back(var);
m_fmls.push_back(fml);
m_assigned.push_back(false);
m_min_cost += w;
m_rmin_cost += w;
m_normalize = true;
register_var(var, true);
}
@ -152,15 +157,20 @@ namespace smt {
return bv;
}
rational const& get_min_cost() const {
return m_min_cost;
rational const& get_min_cost() {
unsynch_mpq_manager mgr;
scoped_mpq q(mgr);
mgr.set(q, m_zmin_cost, m_den.to_mpq().numerator());
m_rmin_cost = rational(q);
return m_rmin_cost;
}
expr* set_min_cost(rational const& c) {
m_normalize = true;
ast_manager& m = get_manager();
std::ostringstream strm;
strm << "cost <= " << c;
m_min_cost = c;
m_rmin_cost = c;
m_min_cost_atom = m.mk_fresh_const(strm.str().c_str(), m.mk_bool_sort());
m_min_cost_atoms.push_back(m_min_cost_atom);
s.mc().insert(m_min_cost_atom->get_decl());
@ -177,17 +187,18 @@ namespace smt {
virtual void assign_eh(bool_var v, bool is_true) {
TRACE("opt", tout << "Assign " << mk_pp(m_vars[m_bool2var[v]].get(), get_manager()) << " " << is_true << "\n";);
if (is_true) {
if (m_normalize) normalize();
context& ctx = get_context();
theory_var tv = m_bool2var[v];
if (m_assigned[tv]) return;
rational const& w = m_weights[tv];
ctx.push_trail(value_trail<context, rational>(m_cost));
mpz const& w = m_zweights[tv];
ctx.push_trail(value_trail<context, scoped_mpz>(m_zcost));
ctx.push_trail(push_back_vector<context, svector<theory_var> >(m_costs));
ctx.push_trail(value_trail<context, bool>(m_assigned[tv]));
m_cost += w;
m_zcost += w;
m_costs.push_back(tv);
m_assigned[tv] = true;
if (m_cost > m_min_cost) {
if (m_zcost > m_zmin_cost) {
block();
}
}
@ -213,10 +224,13 @@ namespace smt {
theory::reset_eh();
m_vars.reset();
m_fmls.reset();
m_weights.reset();
m_rweights.reset();
m_costs.reset();
m_min_cost.reset();
m_cost.reset();
m_rmin_cost.reset();
m_rcost.reset();
m_zweights.reset();
m_zcost.reset();
m_zmin_cost.reset();
m_cost_save.reset();
m_bool2var.reset();
m_var2bool.reset();
@ -254,7 +268,7 @@ namespace smt {
}
bool is_optimal() const {
return m_cost < m_min_cost;
return m_mpz.lt(m_zcost, m_zmin_cost);
}
expr_ref mk_block() {
@ -263,17 +277,22 @@ namespace smt {
compare_cost compare_cost(*this);
svector<theory_var> costs(m_costs);
std::sort(costs.begin(), costs.end(), compare_cost);
rational weight(0);
for (unsigned i = 0; i < costs.size() && weight < m_min_cost; ++i) {
weight += m_weights[costs[i]];
scoped_mpz weight(m_mpz);
m_mpz.reset(weight);
for (unsigned i = 0; i < costs.size() && m_mpz.lt(weight, m_zmin_cost); ++i) {
weight += m_zweights[costs[i]];
disj.push_back(m.mk_not(m_vars[costs[i]].get()));
}
if (m_min_cost_atom) {
disj.push_back(m.mk_not(m_min_cost_atom));
}
if (is_optimal()) {
IF_VERBOSE(1, verbose_stream() << "(wmaxsat with upper bound: " << weight << ")\n";);
m_min_cost = weight;
unsynch_mpq_manager mgr;
scoped_mpq q(mgr);
mgr.set(q, m_zmin_cost, m_den.to_mpq().numerator());
rational rw = rational(q);
IF_VERBOSE(1, verbose_stream() << "(wmaxsat with upper bound: " << rw << ")\n";);
m_zmin_cost = weight;
m_cost_save.reset();
m_cost_save.append(m_costs);
}
@ -307,9 +326,11 @@ namespace smt {
compare_cost compare_cost(*this);
svector<theory_var> costs(m_costs);
std::sort(costs.begin(), costs.end(), compare_cost);
rational weight(0);
for (unsigned i = 0; i < costs.size() && weight < m_min_cost; ++i) {
weight += m_weights[costs[i]];
scoped_mpz weight(m_mpz);
m_mpz.reset(weight);
for (unsigned i = 0; i < costs.size() && weight < m_zmin_cost; ++i) {
weight += m_zweights[costs[i]];
lits.push_back(~literal(m_var2bool[costs[i]]));
}
if (m_min_cost_atom) {
@ -328,13 +349,33 @@ namespace smt {
ctx.mk_th_axiom(get_id(), lits.size(), lits.c_ptr());
}
void normalize() {
m_den = rational::one();
for (unsigned i = 0; i < m_rweights.size(); ++i) {
m_den = lcm(m_den, denominator(m_rweights[i]));
}
m_den = lcm(m_den, denominator(m_rmin_cost));
m_zweights.reset();
for (unsigned i = 0; i < m_rweights.size(); ++i) {
rational r = m_rweights[i]*m_den;
SASSERT(r.is_int());
mpq const& q = r.to_mpq();
m_zweights.push_back(q.numerator());
}
rational r = m_rcost* m_den;
m_zcost = r.to_mpq().numerator();
r = m_rmin_cost * m_den;
m_zmin_cost = r.to_mpq().numerator();
m_normalize = false;
}
class compare_cost {
theory_weighted_maxsat& m_th;
public:
compare_cost(theory_weighted_maxsat& t):m_th(t) {}
bool operator() (theory_var v, theory_var w) const {
return m_th.m_weights[v] > m_th.m_weights[w];
return m_th.m_mpz.gt(m_th.m_zweights[v], m_th.m_zweights[w]);
}
};
};