3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-02-28 02:41:27 +00:00

bug fixes

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2018-02-19 21:49:03 -08:00
parent bb4888ce31
commit 4c1379e8c9
22 changed files with 238 additions and 580 deletions

View file

@ -47,16 +47,6 @@ namespace sat {
return static_cast<pb const&>(*this);
}
ba_solver::eq& ba_solver::constraint::to_eq() {
SASSERT(is_eq());
return static_cast<eq&>(*this);
}
ba_solver::eq const& ba_solver::constraint::to_eq() const{
SASSERT(is_eq());
return static_cast<eq const&>(*this);
}
ba_solver::xr& ba_solver::constraint::to_xr() {
SASSERT(is_xr());
return static_cast<xr&>(*this);
@ -95,15 +85,6 @@ namespace sat {
}
break;
}
case ba_solver::eq_t: {
ba_solver::eq const& e = cnstr.to_eq();
for (ba_solver::eliteral wl : e) {
if (wl.weight != 1) out << wl.weight << " * ";
out << wl.lit << " ";
}
out << " = " << e.k();
break;
}
default:
UNREACHABLE();
}
@ -205,33 +186,6 @@ namespace sat {
}
// -----------------------------------
// eq
ba_solver::eq::eq(unsigned id, literal lit, svector<ba_solver::wliteral> const& wlits, unsigned k):
constraint(eq_t, id, lit, wlits.size(), eq::get_obj_size(wlits.size())),
m_lo(0),
m_hi(0),
m_max(0),
m_k(k)
{
unsigned i = 0;
for (wliteral const& w : wlits) {
m_wlits[i++] = eliteral(w.first, w.second);
m_max += w.first;
}
m_hi = m_max;
m_trail_sz = 0;
}
unsigned ba_solver::eq::index(literal l) const {
for (unsigned i = 0; i < size(); ++i) {
if (l.var() == m_wlits[i].lit.var()) return i;
}
UNREACHABLE();
return UINT_MAX;
}
// -----------------------------------
// xr
@ -271,7 +225,7 @@ namespace sat {
}
if (root != null_literal) {
if (!is_watched(root, c)) watch_literal(root, c);
if (!is_watched(~root, c)) watch_literal(~root, c);
if (!c.is_pure() && !is_watched(~root, c)) watch_literal(~root, c);
}
TRACE("ba", display(tout << "init watch: ", c, true););
SASSERT(root == null_literal || value(root) == l_true);
@ -925,251 +879,6 @@ namespace sat {
out << ">= " << p.k() << "\n";
}
// --------------------
// eq:
ba_solver::constraint* ba_solver::add_eq(literal lit, svector<wliteral> const& wlits, unsigned k, bool learned) {
void * mem = m_allocator.allocate(eq::get_obj_size(wlits.size()));
eq* e = new (mem) eq(next_id(), lit, wlits, k);
e->set_learned(learned);
add_constraint(e);
for (eliteral const& wl : *e) {
watch_literal(wl.lit, *e);
watch_literal(~(wl.lit), *e);
}
return e;
}
void ba_solver::display(std::ostream& out, eq const& e, bool values) const {
display_lit(out, e.lit(), e.size(), values);
if (values) {
if (e.trail_sz() > 0) {
out << "trail: ";
for (unsigned i = 0; i < e.trail_sz(); ++i) {
out << e[i].tweight << " * " << e[i].tlit << " ";
}
}
out << "[" << e.lo() << ":" << e.hi() << "] ";
}
for (eliteral wl : e) {
literal l = wl.lit;
unsigned w = wl.weight;
if (w > 1) out << w << " * ";
out << l;
if (values) {
out << "@(" << value(l);
if (value(l) != l_undef) {
out << ":" << lvl(l);
}
out << ") ";
}
else {
out << " ";
}
}
out << "= " << e.k() << "\n";
}
bool ba_solver::init_watch(eq& e) {
return true;
}
void ba_solver::clear_watch(eq& e) {
for (eliteral const& wl : e) {
unwatch_literal(wl.lit, e);
unwatch_literal(~(wl.lit), e);
}
if (e.lit() != null_literal) {
unwatch_literal(e.lit(), e);
unwatch_literal(~e.lit(), e);
}
}
/*
* \update the bounds for [lo,hi] based on what is unassigned on the trail.
*/
void ba_solver::pop_eq(eq& e) {
unsigned idx = e.trail_sz() - 1;
literal tlit = e[idx].tlit;
if (tlit.sign()) {
e.inc_hi(e[idx].tweight);
}
else {
e.dec_lo(e[idx].tweight);
}
e.set_trail_sz(idx);
}
lbool ba_solver::add_assign(eq& e, literal nl) {
//IF_VERBOSE(0, verbose_stream() << nl << "@" << lvl(nl) << ": " << e << "\n");
SASSERT(value(nl) == l_false);
if (nl.var() == e.lit().var()) {
// no-op
}
else {
unsigned i = e.index(nl);
eliteral wl = e[i];
if (wl.lit == nl) {
e.dec_hi(wl);
}
else {
SASSERT(wl.lit == ~nl);
e.inc_lo(wl);
}
m_eq_to_pop.push_back(&e);
}
if (e.lo() > e.k() || e.hi() < e.k()) {
if (e.lit() == null_literal || value(e.lit()) == l_true) {
set_conflict(e, nl);
return l_false;
}
if (e.lit() != null_literal && value(e.lit()) == l_undef) {
assign(e, ~e.lit());
}
}
else if (e.lo() == e.hi()) {
SASSERT(e.lo() == e.k());
if (e.lit() != null_literal && value(e.lit()) == l_false) {
set_conflict(e, nl);
return l_false;
}
if (e.lit() != null_literal && value(e.lit()) == l_undef) {
assign(e, e.lit());
}
}
else if ((e.lit() == null_literal || value(e.lit()) == l_true)) {
if (e.lo() == e.k()) {
for (eliteral el : e) {
if (value(el.lit) == l_undef) {
//IF_VERBOSE(0, display(verbose_stream() << "proapgate " << ~el.lit << " ", e, true));
assign(e, ~el.lit);
}
}
}
else if (e.hi() == e.k()) {
for (eliteral el : e) {
if (value(el.lit) == l_undef) {
//IF_VERBOSE(0, display(verbose_stream() << "proapgate " << el.lit << " ", e, true));
assign(e, el.lit);
}
}
}
}
return l_true;
}
void ba_solver::simplify(eq& e) {
// no-op for now
}
void ba_solver::recompile(eq& e) {
for (eliteral const& el : e) {
m_weights[el.lit.index()] += el.weight;
}
unsigned k = e.k(), sz = e.size();
unsigned j = 0;
bool is_false = false;
for (unsigned i = 0; i < sz; ++i) {
eliteral el = e[i];
unsigned w1 = m_weights[el.lit.index()];
unsigned w2 = m_weights[(~el.lit).index()];
if (w1 == 0 || w1 < w2) continue;
if (k < w2) {
is_false = true;
break;
}
k -= w2;
w1 -= w2;
if (w1 == 0) continue;
e[j++] = eliteral(w1, el.lit);
}
sz = j;
for (eliteral const& el : e) {
m_weights[el.lit.index()] = 0;
m_weights[(~el.lit).index()] = 0;
}
if (is_false) {
if (e.lit() == null_literal) {
s().mk_clause(0, 0, true);
}
else {
literal lit = ~e.lit();
s().mk_clause(1, &lit, true);
}
remove_constraint(e, "recompiled to false");
return;
}
// update trail
e.set_size(sz);
e.set_k(k);
for (unsigned i = 0; i < sz; ++i) {
e[i].tlit = null_literal;
}
e.set_trail_sz(0);
e.reset_lo();
e.reset_hi();
for (eliteral const& el : e) {
switch (value(el.lit)) {
case l_true: e.inc_lo(el); break;
case l_false: e.dec_hi(el); break;
default: break;
}
}
}
void ba_solver::split_root(eq& e) {
NOT_IMPLEMENTED_YET();
}
void ba_solver::get_antecedents(literal l, eq const& e, literal_vector& r) {
for (eliteral wl : e) {
if (wl.lit.var() == l.var()) continue;
switch (value(wl.lit)) {
case l_true: r.push_back(wl.lit); break;
case l_false: r.push_back(~wl.lit); break;
default: break;
}
}
if (e.lit() != null_literal && l.var() != e.lit().var()) {
switch (value(e.lit())) {
case l_true: r.push_back(e.lit()); break;
case l_false: r.push_back(~e.lit()); break;
default: break;
}
}
}
lbool ba_solver::eval(eq const& e) const {
unsigned trues = 0, undefs = 0;
for (eliteral wl : e) {
switch (value(wl.lit)) {
case l_true: trues += wl.weight; break;
case l_undef: undefs += wl.weight; break;
default: break;
}
}
if (trues + undefs < e.k()) return l_false;
if (trues > e.k()) return l_false;
if (trues == e.k() && undefs == 0) return l_true;
return l_undef;
}
lbool ba_solver::eval(model const& m, eq const& e) const {
unsigned trues = 0, undefs = 0;
for (eliteral wl : e) {
switch (value(m, wl.lit)) {
case l_true: trues += wl.weight; break;
case l_undef: undefs += wl.weight; break;
default: break;
}
}
if (trues + undefs < e.k()) return l_false;
if (trues > e.k()) return l_false;
if (trues == e.k() && undefs == 0) return l_true;
return l_undef;
}
// --------------------
// xr:
@ -1512,17 +1221,6 @@ namespace sat {
for (literal l : m_lemma) process_antecedent(~l, offset);
break;
}
case eq_t: {
eq& e = cnstr.to_eq();
m_lemma.reset();
inc_bound(offset);
inc_coeff(consequent, offset);
get_antecedents(consequent, e, m_lemma);
TRACE("ba", display(tout, e, true); tout << m_lemma << "\n";);
for (literal l : m_lemma) process_antecedent(~l, offset);
break;
}
default:
UNREACHABLE();
break;
@ -1888,7 +1586,6 @@ namespace sat {
case card_t: return init_watch(c.to_card());
case pb_t: return init_watch(c.to_pb());
case xr_t: return init_watch(c.to_xr());
case eq_t: return init_watch(c.to_eq());
}
UNREACHABLE();
return false;
@ -1899,7 +1596,6 @@ namespace sat {
case card_t: return add_assign(c.to_card(), l);
case pb_t: return add_assign(c.to_pb(), l);
case xr_t: return add_assign(c.to_xr(), l);
case eq_t: return add_assign(c.to_eq(), l);
}
UNREACHABLE();
return l_undef;
@ -1950,7 +1646,7 @@ namespace sat {
init_watch(c);
return true;
}
else if (c.lit() != null_literal && value(c.lit()) != l_true && c.tag() != eq_t) {
else if (c.lit() != null_literal && value(c.lit()) != l_true) {
return true;
}
else {
@ -2005,7 +1701,6 @@ namespace sat {
case card_t: return get_reward(c.to_card(), occs);
case pb_t: return get_reward(c.to_pb(), occs);
case xr_t: return 0;
case eq_t: return 1;
default: UNREACHABLE(); return 0;
}
}
@ -2310,6 +2005,7 @@ namespace sat {
}
void ba_solver::watch_literal(literal lit, constraint& c) {
if (c.is_pure() && lit == ~c.lit()) return;
get_wlist(~lit).push_back(watched(c.index()));
}
@ -2318,7 +2014,6 @@ namespace sat {
case card_t: get_antecedents(l, c.to_card(), r); break;
case pb_t: get_antecedents(l, c.to_pb(), r); break;
case xr_t: get_antecedents(l, c.to_xr(), r); break;
case eq_t: get_antecedents(l, c.to_eq(), r); break;
default: UNREACHABLE(); break;
}
}
@ -2342,9 +2037,6 @@ namespace sat {
case xr_t:
clear_watch(c.to_xr());
break;
case eq_t:
clear_watch(c.to_eq());
break;
default:
UNREACHABLE();
}
@ -2367,7 +2059,6 @@ namespace sat {
case card_t: return validate_unit_propagation(c.to_card(), l);
case pb_t: return validate_unit_propagation(c.to_pb(), l);
case xr_t: return true;
case eq_t: return validate_unit_propagation(c.to_eq(), l);
default: UNREACHABLE(); break;
}
return false;
@ -2383,7 +2074,6 @@ namespace sat {
case card_t: return eval(v1, eval(c.to_card()));
case pb_t: return eval(v1, eval(c.to_pb()));
case xr_t: return eval(v1, eval(c.to_xr()));
case eq_t: return eval(v1, eval(c.to_eq()));
default: UNREACHABLE(); break;
}
return l_undef;
@ -2395,7 +2085,6 @@ namespace sat {
case card_t: return eval(v1, eval(m, c.to_card()));
case pb_t: return eval(v1, eval(m, c.to_pb()));
case xr_t: return eval(v1, eval(m, c.to_xr()));
case eq_t: return eval(v1, eval(m, c.to_eq()));
default: UNREACHABLE(); break;
}
return l_undef;
@ -2730,7 +2419,6 @@ namespace sat {
void ba_solver::push() {
m_constraint_to_reinit_lim.push_back(m_constraint_to_reinit.size());
m_eq_to_pop_lim.push_back(m_eq_to_pop.size());
}
void ba_solver::pop(unsigned n) {
@ -2739,13 +2427,6 @@ namespace sat {
m_constraint_to_reinit_last_sz = m_constraint_to_reinit_lim[new_lim];
m_constraint_to_reinit_lim.shrink(new_lim);
m_num_propagations_since_pop = 0;
new_lim = m_eq_to_pop_lim.size() - n;
for (unsigned i = m_eq_to_pop_lim[new_lim]; i < m_eq_to_pop.size(); ++i) {
pop_eq(*m_eq_to_pop[i]);
}
m_eq_to_pop.shrink(m_eq_to_pop_lim[new_lim]);
m_eq_to_pop_lim.shrink(new_lim);
}
void ba_solver::pop_reinit() {
@ -2772,9 +2453,6 @@ namespace sat {
case xr_t:
simplify(c.to_xr());
break;
case eq_t:
simplify(c.to_eq());
break;
default:
UNREACHABLE();
}
@ -2798,6 +2476,7 @@ namespace sat {
for (unsigned sz = m_learned.size(), i = 0; i < sz; ++i) subsumption(*m_learned[i]);
cleanup_clauses();
cleanup_constraints();
update_pure();
}
while (m_simplify_change || trail_sz < s().init_trail_size());
@ -2814,7 +2493,41 @@ namespace sat {
// IF_VERBOSE(0, s().display(verbose_stream()));
// mutex_reduction();
// if (s().m_clauses.size() < 80000) lp_lookahead_reduction();
}
/*
* ~lit does not occur in clauses
* ~lit is only in one constraint use list
* lit == C
* -> ignore assignemnts to ~lit for C
*
* ~lit does not occur in clauses
* lit is only in one constraint use list
* lit == C
* -> negate: ~lit == ~C
*/
void ba_solver::update_pure() {
// return;
for (constraint* cp : m_constraints) {
literal lit = cp->lit();
if (lit != null_literal &&
!cp->is_pure() &&
value(lit) == l_undef &&
get_wlist(~lit).size() == 1 &&
m_clause_use_list.get(lit).empty()) {
clear_watch(*cp);
cp->negate();
lit.neg();
}
if (lit != null_literal &&
!cp->is_pure() &&
m_cnstr_use_list[(~lit).index()].size() == 1 &&
get_wlist(lit).size() == 1 &&
m_clause_use_list.get(~lit).empty()) {
cp->set_pure();
get_wlist(~lit).erase(watched(cp->index())); // just ignore assignments to false
}
}
}
void ba_solver::mutex_reduction() {
@ -2930,9 +2643,6 @@ namespace sat {
case pb_t:
recompile(c.to_pb());
break;
case eq_t:
recompile(c.to_eq());
break;
case xr_t:
NOT_IMPLEMENTED_YET();
break;
@ -3144,7 +2854,6 @@ namespace sat {
case card_t: split_root(c.to_card()); break;
case pb_t: split_root(c.to_pb()); break;
case xr_t: NOT_IMPLEMENTED_YET(); break;
case eq_t: split_root(c.to_eq()); break;
}
}
@ -3265,15 +2974,6 @@ namespace sat {
}
break;
}
case eq_t: {
eq& e = cp->to_eq();
for (eliteral wl : e) {
literal l = wl.lit;
m_cnstr_use_list[l.index()].push_back(&e);
m_cnstr_use_list[(~l).index()].push_back(&e);
}
break;
}
}
}
}
@ -3296,8 +2996,6 @@ namespace sat {
}
break;
}
case eq_t:
break;
default:
break;
}
@ -3372,8 +3070,6 @@ namespace sat {
if (p.k() > 1) subsumption(p);
break;
}
case eq_t:
break;
default:
break;
}
@ -3464,30 +3160,32 @@ namespace sat {
return c1_exclusive + c2.k() + comp.size() <= c1.k();
}
bool ba_solver::subsumes(card& c1, clause& c2, literal_vector & comp) {
unsigned c2_exclusive = 0;
unsigned common = 0;
comp.reset();
/*
\brief L + A >= k subsumes L + C if |A| < k
A + L + B >= k self-subsumes A + ~L + C >= 1
if k + 1 - |B| - |C| - |L| > 0
*/
bool ba_solver::subsumes(card& c1, clause& c2, bool& self) {
unsigned common = 0, complement = 0, c2_exclusive = 0;
self = false;
for (literal l : c2) {
if (is_marked(l)) {
++common;
}
else if (is_marked(~l)) {
comp.push_back(l);
++complement;
}
else {
++c2_exclusive;
}
}
unsigned c1_exclusive = c1.size() - common - comp.size();
bool result = c1_exclusive + 1 <= c1.k();
if (!comp.empty() && result) {
IF_VERBOSE(10, verbose_stream() << "self-subsume clause " << c2 << " is TBD\n";);
return false;
unsigned c1_exclusive = c1.size() - common - complement;
if (complement > 0 && c1.k() + 1 > c1_exclusive + c2_exclusive + common) {
self = true;
return true;
}
return result;
return c1.size() - common < c1.k();
}
/*
@ -3517,8 +3215,6 @@ namespace sat {
case pb_t:
s = subsumes(p1, c->to_pb());
break;
case eq_t:
break;
default:
break;
}
@ -3582,23 +3278,22 @@ namespace sat {
void ba_solver::clause_subsumption(card& c1, literal lit, clause_vector& removed_clauses) {
SASSERT(!c1.was_removed());
literal_vector slit;
clause_use_list::iterator it = m_clause_use_list.get(lit).mk_iterator();
clause_use_list& occurs = m_clause_use_list.get(lit);
clause_use_list::iterator it = occurs.mk_iterator();
while (!it.at_end()) {
clause& c2 = it.curr();
if (!c2.was_removed() && subsumes(c1, c2, slit)) {
if (slit.empty()) {
bool self;
if (!c2.was_removed() && subsumes(c1, c2, self)) {
if (self) {
// self-subsumption is TBD
}
else {
TRACE("ba", tout << "remove\n" << c1 << "\n" << c2 << "\n";);
removed_clauses.push_back(&c2);
++m_stats.m_num_clause_subsumes;
c1.set_learned(false);
}
else {
IF_VERBOSE(11, verbose_stream() << "self-subsume clause is TBD\n";);
// remove literal slit from c2.
TRACE("ba", tout << "TBD remove literals " << slit << " from " << c2 << "\n";);
}
}
}
}
it.next();
}
}
@ -3615,7 +3310,7 @@ namespace sat {
watched w = *it;
if (w.is_binary_clause() && is_marked(w.get_literal())) {
++m_stats.m_num_bin_subsumes;
// IF_VERBOSE(10, verbose_stream() << c1 << " subsumes (" << lit << " " << w.get_literal() << ")\n";);
IF_VERBOSE(10, verbose_stream() << c1 << " subsumes (" << lit << " " << w.get_literal() << ")\n";);
if (!w.is_learned()) {
c1.set_learned(false);
}
@ -3745,15 +3440,6 @@ namespace sat {
result->add_xr(lits, x.learned());
break;
}
case eq_t: {
eq const& e = cp->to_eq();
wlits.reset();
for (eliteral w : e) {
wlits.push_back(wliteral(w.weight, w.lit));
}
result->add_eq(e.lit(), wlits, e.k(), e.learned());
break;
}
default:
UNREACHABLE();
}
@ -3791,14 +3477,6 @@ namespace sat {
}
break;
}
case eq_t: {
eq const& e = cp->to_eq();
for (eliteral w : e) {
ul.insert(w.lit, idx);
ul.insert(~(w.lit), idx);
}
break;
}
default:
UNREACHABLE();
}
@ -3840,8 +3518,6 @@ namespace sat {
}
return weight >= p.k();
}
case eq_t:
break;
default:
break;
}
@ -3963,7 +3639,6 @@ namespace sat {
case card_t: display(out, c.to_card(), values); break;
case pb_t: display(out, c.to_pb(), values); break;
case xr_t: display(out, c.to_xr(), values); break;
case eq_t: display(out, c.to_eq(), values); break;
default: UNREACHABLE(); break;
}
}
@ -4280,13 +3955,6 @@ namespace sat {
if (lxr != null_literal) ineq.push(~lxr, offset);
break;
}
case eq_t: {
eq& e = cnstr.to_eq();
ineq.reset(e.k());
for (eliteral wl : e) ineq.push(wl.lit, wl.weight);
if (e.lit() != null_literal) ineq.push(~e.lit(), e.k());
break;
}
default:
UNREACHABLE();
break;