mirror of
https://github.com/Z3Prover/z3
synced 2025-04-23 09:05:31 +00:00
reorg sls
This commit is contained in:
parent
8e482df62a
commit
5ebcc3e447
25 changed files with 1923 additions and 1107 deletions
|
@ -4,8 +4,11 @@ z3_add_component(ast_sls
|
|||
bv_sls.cpp
|
||||
bv_sls_eval.cpp
|
||||
bv_sls_fixed.cpp
|
||||
bv_sls_terms.cpp
|
||||
bv_sls_terms.cpp
|
||||
sls_arith_int.cpp
|
||||
sls_cc.cpp
|
||||
sls_engine.cpp
|
||||
sls_smt.cpp
|
||||
sls_valuation.cpp
|
||||
COMPONENT_DEPENDENCIES
|
||||
ast
|
||||
|
|
808
src/ast/sls/sls_arith_int.cpp
Normal file
808
src/ast/sls/sls_arith_int.cpp
Normal file
|
@ -0,0 +1,808 @@
|
|||
/*++
|
||||
Copyright (c) 2023 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
arith_sls_int.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
Local search dispatch for NIA
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2023-02-07
|
||||
|
||||
--*/
|
||||
|
||||
#include "ast/sls/sls_arith_int.h"
|
||||
#include "ast/ast_ll_pp.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
template<typename int_t>
|
||||
arith_plugin<int_t>::arith_plugin(context& ctx) :
|
||||
plugin(ctx),
|
||||
a(m) {
|
||||
m_fid = a.get_family_id();
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::reset() {
|
||||
m_bool_vars.reset();
|
||||
m_vars.reset();
|
||||
m_expr2var.reset();
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::save_best_values() {
|
||||
for (auto& v : m_vars)
|
||||
v.m_best_value = v.m_value;
|
||||
check_ineqs();
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::store_best_values() {
|
||||
}
|
||||
|
||||
// distance to true
|
||||
template<typename int_t>
|
||||
int_t arith_plugin<int_t>::dtt(bool sign, int_t const& args, ineq const& ineq) const {
|
||||
int_t zero{ 0 };
|
||||
switch (ineq.m_op) {
|
||||
case ineq_kind::LE:
|
||||
if (sign) {
|
||||
if (args + ineq.m_coeff <= 0)
|
||||
return -ineq.m_coeff - args + 1;
|
||||
return zero;
|
||||
}
|
||||
if (args + ineq.m_coeff <= 0)
|
||||
return zero;
|
||||
return args + ineq.m_coeff;
|
||||
case ineq_kind::EQ:
|
||||
if (sign) {
|
||||
if (args + ineq.m_coeff == 0)
|
||||
return int_t(1);
|
||||
return zero;
|
||||
}
|
||||
if (args + ineq.m_coeff == 0)
|
||||
return zero;
|
||||
return int_t(1);
|
||||
case ineq_kind::LT:
|
||||
if (sign) {
|
||||
if (args + ineq.m_coeff < 0)
|
||||
return -ineq.m_coeff - args;
|
||||
return zero;
|
||||
}
|
||||
if (args + ineq.m_coeff < 0)
|
||||
return zero;
|
||||
return args + ineq.m_coeff + 1;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
return zero;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// dtt is high overhead. It walks ineq.m_args
|
||||
// m_vars[w].m_value can be computed outside and shared among calls
|
||||
// different data-structures for storing coefficients
|
||||
//
|
||||
template<typename int_t>
|
||||
int_t arith_plugin<int_t>::dtt(bool sign, ineq const& ineq, var_t v, int_t const& new_value) const {
|
||||
for (auto const& [coeff, w] : ineq.m_args)
|
||||
if (w == v)
|
||||
return dtt(sign, ineq.m_args_value + coeff * (new_value - m_vars[v].m_value), ineq);
|
||||
return int_t(1);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
int_t arith_plugin<int_t>::dtt(bool sign, ineq const& ineq, int_t const& coeff, int_t const& old_value, int_t const& new_value) const {
|
||||
return dtt(sign, ineq.m_args_value + coeff * (new_value - old_value), ineq);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
bool arith_plugin<int_t>::cm(ineq const& ineq, var_t v, int_t& new_value) {
|
||||
for (auto const& [coeff, w] : ineq.m_args)
|
||||
if (w == v)
|
||||
return cm(ineq, v, coeff, new_value);
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
bool arith_plugin<int_t>::cm(ineq const& ineq, var_t v, int_t const& coeff, int_t& new_value) {
|
||||
auto bound = -ineq.m_coeff;
|
||||
auto argsv = ineq.m_args_value;
|
||||
bool solved = false;
|
||||
int_t delta = argsv - bound;
|
||||
|
||||
if (ineq.is_true()) {
|
||||
switch (ineq.m_op) {
|
||||
case ineq_kind::LE:
|
||||
// args <= bound -> args > bound
|
||||
SASSERT(argsv <= bound);
|
||||
SASSERT(delta <= 0);
|
||||
delta -= 1 + (ctx.rand() % 10);
|
||||
new_value = value(v) + div(abs(delta) + abs(coeff) - 1, coeff);
|
||||
VERIFY(argsv + coeff * (new_value - value(v)) > bound);
|
||||
return true;
|
||||
case ineq_kind::LT:
|
||||
// args < bound -> args >= bound
|
||||
SASSERT(argsv <= bound);
|
||||
SASSERT(delta <= 0);
|
||||
delta = abs(delta) + ctx.rand() % 10;
|
||||
new_value = value(v) + div(delta + abs(coeff) - 1, coeff);
|
||||
VERIFY(argsv + coeff * (new_value - value(v)) >= bound);
|
||||
return true;
|
||||
case ineq_kind::EQ: {
|
||||
delta = abs(delta) + 1 + ctx.rand() % 10;
|
||||
int sign = ctx.rand() % 2 == 0 ? 1 : -1;
|
||||
new_value = value(v) + sign * div(abs(delta) + abs(coeff) - 1, coeff);
|
||||
VERIFY(argsv + coeff * (new_value - value(v)) != bound);
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
UNREACHABLE();
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
switch (ineq.m_op) {
|
||||
case ineq_kind::LE:
|
||||
SASSERT(argsv > bound);
|
||||
SASSERT(delta > 0);
|
||||
delta += rand() % 10;
|
||||
new_value = value(v) - div(delta + abs(coeff) - 1, coeff);
|
||||
VERIFY(argsv + coeff * (new_value - value(v)) <= bound);
|
||||
return true;
|
||||
case ineq_kind::LT:
|
||||
SASSERT(argsv >= bound);
|
||||
SASSERT(delta >= 0);
|
||||
delta += 1 + rand() % 10;
|
||||
new_value = value(v) - div(delta + abs(coeff) - 1, coeff);
|
||||
VERIFY(argsv + coeff * (new_value - value(v)) < bound);
|
||||
return true;
|
||||
case ineq_kind::EQ:
|
||||
SASSERT(delta != 0);
|
||||
if (delta < 0)
|
||||
new_value = value(v) + div(abs(delta) + abs(coeff) - 1, coeff);
|
||||
else
|
||||
new_value = value(v) - div(delta + abs(coeff) - 1, coeff);
|
||||
solved = argsv + coeff * (new_value - value(v)) == bound;
|
||||
if (!solved && abs(coeff) == 1) {
|
||||
verbose_stream() << "did not solve equality " << ineq << " for " << v << "\n";
|
||||
verbose_stream() << new_value << " " << value(v) << " delta " << delta << " lhs " << (argsv + coeff * (new_value - value(v))) << " bound " << bound << "\n";
|
||||
UNREACHABLE();
|
||||
}
|
||||
return solved;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// flip on the first positive score
|
||||
// it could be changed to flip on maximal positive score
|
||||
// or flip on maximal non-negative score
|
||||
// or flip on first non-negative score
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::repair(sat::literal lit, ineq const& ineq) {
|
||||
int_t new_value;
|
||||
if (UINT_MAX == ineq.m_var_to_flip)
|
||||
dtt_reward(lit);
|
||||
auto v = ineq.m_var_to_flip;
|
||||
if (v == UINT_MAX) {
|
||||
IF_VERBOSE(1, verbose_stream() << "no var to flip\n");
|
||||
return;
|
||||
}
|
||||
// verbose_stream() << "var to flip " << v << "\n";
|
||||
if (!cm(ineq, v, new_value)) {
|
||||
IF_VERBOSE(1, verbose_stream() << "no critical move for " << v << "\n");
|
||||
return;
|
||||
}
|
||||
update(v, new_value);
|
||||
}
|
||||
|
||||
//
|
||||
// dscore(op) = sum_c (dts(c,alpha) - dts(c,alpha_after)) * weight(c)
|
||||
// TODO - use cached dts instead of computed dts
|
||||
// cached dts has to be updated when the score of literals are updated.
|
||||
//
|
||||
template<typename int_t>
|
||||
double arith_plugin<int_t>::dscore(var_t v, int_t const& new_value) const {
|
||||
double score = 0;
|
||||
auto const& vi = m_vars[v];
|
||||
for (auto const& [coeff, bv] : vi.m_bool_vars) {
|
||||
sat::literal lit(bv, false);
|
||||
for (auto cl : ctx.get_use_list(lit))
|
||||
score += (compute_dts(cl) - dts(cl, v, new_value)).get_int64() * ctx.get_weight(cl);
|
||||
for (auto cl : ctx.get_use_list(~lit))
|
||||
score += (compute_dts(cl) - dts(cl, v, new_value)).get_int64() * ctx.get_weight(cl);
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
//
|
||||
// cm_score is costly. It involves several cache misses.
|
||||
// Note that
|
||||
// - get_use_list(lit).size() is "often" 1 or 2
|
||||
// - dtt_old can be saved
|
||||
//
|
||||
template<typename int_t>
|
||||
int arith_plugin<int_t>::cm_score(var_t v, int_t const& new_value) {
|
||||
int score = 0;
|
||||
auto& vi = m_vars[v];
|
||||
int_t old_value = vi.m_value;
|
||||
for (auto const& [coeff, bv] : vi.m_bool_vars) {
|
||||
auto const& ineq = *atom(bv);
|
||||
bool old_sign = sign(bv);
|
||||
int_t dtt_old = dtt(old_sign, ineq);
|
||||
int_t dtt_new = dtt(old_sign, ineq, coeff, old_value, new_value);
|
||||
if ((dtt_old == 0) == (dtt_new == 0))
|
||||
continue;
|
||||
sat::literal lit(bv, old_sign);
|
||||
if (dtt_old == 0)
|
||||
// flip from true to false
|
||||
lit.neg();
|
||||
|
||||
// lit flips form false to true:
|
||||
|
||||
for (auto cl : ctx.get_use_list(lit)) {
|
||||
auto const& clause = ctx.get_clause(cl);
|
||||
if (!clause.is_true())
|
||||
++score;
|
||||
}
|
||||
|
||||
// ignore the situation where clause contains multiple literals using v
|
||||
for (auto cl : ctx.get_use_list(~lit)) {
|
||||
auto const& clause = ctx.get_clause(cl);
|
||||
if (clause.m_num_trues == 1)
|
||||
--score;
|
||||
}
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
int_t arith_plugin<int_t>::compute_dts(unsigned cl) const {
|
||||
int_t d(1), d2;
|
||||
bool first = true;
|
||||
for (auto a : ctx.get_clause(cl)) {
|
||||
auto const* ineq = atom(a.var());
|
||||
if (!ineq)
|
||||
continue;
|
||||
d2 = dtt(a.sign(), *ineq);
|
||||
if (first)
|
||||
d = d2, first = false;
|
||||
else
|
||||
d = std::min(d, d2);
|
||||
if (d == 0)
|
||||
break;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
int_t arith_plugin<int_t>::dts(unsigned cl, var_t v, int_t const& new_value) const {
|
||||
int_t d(1), d2;
|
||||
bool first = true;
|
||||
for (auto lit : ctx.get_clause(cl)) {
|
||||
auto const* ineq = atom(lit.var());
|
||||
if (!ineq)
|
||||
continue;
|
||||
d2 = dtt(lit.sign(), *ineq, v, new_value);
|
||||
if (first)
|
||||
d = d2, first = false;
|
||||
else
|
||||
d = std::min(d, d2);
|
||||
if (d == 0)
|
||||
break;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::update(var_t v, int_t const& new_value) {
|
||||
auto& vi = m_vars[v];
|
||||
auto old_value = vi.m_value;
|
||||
if (old_value == new_value)
|
||||
return;
|
||||
for (auto const& [coeff, bv] : vi.m_bool_vars) {
|
||||
auto& ineq = *atom(bv);
|
||||
bool old_sign = sign(bv);
|
||||
sat::literal lit(bv, old_sign);
|
||||
SASSERT(ctx.is_true(lit));
|
||||
ineq.m_args_value += coeff * (new_value - old_value);
|
||||
int_t dtt_new = dtt(old_sign, ineq);
|
||||
if (dtt_new != 0)
|
||||
ctx.flip(bv);
|
||||
SASSERT(dtt(sign(bv), ineq) == 0);
|
||||
}
|
||||
vi.m_value = new_value;
|
||||
for (auto idx : vi.m_muls) {
|
||||
auto const& [v, monomial] = m_muls[idx];
|
||||
|
||||
int_t prod(1);
|
||||
for (auto w : monomial)
|
||||
prod *= value(w);
|
||||
if (value(v) != prod)
|
||||
m_vars_to_update.push_back({ v, prod });
|
||||
}
|
||||
for (auto const& idx : vi.m_adds) {
|
||||
auto const& ad = m_adds[idx];
|
||||
auto const& args = ad.m_args;
|
||||
auto v = ad.m_var;
|
||||
int_t sum(ad.m_coeff);
|
||||
for (auto [c, w] : args)
|
||||
sum += c * value(w);
|
||||
if (value(v) != sum)
|
||||
m_vars_to_update.push_back({ v, sum });
|
||||
}
|
||||
if (vi.m_add_idx != UINT_MAX || vi.m_mul_idx != UINT_MAX)
|
||||
// add repair actions for additions.
|
||||
m_defs_to_update.push_back(v);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
typename arith_plugin<int_t>::ineq& arith_plugin<int_t>::new_ineq(ineq_kind op, int_t const& coeff) {
|
||||
auto* i = alloc(ineq);
|
||||
i->m_coeff = coeff;
|
||||
i->m_op = op;
|
||||
return *i;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::add_arg(linear_term& ineq, int_t const& c, var_t v) {
|
||||
ineq.m_args.push_back({ c, v });
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
bool arith_plugin<int_t>::is_int64(expr* e, int_t& i) {
|
||||
rational r;
|
||||
if (a.is_numeral(e, r) && r.is_int64()) {
|
||||
i = int_t(r.get_int64());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool arith_plugin<checked_int64<true>>::is_int(expr* e, checked_int64<true>& i) {
|
||||
return is_int64(e, i);
|
||||
}
|
||||
|
||||
bool arith_plugin<rational>::is_int(expr* e, rational& i) {
|
||||
return a.is_numeral(e, i) && i.is_int();
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
bool arith_plugin<int_t>::is_int(expr* e, int_t& i) {
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::add_args(linear_term& term, expr* e, int_t const& coeff) {
|
||||
auto v = m_expr2var.get(e->get_id(), UINT_MAX);
|
||||
if (v != UINT_MAX) {
|
||||
add_arg(term, coeff, v);
|
||||
return;
|
||||
}
|
||||
expr* x, * y;
|
||||
int_t i;
|
||||
if (is_int(e, i)) {
|
||||
term.m_coeff += coeff * i;
|
||||
return;
|
||||
}
|
||||
if (a.is_add(e)) {
|
||||
for (expr* arg : *to_app(e))
|
||||
add_args(term, arg, coeff);
|
||||
return;
|
||||
}
|
||||
if (a.is_sub(e, x, y)) {
|
||||
add_args(term, x, coeff);
|
||||
add_args(term, y, -coeff);
|
||||
return;
|
||||
}
|
||||
|
||||
if (a.is_mul(e)) {
|
||||
unsigned_vector m;
|
||||
int_t c = coeff;
|
||||
for (expr* arg : *to_app(e))
|
||||
if (is_int(x, i))
|
||||
c *= i;
|
||||
else
|
||||
m.push_back(mk_term(arg));
|
||||
switch (m.size()) {
|
||||
case 0:
|
||||
term.m_coeff += c;
|
||||
break;
|
||||
case 1:
|
||||
add_arg(term, c, m[0]);
|
||||
break;
|
||||
default: {
|
||||
auto v = mk_var(e);
|
||||
unsigned idx = m_muls.size();
|
||||
m_muls.push_back({ v, m });
|
||||
int_t prod(1);
|
||||
for (auto w : m)
|
||||
m_vars[w].m_muls.push_back(idx), prod *= value(w);
|
||||
m_vars[v].m_mul_idx = idx;
|
||||
m_vars[v].m_value = prod;
|
||||
add_arg(term, c, v);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (a.is_uminus(e, x)) {
|
||||
add_args(term, x, -coeff);
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_uninterp(e)) {
|
||||
auto v = mk_var(e);
|
||||
add_arg(term, coeff, v);
|
||||
return;
|
||||
}
|
||||
|
||||
UNREACHABLE();
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
typename arith_plugin<int_t>::var_t arith_plugin<int_t>::mk_term(expr* e) {
|
||||
auto v = m_expr2var.get(e->get_id(), UINT_MAX);
|
||||
if (v != UINT_MAX)
|
||||
return v;
|
||||
linear_term t = linear_term({ {}, 0 });
|
||||
add_args(t, e, int_t(1));
|
||||
if (t.m_coeff == 1 && t.m_args.size() == 1 && t.m_args[0].first == 1)
|
||||
return t.m_args[0].second;
|
||||
v = mk_var(e);
|
||||
auto idx = m_adds.size();
|
||||
int_t sum(t.m_coeff);
|
||||
m_adds.push_back({ t.m_args, t.m_coeff, v });
|
||||
for (auto const& [c, w] : t.m_args)
|
||||
m_vars[w].m_adds.push_back(idx), sum += c * value(w);
|
||||
m_vars[v].m_add_idx = idx;
|
||||
m_vars[v].m_value = sum;
|
||||
return v;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
unsigned arith_plugin<int_t>::mk_var(expr* e) {
|
||||
unsigned v = m_expr2var.get(e->get_id(), UINT_MAX);
|
||||
if (v == UINT_MAX) {
|
||||
v = m_vars.size();
|
||||
m_expr2var.setx(e->get_id(), v, UINT_MAX);
|
||||
m_vars.push_back(var_info(e, var_kind::INT));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::init_bool_var(sat::bool_var bv) {
|
||||
if (m_bool_vars.get(bv, nullptr))
|
||||
return;
|
||||
expr* e = ctx.atom(bv);
|
||||
// verbose_stream() << "bool var " << bv << " " << mk_bounded_pp(e, m) << "\n";
|
||||
if (!e)
|
||||
return;
|
||||
expr* x, * y;
|
||||
m_bool_vars.reserve(bv + 1);
|
||||
if (a.is_le(e, x, y) || a.is_ge(e, y, x)) {
|
||||
auto& ineq = new_ineq(ineq_kind::LE, int_t(0));
|
||||
add_args(ineq, x, int_t(1));
|
||||
add_args(ineq, y, int_t(-1));
|
||||
init_ineq(bv, ineq);
|
||||
}
|
||||
else if ((a.is_lt(e, x, y) || a.is_gt(e, y, x)) && a.is_int(x)) {
|
||||
auto& ineq = new_ineq(ineq_kind::LE, int_t(1));
|
||||
add_args(ineq, x, int_t(1));
|
||||
add_args(ineq, y, int_t(-1));
|
||||
init_ineq(bv, ineq);
|
||||
}
|
||||
else if (m.is_eq(e, x, y) && a.is_int_real(x)) {
|
||||
auto& ineq = new_ineq(ineq_kind::EQ, int_t(0));
|
||||
add_args(ineq, x, int_t(1));
|
||||
add_args(ineq, y, int_t(-1));
|
||||
init_ineq(bv, ineq);
|
||||
}
|
||||
else {
|
||||
SASSERT(!a.is_arith_expr(e));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::init_ineq(sat::bool_var bv, ineq& i) {
|
||||
i.m_args_value = 0;
|
||||
for (auto const& [coeff, v] : i.m_args) {
|
||||
m_vars[v].m_bool_vars.push_back({ coeff, bv });
|
||||
i.m_args_value += coeff * value(v);
|
||||
}
|
||||
m_bool_vars.set(bv, &i);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::init_bool_var_assignment(sat::bool_var v) {
|
||||
auto* ineq = m_bool_vars.get(v, nullptr);
|
||||
if (ineq && ctx.is_true(sat::literal(v, false)) != (dtt(false, *ineq) == 0))
|
||||
ctx.flip(v);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::repair(sat::literal lit) {
|
||||
if (!ctx.is_true(lit))
|
||||
return;
|
||||
auto const* ineq = atom(lit.var());
|
||||
if (!ineq)
|
||||
return;
|
||||
if (ineq->is_true() != lit.sign())
|
||||
return;
|
||||
TRACE("sls", tout << "repair " << lit << "\n");
|
||||
repair(lit, *ineq);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::propagate_updates() {
|
||||
while (!m_defs_to_update.empty() || !m_vars_to_update.empty()) {
|
||||
while (!m_vars_to_update.empty()) {
|
||||
auto [w, new_value1] = m_vars_to_update.back();
|
||||
m_vars_to_update.pop_back();
|
||||
update(w, new_value1);
|
||||
}
|
||||
repair_defs();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::repair_defs() {
|
||||
while (!m_defs_to_update.empty()) {
|
||||
auto v = m_defs_to_update.back();
|
||||
m_defs_to_update.pop_back();
|
||||
auto const& vi = m_vars[v];
|
||||
if (vi.m_mul_idx != UINT_MAX)
|
||||
repair_mul(m_muls[vi.m_mul_idx]);
|
||||
if (vi.m_add_idx != UINT_MAX)
|
||||
repair_add(m_adds[vi.m_add_idx]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::repair_add(add_def const& ad) {
|
||||
auto v = ad.m_var;
|
||||
auto const& coeffs = ad.m_args;
|
||||
int_t sum(ad.m_coeff);
|
||||
int_t val = value(v);
|
||||
for (auto const& [c, w] : coeffs)
|
||||
sum += c * value(w);
|
||||
if (val == sum)
|
||||
return;
|
||||
if (rand() % 20 == 0)
|
||||
update(v, sum);
|
||||
else {
|
||||
auto const& [c, w] = coeffs[rand() % coeffs.size()];
|
||||
int_t delta = sum - val;
|
||||
int_t new_value = value(w) + div(delta, c);
|
||||
update(w, new_value);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::repair_mul(mul_def const& md) {
|
||||
int_t product(1);
|
||||
int_t val = value(md.m_var);
|
||||
for (auto v : md.m_monomial)
|
||||
product *= value(v);
|
||||
if (product == val)
|
||||
return;
|
||||
if (rand() % 20 == 0) {
|
||||
update(md.m_var, product);
|
||||
}
|
||||
else if (val == 0) {
|
||||
auto v = md.m_monomial[rand() % md.m_monomial.size()];
|
||||
int_t zero(0);
|
||||
update(v, zero);
|
||||
}
|
||||
else if (val == 1 || val == -1) {
|
||||
product = 1;
|
||||
for (auto v : md.m_monomial) {
|
||||
int_t new_value(1);
|
||||
if (rand() % 2 == 0)
|
||||
new_value = -1;
|
||||
product *= new_value;
|
||||
update(v, new_value);
|
||||
}
|
||||
if (product != val) {
|
||||
auto last = md.m_monomial.back();
|
||||
update(last, -value(last));
|
||||
}
|
||||
}
|
||||
else {
|
||||
product = 1;
|
||||
for (auto v : md.m_monomial) {
|
||||
int_t new_value{ 1 };
|
||||
if (rand() % 2 == 0)
|
||||
new_value = -1;
|
||||
product *= new_value;
|
||||
update(v, new_value);
|
||||
}
|
||||
auto v = md.m_monomial[rand() % md.m_monomial.size()];
|
||||
if ((product < 0 && 0 < val) || (val < 0 && 0 < product))
|
||||
update(v, -val * value(v));
|
||||
else
|
||||
update(v, val * value(v));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
double arith_plugin<int_t>::reward(sat::literal lit) {
|
||||
if (m_dscore_mode)
|
||||
return dscore_reward(lit.var());
|
||||
else
|
||||
return dtt_reward(lit);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
double arith_plugin<int_t>::dtt_reward(sat::literal lit) {
|
||||
auto* ineq = atom(lit.var());
|
||||
if (!ineq)
|
||||
return -1;
|
||||
int_t new_value;
|
||||
double max_result = -1;
|
||||
unsigned n = 0;
|
||||
for (auto const& [coeff, x] : ineq->m_args) {
|
||||
if (!cm(*ineq, x, coeff, new_value))
|
||||
continue;
|
||||
double result = 0;
|
||||
auto old_value = m_vars[x].m_value;
|
||||
for (auto const& [coeff, bv] : m_vars[x].m_bool_vars) {
|
||||
result += ctx.reward(bv);
|
||||
#if 0
|
||||
bool old_sign = sign(bv);
|
||||
auto dtt_old = dtt(old_sign, *atom(bv));
|
||||
auto dtt_new = dtt(old_sign, *atom(bv), coeff, old_value, new_value);
|
||||
if ((dtt_new == 0) != (dtt_old == 0))
|
||||
result += ctx.reward(bv);
|
||||
#endif
|
||||
}
|
||||
if (result > max_result || max_result == -1 || (result == max_result && (rand() % ++n == 0))) {
|
||||
max_result = result;
|
||||
ineq->m_var_to_flip = x;
|
||||
}
|
||||
}
|
||||
return max_result;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
double arith_plugin<int_t>::dscore_reward(sat::bool_var bv) {
|
||||
m_dscore_mode = false;
|
||||
bool old_sign = sign(bv);
|
||||
sat::literal litv(bv, old_sign);
|
||||
auto* ineq = atom(bv);
|
||||
if (!ineq)
|
||||
return 0;
|
||||
SASSERT(ineq->is_true() != old_sign);
|
||||
int_t new_value;
|
||||
|
||||
for (auto const& [coeff, v] : ineq->m_args) {
|
||||
double result = 0;
|
||||
if (cm(*ineq, v, coeff, new_value))
|
||||
result = dscore(v, new_value);
|
||||
// just pick first positive, or pick a max?
|
||||
if (result > 0) {
|
||||
ineq->m_var_to_flip = v;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// switch to dscore mode
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::on_rescale() {
|
||||
m_dscore_mode = true;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::on_restart() {
|
||||
for (unsigned v = 0; v < ctx.num_bool_vars(); ++v)
|
||||
init_bool_var_assignment(v);
|
||||
check_ineqs();
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::check_ineqs() {
|
||||
auto check_bool_var = [&](sat::bool_var bv) {
|
||||
auto const* ineq = atom(bv);
|
||||
if (!ineq)
|
||||
return;
|
||||
int_t d = dtt(sign(bv), *ineq);
|
||||
sat::literal lit(bv, sign(bv));
|
||||
if (ctx.is_true(lit) != (d == 0)) {
|
||||
verbose_stream() << "invalid assignment " << bv << " " << *ineq << "\n";
|
||||
}
|
||||
VERIFY(ctx.is_true(lit) == (d == 0));
|
||||
};
|
||||
for (unsigned v = 0; v < ctx.num_bool_vars(); ++v)
|
||||
check_bool_var(v);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::register_term(expr* e) {
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
expr_ref arith_plugin<int_t>::get_value(expr* e) {
|
||||
auto v = mk_var(e);
|
||||
return expr_ref(a.mk_numeral(rational(m_vars[v].m_value.get_int64(), rational::i64()), a.is_int(e)), m);
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
lbool arith_plugin<int_t>::check() {
|
||||
// repair each root literal
|
||||
for (sat::literal lit : ctx.root_literals())
|
||||
repair(lit);
|
||||
|
||||
propagate_updates();
|
||||
|
||||
// update literal assignment based on current model
|
||||
for (unsigned v = 0; v < ctx.num_bool_vars(); ++v)
|
||||
init_bool_var_assignment(v);
|
||||
|
||||
return ctx.unsat().empty() ? l_true : l_undef;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
bool arith_plugin<int_t>::is_sat() {
|
||||
for (auto const& clause : ctx.clauses()) {
|
||||
bool sat = false;
|
||||
for (auto lit : clause.m_clause) {
|
||||
if (!ctx.is_true(lit))
|
||||
continue;
|
||||
auto ineq = atom(lit.var());
|
||||
if (!ineq) {
|
||||
sat = true;
|
||||
break;
|
||||
}
|
||||
if (ineq->is_true() != lit.sign()) {
|
||||
sat = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!sat)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
std::ostream& arith_plugin<int_t>::display(std::ostream& out) const {
|
||||
for (unsigned v = 0; v < ctx.num_bool_vars(); ++v) {
|
||||
auto ineq = atom(v);
|
||||
if (ineq)
|
||||
out << v << ": " << *ineq << "\n";
|
||||
}
|
||||
for (unsigned v = 0; v < m_vars.size(); ++v) {
|
||||
auto const& vi = m_vars[v];
|
||||
out << "v" << v << " := " << vi.m_value << " " << vi.m_best_value << " ";
|
||||
out << mk_bounded_pp(vi.m_expr, m) << " - ";
|
||||
for (auto [c, bv] : vi.m_bool_vars)
|
||||
out << c << "@" << bv << " ";
|
||||
out << "\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template<typename int_t>
|
||||
void arith_plugin<int_t>::mk_model(model& mdl) {
|
||||
for (auto const& v : m_vars) {
|
||||
expr* e = v.m_expr;
|
||||
if (is_uninterp_const(e))
|
||||
mdl.register_decl(to_app(e)->get_decl(), get_value(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template class sls::arith_plugin<checked_int64<true>>;
|
||||
template class sls::arith_plugin<rational>;
|
193
src/ast/sls/sls_arith_int.h
Normal file
193
src/ast/sls/sls_arith_int.h
Normal file
|
@ -0,0 +1,193 @@
|
|||
/*++
|
||||
Copyright (c) 2020 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
arith_local_search.h
|
||||
|
||||
Abstract:
|
||||
|
||||
Theory plugin for arithmetic local search
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2020-09-08
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "util/obj_pair_set.h"
|
||||
#include "util/checked_int64.h"
|
||||
#include "ast/ast_trail.h"
|
||||
#include "ast/arith_decl_plugin.h"
|
||||
#include "ast/sls/sls_smt.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
using theory_var = int;
|
||||
|
||||
// local search portion for arithmetic
|
||||
template<typename int_t>
|
||||
class arith_plugin : public plugin {
|
||||
enum class ineq_kind { EQ, LE, LT};
|
||||
enum class var_kind { INT, REAL };
|
||||
typedef unsigned var_t;
|
||||
typedef unsigned atom_t;
|
||||
|
||||
struct config {
|
||||
double cb = 0.0;
|
||||
unsigned L = 20;
|
||||
unsigned t = 45;
|
||||
unsigned max_no_improve = 500000;
|
||||
double sp = 0.0003;
|
||||
};
|
||||
|
||||
struct stats {
|
||||
unsigned m_num_flips = 0;
|
||||
};
|
||||
|
||||
// typedef checked_int64<true> int_t;
|
||||
|
||||
public:
|
||||
struct linear_term {
|
||||
vector<std::pair<int_t, var_t>> m_args;
|
||||
int_t m_coeff;
|
||||
};
|
||||
// encode args <= bound, args = bound, args < bound
|
||||
struct ineq : public linear_term {
|
||||
ineq_kind m_op = ineq_kind::LE;
|
||||
int_t m_args_value;
|
||||
unsigned m_var_to_flip = UINT_MAX;
|
||||
|
||||
bool is_true() const {
|
||||
switch (m_op) {
|
||||
case ineq_kind::LE:
|
||||
return m_args_value + m_coeff <= 0;
|
||||
case ineq_kind::EQ:
|
||||
return m_args_value + m_coeff == 0;
|
||||
default:
|
||||
return m_args_value + m_coeff < 0;
|
||||
}
|
||||
}
|
||||
std::ostream& display(std::ostream& out) const {
|
||||
bool first = true;
|
||||
for (auto const& [c, v] : m_args)
|
||||
out << (first ? "" : " + ") << c << " * v" << v, first = false;
|
||||
if (m_coeff != 0)
|
||||
out << " + " << m_coeff;
|
||||
switch (m_op) {
|
||||
case ineq_kind::LE:
|
||||
return out << " <= " << 0 << "(" << m_args_value << ")";
|
||||
case ineq_kind::EQ:
|
||||
return out << " == " << 0 << "(" << m_args_value << ")";
|
||||
default:
|
||||
return out << " < " << 0 << "(" << m_args_value << ")";
|
||||
}
|
||||
}
|
||||
};
|
||||
private:
|
||||
|
||||
struct var_info {
|
||||
var_info(expr* e, var_kind k): m_expr(e), m_kind(k) {}
|
||||
expr* m_expr;
|
||||
int_t m_value{ 0 };
|
||||
int_t m_best_value{ 0 };
|
||||
var_kind m_kind;
|
||||
unsigned m_add_idx = UINT_MAX;
|
||||
unsigned m_mul_idx = UINT_MAX;
|
||||
vector<std::pair<int_t, sat::bool_var>> m_bool_vars;
|
||||
unsigned_vector m_muls;
|
||||
unsigned_vector m_adds;
|
||||
};
|
||||
|
||||
struct mul_def {
|
||||
unsigned m_var;
|
||||
unsigned_vector m_monomial;
|
||||
};
|
||||
|
||||
struct add_def : public linear_term {
|
||||
unsigned m_var;
|
||||
};
|
||||
|
||||
stats m_stats;
|
||||
config m_config;
|
||||
scoped_ptr_vector<ineq> m_bool_vars;
|
||||
vector<var_info> m_vars;
|
||||
vector<mul_def> m_muls;
|
||||
vector<add_def> m_adds;
|
||||
unsigned_vector m_expr2var;
|
||||
bool m_dscore_mode = false;
|
||||
arith_util a;
|
||||
|
||||
unsigned get_num_vars() const { return m_vars.size(); }
|
||||
|
||||
void repair_mul(mul_def const& md);
|
||||
void repair_add(add_def const& ad);
|
||||
unsigned_vector m_defs_to_update;
|
||||
vector<std::pair<var_t, int_t>> m_vars_to_update;
|
||||
void propagate_updates();
|
||||
void repair_defs();
|
||||
void repair(sat::literal lit);
|
||||
void repair(sat::literal lit, ineq const& ineq);
|
||||
|
||||
double reward(sat::literal lit);
|
||||
|
||||
bool sign(sat::bool_var v) const { return !ctx.is_true(sat::literal(v, false)); }
|
||||
ineq* atom(sat::bool_var bv) const { return m_bool_vars.get(bv, nullptr); }
|
||||
|
||||
|
||||
int_t dtt(bool sign, ineq const& ineq) const { return dtt(sign, ineq.m_args_value, ineq); }
|
||||
int_t dtt(bool sign, int_t const& args_value, ineq const& ineq) const;
|
||||
int_t dtt(bool sign, ineq const& ineq, var_t v, int_t const& new_value) const;
|
||||
int_t dtt(bool sign, ineq const& ineq, int_t const& coeff, int_t const& old_value, int_t const& new_value) const;
|
||||
int_t dts(unsigned cl, var_t v, int_t const& new_value) const;
|
||||
int_t compute_dts(unsigned cl) const;
|
||||
bool cm(ineq const& ineq, var_t v, int_t& new_value);
|
||||
bool cm(ineq const& ineq, var_t v, int_t const& coeff, int_t& new_value);
|
||||
int cm_score(var_t v, int_t const& new_value);
|
||||
void update(var_t v, int_t const& new_value);
|
||||
double dscore_reward(sat::bool_var v);
|
||||
double dtt_reward(sat::literal lit);
|
||||
double dscore(var_t v, int_t const& new_value) const;
|
||||
void save_best_values();
|
||||
void store_best_values();
|
||||
unsigned mk_var(expr* e);
|
||||
ineq& new_ineq(ineq_kind op, int_t const& bound);
|
||||
void add_arg(linear_term& term, int_t const& c, var_t v);
|
||||
void add_args(linear_term& term, expr* e, int_t const& sign);
|
||||
var_t mk_term(expr* e);
|
||||
void init_ineq(sat::bool_var bv, ineq& i);
|
||||
|
||||
void init_bool_var_assignment(sat::bool_var v);
|
||||
|
||||
int_t value(var_t v) const { return m_vars[v].m_value; }
|
||||
bool is_int64(expr* e, int_t& i);
|
||||
bool is_int(expr* e, int_t& i);
|
||||
|
||||
void check_ineqs();
|
||||
|
||||
public:
|
||||
arith_plugin(context& ctx);
|
||||
~arith_plugin() override {}
|
||||
void init_bool_var(sat::bool_var v) override;
|
||||
void register_term(expr* e) override;
|
||||
expr_ref get_value(expr* e) override;
|
||||
lbool check() override;
|
||||
bool is_sat() override;
|
||||
void reset() override;
|
||||
|
||||
void on_rescale() override;
|
||||
void on_restart() override;
|
||||
std::ostream& display(std::ostream& out) const override;
|
||||
void mk_model(model& mdl) override;
|
||||
};
|
||||
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, typename arith_plugin<checked_int64<true>>::ineq const& ineq) {
|
||||
return ineq.display(out);
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, typename arith_plugin<rational>::ineq const& ineq) {
|
||||
return ineq.display(out);
|
||||
}
|
||||
}
|
145
src/ast/sls/sls_cc.cpp
Normal file
145
src/ast/sls/sls_cc.cpp
Normal file
|
@ -0,0 +1,145 @@
|
|||
/*++
|
||||
Copyright (c) 2024 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
sls_cc.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
Congruence Closure for SLS
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2024-06-24
|
||||
|
||||
--*/
|
||||
|
||||
#include "ast/sls/sls_cc.h"
|
||||
#include "ast/ast_ll_pp.h"
|
||||
#include "ast/ast_pp.h"
|
||||
|
||||
|
||||
namespace sls {
|
||||
|
||||
cc_plugin::cc_plugin(context& c):
|
||||
plugin(c),
|
||||
m_values(8U, value_hash(*this), value_eq(*this)) {
|
||||
m_fid = m.mk_family_id("cc");
|
||||
}
|
||||
|
||||
cc_plugin::~cc_plugin() {}
|
||||
|
||||
expr_ref cc_plugin::get_value(expr* e) {
|
||||
UNREACHABLE();
|
||||
return expr_ref(m);
|
||||
}
|
||||
|
||||
void cc_plugin::reset() {
|
||||
m_app.reset();
|
||||
}
|
||||
|
||||
void cc_plugin::register_term(expr* e) {
|
||||
if (!is_app(e))
|
||||
return;
|
||||
if (!is_uninterp(e))
|
||||
return;
|
||||
app* a = to_app(e);
|
||||
if (a->get_num_args() == 0)
|
||||
return;
|
||||
auto f = a->get_decl();
|
||||
if (!m_app.contains(f))
|
||||
m_app.insert(f, ptr_vector<app>());
|
||||
m_app[f].push_back(a);
|
||||
}
|
||||
|
||||
unsigned cc_plugin::value_hash::operator()(app* t) const {
|
||||
unsigned r = 0;
|
||||
for (auto arg : *t)
|
||||
r *= 3, r += cc.ctx.get_value(arg)->hash();
|
||||
return r;
|
||||
}
|
||||
|
||||
bool cc_plugin::value_eq::operator()(app* a, app* b) const {
|
||||
SASSERT(a->get_num_args() == b->get_num_args());
|
||||
for (unsigned i = a->get_num_args(); i-- > 0; )
|
||||
if (cc.ctx.get_value(a->get_arg(i)) != cc.ctx.get_value(b->get_arg(i)))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool cc_plugin::is_sat() {
|
||||
for (auto& [f, ts] : m_app) {
|
||||
if (ts.size() <= 1)
|
||||
continue;
|
||||
m_values.reset();
|
||||
for (auto* t : ts) {
|
||||
app* u;
|
||||
if (!ctx.is_relevant(t))
|
||||
continue;
|
||||
if (m_values.find(t, u)) {
|
||||
if (ctx.get_value(t) != ctx.get_value(u))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
m_values.insert(t);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
lbool cc_plugin::check() {
|
||||
bool new_constraint = false;
|
||||
for (auto & [f, ts] : m_app) {
|
||||
if (ts.size() <= 1)
|
||||
continue;
|
||||
m_values.reset();
|
||||
for (auto * t : ts) {
|
||||
app* u;
|
||||
if (!ctx.is_relevant(t))
|
||||
continue;
|
||||
if (m_values.find(t, u)) {
|
||||
if (ctx.get_value(t) == ctx.get_value(u))
|
||||
continue;
|
||||
expr_ref_vector ors(m);
|
||||
for (unsigned i = t->get_num_args(); i-- > 0; )
|
||||
ors.push_back(m.mk_not(m.mk_eq(t->get_arg(i), u->get_arg(i))));
|
||||
ors.push_back(m.mk_eq(t, u));
|
||||
ctx.add_constraint(m.mk_or(ors));
|
||||
new_constraint = true;
|
||||
}
|
||||
else
|
||||
m_values.insert(t);
|
||||
}
|
||||
}
|
||||
return new_constraint ? l_undef : l_true;
|
||||
}
|
||||
|
||||
std::ostream& cc_plugin::display(std::ostream& out) const {
|
||||
for (auto& [f, ts] : m_app) {
|
||||
for (auto* t : ts)
|
||||
out << mk_bounded_pp(t, m) << "\n";
|
||||
out << "\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void cc_plugin::mk_model(model& mdl) {
|
||||
expr_ref_vector args(m);
|
||||
for (auto& [f, ts] : m_app) {
|
||||
func_interp* fi = alloc(func_interp, m, f->get_arity());
|
||||
mdl.register_decl(f, fi);
|
||||
m_values.reset();
|
||||
for (auto* t : ts) {
|
||||
if (m_values.contains(t))
|
||||
continue;
|
||||
args.reset();
|
||||
expr_ref val = ctx.get_value(t);
|
||||
for (auto arg : *t)
|
||||
args.push_back(ctx.get_value(arg));
|
||||
fi->insert_new_entry(args.data(), val);
|
||||
m_values.insert(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
51
src/ast/sls/sls_cc.h
Normal file
51
src/ast/sls/sls_cc.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*++
|
||||
Copyright (c) 2024 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
cc_sls.h
|
||||
|
||||
Abstract:
|
||||
|
||||
Congruence Closure for SLS
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2024-06-24
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "util/hashtable.h"
|
||||
#include "ast/sls/sls_smt.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
class cc_plugin : public plugin {
|
||||
obj_map<func_decl, ptr_vector<app>> m_app;
|
||||
struct value_hash {
|
||||
cc_plugin& cc;
|
||||
value_hash(cc_plugin& cc) : cc(cc) {}
|
||||
unsigned operator()(app* t) const;
|
||||
};
|
||||
struct value_eq {
|
||||
cc_plugin& cc;
|
||||
value_eq(cc_plugin& cc) : cc(cc) {}
|
||||
bool operator()(app* a, app* b) const;
|
||||
};
|
||||
hashtable<app*, value_hash, value_eq> m_values;
|
||||
public:
|
||||
cc_plugin(context& c);
|
||||
~cc_plugin() override;
|
||||
family_id fid() { return m_fid; }
|
||||
expr_ref get_value(expr* e) override;
|
||||
lbool check() override;
|
||||
bool is_sat() override;
|
||||
void reset() override;
|
||||
void register_term(expr* e) override;
|
||||
void init_bool_var(sat::bool_var v) override {}
|
||||
std::ostream& display(std::ostream& out) const override;
|
||||
void mk_model(model& mdl) override;
|
||||
};
|
||||
|
||||
}
|
256
src/ast/sls/sls_smt.cpp
Normal file
256
src/ast/sls/sls_smt.cpp
Normal file
|
@ -0,0 +1,256 @@
|
|||
/*++
|
||||
Copyright (c) 2024 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
smt_sls.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
A Stochastic Local Search (SLS) Context.
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2024-06-24
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "ast/sls/sls_smt.h"
|
||||
#include "ast/sls/sls_cc.h"
|
||||
#include "ast/sls/sls_arith_int.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
plugin::plugin(context& c):
|
||||
ctx(c),
|
||||
m(c.get_manager()) {
|
||||
reset();
|
||||
}
|
||||
|
||||
context::context(ast_manager& m, sat_solver_context& s) :
|
||||
m(m), s(s), m_atoms(m), m_subterms(m) {
|
||||
reset();
|
||||
}
|
||||
|
||||
void context::register_plugin(plugin* p) {
|
||||
m_plugins.reserve(p->fid() + 1);
|
||||
m_plugins.set(p->fid(), p);
|
||||
}
|
||||
|
||||
void context::register_atom(sat::bool_var v, expr* e) {
|
||||
m_atoms.setx(v, e);
|
||||
m_atom2bool_var.setx(e->get_id(), v, UINT_MAX);
|
||||
}
|
||||
|
||||
typedef arith_plugin<checked_int64<true>> arith64;
|
||||
|
||||
void context::reset() {
|
||||
m_plugins.reset();
|
||||
m_atoms.reset();
|
||||
m_atom2bool_var.reset();
|
||||
m_initialized = false;
|
||||
m_parents.reset();
|
||||
m_relevant.reset();
|
||||
m_visited.reset();
|
||||
m_subterms.reset();
|
||||
register_plugin(alloc(cc_plugin, *this));
|
||||
register_plugin(alloc(arith64, *this));
|
||||
}
|
||||
|
||||
lbool context::check() {
|
||||
//
|
||||
// initialize data-structures if not done before.
|
||||
// identify minimal feasible assignment to literals.
|
||||
// sub-expressions within assignment are relevant.
|
||||
// Use timestamps to make it incremental.
|
||||
//
|
||||
init();
|
||||
while (unsat().empty()) {
|
||||
reinit_relevant();
|
||||
for (auto p : m_plugins) {
|
||||
lbool r;
|
||||
if (p && (r = p->check()) != l_true)
|
||||
return r;
|
||||
}
|
||||
if (m_new_constraint)
|
||||
return l_undef;
|
||||
if (all_of(m_plugins, [&](auto* p) { return !p || p->is_sat(); })) {
|
||||
model_ref mdl = alloc(model, m);
|
||||
for (auto p : m_plugins)
|
||||
if (p)
|
||||
p->mk_model(*mdl);
|
||||
s.on_model(mdl);
|
||||
verbose_stream() << *mdl << "\n";
|
||||
return l_true;
|
||||
}
|
||||
}
|
||||
return l_undef;
|
||||
}
|
||||
|
||||
expr_ref context::get_value(expr* e) {
|
||||
if (m.is_bool(e)) {
|
||||
auto v = m_atom2bool_var[e->get_id()];
|
||||
return expr_ref(is_true(sat::literal(v, false)) ? m.mk_true() : m.mk_false(), m);
|
||||
}
|
||||
sort* s = e->get_sort();
|
||||
auto fid = s->get_family_id();
|
||||
auto p = m_plugins.get(fid, nullptr);
|
||||
if (p)
|
||||
return p->get_value(e);
|
||||
UNREACHABLE();
|
||||
return expr_ref(e, m);
|
||||
}
|
||||
|
||||
void context::set_value(expr* e, expr* v) {
|
||||
NOT_IMPLEMENTED_YET();
|
||||
}
|
||||
|
||||
bool context::is_relevant(expr* e) {
|
||||
unsigned id = e->get_id();
|
||||
if (m_relevant.contains(id))
|
||||
return true;
|
||||
if (m_visited.contains(id))
|
||||
return false;
|
||||
m_visited.insert(id);
|
||||
for (auto p : m_parents[id]) {
|
||||
if (is_relevant(p)) {
|
||||
m_relevant.insert(id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void context::add_constraint(expr* e) {
|
||||
expr_ref _e(e, m);
|
||||
sat::literal_vector lits;
|
||||
auto add_literal = [&](expr* e) {
|
||||
bool is_neg = m.is_not(e, e);
|
||||
auto v = mk_atom(e);
|
||||
lits.push_back(sat::literal(v, is_neg));
|
||||
};
|
||||
if (m.is_or(e))
|
||||
for (auto arg : *to_app(e))
|
||||
add_literal(arg);
|
||||
else
|
||||
add_literal(e);
|
||||
TRACE("sls", tout << "new clause " << lits << "\n");
|
||||
s.add_clause(lits.size(), lits.data());
|
||||
m_new_constraint = true;
|
||||
}
|
||||
|
||||
sat::bool_var context::mk_atom(expr* e) {
|
||||
auto v = m_atom2bool_var.get(e->get_id(), sat::null_bool_var);
|
||||
if (v == sat::null_bool_var) {
|
||||
v = s.add_var();
|
||||
register_subterms(e);
|
||||
register_atom(v, e);
|
||||
init_bool_var(v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
void context::init_bool_var(sat::bool_var v) {
|
||||
for (auto p : m_plugins)
|
||||
if (p)
|
||||
p->init_bool_var(v);
|
||||
}
|
||||
|
||||
void context::init() {
|
||||
m_new_constraint = false;
|
||||
if (m_initialized)
|
||||
return;
|
||||
m_initialized = true;
|
||||
register_terms();
|
||||
for (sat::bool_var v = 0; v < num_bool_vars(); ++v)
|
||||
init_bool_var(v);
|
||||
}
|
||||
|
||||
void context::register_terms() {
|
||||
for (auto a : m_atoms)
|
||||
if (a)
|
||||
register_subterms(a);
|
||||
}
|
||||
|
||||
void context::register_subterms(expr* e) {
|
||||
auto is_visited = [&](expr* e) {
|
||||
return nullptr != m_subterms.get(e->get_id(), nullptr);
|
||||
};
|
||||
auto visit = [&](expr* e) {
|
||||
m_subterms.setx(e->get_id(), e);
|
||||
};
|
||||
if (is_visited(e))
|
||||
return;
|
||||
m_todo.push_back(e);
|
||||
while (!m_todo.empty()) {
|
||||
expr* e = m_todo.back();
|
||||
if (is_visited(e))
|
||||
m_todo.pop_back();
|
||||
else if (is_app(e)) {
|
||||
if (all_of(*to_app(e), [&](expr* arg) { return is_visited(arg); })) {
|
||||
for (expr* arg : *to_app(e)) {
|
||||
m_parents.reserve(arg->get_id() + 1);
|
||||
m_parents[arg->get_id()].push_back(e);
|
||||
}
|
||||
register_term(e);
|
||||
visit(e);
|
||||
m_todo.pop_back();
|
||||
}
|
||||
else {
|
||||
for (expr* arg : *to_app(e))
|
||||
m_todo.push_back(arg);
|
||||
}
|
||||
}
|
||||
else {
|
||||
register_term(e);
|
||||
visit(e);
|
||||
m_todo.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void context::register_term(expr* e) {
|
||||
for (auto p : m_plugins)
|
||||
if (p)
|
||||
p->register_term(e);
|
||||
}
|
||||
|
||||
void context::reinit_relevant() {
|
||||
m_relevant.reset();
|
||||
m_visited.reset();
|
||||
m_root_literals.reset();
|
||||
for (auto const& clause : s.clauses()) {
|
||||
bool has_relevant = false;
|
||||
unsigned n = 0;
|
||||
sat::literal selected_lit = sat::null_literal;
|
||||
for (auto lit : clause) {
|
||||
auto atm = m_atoms.get(lit.var(), nullptr);
|
||||
if (!atm)
|
||||
continue;
|
||||
auto a = atm->get_id();
|
||||
if (!is_true(lit))
|
||||
continue;
|
||||
if (m_relevant.contains(a)) {
|
||||
has_relevant = true;
|
||||
break;
|
||||
}
|
||||
if (m_rand() % ++n == 0)
|
||||
selected_lit = lit;
|
||||
}
|
||||
if (!has_relevant && selected_lit != sat::null_literal) {
|
||||
m_relevant.insert(m_atoms[selected_lit.var()]->get_id());
|
||||
m_root_literals.push_back(selected_lit);
|
||||
}
|
||||
}
|
||||
shuffle(m_root_literals.size(), m_root_literals.data(), m_rand);
|
||||
}
|
||||
|
||||
std::ostream& context::display(std::ostream& out) const {
|
||||
for (auto p : m_plugins) {
|
||||
if (p)
|
||||
p->display(out);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
125
src/ast/sls/sls_smt.h
Normal file
125
src/ast/sls/sls_smt.h
Normal file
|
@ -0,0 +1,125 @@
|
|||
/*++
|
||||
Copyright (c) 2024 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
smt_sls.h
|
||||
|
||||
Abstract:
|
||||
|
||||
A Stochastic Local Search (SLS) Context.
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2024-06-24
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "util/sat_literal.h"
|
||||
#include "util/sat_sls.h"
|
||||
#include "ast/ast.h"
|
||||
#include "model/model.h"
|
||||
#include "util/scoped_ptr_vector.h"
|
||||
#include "util/obj_hashtable.h"
|
||||
|
||||
namespace sls {
|
||||
|
||||
class context;
|
||||
|
||||
class plugin {
|
||||
protected:
|
||||
context& ctx;
|
||||
ast_manager& m;
|
||||
family_id m_fid;
|
||||
public:
|
||||
plugin(context& c);
|
||||
virtual ~plugin() {}
|
||||
virtual family_id fid() { return m_fid; }
|
||||
virtual void register_term(expr* e) = 0;
|
||||
virtual expr_ref get_value(expr* e) = 0;
|
||||
virtual void init_bool_var(sat::bool_var v) = 0;
|
||||
virtual lbool check() = 0;
|
||||
virtual bool is_sat() = 0;
|
||||
virtual void reset() {};
|
||||
virtual void on_rescale() {};
|
||||
virtual void on_restart() {};
|
||||
virtual std::ostream& display(std::ostream& out) const = 0;
|
||||
virtual void mk_model(model& mdl) = 0;
|
||||
};
|
||||
|
||||
using clause = std::initializer_list <sat::literal>;
|
||||
|
||||
class sat_solver_context {
|
||||
public:
|
||||
virtual vector<sat::clause_info> const& clauses() const = 0;
|
||||
virtual sat::clause_info const& get_clause(unsigned idx) const = 0;
|
||||
virtual std::initializer_list<unsigned> get_use_list(sat::literal lit) = 0;
|
||||
virtual void flip(sat::bool_var v) = 0;
|
||||
virtual double reward(sat::bool_var v) = 0;
|
||||
virtual double get_weigth(unsigned clause_idx) = 0;
|
||||
virtual bool is_true(sat::literal lit) = 0;
|
||||
virtual unsigned num_vars() const = 0;
|
||||
virtual indexed_uint_set const& unsat() const = 0;
|
||||
virtual void on_model(model_ref& mdl) = 0;
|
||||
virtual sat::bool_var add_var() = 0;
|
||||
virtual void add_clause(unsigned n, sat::literal const* lits) = 0;
|
||||
};
|
||||
|
||||
class context {
|
||||
ast_manager& m;
|
||||
sat_solver_context& s;
|
||||
scoped_ptr_vector<plugin> m_plugins;
|
||||
indexed_uint_set m_relevant, m_visited;
|
||||
expr_ref_vector m_atoms;
|
||||
unsigned_vector m_atom2bool_var;
|
||||
vector<ptr_vector<expr>> m_parents;
|
||||
sat::literal_vector m_root_literals;
|
||||
random_gen m_rand;
|
||||
bool m_initialized = false;
|
||||
bool m_new_constraint = false;
|
||||
expr_ref_vector m_subterms;
|
||||
|
||||
void register_plugin(plugin* p);
|
||||
|
||||
void init();
|
||||
void init_bool_var(sat::bool_var v);
|
||||
void register_terms();
|
||||
ptr_vector<expr> m_todo;
|
||||
void register_subterms(expr* e);
|
||||
void register_term(expr* e);
|
||||
sat::bool_var mk_atom(expr* e);
|
||||
|
||||
public:
|
||||
context(ast_manager& m, sat_solver_context& s);
|
||||
|
||||
// Between SAT/SMT solver and context.
|
||||
void register_atom(sat::bool_var v, expr* e);
|
||||
void reset();
|
||||
lbool check();
|
||||
|
||||
// expose sat_solver to plugins
|
||||
vector<sat::clause_info> const& clauses() const { return s.clauses(); }
|
||||
sat::clause_info const& get_clause(unsigned idx) const { return s.get_clause(idx); }
|
||||
std::initializer_list<unsigned> get_use_list(sat::literal lit) { return s.get_use_list(lit); }
|
||||
double get_weight(unsigned clause_idx) { return s.get_weigth(clause_idx); }
|
||||
unsigned num_bool_vars() const { return s.num_vars(); }
|
||||
bool is_true(sat::literal lit) { return s.is_true(lit); }
|
||||
expr* atom(sat::bool_var v) { return m_atoms.get(v, nullptr); }
|
||||
void flip(sat::bool_var v) { s.flip(v); }
|
||||
double reward(sat::bool_var v) { return s.reward(v); }
|
||||
indexed_uint_set const& unsat() const { return s.unsat(); }
|
||||
unsigned rand() { return m_rand(); }
|
||||
sat::literal_vector const& root_literals() const { return m_root_literals; }
|
||||
|
||||
void reinit_relevant();
|
||||
|
||||
// Between plugin solvers
|
||||
expr_ref get_value(expr* e);
|
||||
void set_value(expr* e, expr* v);
|
||||
bool is_relevant(expr* e);
|
||||
void add_constraint(expr* e);
|
||||
ast_manager& get_manager() { return m; }
|
||||
std::ostream& display(std::ostream& out) const;
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue