3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-04-26 22:03:34 +00:00

viable revisit v1

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2023-12-26 14:10:43 -08:00
parent 9cd838f705
commit 6466345755
6 changed files with 638 additions and 43 deletions

View file

@ -20,6 +20,8 @@ Notes:
#include "util/log.h"
#include "sat/smt/polysat/viable.h"
#include "sat/smt/polysat/core.h"
#include "sat/smt/polysat/number.h"
#include "sat/smt/polysat/refine.h"
#include "sat/smt/polysat/ule_constraint.h"
namespace polysat {
@ -134,20 +136,21 @@ namespace polysat {
// Refine?
//
lbool viable::next_viable(rational& val) {
unsigned rounds = 0;
do {
if (rounds > 10)
return l_undef;
++rounds;
rational val0 = val;
auto r = next_viable_unit(val);
if (r != l_true)
return r;
if (val0 != val)
continue;
if (!m_fixed_bits.next(val))
return l_false;
if (val0 != val)
if (refine_equal_lin(m_var, val))
continue;
if (refine_disequal_lin(m_var, val))
continue;
r = next_viable_non_unit(val);
if (r != l_true)
return r;
if (val0 != val)
continue;
}
@ -157,10 +160,9 @@ namespace polysat {
//
//
// from smallest overlap [w] to largest
// from smallest layer [bit_width, entries] to largest
// check if val is allowed by entries
//
// from smallest size(w) overlap [w] to largest
// from smallest bit_width layer [bit_width, entries] to largest
// check if val is allowed by entries or advance val to next allowed value
//
lbool viable::next_viable_unit(rational& val) {
for (auto const& [w, offset] : m_overlaps) {
@ -172,7 +174,7 @@ namespace polysat {
}
lbool viable::next_viable_overlap(pvar w, rational& val) {
for (auto const& layer : m_units[w].get_layers()) {
for (auto& layer : m_units[w].get_layers()) {
auto r = next_viable_layer(w, layer, val);
if (r != l_true)
return r;
@ -180,43 +182,341 @@ namespace polysat {
return l_true;
}
lbool viable::next_viable_layer(pvar w, layer const& layer, rational& val) {
unsigned num_bits_w = c.size(w);
unsigned num_bits_l = layer.bit_width;
/*
* v in [lo, hi[:
* - hi >= v: forward(v) := hi
* - hi < v: l_false
* 2^k v in [lo, hi[:
* - hi > 2^k v: forward(v) := hi//2^k + 2^k(v//2^k)
* - hi <= 2^k v: forward(v) := hi//2^k + 2^k(v//2^k + 1) unless it overflows.
* w is a suffix of v of width w.width <= v.width with forbidden 2^l w not in [lo, hi[ and 2^l v[w.width-1:0] in [lo, hi[.
* - set k := l + v.width - w.width, lo' := 2^{v.width-w.width} lo, hi' := 2^{v.width-w.width} hi.
*/
lbool viable::next_viable_layer(pvar w, layer& layer, rational& val) {
unsigned v_width = m_num_bits;
unsigned w_width = c.size(w);
unsigned l = w_width - layer.bit_width;
SASSERT(v_width >= w_width);
SASSERT(layer.bit_width <= w_width);
auto is_before = [&](entry* e) {
return false;
};
auto is_after = [&](entry* e) {
return false;
};
auto is_conflicting = [&](entry* e) {
return false;
};
auto increase_value = [&](entry* e) {
};
auto first = layer.entries, e = first;
do {
if (is_conflicting(e))
increase_value(e);
if (is_before(e))
e = e->next();
else if (is_after(e))
bool is_zero = val.is_zero(), wrapped = false;
rational val1 = val;
rational const& p2l = rational::power_of_two(l);
rational const& p2w = rational::power_of_two(w_width);
while (true) {
if (l > 0)
val1 *= p2l;
if (w_width < v_width || l > 0)
val1 = mod(val1, p2w);
auto e = find_overlap(val1, layer.entries);
if (!e) {
if (l > 0)
val1 /= p2l;
break;
}
// TODO check if admitted: layer.entries = e;
m_explain.push_back(e);
auto hi = e->interval.hi_val();
if (hi < val1) {
if (is_zero)
return l_false;
if (w_width == v_width && l == 0)
return l_false;
// start from 0 and find the next viable value within this layer.
val1 = 0;
wrapped = true;
}
val1 = hi;
SASSERT(val1 < p2w);
// p2l * x = val1 = hi
if (l > 0)
val1 = hi / p2l;
SASSERT(val1.is_int());
}
SASSERT(val1 < p2w);
if (w_width < v_width) {
if (l > 0)
NOT_IMPLEMENTED_YET();
rational val2 = val;
if (wrapped) {
val2 = mod(div(val2, p2w) + 1, p2w) * p2w;
if (val2 == 0)
return l_false;
}
else
return l_false;
} while (e != first);
val2 = clear_lower_bits(val2, w_width);
val = val1 + val2;
}
else if (l > 0) {
NOT_IMPLEMENTED_YET();
}
else
val = val1;
return l_true;
}
// Find interval that contains 'val', or, if no such interval exists, null.
viable::entry* viable::find_overlap(rational const& val, entry* entries) {
SASSERT(entries);
// display_all(std::cerr << "entries:\n\t", 0, entries, "\n\t");
entry* const first = entries;
entry* e = entries;
do {
auto const& i = e->interval;
if (i.currently_contains(val))
return e;
entry* const n = e->next();
// there is only one interval, and it does not contain 'val'
if (e == n)
return nullptr;
// check whether 'val' is contained in the gap between e and n
bool const overlapping = e->interval.currently_contains(n->interval.lo_val());
if (!overlapping && r_interval::contains(e->interval.hi_val(), n->interval.lo_val(), val))
return nullptr;
e = n;
}
while (e != first);
UNREACHABLE();
return nullptr;
}
lbool viable::next_viable_non_unit(rational& val) {
return l_undef;
bool viable::refine_equal_lin(pvar v, rational const& val) {
// LOG_H2("refine-equal-lin with v" << v << ", val = " << val);
entry const* e = m_equal_lin[v];
if (!e)
return true;
entry const* first = e;
auto& m = c.var2pdd(v);
unsigned const N = m.power_of_2();
rational const& max_value = m.max_value();
rational const& mod_value = m.two_to_N();
SASSERT(0 <= val && val <= max_value);
// Rotate the 'first' entry, to prevent getting stuck in a refinement loop
// with an early entry when a later entry could give a better interval.
m_equal_lin[v] = m_equal_lin[v]->next();
do {
rational coeff_val = mod(e->coeff * val, mod_value);
if (e->interval.currently_contains(coeff_val)) {
// IF_LOGGING(
// verbose_stream() << "refine-equal-lin for v" << v << " in src: ";
// for (const auto& src : e->src)
// verbose_stream() << lit_pp(s, src) << "\n";
// );
// LOG("forbidden interval v" << v << " " << num_pp(s, v, val) << " " << num_pp(s, v, e->coeff, true) << " * " << e->interval);
if (mod(e->interval.hi_val() + 1, mod_value) == e->interval.lo_val()) {
// We have an equation: a * v == b
rational const a = e->coeff;
rational const b = e->interval.hi_val();
LOG("refine-equal-lin: equation detected: " << dd::val_pp(m, a, true) << " * v" << v << " == " << dd::val_pp(m, b, false));
unsigned const parity_a = get_parity(a, N);
unsigned const parity_b = get_parity(b, N);
if (parity_a > parity_b) {
// No solution
LOG("refined: no solution due to parity");
entry* ne = alloc_entry(v, e->constraint_index);
ne->refined = true;
ne->src = e->src;
ne->side_cond = e->side_cond;
ne->coeff = 1;
ne->bit_width = e->bit_width;
ne->interval = eval_interval::full();
intersect(v, ne);
return false;
}
if (parity_a == 0) {
// "fast path" for odd a
rational a_inv;
VERIFY(a.mult_inverse(N, a_inv));
rational const hi = mod(a_inv * b, mod_value);
rational const lo = mod(hi + 1, mod_value);
// LOG("refined to [" << num_pp(c, v, lo) << ", " << num_pp(c, v, hi) << "[");
SASSERT_EQ(mod(a * hi, mod_value), b); // hi is the solution
entry* ne = alloc_entry(v, e->constraint_index);
ne->refined = true;
ne->src = e->src;
ne->side_cond = e->side_cond;
ne->coeff = 1;
ne->bit_width = e->bit_width;
ne->interval = eval_interval::proper(m.mk_val(lo), lo, m.mk_val(hi), hi);
SASSERT(ne->interval.currently_contains(val));
intersect(v, ne);
return false;
}
// 2^k * v == a_inv * b
// 2^k solutions because only the lower N-k bits of v are fixed.
//
// Smallest solution is v0 == a_inv * (b >> k)
// Solutions are of the form v_i = v0 + 2^(N-k) * i for i in { 0, 1, ..., 2^k - 1 }.
// Forbidden intervals: [v_i + 1; v_{i+1}[ == [ v_i + 1; v_i + 2^(N-k) [
// We need the interval that covers val:
// v_i + 1 <= val < v_i + 2^(N-k)
//
// TODO: create one interval for v[N-k:] instead of 2^k intervals for v.
unsigned const k = parity_a;
rational const a_inv = a.pseudo_inverse(N);
unsigned const N_minus_k = N - k;
rational const two_to_N_minus_k = rational::power_of_two(N_minus_k);
rational const v0 = mod(a_inv * machine_div2k(b, k), two_to_N_minus_k);
SASSERT(mod(val, two_to_N_minus_k) != v0); // val is not a solution
rational const vi = v0 + clear_lower_bits(mod(val - v0, mod_value), N_minus_k);
rational const lo = mod(vi + 1, mod_value);
rational const hi = mod(vi + two_to_N_minus_k, mod_value);
// LOG("refined to [" << num_pp(c, v, lo) << ", " << num_pp(c, v, hi) << "[");
SASSERT_EQ(mod(a * (lo - 1), mod_value), b); // lo-1 is a solution
SASSERT_EQ(mod(a * hi, mod_value), b); // hi is a solution
entry* ne = alloc_entry(v, e->constraint_index);
ne->refined = true;
ne->src = e->src;
ne->side_cond = e->side_cond;
ne->coeff = 1;
ne->bit_width = e->bit_width;
ne->interval = eval_interval::proper(m.mk_val(lo), lo, m.mk_val(hi), hi);
SASSERT(ne->interval.currently_contains(val));
intersect(v, ne);
return false;
}
// TODO: special handling for the even factors of e->coeff = 2^k * a', a' odd
// (create one interval for v[N-k:] instead of 2^k intervals for v)
// TODO: often, the intervals alternate between short forbidden and short allowed intervals.
// we should be able to handle this similarly to compute_y_bounds,
// and be able to represent such periodic intervals (inside safe bounds).
//
// compute_y_bounds calculates with inclusive upper bound, so we need to adjust argument and result accordingly.
rational const hi_val_incl = e->interval.hi_val().is_zero() ? max_value : (e->interval.hi_val() - 1);
auto [lo, hi] = refine_equal::compute_y_bounds(val, e->coeff, e->interval.lo_val(), hi_val_incl, mod_value);
hi += 1;
//LOG("refined to [" << num_pp(c, v, lo) << ", " << num_pp(c, v, hi) << "[");
// verbose_stream() << "lo=" << lo << " val=" << val << " hi=" << hi << "\n";
if (lo <= hi) {
SASSERT(0 <= lo && lo <= val && val < hi && hi <= mod_value);
}
else {
SASSERT(0 < hi && hi < lo && lo < mod_value && (val < hi || lo <= val));
}
bool full = (lo == 0 && hi == mod_value);
if (hi == mod_value)
hi = 0;
entry* ne = alloc_entry(v, e->constraint_index);
ne->refined = true;
ne->src = e->src;
ne->side_cond = e->side_cond;
ne->coeff = 1;
ne->bit_width = e->bit_width;
if (full)
ne->interval = eval_interval::full();
else
ne->interval = eval_interval::proper(m.mk_val(lo), lo, m.mk_val(hi), hi);
SASSERT(ne->interval.currently_contains(val));
intersect(v, ne);
return false;
}
e = e->next();
} while (e != first);
return true;
}
bool viable::refine_disequal_lin(pvar v, rational const& val) {
// LOG_H2("refine-disequal-lin with v" << v << ", val = " << val);
entry const* e = m_diseq_lin[v];
if (!e)
return true;
entry const* first = e;
auto& m = c.var2pdd(v);
rational const& max_value = m.max_value();
rational const& mod_value = m.two_to_N();
SASSERT(0 <= val && val <= max_value);
// Rotate the 'first' entry, to prevent getting stuck in a refinement loop
// with an early entry when a later entry could give a better interval.
m_diseq_lin[v] = m_diseq_lin[v]->next();
do {
// IF_LOGGING(
// verbose_stream() << "refine-disequal-lin for v" << v << " in src: ";
// for (const auto& src : e->src)
// verbose_stream() << lit_pp(s, src) << "\n";
// );
// We compute an interval if the concrete value 'val' violates the constraint:
// p*val + q > r*val + s if e->src.is_positive()
// p*val + q >= r*val + s if e->src.is_negative()
// Note that e->interval is meaningless in this case,
// we just use it to transport the values p,q,r,s
rational const& p = e->interval.lo_val();
rational const& q_ = e->interval.lo().val();
rational const& r = e->interval.hi_val();
rational const& s_ = e->interval.hi().val();
SASSERT(p != r && p != 0 && r != 0);
SASSERT(e->src.size() == 1);
rational const a = mod(p * val + q_, mod_value);
rational const b = mod(r * val + s_, mod_value);
rational const np = mod_value - p;
rational const nr = mod_value - r;
int const corr = e->src[0].is_negative() ? 1 : 0;
auto delta_l = [&](rational const& val) {
rational num = a - b + corr;
rational l1 = floor(b / r);
rational l2 = val;
if (p > r)
l2 = ceil(num / (p - r)) - 1;
rational l3 = ceil(num / (p + nr)) - 1;
rational l4 = ceil((mod_value - a) / np) - 1;
rational d1 = l3;
rational d2 = std::min(l1, l2);
rational d3 = std::min(l1, l4);
rational d4 = std::min(l2, l4);
rational dmax = std::max(std::max(d1, d2), std::max(d3, d4));
return std::min(val, dmax);
};
auto delta_u = [&](rational const& val) {
rational num = a - b + corr;
rational h1 = floor(b / nr);
rational h2 = max_value - val;
if (r > p)
h2 = ceil(num / (r - p)) - 1;
rational h3 = ceil(num / (np + r)) - 1;
rational h4 = ceil((mod_value - a) / p) - 1;
rational d1 = h3;
rational d2 = std::min(h1, h2);
rational d3 = std::min(h1, h4);
rational d4 = std::min(h2, h4);
rational dmax = std::max(std::max(d1, d2), std::max(d3, d4));
return std::min(max_value - val, dmax);
};
if (a > b || (e->src[0].is_negative() && a == b)) {
rational lo = val - delta_l(val);
rational hi = val + delta_u(val) + 1;
LOG("refine-disequal-lin: " << " [" << lo << ", " << hi << "[");
SASSERT(0 <= lo && lo <= val);
SASSERT(val <= hi && hi <= mod_value);
if (hi == mod_value) hi = 0;
pdd lop = c.var2pdd(v).mk_val(lo);
pdd hip = c.var2pdd(v).mk_val(hi);
entry* ne = alloc_entry(v, e->constraint_index);
ne->refined = true;
ne->src = e->src;
ne->side_cond = e->side_cond;
ne->coeff = 1;
ne->bit_width = e->bit_width;
ne->interval = eval_interval::proper(lop, lo, hip, hi);
intersect(v, ne);
return false;
}
e = e->next();
} while (e != first);
return true;
}
/*