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

Added function to select the next variable to split on (User-Propagator) (#6096)

* Added function to select the next variable to split on

* Fixed typo

* Small fixes

* uint -> int
This commit is contained in:
Clemens Eisenhofer 2022-06-19 19:49:25 +02:00 committed by GitHub
parent f08e3d70a9
commit 2fa60aa43c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 255 additions and 104 deletions

View file

@ -91,8 +91,10 @@ namespace sat {
virtual double get_reward(literal l, ext_constraint_idx idx, literal_occs_fun& occs) const { return 0; }
virtual void get_antecedents(literal l, ext_justification_idx idx, literal_vector & r, bool probing) = 0;
virtual bool is_extended_binary(ext_justification_idx idx, literal_vector & r) { return false; }
virtual void asserted(literal l) {};
virtual void set_eliminated(bool_var v) {};
virtual bool decide(bool_var& var, lbool& phase) { return false; }
virtual bool get_case_split(bool_var& var, lbool& phase) { return false; }
virtual void asserted(literal l) {}
virtual void set_eliminated(bool_var v) {}
virtual check_result check() = 0;
virtual lbool resolve_conflict() { return l_undef; } // stores result in sat::solver::m_lemma
virtual void push() = 0;

View file

@ -1661,49 +1661,66 @@ namespace sat {
return null_bool_var;
}
bool solver::decide() {
bool_var next = next_var();
if (next == null_bool_var)
return false;
push();
m_stats.m_decision++;
bool solver::guess(bool_var next) {
lbool lphase = m_ext ? m_ext->get_phase(next) : l_undef;
bool phase = lphase == l_true;
if (lphase == l_undef) {
switch (m_config.m_phase) {
if (lphase != l_undef)
return lphase == l_true;
switch (m_config.m_phase) {
case PS_ALWAYS_TRUE:
phase = true;
break;
return true;
case PS_ALWAYS_FALSE:
phase = false;
break;
return false;
case PS_BASIC_CACHING:
phase = m_phase[next];
break;
return m_phase[next];
case PS_FROZEN:
phase = m_best_phase[next];
break;
return m_best_phase[next];
case PS_SAT_CACHING:
if (m_search_state == s_unsat) {
phase = m_phase[next];
}
else {
phase = m_best_phase[next];
}
break;
if (m_search_state == s_unsat)
return m_phase[next];
return m_best_phase[next];
case PS_RANDOM:
phase = (m_rand() % 2) == 0;
break;
return (m_rand() % 2) == 0;
default:
UNREACHABLE();
phase = false;
break;
}
return false;
}
}
literal next_lit(next, !phase);
bool solver::decide() {
bool_var next;
lbool phase = l_undef;
bool is_pos;
bool used_queue = false;
if (!m_ext || !m_ext->get_case_split(next, phase)) {
used_queue = true;
next = next_var();
if (next == null_bool_var)
return false;
}
push();
m_stats.m_decision++;
if (phase == l_undef)
phase = guess(next) ? l_true: l_false;
literal next_lit(next, false);
if (m_ext && m_ext->decide(next, phase)) {
if (used_queue)
m_case_split_queue.unassign_var_eh(next);
next_lit = literal(next, false);
}
if (phase == l_undef)
is_pos = guess(next);
else
is_pos = phase == l_true;
if (!is_pos)
next_lit.neg();
TRACE("sat_decide", tout << scope_lvl() << ": next-case-split: " << next_lit << "\n";);
assign_scoped(next_lit);
return true;

View file

@ -541,6 +541,7 @@ namespace sat {
unsigned m_next_simplify { 0 };
bool m_simplify_enabled { true };
bool m_restart_enabled { true };
bool guess(bool_var next);
bool decide();
bool_var next_var();
lbool bounded_search();

View file

@ -56,6 +56,18 @@ namespace user_solver {
void solver::register_cb(expr* e) {
add_expr(e);
}
void solver::next_split_cb(expr* e, unsigned idx, lbool phase) {
if (e == nullptr) {
m_next_split_expr = nullptr;
return;
}
force_push();
ctx.internalize(e, false);
m_next_split_expr = e;
m_next_split_idx = idx;
m_next_split_phase = phase;
}
sat::check_result solver::check() {
if (!(bool)m_final_eh)
@ -72,6 +84,41 @@ namespace user_solver {
m_id2justification.setx(v, sat::literal_vector(num_lits, jlits), sat::literal_vector());
m_fixed_eh(m_user_context, this, var2expr(v), value);
}
bool solver::decide(sat::bool_var& var, lbool& phase) {
if (!m_decide_eh)
return false;
euf::enode* original_enode = bool_var2enode(var);
if (!is_attached_to_var(original_enode))
return false;
unsigned new_bit = 0; // ignored; currently no bv-support
expr* e = bool_var2expr(var);
m_decide_eh(m_user_context, this, &e, &new_bit, &phase);
euf::enode* new_enode = ctx.get_enode(e);
if (original_enode == new_enode)
return false;
var = new_enode->bool_var();
return true;
}
bool solver::get_case_split(sat::bool_var& var, lbool &phase){
if (!m_next_split_expr)
return false;
euf::enode* n = ctx.get_enode(m_next_split_expr);
var = n->bool_var();
phase = m_next_split_phase;
m_next_split_expr = nullptr;
return true;
}
void solver::asserted(sat::literal lit) {
if (!m_fixed_eh)

View file

@ -56,24 +56,28 @@ namespace user_solver {
void reset() { memset(this, 0, sizeof(*this)); }
};
void* m_user_context;
user_propagator::push_eh_t m_push_eh;
user_propagator::pop_eh_t m_pop_eh;
user_propagator::fresh_eh_t m_fresh_eh;
user_propagator::final_eh_t m_final_eh;
user_propagator::fixed_eh_t m_fixed_eh;
user_propagator::eq_eh_t m_eq_eh;
user_propagator::eq_eh_t m_diseq_eh;
user_propagator::created_eh_t m_created_eh;
void* m_user_context;
user_propagator::push_eh_t m_push_eh = nullptr;
user_propagator::pop_eh_t m_pop_eh = nullptr;
user_propagator::fresh_eh_t m_fresh_eh = nullptr;
user_propagator::final_eh_t m_final_eh = nullptr;
user_propagator::fixed_eh_t m_fixed_eh = nullptr;
user_propagator::eq_eh_t m_eq_eh = nullptr;
user_propagator::eq_eh_t m_diseq_eh = nullptr;
user_propagator::created_eh_t m_created_eh = nullptr;
user_propagator::decide_eh_t m_decide_eh = nullptr;
user_propagator::context_obj* m_api_context = nullptr;
unsigned m_qhead = 0;
vector<prop_info> m_prop;
unsigned_vector m_prop_lim;
vector<sat::literal_vector> m_id2justification;
sat::literal_vector m_lits;
euf::enode_pair_vector m_eqs;
unsigned_vector m_fixed_ids;
stats m_stats;
unsigned m_qhead = 0;
vector<prop_info> m_prop;
unsigned_vector m_prop_lim;
vector<sat::literal_vector> m_id2justification;
sat::literal_vector m_lits;
euf::enode_pair_vector m_eqs;
unsigned_vector m_fixed_ids;
stats m_stats;
expr* m_next_split_expr = nullptr;
unsigned m_next_split_idx;
lbool m_next_split_phase;
struct justification {
unsigned m_propagation_index { 0 };
@ -94,7 +98,7 @@ namespace user_solver {
void propagate_consequence(prop_info const& prop);
void propagate_new_fixed(prop_info const& prop);
void validate_propagation();
void validate_propagation();
bool visit(expr* e) override;
bool visited(expr* e) override;
@ -126,14 +130,19 @@ namespace user_solver {
void register_eq(user_propagator::eq_eh_t& eq_eh) { m_eq_eh = eq_eh; }
void register_diseq(user_propagator::eq_eh_t& diseq_eh) { m_diseq_eh = diseq_eh; }
void register_created(user_propagator::created_eh_t& created_eh) { m_created_eh = created_eh; }
void register_decide(user_propagator::decide_eh_t& decide_eh) { m_decide_eh = decide_eh; }
bool has_fixed() const { return (bool)m_fixed_eh; }
void propagate_cb(unsigned num_fixed, expr* const* fixed_ids, unsigned num_eqs, expr* const* lhs, expr* const* rhs, expr* conseq) override;
void register_cb(expr* e) override;
void next_split_cb(expr* e, unsigned idx, lbool phase) override;
void new_fixed_eh(euf::theory_var v, expr* value, unsigned num_lits, sat::literal const* jlits);
bool decide(sat::bool_var& var, lbool& phase) override;
bool get_case_split(sat::bool_var& var, lbool &phase) override;
void asserted(sat::literal lit) override;
sat::check_result check() override;
void push_core() override;