3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-10-16 20:40:27 +00:00

Bit-Propagation for most operations (Backtracking missing)

This commit is contained in:
Clemens Eisenhofer 2022-12-24 16:37:53 +01:00
parent c8b9127028
commit 173fb9c2bd
7 changed files with 706 additions and 150 deletions

View file

@ -24,11 +24,13 @@ namespace polysat {
class constraint;
class fixed_bits;
struct bit_dependency {
class bit_dependency {
optional<pdd> m_pdd;
unsigned m_bit_idx;
bit_dependency() : m_pdd(optional<pdd>::undef()), m_bit_idx(0) {}
public:
bit_dependency() : m_pdd(optional<dd::pdd>::undef()), m_bit_idx(0) {}
bit_dependency(const bit_dependency& v) = default;
bit_dependency(bit_dependency&& v) = default;
@ -38,6 +40,20 @@ namespace polysat {
return m_pdd == other.m_pdd && m_bit_idx == other.m_bit_idx;
}
bit_dependency& operator=(bit_dependency&& other) {
m_pdd = other.m_pdd;
m_bit_idx = other.m_bit_idx;
return *this;
}
bit_dependency& operator=(bit_dependency& other) {
m_pdd = other.m_pdd;
m_bit_idx = other.m_bit_idx;
return *this;
}
unsigned idx() const { return m_bit_idx; }
const pdd& pdd() const { return *m_pdd; }
};
using bit_dependencies = vector<bit_dependency>;
@ -45,11 +61,42 @@ namespace polysat {
class bit_justication {
protected:
static bit_justication* get_other_justification(const fixed_bits& fixed, const pdd& p, unsigned idx);
static const tbv_ref& get_tbv(fixed_bits& fixed, const pdd& p);
static bool fix_value(fixed_bits& fixed, const pdd& p, tbv_ref& tbv, unsigned idx, tbit val, bit_justication* j);
static const tbv_ref* get_tbv(fixed_bits& fixed, const pdd& p);
static bool fix_value_core(solver& s, fixed_bits& fixed, const pdd& p, tbv_ref& tbv, unsigned idx, tbit val, bit_justication** j);
static bool fix_value_core(solver& s, fixed_bits& fixed, const pdd& p, tbv_ref& tbv, unsigned idx, tbit val, bit_justication* j);
public:
virtual bool can_dealloc() { return true; }
virtual bool has_constraint(constraint*& constr) { return false; }
virtual void get_dependencies(fixed_bits& fixed, bit_dependencies& to_process) = 0;
virtual void get_dependencies(fixed_bits& fixed, bit_dependencies& to_process) = 0; // returns if element may be deallocated after call (usually true)
};
// if multiple bits are justified by the same justification
class bit_justication_shared : public bit_justication {
bit_justication* m_justification;
unsigned m_references = 0;
public:
bit_justication_shared() = default;
bit_justication_shared(bit_justication* j) : m_justification(j), m_references(1) {}
bit_justication* get_justification() { return m_justification; }
virtual bool can_dealloc() {
m_references = m_references == 0 ? 0 : m_references - 1;
if (m_references != 0)
return false;
if (m_justification->can_dealloc()) {
dealloc(m_justification);
m_justification = nullptr;
}
return true;
}
virtual void get_dependencies(fixed_bits& fixed, bit_dependencies& to_process) override {
SASSERT(m_justification);
m_justification->get_dependencies(fixed, to_process);
}
void inc_ref() { m_references++; }
};
class bit_justication_constraint : public bit_justication {
@ -59,8 +106,9 @@ namespace polysat {
// A pdd might occur multiple times if more bits of it are relevant
bit_dependencies m_dependencies;
bit_justication_constraint(constraint* c) : m_constraint(c) { }
bit_justication_constraint(constraint* c, bit_dependencies&& dep) : m_constraint(c), m_dependencies(dep) { }
bit_justication_constraint(constraint* c) : m_constraint(c) {}
bit_justication_constraint(constraint* c, const bit_dependencies& dep) : m_constraint(c), m_dependencies(dep) {}
bit_justication_constraint(constraint* c, bit_dependencies&& dep) : m_constraint(c), m_dependencies(dep) {}
public:
@ -74,16 +122,18 @@ namespace polysat {
static bit_justication_constraint* mk_assignment(constraint* c) { return alloc(bit_justication_constraint, c ); }
static bit_justication_constraint* mk_unary(constraint* c, bit_dependency v) {
bit_dependencies dep(1);
bit_dependencies dep;
dep.push_back(std::move(v));
return alloc(bit_justication_constraint, c, std::move(dep));
}
static bit_justication_constraint* mk_binary(constraint* c, bit_dependency v1, bit_dependency v2) {
bit_dependencies dep(2);
bit_dependencies dep;
dep.push_back(std::move(v1));
dep.push_back(std::move(v2));
return alloc(bit_justication_constraint, c, std::move(dep));
}
static bit_justication_constraint* mk(constraint* c, const bit_dependencies& dep) { return alloc(bit_justication_constraint, c, dep); }
// gives the highest bits such that they already enforce a value of "tbv" that is at least "val"
static bit_justication_constraint* mk_justify_at_least(constraint *c, const pdd& v, const tbv_ref& tbv, const rational& least);
// similar to mk_justify_at_least: gives the highest bits such that they already enforce a value of "tbv" that is at most "val"
@ -97,15 +147,20 @@ namespace polysat {
class bit_justication_mul : public bit_justication {
unsigned m_idx;
optional<pdd> m_c1, m_c2;
optional<pdd> m_p, m_q, m_r;
unsigned_vector m_bit_indexes;
public:
bit_justication_mul() = default;
bit_justication_mul(unsigned idx, const pdd& c1, const pdd& c2) : m_idx(idx), m_c1(c1), m_c2(c2) {}
bit_justication_mul(unsigned idx, const pdd& p, const pdd& q) : m_idx(idx), m_p(p), m_q(q) {}
bit_justication_mul(unsigned idx, const pdd& p, const pdd& q, const pdd& r) : m_idx(idx), m_p(p), m_q(q), m_r(r) {}
static tbv_ref& mul(fixed_bits& fixed, const pdd& p, const tbv_ref& in1, const tbv_ref& in2);
// propagates from p, q => r (forward) and r, p/q => q/p (backward)
static void propagate(solver& s, fixed_bits& fixed, const pdd& r, const pdd &p, const pdd &q);
void get_dependencies(fixed_bits& fixed, bit_dependencies& to_process) override;
void get_dependencies_forward(fixed_bits &fixed, bit_dependencies &to_process, const tbv_ref& p_tbv, const tbv_ref& q_tbv, unsigned relevant_range);
void get_dependencies_backward(fixed_bits& fixed, bit_dependencies& to_process, const tbv_ref& p_tbv, const tbv_ref& q_tbv, unsigned relevant_range);
};
class bit_justication_add : public bit_justication {
@ -118,12 +173,12 @@ namespace polysat {
bit_justication_add() = default;
bit_justication_add(unsigned idx, const pdd& c1, const pdd& c2) : m_idx(idx), m_c1(c1), m_c2(c2) {}
static tbv_ref& add(fixed_bits& fixed, const pdd& p, const tbv_ref& in1, const tbv_ref& in2);
static void propagate(solver& s, fixed_bits& fixed, const pdd& r, const pdd& p, const pdd& q);
void get_dependencies(fixed_bits& fixed, bit_dependencies& to_process) override;
};
class fixed_bits {
class fixed_bits final {
friend bit_justication;
@ -137,19 +192,19 @@ namespace polysat {
return args ? args->hash() : 0;
}
};
using pdd_to_tbv_map = map<pdd_to_tbv_key, optional<tbv_ref>, pdd_to_tbv_hash, pdd_to_tbv_eq>;
using pdd_to_tbv_map = map<pdd_to_tbv_key, tbv_ref*, pdd_to_tbv_hash, pdd_to_tbv_eq>;
using tbv_to_justification_key = bit_dependency;
using tbv_to_justification_eq = default_eq<tbv_to_justification_key>;
struct tbv_to_justification_hash {
unsigned operator()(tbv_to_justification_key const& args) const {
return combine_hash((*args.m_pdd).hash(), args.m_bit_idx);
return combine_hash(args.pdd().hash(), args.idx());
}
};
using tbv_to_justification_map = map<tbv_to_justification_key, bit_justication*, tbv_to_justification_hash, tbv_to_justification_eq>;
//vector<optional<tbv_ref>> m_var_to_tbv;
pdd_to_tbv_map m_var_to_tbv;
pdd_to_tbv_map m_var_to_tbv; // TODO: free tbv_ref pointers
tbv_to_justification_map m_tbv_to_justification; // the elements are pointers. Deallocate them before replacing them
bool m_consistent = true; // in case evaluating results in a bit-conflict
@ -158,13 +213,22 @@ namespace polysat {
tbv_manager& get_manager(unsigned sz);
clause_ref get_explanation(solver& s, bit_justication* j1, bit_justication* j2);
bool fix_value(const pdd& p, tbv_ref& tbv, unsigned idx, tbit val, bit_justication* j);
bool fix_value_core(const pdd& p, tbv_ref& tbv, unsigned idx, tbit val, bit_justication* j);
bool fix_value(solver& s, const pdd& p, tbv_ref& tbv, unsigned idx, tbit val, bit_justication* j);
void propagate_to_subterm(solver& s, const pdd& p);
public:
fixed_bits(solver& s) : m_solver(s) {}
tbv_ref& get_tbv(const pdd& p);
~fixed_bits() {
for (auto& tbv : m_var_to_tbv) {
dealloc(tbv.m_value);
}
}
tbv_ref* get_tbv(const pdd& p);
// #count [min; max]
static std::pair<unsigned, unsigned> leading_zeros(const tbv_ref& ref);
@ -178,7 +242,7 @@ namespace polysat {
bool fix_value(solver& s, const pdd& p, unsigned idx, tbit val, bit_justication* j);
void clear_value(const pdd& p, unsigned idx);
tbv_ref& eval(solver& s, const pdd& p);
tbv_ref* eval(solver& s, const pdd& p);
};
}