3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-10-29 02:39:24 +00:00

Almost cleaned up version.

This commit is contained in:
Andreas Froehlich 2014-04-22 00:32:45 +01:00
parent 5ab65d52a6
commit c1741d7941
6 changed files with 177 additions and 490 deletions

View file

@ -39,11 +39,7 @@ class sls_tracker {
mpz m_zero, m_one, m_two;
struct value_score {
#if _EARLY_PRUNE_
value_score() : m(0), value(unsynch_mpz_manager::mk_z(0)), score(0.0), distance(0), touched(1), score_prune(0.0), has_pos_occ(0), has_neg_occ(0) { };
#else
value_score() : m(0), value(unsynch_mpz_manager::mk_z(0)), score(0.0), distance(0), touched(1) { };
#endif
~value_score() { if (m) m->del(value); }
unsynch_mpz_manager * m;
mpz value;
@ -80,20 +76,22 @@ private:
ptr_vector<func_decl> m_constants;
ptr_vector<func_decl> m_temp_constants;
occ_type m_constants_occ;
#if _UCT_
unsigned m_walksat;
unsigned m_ucb;
double m_ucb_constant;
unsigned m_ucb_init;
double m_ucb_forget;
unsigned m_touched;
#endif
double m_scale_unsat;
unsigned m_paws_init;
#if _REAL_RS_
ptr_vector<expr> m_unsat_expr;
obj_map<expr, unsigned> m_where_false;
expr** m_list_false;
#endif
#if _PAWS_
obj_map<expr, unsigned> m_weights;
#endif
#if _CACHE_TOP_SCORE_
double m_top_sum;
#endif
unsigned m_equal_scores;
public:
@ -114,6 +112,17 @@ public:
m_mpz_manager.del(m_two);
}
void updt_params(params_ref const & _p) {
sls_params p(_p);
m_walksat = p.walksat();
m_ucb = p.walksat_ucb();
m_ucb_constant = p.walksat_ucb_constant();
m_ucb_init = p.walksat_ucb_init();
m_ucb_forget = p.walksat_ucb_forget();
m_scale_unsat = p.scale_unsat();
m_paws_init = p.paws_init();
}
unsigned get_formula_size() {
return m_scores.size();
}
@ -147,13 +156,8 @@ public:
return ++m_equal_scores;
}
#if _CACHE_TOP_SCORE_
inline void adapt_top_sum(expr * e, double add, double sub) {
#if _PAWS_
m_top_sum += m_weights.find(e) * (add - sub);
#else
m_top_sum += add - sub;
#endif
}
inline void set_top_sum(double new_score) {
@ -163,7 +167,6 @@ public:
inline double get_top_sum() {
return m_top_sum;
}
#endif
inline void set_value(expr * n, const mpz & r) {
SASSERT(m_scores.contains(n));
@ -209,7 +212,6 @@ public:
return get_score(ep);
}
#if _EARLY_PRUNE_
inline void set_score_prune(expr * n, double score) {
SASSERT(m_scores.contains(n));
m_scores.find(n).score_prune = score;
@ -229,7 +231,6 @@ public:
SASSERT(m_scores.contains(n));
return m_scores.find(n).has_neg_occ;
}
#endif
inline unsigned get_distance(expr * n) {
SASSERT(m_scores.contains(n));
@ -310,17 +311,20 @@ public:
}
#endif
void uct_forget(ptr_vector<expr> & as) {
expr * e;
unsigned touched_old, touched_new;
for (unsigned i = 0; i < as.size(); i++)
inline void ucb_forget(ptr_vector<expr> & as) {
if (m_ucb_forget < 1.0)
{
e = as[i];
touched_old = m_scores.find(e).touched;
touched_new = (unsigned)((touched_old - 1) * _UCT_FORGET_FACTOR_ + 1);
m_scores.find(e).touched = touched_new;
m_touched += touched_new - touched_old;
expr * e;
unsigned touched_old, touched_new;
for (unsigned i = 0; i < as.size(); i++)
{
e = as[i];
touched_old = m_scores.find(e).touched;
touched_new = (unsigned)((touched_old - 1) * m_ucb_forget + 1);
m_scores.find(e).touched = touched_new;
m_touched += touched_new - touched_old;
}
}
}
@ -490,26 +494,21 @@ public:
//}
#endif
#if _PAWS_
for (unsigned i = 0; i < sz; i++)
{
expr * e = as[i];
if (!m_weights.contains(e))
m_weights.insert(e, _PAWS_INIT_);
m_weights.insert(e, m_paws_init);
}
#endif
#if _EARLY_PRUNE_
for (unsigned i = 0; i < sz; i++)
setup_occs(as[i]);
#endif
#if _UCT_
m_touched = _UCT_INIT_ ? as.size() : 1;
#endif
m_touched = m_ucb_init ? as.size() : 1;
}
#if _PAWS_
void increase_weight(expr * e)
{
m_weights.find(e)++;
@ -518,14 +517,13 @@ public:
void decrease_weight(expr * e)
{
unsigned old_weight = m_weights.find(e);
m_weights.find(e) = old_weight > _PAWS_INIT_ ? old_weight - 1 : _PAWS_INIT_;
m_weights.find(e) = old_weight > m_paws_init ? old_weight - 1 : m_paws_init;
}
unsigned get_weight(expr * e)
{
return m_weights.find(e);
}
#endif
#if _REAL_RS_
void make_assertion(expr * e)
@ -684,7 +682,6 @@ public:
}
}
#if _EARLY_PRUNE_
void setup_occs(expr * n, bool negated = false) {
if (m_manager.is_bool(n))
{
@ -717,7 +714,6 @@ public:
else
NOT_IMPLEMENTED_YET();
}
#endif
double score_bool(expr * n, bool negated = false) {
TRACE("sls_score", tout << ((negated)?"NEG ":"") << "BOOL: " << mk_ismt2_pp(n, m_manager) << std::endl; );
@ -735,20 +731,17 @@ public:
SASSERT(!negated);
app * a = to_app(n);
expr * const * args = a->get_args();
// Andreas: Seems to have no effect. Probably it does not even occur.
#if _SCORE_AND_AVG_
/* Andreas: Seems to have no effect. But maybe you want to try it again at some point.
double sum = 0.0;
for (unsigned i = 0; i < a->get_num_args(); i++)
sum += get_score(args[i]);
res = sum / (double) a->get_num_args();
#else
res = sum / (double) a->get_num_args(); */
double min = 1.0;
for (unsigned i = 0; i < a->get_num_args(); i++) {
double cur = get_score(args[i]);
if (cur < min) min = cur;
}
res = min;
#endif
}
else if (m_manager.is_or(n)) {
SASSERT(!negated);
@ -927,13 +920,11 @@ public:
SASSERT(res >= 0.0 && res <= 1.0);
#if _WEIGHT_DIST_
app * a = to_app(n);
family_id afid = a->get_family_id();
if (afid == m_bv_util.get_family_id())
if (res < 1.0) res *= _WEIGHT_DIST_FACTOR_;
#endif
if (res < 1.0) res *= m_scale_unsat;
TRACE("sls_score", tout << "SCORE = " << res << std::endl; );
return res;
@ -995,8 +986,8 @@ public:
return m_temp_constants;
}
ptr_vector<func_decl> & get_unsat_constants_gsat(ptr_vector<expr> const & as, unsigned sz) {
ptr_vector<func_decl> & get_unsat_constants_gsat(ptr_vector<expr> const & as) {
unsigned sz = as.size();
if (sz == 1) {
if (m_mpz_manager.neq(get_value(as[0]), m_one))
return get_constants();
@ -1033,89 +1024,88 @@ public:
}
ptr_vector<func_decl> & get_unsat_constants(ptr_vector<expr> const & as) {
#if _FOCUS_
expr * e = get_unsat_assertion(as);
if (!e)
if (m_walksat)
{
m_temp_constants.reset();
return m_temp_constants;
}
expr * e = get_unsat_assertion(as);
return get_unsat_constants_walksat(e);
#else
return m_tracker.get_unsat_constants_gsat(as, sz);
#endif
if (!e)
{
m_temp_constants.reset();
return m_temp_constants;
}
return get_unsat_constants_walksat(e);
}
else
return get_unsat_constants_gsat(as);
}
expr * get_unsat_assertion(ptr_vector<expr> const & as) {
unsigned sz = as.size();
if (sz == 1) {
if (m_mpz_manager.neq(get_value(as[0]), m_one))
return as[0];
else
return 0;
}
m_temp_constants.reset();
#if _UCT_
unsigned pos = -1;
value_score vscore;
double max = -1.0;
if (m_ucb)
{
value_score vscore;
double max = -1.0;
for (unsigned i = 0; i < sz; i++) {
expr * e = as[i];
// for (unsigned i = 0; i < m_where_false.size(); i++) {
// expr * e = m_list_false[i];
vscore = m_scores.find(e);
#if _UCT_ == 1
double q = vscore.score + _UCT_CONSTANT_ * sqrt(log((double)m_touched) / vscore.touched);
#elif _UCT_ == 3
double q = vscore.score + m_ucb_constant * sqrt(log((double)m_touched) / vscore.touched);
#if _UCT_ == 3
double q = vscore.score + _UCT_CONSTANT_ * sqrt(log(m_touched)/vscore.touched) + (get_random_uint(16) * 0.1 / (2<<16));
#endif
if (q > max && m_mpz_manager.neq(get_value(e), m_one) ) { max = q; pos = i; }
if (q > max && m_mpz_manager.neq(get_value(e), m_one) ) { max = q; pos = i; }
}
if (pos == static_cast<unsigned>(-1))
return 0;
if (pos == static_cast<unsigned>(-1))
return 0;
#if _UCT_
m_scores.find(as[pos]).touched++;
m_touched++;
#endif
m_scores.find(as[pos]).touched++;
m_touched++;
// return m_list_false[pos];
}
else
{
unsigned cnt_unsat = 0;
for (unsigned i = 0; i < sz; i++)
if (m_mpz_manager.neq(get_value(as[i]), m_one) && (get_random_uint(16) % ++cnt_unsat == 0)) pos = i;
if (pos == static_cast<unsigned>(-1))
return 0;
}
return as[pos];
#elif _REAL_RS_
#if _REAL_RS_
//unsigned pos = m_false_list[get_random_uint(16) % m_cnt_false];
//expr * e = m_unsat_expr[get_random_uint(16) % m_unsat_expr.size()];
sz = m_where_false.size();
if (sz == 0)
return 0;
return m_list_false[get_random_uint(16) % sz];
#else
unsigned cnt_unsat = 0, pos = -1;
for (unsigned i = 0; i < sz; i++)
if (m_mpz_manager.neq(get_value(as[i]), m_one) && (get_random_uint(16) % ++cnt_unsat == 0)) pos = i;
if (pos == static_cast<unsigned>(-1))
return 0;
#endif
return as[pos];
}
expr * get_new_unsat_assertion(goal_ref const & g, expr * e) {
unsigned sz = g->size();
expr * get_new_unsat_assertion(ptr_vector<expr> const & as, expr * e) {
unsigned sz = as.size();
if (sz == 1)
return 0;
m_temp_constants.reset();
unsigned cnt_unsat = 0, pos = -1;
for (unsigned i = 0; i < sz; i++)
if (m_mpz_manager.neq(get_value(g->form(i)), m_one) && (get_random_uint(16) % ++cnt_unsat == 0) && (g->form(i) != e)) pos = i;
if (m_mpz_manager.neq(get_value(as[i]), m_one) && (get_random_uint(16) % ++cnt_unsat == 0) && (as[i] != e)) pos = i;
if (pos == static_cast<unsigned>(-1))
return 0;
return g->form(pos);
return as[pos];
}
};