3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-23 09:05:31 +00:00

fix mixed integer/real bugs for maximization exposed by non-termination in slow.smt. partially fixes issue #56

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2015-06-23 12:05:19 +02:00
parent d32e4a9476
commit d9522cfd07
5 changed files with 130 additions and 175 deletions

View file

@ -27,10 +27,8 @@ Notes:
typedef obj_map<expr, expr *> expr2expr_map;
void extract_clauses_and_dependencies(goal_ref const& g, expr_ref_vector& clauses, ptr_vector<expr>& assumptions, scoped_ptr<expr2expr_map>& bool2dep, ref<filter_model_converter>& fmc) {
scoped_ptr<expr2expr_map> dep2bool;
dep2bool = alloc(expr2expr_map);
bool2dep = alloc(expr2expr_map);
void extract_clauses_and_dependencies(goal_ref const& g, expr_ref_vector& clauses, ptr_vector<expr>& assumptions, expr2expr_map& bool2dep, ref<filter_model_converter>& fmc) {
expr2expr_map dep2bool;
ptr_vector<expr> deps;
ast_manager& m = g->m();
expr_ref_vector clause(m);
@ -38,7 +36,7 @@ void extract_clauses_and_dependencies(goal_ref const& g, expr_ref_vector& clause
for (unsigned i = 0; i < sz; i++) {
expr * f = g->form(i);
expr_dependency * d = g->dep(i);
if (d == 0) {
if (d == 0 || !g->unsat_core_enabled()) {
clauses.push_back(f);
}
else {
@ -54,19 +52,19 @@ void extract_clauses_and_dependencies(goal_ref const& g, expr_ref_vector& clause
expr * d = *it;
if (is_uninterp_const(d) && m.is_bool(d)) {
// no need to create a fresh boolean variable for d
if (!bool2dep->contains(d)) {
if (!bool2dep.contains(d)) {
assumptions.push_back(d);
bool2dep->insert(d, d);
bool2dep.insert(d, d);
}
clause.push_back(m.mk_not(d));
}
else {
// must normalize assumption
expr * b = 0;
if (!dep2bool->find(d, b)) {
if (!dep2bool.find(d, b)) {
b = m.mk_fresh_const(0, m.mk_bool_sort());
dep2bool->insert(d, b);
bool2dep->insert(b, d);
dep2bool.insert(d, b);
bool2dep.insert(b, d);
assumptions.push_back(b);
if (!fmc) {
fmc = alloc(filter_model_converter, m);
@ -78,7 +76,7 @@ void extract_clauses_and_dependencies(goal_ref const& g, expr_ref_vector& clause
}
SASSERT(clause.size() > 1);
expr_ref cls(m);
cls = mk_or(m, clauses.size(), clauses.c_ptr());
cls = mk_or(m, clause.size(), clause.c_ptr());
clauses.push_back(cls);
}
}
@ -208,7 +206,7 @@ public:
SASSERT(m_ctx != 0);
expr_ref_vector clauses(m);
scoped_ptr<expr2expr_map> bool2dep;
expr2expr_map bool2dep;
ptr_vector<expr> assumptions;
ref<filter_model_converter> fmc;
if (in->unsat_core_enabled()) {
@ -273,7 +271,7 @@ public:
for (unsigned i = 0; i < sz; i++) {
expr * b = m_ctx->get_unsat_core_expr(i);
SASSERT(is_uninterp_const(b) && m.is_bool(b));
expr * d = bool2dep->find(b);
expr * d = bool2dep.find(b);
lcore = m.mk_join(lcore, m.mk_leaf(d));
}
}

View file

@ -32,7 +32,7 @@ tactic * mk_smt_tactic(params_ref const & p = params_ref());
tactic * mk_smt_tactic_using(bool auto_config = true, params_ref const & p = params_ref());
void extract_clauses_and_dependencies(goal_ref const& g, expr_ref_vector& clauses, ptr_vector<expr>& assumptions, scoped_ptr<obj_map<expr, expr*> >& bool2dep, ref<filter_model_converter>& fmc);
void extract_clauses_and_dependencies(goal_ref const& g, expr_ref_vector& clauses, ptr_vector<expr>& assumptions, obj_map<expr, expr*>& bool2dep, ref<filter_model_converter>& fmc);
/*
ADD_TACTIC("smt", "apply a SAT based SMT solver.", "mk_smt_tactic(p)")

View file

@ -879,7 +879,6 @@ namespace smt {
row m_tmp_row;
void add_tmp_row(row & r1, numeral const & coeff, row const & r2);
theory_var pick_var_to_leave(bool has_int, theory_var x_j, bool inc, numeral & a_ij, inf_numeral & gain, bool& skiped_row);
bool is_safe_to_leave(theory_var x, bool inc, bool& has_int, bool& is_shared);
template<bool invert>
void add_tmp_row_entry(row & r, numeral const & coeff, theory_var v);

View file

@ -989,83 +989,6 @@ namespace smt {
}
/**
\brief Select tightest variable x_i to pivot with x_j. The goal
is to select a x_i such that the value of x_j is increased
(decreased) if inc = true (inc = false), and the tableau
remains feasible. Store the gain in x_j of the pivoting
operation in 'gain'. Note the gain can be too much. That is,
it may make x_i infeasible. In this case, instead of pivoting
we move x_j to its upper bound (lower bound) when inc = true (inc = false).
If no x_i imposes a restriction on x_j, then return null_theory_var.
That is, x_j is free to move to its upper bound (lower bound).
Get the equations for x_j:
x_i1 = coeff_1 * x_j + rest_1
...
x_in = coeff_n * x_j + rest_n
gain_k := (upper_bound(x_ik) - value(x_ik))/coeff_k
*/
template<typename Ext>
theory_var theory_arith<Ext>::pick_var_to_leave(
bool has_int, theory_var x_j, bool inc,
numeral & a_ij, inf_numeral & gain, bool& skipped_row) {
TRACE("opt", tout << "selecting variable to replace v" << x_j << ", inc: " << inc << "\n";);
theory_var x_i = null_theory_var;
inf_numeral curr_gain;
column & c = m_columns[x_j];
typename svector<col_entry>::iterator it = c.begin_entries();
typename svector<col_entry>::iterator end = c.end_entries();
for (; it != end; ++it) {
if (!it->is_dead()) {
row & r = m_rows[it->m_row_id];
theory_var s = r.get_base_var();
if (s != null_theory_var && !is_quasi_base(s)) {
numeral const & coeff = r[it->m_row_idx].m_coeff;
bool inc_s = coeff.is_neg() ? inc : !inc;
bound * b = get_bound(s, inc_s);
if (b) {
curr_gain = get_value(s);
curr_gain -= b->get_value();
curr_gain /= coeff;
if (curr_gain.is_neg())
curr_gain.neg();
if (x_i == null_theory_var || (curr_gain < gain) || (gain.is_zero() && curr_gain.is_zero() && s < x_i)) {
if (is_int(s) && !curr_gain.is_int()) {
skipped_row = true;
continue;
}
if (is_int(x_j) && !curr_gain.is_int()) {
skipped_row = true;
continue;
}
if (!curr_gain.is_int() && has_int) {
skipped_row = true;
continue;
}
x_i = s;
a_ij = coeff;
gain = curr_gain;
TRACE("opt",
tout << "x_i: v" << x_i << ", gain: " << gain << "\n";
tout << "value(s): (" << get_value(s) << " - " << b->get_value() << ")/" << coeff << "\n";
display_row(tout, r, true);
);
}
}
}
TRACE("opt", tout << "x_i: v" << x_i << ", gain: " << gain << "\n";);
}
}
TRACE("opt", tout << "x_i v" << x_i << "\n";);
return x_i;
}
template<typename Ext>
bool theory_arith<Ext>::get_theory_vars(expr * n, uint_set & vars) {
rational r;
@ -1388,6 +1311,9 @@ namespace smt {
bool& has_shared, // determine if pivot involves shared variable
theory_var& x_i) { // base variable to pivot with x_j
if (is_int(x_j) && !get_value(x_j).is_int()) {
return false;
}
x_i = null_theory_var;
context& ctx = get_context();
column & c = m_columns[x_j];
@ -1447,8 +1373,7 @@ namespace smt {
template<typename Ext>
void theory_arith<Ext>::normalize_gain(numeral const& divisor, inf_numeral & max_gain) const {
SASSERT(divisor.is_int());
SASSERT(divisor.is_pos());
if (!divisor.is_one() && !max_gain.is_minus_one()) {
if (!divisor.is_minus_one() && !divisor.is_one() && !max_gain.is_minus_one()) {
max_gain = floor(max_gain/divisor)*divisor;
}
}
@ -1473,14 +1398,15 @@ namespace smt {
if (is_int(x)) {
min_gain = inf_numeral::one();
}
TRACE("opt",
tout << "v" << x << " := " << get_value(x) << " "
<< "min gain: " << min_gain << " "
<< "max gain: " << max_gain << "\n";);
SASSERT(max_gain.is_minus_one() || !max_gain.is_neg());
SASSERT(min_gain.is_minus_one() || min_gain.is_one());
SASSERT(!is_int(x) || max_gain.is_int());
SASSERT(is_int(x) == min_gain.is_one());
TRACE("opt",
tout << "v" << x << " "
<< "min gain: " << min_gain << " "
<< "max gain: " << max_gain << "\n";);
}
@ -1512,14 +1438,18 @@ namespace smt {
if (is_int(x_i)) den_aij = denominator(a_ij);
SASSERT(den_aij.is_pos() && den_aij.is_int());
if (is_int(x_i) && !den_aij.is_one()) {
SASSERT(min_gain.is_pos());
if (is_int(x_i) && !den_aij.is_one() && min_gain.is_pos()) {
min_gain = inf_numeral(lcm(min_gain.get_rational(), den_aij));
normalize_gain(min_gain.get_rational(), max_gain);
}
if (!max_inc.is_minus_one()) {
if (is_int(x_i)) {
TRACE("opt",
tout << "v" << x_i << " a_ij " << a_ij << " "
<< "min gain: " << min_gain << " "
<< "max gain: " << max_gain << "\n";);
max_inc = floor(max_inc);
normalize_gain(min_gain.get_rational(), max_inc);
}
@ -1539,9 +1469,9 @@ namespace smt {
<< (is_tighter?"true":"false") << "\n";);
SASSERT(max_gain.is_minus_one() || !max_gain.is_neg());
SASSERT(min_gain.is_minus_one() || !min_gain.is_neg());
SASSERT(!is_int(x_i) || min_gain.is_pos());
SASSERT(!is_int(x_i) || min_gain.is_int());
SASSERT(!is_int(x_i) || max_gain.is_int());
//SASSERT(!is_int(x_i) || min_gain.is_pos());
//SASSERT(!is_int(x_i) || min_gain.is_int());
//SASSERT(!is_int(x_i) || max_gain.is_int());
return is_tighter;
}
@ -1759,6 +1689,10 @@ namespace smt {
unsigned& best_efforts, // is bound move a best effort?
bool& has_shared) { // does move include shared variables?
inf_numeral min_gain, max_gain;
if (is_int(x_i) && !get_value(x_i).is_int()) {
++best_efforts;
return false;
}
init_gains(x_i, inc, min_gain, max_gain);
column & c = m_columns[x_i];
typename svector<col_entry>::iterator it = c.begin_entries();