Skip to content

Commit

Permalink
deal with compiler warnings and include value exchange prior to final…
Browse files Browse the repository at this point in the history
… check.
  • Loading branch information
NikolajBjorner committed Jan 24, 2025
1 parent ce615ee commit 0e8969c
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 146 deletions.
2 changes: 2 additions & 0 deletions src/ast/sls/sat_ddfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ namespace sat {
for (unsigned i = 0; i < num_vars(); ++i)
m_model[i] = to_lbool(value(i));
save_priorities();
if (m_plugin && !m_in_external_flip && m_restart_count == 0)
m_plugin->on_restart(); // import values if there are any updated ones.
if (m_plugin && !m_in_external_flip)
m_last_result = m_plugin->on_save_model();
}
Expand Down
56 changes: 36 additions & 20 deletions src/ast/sls/sls_arith_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,13 @@ Module Name:
Nikolaj Bjorner (nbjorner) 2023-02-07
Notes:
Uses quadratic solver method from nia_ls in hybrid-smt
(with a bug fix for when order of roots are swapped)
Other features from nia_ls are also used as a starting point,
such as tabu and fallbacks.
Todo:
- add fairness for which variable to flip and direction (by age fifo).
- maintain age per variable, per sign
- include more general tabu measure
-
- random walk when there is no applicable update
- repair_down can fail repeatedely. Then allow a mode to reset arguments similar to
repair of literals.
- avoid overflow for nested products
Done:
- add tabu for flipping variable back to the same value.
- remember last variable/delta and block -delta = last_delta && last_variable = current_variable
- include measures for bounded updates
- per variable maintain increasing range
--*/

#include "ast/sls/sls_arith_base.h"
Expand Down Expand Up @@ -1439,6 +1421,20 @@ namespace sls {
}
for (auto bv : m_tmp_set)
vi.m_bool_vars_of.push_back(bv);

m_tmp_nat_set.reset();
m_tmp_nat_set.assure_domain(ctx.clauses().size() + 1);

for (auto bv : vi.m_bool_vars_of) {
for (auto lit : { sat::literal(bv, false), sat::literal(bv, true) }) {
for (auto ci : ctx.get_use_list(lit)) {
if (m_tmp_nat_set.contains(ci))
continue;
m_tmp_nat_set.insert(ci);
vi.m_clauses_of.push_back(ci);
}
}
}
}

template<typename num_t>
Expand Down Expand Up @@ -2435,6 +2431,7 @@ namespace sls {
template<typename num_t>
void arith_base<num_t>::collect_statistics(statistics& st) const {
st.update("sls-arith-steps", m_stats.m_steps);
st.update("sls-arith-propagations", m_stats.m_propagations);
}

template<typename num_t>
Expand Down Expand Up @@ -2838,6 +2835,24 @@ namespace sls {
m_fixed_atoms.insert(bv);
}

template<typename num_t>
void arith_base<num_t>::add_lookahead(sat::bool_var bv) {
auto* ineq = get_ineq(bv);
if (!ineq)
return;
num_t na, nb;
for (auto const& [x, nl] : ineq->m_nonlinear) {
if (is_fixed(x))
continue;
if (is_linear(x, nl, nb))
find_linear_moves(*ineq, x, nb);
else if (is_quadratic(x, nl, na, nb))
find_quadratic_moves(*ineq, x, na, nb, ineq->m_args_value);
else
;
}
}

// for every variable e, for every atom containing e
// add lookahead for e.
// m_fixable_atoms contains atoms that can be fixed.
Expand Down Expand Up @@ -3254,6 +3269,7 @@ namespace sls {

template<typename num_t>
void arith_base<num_t>::start_propagation() {
++m_stats.m_propagations;
updt_params();
if (m_config.use_clausal_lookahead)
m_clausal_sls.search();
Expand Down
4 changes: 4 additions & 0 deletions src/ast/sls/sls_arith_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace sls {
struct stats {
unsigned m_steps = 0;
unsigned m_restarts = 0;
unsigned m_propagations = 0;
};

public:
Expand Down Expand Up @@ -120,6 +121,7 @@ namespace sls {
unsigned m_def_idx = UINT_MAX;
vector<std::pair<num_t, sat::bool_var>> m_linear_occurs;
sat::bool_var_vector m_bool_vars_of;
unsigned_vector m_clauses_of;
unsigned_vector m_muls;
unsigned_vector m_adds;
optional<bound> m_lo, m_hi;
Expand Down Expand Up @@ -216,6 +218,7 @@ namespace sls {
svector<double> m_prob_break;
indexed_uint_set m_bool_var_atoms;
indexed_uint_set m_tmp_set;
nat_set m_tmp_nat_set;

void invariant();
void invariant(ineq const& i);
Expand Down Expand Up @@ -381,6 +384,7 @@ namespace sls {
double lookahead(expr* e, bool update_score);
void add_lookahead(bool_info& i, expr* e);
void add_lookahead(bool_info& i, sat::bool_var bv);
void add_lookahead(sat::bool_var bv);
ptr_vector<expr> const& get_fixable_exprs(expr* e);
bool apply_move(expr* f, ptr_vector<expr> const& vars, arith_move_type t);
expr* get_candidate_unsat();
Expand Down
127 changes: 42 additions & 85 deletions src/ast/sls/sls_arith_clausal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,14 @@ namespace sls {
template<typename num_t>
void arith_clausal<num_t>::add_lookahead_on_unsat_vars() {
a.m_updates.reset();
a.m_fixed_atoms.reset();
TRACE("arith_verbose", tout << "unsat-vars ";
for (auto v : ctx.unsat_vars())
if (a.get_ineq(v)) tout << mk_bounded_pp(ctx.atom(v), a.m) << " ";
tout << "\n";);

for (auto v : ctx.unsat_vars()) {
auto* ineq = a.get_ineq(v);
if (!ineq)
continue;
auto e = ctx.atom(v);
auto& i = a.get_bool_info(e);
auto const& vars = a.get_fixable_exprs(e);
for (auto v : vars)
a.add_lookahead(i, v);
}
for (auto v : ctx.unsat_vars())
a.add_lookahead(v);

}

/**
Expand All @@ -146,7 +138,6 @@ namespace sls {
template<typename num_t>
void arith_clausal<num_t>::add_lookahead_on_false_literals() {
a.m_updates.reset();
a.m_fixed_atoms.reset();

unsigned sz = a.m_bool_var_atoms.size();
bool is_big = sz > 45u;
Expand All @@ -164,47 +155,25 @@ namespace sls {
};

unsigned idx = 0;
//unsigned num_sampled = 0;
for (unsigned i = std::min(sz, 45u); i-- > 0;) {
if (is_big) {
if (is_big) {
for (unsigned i = 45, j = 90; j-- > 0 && i-- > 0 && sz > 0;) {
idx = ctx.rand(sz);
bv = a.m_bool_var_atoms[idx];
}
else
bv = a.m_bool_var_atoms[i];

if (occurs_negative(bv)) {
auto e = ctx.atom(bv);
auto& i = a.get_bool_info(e);
a.add_lookahead(i, bv);
//++num_sampled;
}

if (is_big) {
--sz;
a.m_bool_var_atoms.swap_elems(idx, sz);
if (occurs_negative(bv))
a.add_lookahead(bv);
else
++i;
}
}

#if 0
for (auto bv : a.m_bool_var_atoms) {
if (ctx.unsat_vars().contains(bv))
continue;
auto* ineq = a.get_ineq(bv);
if (!ineq)
continue;
sat::literal lit(bv, !ineq->is_true());
auto const& ul = ctx.get_use_list(~lit);
if (ul.begin() == ul.end())
continue;
// literal is false in some clause but none of the clauses where it occurs false are unsat.

auto e = ctx.atom(bv);
auto& i = a.get_bool_info(e);

a.add_lookahead(i, bv);
else {
for (unsigned i = 0; i < sz; ++i) {
bv = a.m_bool_var_atoms[i];
if (occurs_negative(bv))
a.add_lookahead(bv);
}
}
#endif
}

template<typename num_t>
Expand Down Expand Up @@ -300,50 +269,38 @@ namespace sls {
if (!a.update_num(v, delta))
return -1;
double score = 0;
m_tmp_nat_set.reset();
m_tmp_nat_set.assure_domain(ctx.clauses().size() + 1);
for (auto bv : vi.m_bool_vars_of) {
for (auto lit : { sat::literal(bv, false), sat::literal(bv, true) }) {
for (auto ci : ctx.get_use_list(lit)) {
if (m_tmp_nat_set.contains(ci)) {
continue;
}
m_tmp_nat_set.insert(ci);

auto const& c = ctx.get_clause(ci);
unsigned num_true = 0;
for (auto lit : c) {
auto bv = lit.var();
auto ineq = a.get_ineq(bv);
if (ineq) {
if (ineq->is_true() != lit.sign())
++num_true;
}
else if (ctx.is_true(lit))
++num_true;
}

CTRACE("arith_verbose", c.m_num_trues != num_true && (c.m_num_trues == 0 || num_true == 0),
tout << "clause: " << c
<< " v" << v << " += " << delta
<< " new-true lits: " << num_true
<< " old-true lits: " << c.m_num_trues
<< " w: " << c.m_weight << "\n";
for (auto lit : c)
if (a.get_ineq(lit.var()))
tout << lit << " " << *a.get_ineq(lit.var()) << "\n";);
if (c.m_num_trues > 0 && num_true == 0)
score -= c.m_weight;
else if (c.m_num_trues == 0 && num_true > 0)
score += c.m_weight;
for (auto ci : vi.m_clauses_of) {
auto const& c = ctx.get_clause(ci);
unsigned num_true = 0;
for (auto lit : c) {
auto bv = lit.var();
auto ineq = a.get_ineq(bv);
if (ineq) {
if (ineq->is_true() != lit.sign())
++num_true;
}
else if (ctx.is_true(lit))
++num_true;
}


CTRACE("arith_verbose", c.m_num_trues != num_true && (c.m_num_trues == 0 || num_true == 0),
tout << "clause: " << c
<< " v" << v << " += " << delta
<< " new-true lits: " << num_true
<< " old-true lits: " << c.m_num_trues
<< " w: " << c.m_weight << "\n";
for (auto lit : c)
if (a.get_ineq(lit.var()))
tout << lit << " " << *a.get_ineq(lit.var()) << "\n";);
if (c.m_num_trues > 0 && num_true == 0)
score -= c.m_weight;
else if (c.m_num_trues == 0 && num_true > 0)
score += c.m_weight;
}

// verbose_stream() << num_clauses << " " << num_dup << "\n";
// revert the update
a.update_args_value(v, vi.value() - delta);

a.update_args_value(v, vi.value() - delta);
return score;
}

Expand Down
2 changes: 0 additions & 2 deletions src/ast/sls/sls_arith_clausal.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ namespace sls {
unsigned m_best_last_step = 0;
unsigned m_num_lookaheads = 0;

nat_set m_tmp_nat_set;

// avoid checking the same updates twice
var_t m_last_var = UINT_MAX;
num_t m_last_delta;
Expand Down
2 changes: 1 addition & 1 deletion src/ast/sls/sls_basic_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace sls {
}

expr_ref basic_plugin::eval_ite(app* e) {
expr* c, * th, * el;
expr* c = nullptr, * th = nullptr, * el = nullptr;
VERIFY(m.is_ite(e, c, th, el));
if (bval0(c))
return ctx.get_value(th);
Expand Down
4 changes: 1 addition & 3 deletions src/ast/sls/sls_bv_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,12 @@ double sls_engine::find_best_move_mc(ptr_vector<func_decl> & to_evaluate, double
// main search loop
lbool sls_engine::search() {
lbool res = l_undef;
double score = 0.0, old_score = 0.0;
double score = 0.0;
unsigned new_const = (unsigned)-1, new_bit;
mpz new_value;
move_type move;

score = rescore();
unsigned sz = m_assertions.size();

while (check_restart(m_stats.m_moves)) {
if (!m_manager.inc())
Expand Down Expand Up @@ -435,7 +434,6 @@ lbool sls_engine::search() {
continue;
}

old_score = score;
new_const = (unsigned)-1;

// find best increasing move
Expand Down
4 changes: 1 addition & 3 deletions src/ast/sls/sls_bv_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ namespace sls {
}

void bv_eval::fold_oper(bvect& out, app* t, unsigned i, std::function<void(bvect&, bvval const&)> const& f) {
auto i2 = i == 0 ? 1 : 0;
unsigned i2 = i == 0 ? 1 : 0;
auto const& c = wval(t->get_arg(i2));
for (unsigned j = 0; j < c.nw; ++j)
out[j] = c.bits()[j];
Expand Down Expand Up @@ -1242,8 +1242,6 @@ namespace sls {
bool bv_eval::try_repair_sdiv(bvect const& e, bvval& a, bvval& b, unsigned i) {

bool sign_a = a.sign();
bool sign_b = b.sign();
bool sign_e = e.get(a.bw - 1);

// y = 0, x >= 0 -> -1
if (i == 0 && b.is_zero() && a.is_ones(e) && a.try_set(m_zero))
Expand Down
2 changes: 0 additions & 2 deletions src/ast/sls/sls_bv_lookahead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,15 +676,13 @@ namespace sls {
for (unsigned i = 0; i < m_update_stack[depth].size(); ++i) {
auto [e, is_bv] = m_update_stack[depth][i];
TRACE("bv_verbose", tout << "update " << mk_bounded_pp(e, m) << "\n";);
bool old_truth = false;
if (t == e)
;
else if (is_bv) {
m_ev.eval(e); // updates wval(e).eval
wval(e).commit_eval_ignore_tabu();
}
else {
old_truth = m_ev.get_bool_value(e);
SASSERT(m.is_bool(e));
auto v1 = m_ev.bval1(e);

Expand Down
Loading

0 comments on commit 0e8969c

Please sign in to comment.