mirror of
https://github.com/Z3Prover/z3
synced 2025-06-16 10:56:16 +00:00
improve quantifier elimination for arithmetic
This update changes the handling of mod and adds support for nested div terms. Simple use cases that are handled using small results are given below. ``` (declare-const x Int) (declare-const y Int) (declare-const z Int) (assert (exists ((x Int)) (and (<= y (* 10 x)) (<= (* 10 x) z)))) (apply qe2) (reset) (declare-const y Int) (assert (exists ((x Int)) (and (> x 0) (= (div x 41) y)))) (apply qe2) (reset) (declare-const y Int) (assert (exists ((x Int)) (= (mod x 41) y))) (apply qe2) (reset) ``` The main idea is to introduce definition rows for mod/div terms. Elimination of variables under mod/div is defined by rewriting the variable to multiples of the mod/divisior and remainder. The functionality is disabled in this push.
This commit is contained in:
parent
786280c646
commit
03385bf78d
3 changed files with 1795 additions and 1432 deletions
File diff suppressed because it is too large
Load diff
|
@ -30,7 +30,9 @@ namespace opt {
|
||||||
t_eq,
|
t_eq,
|
||||||
t_lt,
|
t_lt,
|
||||||
t_le,
|
t_le,
|
||||||
t_mod
|
t_divides,
|
||||||
|
t_mod,
|
||||||
|
t_div
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,6 +59,7 @@ namespace opt {
|
||||||
ineq_type m_type; // inequality type
|
ineq_type m_type; // inequality type
|
||||||
rational m_value; // value of m_vars + m_coeff under interpretation of m_var2value.
|
rational m_value; // value of m_vars + m_coeff under interpretation of m_var2value.
|
||||||
bool m_alive; // rows can be marked dead if they have been processed.
|
bool m_alive; // rows can be marked dead if they have been processed.
|
||||||
|
unsigned m_id; // variable defined by row (used for mod_t and div_t)
|
||||||
void reset() { m_vars.reset(); m_coeff.reset(); m_value.reset(); }
|
void reset() { m_vars.reset(); m_coeff.reset(); m_value.reset(); }
|
||||||
|
|
||||||
row& normalize();
|
row& normalize();
|
||||||
|
@ -85,9 +88,9 @@ namespace opt {
|
||||||
static const unsigned m_objective_id = 0;
|
static const unsigned m_objective_id = 0;
|
||||||
vector<unsigned_vector> m_var2row_ids;
|
vector<unsigned_vector> m_var2row_ids;
|
||||||
vector<rational> m_var2value;
|
vector<rational> m_var2value;
|
||||||
bool_vector m_var2is_int;
|
bool_vector m_var2is_int;
|
||||||
vector<var> m_new_vars;
|
vector<var> m_new_vars;
|
||||||
unsigned_vector m_lub, m_glb, m_mod;
|
unsigned_vector m_lub, m_glb, m_divides, m_mod, m_div;
|
||||||
unsigned_vector m_above, m_below;
|
unsigned_vector m_above, m_below;
|
||||||
unsigned_vector m_retired_rows;
|
unsigned_vector m_retired_rows;
|
||||||
|
|
||||||
|
@ -122,14 +125,18 @@ namespace opt {
|
||||||
|
|
||||||
void sub(unsigned dst, rational const& c);
|
void sub(unsigned dst, rational const& c);
|
||||||
|
|
||||||
void del_var(unsigned dst, unsigned x);
|
void set_row(unsigned row_id, vector<var> const& coeffs, rational const& c, rational const& m, ineq_type rel);
|
||||||
|
|
||||||
void set_row(unsigned row_id, vector<var> const& coeffs, rational const& c, rational const& m, ineq_type rel);
|
void add_lower_bound(unsigned x, rational const& lo);
|
||||||
|
|
||||||
|
void add_upper_bound(unsigned x, rational const& hi);
|
||||||
|
|
||||||
void add_constraint(vector<var> const& coeffs, rational const& c, rational const& m, ineq_type r);
|
void add_constraint(vector<var> const& coeffs, rational const& c, rational const& m, ineq_type r);
|
||||||
|
|
||||||
void replace_var(unsigned row_id, unsigned x, rational const& A, unsigned y, rational const& B);
|
void replace_var(unsigned row_id, unsigned x, rational const& A, unsigned y, rational const& B);
|
||||||
|
|
||||||
|
void replace_var(unsigned row_id, unsigned x, rational const& A, unsigned y, rational const& B, unsigned z);
|
||||||
|
|
||||||
void replace_var(unsigned row_id, unsigned x, rational const& C);
|
void replace_var(unsigned row_id, unsigned x, rational const& C);
|
||||||
|
|
||||||
void normalize(unsigned row_id);
|
void normalize(unsigned row_id);
|
||||||
|
@ -138,7 +145,7 @@ namespace opt {
|
||||||
|
|
||||||
unsigned new_row();
|
unsigned new_row();
|
||||||
|
|
||||||
unsigned copy_row(unsigned row_id);
|
unsigned copy_row(unsigned row_id, unsigned excl = UINT_MAX);
|
||||||
|
|
||||||
rational n_sign(rational const& b) const;
|
rational n_sign(rational const& b) const;
|
||||||
|
|
||||||
|
@ -150,8 +157,12 @@ namespace opt {
|
||||||
|
|
||||||
def solve_for(unsigned row_id, unsigned x, bool compute_def);
|
def solve_for(unsigned row_id, unsigned x, bool compute_def);
|
||||||
|
|
||||||
def solve_mod(unsigned x, unsigned_vector const& mod_rows, bool compute_def);
|
def solve_divides(unsigned x, unsigned_vector const& divide_rows, bool compute_def);
|
||||||
|
|
||||||
|
def solve_mod(unsigned x, unsigned_vector const& divide_rows, bool compute_def);
|
||||||
|
|
||||||
|
def solve_div(unsigned x, unsigned_vector const& divide_rows, bool compute_def);
|
||||||
|
|
||||||
bool is_int(unsigned x) const { return m_var2is_int[x]; }
|
bool is_int(unsigned x) const { return m_var2is_int[x]; }
|
||||||
|
|
||||||
void retire_row(unsigned row_id);
|
void retire_row(unsigned row_id);
|
||||||
|
@ -173,6 +184,17 @@ namespace opt {
|
||||||
// add a divisibility constraint. The row should divide m.
|
// add a divisibility constraint. The row should divide m.
|
||||||
void add_divides(vector<var> const& coeffs, rational const& c, rational const& m);
|
void add_divides(vector<var> const& coeffs, rational const& c, rational const& m);
|
||||||
|
|
||||||
|
|
||||||
|
// add sub-expression for modulus
|
||||||
|
// v = add_mod(coeffs, m) means
|
||||||
|
// v = coeffs mod m
|
||||||
|
unsigned add_mod(vector<var> const& coeffs, rational const& c, rational const& m);
|
||||||
|
|
||||||
|
// add sub-expression for div
|
||||||
|
// v = add_div(coeffs, m) means
|
||||||
|
// v = coeffs div m
|
||||||
|
unsigned add_div(vector<var> const& coeffs, rational const& c, rational const& m);
|
||||||
|
|
||||||
// Set the objective function (linear).
|
// Set the objective function (linear).
|
||||||
void set_objective(vector<var> const& coeffs, rational const& c);
|
void set_objective(vector<var> const& coeffs, rational const& c);
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,6 @@ Revision History:
|
||||||
#include "ast/ast_util.h"
|
#include "ast/ast_util.h"
|
||||||
#include "ast/arith_decl_plugin.h"
|
#include "ast/arith_decl_plugin.h"
|
||||||
#include "ast/ast_pp.h"
|
#include "ast/ast_pp.h"
|
||||||
#include "ast/rewriter/th_rewriter.h"
|
|
||||||
#include "ast/expr_functors.h"
|
#include "ast/expr_functors.h"
|
||||||
#include "ast/rewriter/expr_safe_replace.h"
|
#include "ast/rewriter/expr_safe_replace.h"
|
||||||
#include "math/simplex/model_based_opt.h"
|
#include "math/simplex/model_based_opt.h"
|
||||||
|
@ -32,13 +31,13 @@ Revision History:
|
||||||
#include "model/model_v2_pp.h"
|
#include "model/model_v2_pp.h"
|
||||||
|
|
||||||
namespace mbp {
|
namespace mbp {
|
||||||
|
|
||||||
struct arith_project_plugin::imp {
|
struct arith_project_plugin::imp {
|
||||||
|
|
||||||
ast_manager& m;
|
ast_manager& m;
|
||||||
arith_util a;
|
arith_util a;
|
||||||
bool m_check_purified { true }; // check that variables are properly pure
|
bool m_check_purified = true; // check that variables are properly pure
|
||||||
bool m_apply_projection { false };
|
bool m_apply_projection = false;
|
||||||
|
|
||||||
|
|
||||||
imp(ast_manager& m) :
|
imp(ast_manager& m) :
|
||||||
|
@ -48,10 +47,10 @@ namespace mbp {
|
||||||
|
|
||||||
void insert_mul(expr* x, rational const& v, obj_map<expr, rational>& ts) {
|
void insert_mul(expr* x, rational const& v, obj_map<expr, rational>& ts) {
|
||||||
rational w;
|
rational w;
|
||||||
if (ts.find(x, w))
|
if (ts.find(x, w))
|
||||||
ts.insert(x, w + v);
|
ts.insert(x, w + v);
|
||||||
else
|
else
|
||||||
ts.insert(x, v);
|
ts.insert(x, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,15 +64,15 @@ namespace mbp {
|
||||||
rational c(0), mul(1);
|
rational c(0), mul(1);
|
||||||
expr_ref t(m);
|
expr_ref t(m);
|
||||||
opt::ineq_type ty = opt::t_le;
|
opt::ineq_type ty = opt::t_le;
|
||||||
expr* e1, *e2;
|
expr* e1, * e2;
|
||||||
DEBUG_CODE(expr_ref val(m);
|
DEBUG_CODE(expr_ref val(m);
|
||||||
eval(lit, val);
|
eval(lit, val);
|
||||||
CTRACE("qe", !m.is_true(val), tout << mk_pp(lit, m) << " := " << val << "\n";);
|
CTRACE("qe", !m.is_true(val), tout << mk_pp(lit, m) << " := " << val << "\n";);
|
||||||
SASSERT(m.limit().is_canceled() || !m.is_false(val)););
|
SASSERT(m.limit().is_canceled() || !m.is_false(val)););
|
||||||
|
|
||||||
if (!m.inc())
|
if (!m.inc())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
TRACE("opt", tout << mk_pp(lit, m) << " " << a.is_lt(lit) << " " << a.is_gt(lit) << "\n";);
|
TRACE("opt", tout << mk_pp(lit, m) << " " << a.is_lt(lit) << " " << a.is_gt(lit) << "\n";);
|
||||||
bool is_not = m.is_not(lit, lit);
|
bool is_not = m.is_not(lit, lit);
|
||||||
if (is_not) {
|
if (is_not) {
|
||||||
|
@ -86,37 +85,35 @@ namespace mbp {
|
||||||
ty = is_not ? opt::t_lt : opt::t_le;
|
ty = is_not ? opt::t_lt : opt::t_le;
|
||||||
}
|
}
|
||||||
else if ((a.is_lt(lit, e1, e2) || a.is_gt(lit, e2, e1))) {
|
else if ((a.is_lt(lit, e1, e2) || a.is_gt(lit, e2, e1))) {
|
||||||
linearize(mbo, eval, mul, e1, c, fmls, ts, tids);
|
linearize(mbo, eval, mul, e1, c, fmls, ts, tids);
|
||||||
linearize(mbo, eval, -mul, e2, c, fmls, ts, tids);
|
linearize(mbo, eval, -mul, e2, c, fmls, ts, tids);
|
||||||
ty = is_not ? opt::t_le: opt::t_lt;
|
ty = is_not ? opt::t_le : opt::t_lt;
|
||||||
}
|
}
|
||||||
else if (m.is_eq(lit, e1, e2) && !is_not && is_arith(e1)) {
|
else if (m.is_eq(lit, e1, e2) && !is_not && is_arith(e1)) {
|
||||||
linearize(mbo, eval, mul, e1, c, fmls, ts, tids);
|
linearize(mbo, eval, mul, e1, c, fmls, ts, tids);
|
||||||
linearize(mbo, eval, -mul, e2, c, fmls, ts, tids);
|
linearize(mbo, eval, -mul, e2, c, fmls, ts, tids);
|
||||||
ty = opt::t_eq;
|
ty = opt::t_eq;
|
||||||
}
|
}
|
||||||
else if (m.is_eq(lit, e1, e2) && is_not && is_arith(e1)) {
|
else if (m.is_eq(lit, e1, e2) && is_not && is_arith(e1)) {
|
||||||
|
|
||||||
rational r1, r2;
|
rational r1, r2;
|
||||||
expr_ref val1 = eval(e1);
|
expr_ref val1 = eval(e1);
|
||||||
expr_ref val2 = eval(e2);
|
expr_ref val2 = eval(e2);
|
||||||
//TRACE("qe", tout << mk_pp(e1, m) << " " << val1 << "\n";);
|
|
||||||
//TRACE("qe", tout << mk_pp(e2, m) << " " << val2 << "\n";);
|
|
||||||
if (!a.is_numeral(val1, r1)) return false;
|
if (!a.is_numeral(val1, r1)) return false;
|
||||||
if (!a.is_numeral(val2, r2)) return false;
|
if (!a.is_numeral(val2, r2)) return false;
|
||||||
SASSERT(r1 != r2);
|
SASSERT(r1 != r2);
|
||||||
if (r1 < r2) {
|
if (r1 < r2) {
|
||||||
std::swap(e1, e2);
|
std::swap(e1, e2);
|
||||||
}
|
}
|
||||||
ty = opt::t_lt;
|
ty = opt::t_lt;
|
||||||
linearize(mbo, eval, mul, e1, c, fmls, ts, tids);
|
linearize(mbo, eval, mul, e1, c, fmls, ts, tids);
|
||||||
linearize(mbo, eval, -mul, e2, c, fmls, ts, tids);
|
linearize(mbo, eval, -mul, e2, c, fmls, ts, tids);
|
||||||
}
|
}
|
||||||
else if (m.is_distinct(lit) && !is_not && is_arith(to_app(lit)->get_arg(0))) {
|
else if (m.is_distinct(lit) && !is_not && is_arith(to_app(lit)->get_arg(0))) {
|
||||||
expr_ref val(m);
|
expr_ref val(m);
|
||||||
rational r;
|
rational r;
|
||||||
app* alit = to_app(lit);
|
app* alit = to_app(lit);
|
||||||
vector<std::pair<expr*,rational> > nums;
|
vector<std::pair<expr*, rational> > nums;
|
||||||
for (expr* arg : *alit) {
|
for (expr* arg : *alit) {
|
||||||
val = eval(arg);
|
val = eval(arg);
|
||||||
TRACE("qe", tout << mk_pp(arg, m) << " " << val << "\n";);
|
TRACE("qe", tout << mk_pp(arg, m) << " " << val << "\n";);
|
||||||
|
@ -125,8 +122,8 @@ namespace mbp {
|
||||||
}
|
}
|
||||||
std::sort(nums.begin(), nums.end(), compare_second());
|
std::sort(nums.begin(), nums.end(), compare_second());
|
||||||
for (unsigned i = 0; i + 1 < nums.size(); ++i) {
|
for (unsigned i = 0; i + 1 < nums.size(); ++i) {
|
||||||
SASSERT(nums[i].second < nums[i+1].second);
|
SASSERT(nums[i].second < nums[i + 1].second);
|
||||||
expr_ref fml(a.mk_lt(nums[i].first, nums[i+1].first), m);
|
expr_ref fml(a.mk_lt(nums[i].first, nums[i + 1].first), m);
|
||||||
if (!linearize(mbo, eval, fml, fmls, tids)) {
|
if (!linearize(mbo, eval, fml, fmls, tids)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -139,14 +136,14 @@ namespace mbp {
|
||||||
map<rational, expr*, rational::hash_proc, rational::eq_proc> values;
|
map<rational, expr*, rational::hash_proc, rational::eq_proc> values;
|
||||||
bool found_eq = false;
|
bool found_eq = false;
|
||||||
for (unsigned i = 0; !found_eq && i < to_app(lit)->get_num_args(); ++i) {
|
for (unsigned i = 0; !found_eq && i < to_app(lit)->get_num_args(); ++i) {
|
||||||
expr* arg1 = to_app(lit)->get_arg(i), *arg2 = nullptr;
|
expr* arg1 = to_app(lit)->get_arg(i), * arg2 = nullptr;
|
||||||
rational r;
|
rational r;
|
||||||
expr_ref val = eval(arg1);
|
expr_ref val = eval(arg1);
|
||||||
TRACE("qe", tout << mk_pp(arg1, m) << " " << val << "\n";);
|
TRACE("qe", tout << mk_pp(arg1, m) << " " << val << "\n";);
|
||||||
if (!a.is_numeral(val, r)) return false;
|
if (!a.is_numeral(val, r)) return false;
|
||||||
if (values.find(r, arg2)) {
|
if (values.find(r, arg2)) {
|
||||||
ty = opt::t_eq;
|
ty = opt::t_eq;
|
||||||
linearize(mbo, eval, mul, arg1, c, fmls, ts, tids);
|
linearize(mbo, eval, mul, arg1, c, fmls, ts, tids);
|
||||||
linearize(mbo, eval, -mul, arg2, c, fmls, ts, tids);
|
linearize(mbo, eval, -mul, arg2, c, fmls, ts, tids);
|
||||||
found_eq = true;
|
found_eq = true;
|
||||||
}
|
}
|
||||||
|
@ -169,27 +166,37 @@ namespace mbp {
|
||||||
//
|
//
|
||||||
// convert linear arithmetic term into an inequality for mbo.
|
// convert linear arithmetic term into an inequality for mbo.
|
||||||
//
|
//
|
||||||
void linearize(opt::model_based_opt& mbo, model_evaluator& eval, rational const& mul, expr* t, rational& c,
|
void linearize(opt::model_based_opt& mbo, model_evaluator& eval, rational const& mul, expr* t, rational& c,
|
||||||
expr_ref_vector& fmls, obj_map<expr, rational>& ts, obj_map<expr, unsigned>& tids) {
|
expr_ref_vector& fmls, obj_map<expr, rational>& ts, obj_map<expr, unsigned>& tids) {
|
||||||
expr* t1, *t2, *t3;
|
expr* t1, * t2, * t3;
|
||||||
rational mul1;
|
rational mul1;
|
||||||
expr_ref val(m);
|
expr_ref val(m);
|
||||||
if (a.is_mul(t, t1, t2) && is_numeral(t1, mul1))
|
|
||||||
linearize(mbo, eval, mul* mul1, t2, c, fmls, ts, tids);
|
auto add_def = [&](expr* t1, rational const& m, vars& coeffs) {
|
||||||
else if (a.is_mul(t, t1, t2) && is_numeral(t2, mul1))
|
obj_map<expr, rational> ts0;
|
||||||
linearize(mbo, eval, mul* mul1, t1, c, fmls, ts, tids);
|
rational mul0(1), c0(0);
|
||||||
|
linearize(mbo, eval, mul0, t1, c0, fmls, ts0, tids);
|
||||||
|
extract_coefficients(mbo, eval, ts0, tids, coeffs);
|
||||||
|
insert_mul(t, mul, ts);
|
||||||
|
return c0;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (a.is_mul(t, t1, t2) && is_numeral(t1, mul1))
|
||||||
|
linearize(mbo, eval, mul * mul1, t2, c, fmls, ts, tids);
|
||||||
|
else if (a.is_mul(t, t1, t2) && is_numeral(t2, mul1))
|
||||||
|
linearize(mbo, eval, mul * mul1, t1, c, fmls, ts, tids);
|
||||||
else if (a.is_uminus(t, t1))
|
else if (a.is_uminus(t, t1))
|
||||||
linearize(mbo, eval, -mul, t1, c, fmls, ts, tids);
|
linearize(mbo, eval, -mul, t1, c, fmls, ts, tids);
|
||||||
else if (a.is_numeral(t, mul1))
|
else if (a.is_numeral(t, mul1))
|
||||||
c += mul * mul1;
|
c += mul * mul1;
|
||||||
else if (a.is_add(t)) {
|
else if (a.is_add(t)) {
|
||||||
for (expr* arg : *to_app(t))
|
for (expr* arg : *to_app(t))
|
||||||
linearize(mbo, eval, mul, arg, c, fmls, ts, tids);
|
linearize(mbo, eval, mul, arg, c, fmls, ts, tids);
|
||||||
}
|
}
|
||||||
else if (a.is_sub(t, t1, t2)) {
|
else if (a.is_sub(t, t1, t2)) {
|
||||||
linearize(mbo, eval, mul, t1, c, fmls, ts, tids);
|
linearize(mbo, eval, mul, t1, c, fmls, ts, tids);
|
||||||
linearize(mbo, eval, -mul, t2, c, fmls, ts, tids);
|
linearize(mbo, eval, -mul, t2, c, fmls, ts, tids);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (m.is_ite(t, t1, t2, t3)) {
|
else if (m.is_ite(t, t1, t2, t3)) {
|
||||||
val = eval(t1);
|
val = eval(t1);
|
||||||
|
@ -210,21 +217,30 @@ namespace mbp {
|
||||||
else if (a.is_mod(t, t1, t2) && is_numeral(t2, mul1) && !mul1.is_zero()) {
|
else if (a.is_mod(t, t1, t2) && is_numeral(t2, mul1) && !mul1.is_zero()) {
|
||||||
rational r;
|
rational r;
|
||||||
val = eval(t);
|
val = eval(t);
|
||||||
if (!a.is_numeral(val, r)) {
|
if (!a.is_numeral(val, r))
|
||||||
throw default_exception("mbp evaluation didn't produce an integer");
|
throw default_exception("mbp evaluation didn't produce an integer");
|
||||||
}
|
c += mul * r;
|
||||||
c += mul*r;
|
|
||||||
// t1 mod mul1 == r
|
// t1 mod mul1 == r
|
||||||
rational c0(-r), mul0(1);
|
|
||||||
obj_map<expr, rational> ts0;
|
|
||||||
linearize(mbo, eval, mul0, t1, c0, fmls, ts0, tids);
|
|
||||||
vars coeffs;
|
vars coeffs;
|
||||||
extract_coefficients(mbo, eval, ts0, tids, coeffs);
|
rational c0 = add_def(t1, mul1, coeffs);
|
||||||
mbo.add_divides(coeffs, c0, mul1);
|
mbo.add_divides(coeffs, c0 - r, mul1);
|
||||||
}
|
}
|
||||||
else {
|
else if (false && a.is_mod(t, t1, t2) && is_numeral(t2, mul1) && !mul1.is_zero()) {
|
||||||
|
// v = t1 mod mul1
|
||||||
|
vars coeffs;
|
||||||
|
rational c0 = add_def(t1, mul1, coeffs);
|
||||||
|
unsigned v = mbo.add_mod(coeffs, c0, mul1);
|
||||||
|
tids.insert(t, v);
|
||||||
|
}
|
||||||
|
else if (false && a.is_idiv(t, t1, t2) && is_numeral(t2, mul1) && mul1 > 0) {
|
||||||
|
// v = t1 div mul1
|
||||||
|
vars coeffs;
|
||||||
|
rational c0 = add_def(t1, mul1, coeffs);
|
||||||
|
unsigned v = mbo.add_div(coeffs, c0, mul1);
|
||||||
|
tids.insert(t, v);
|
||||||
|
}
|
||||||
|
else
|
||||||
insert_mul(t, mul, ts);
|
insert_mul(t, mul, ts);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_numeral(expr* t, rational& r) {
|
bool is_numeral(expr* t, rational& r) {
|
||||||
|
@ -232,8 +248,8 @@ namespace mbp {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct compare_second {
|
struct compare_second {
|
||||||
bool operator()(std::pair<expr*, rational> const& a,
|
bool operator()(std::pair<expr*, rational> const& a,
|
||||||
std::pair<expr*, rational> const& b) const {
|
std::pair<expr*, rational> const& b) const {
|
||||||
return a.second < b.second;
|
return a.second < b.second;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -243,14 +259,14 @@ namespace mbp {
|
||||||
}
|
}
|
||||||
|
|
||||||
rational n_sign(rational const& b) {
|
rational n_sign(rational const& b) {
|
||||||
return rational(b.is_pos()?-1:1);
|
return rational(b.is_pos() ? -1 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator()(model& model, app* v, app_ref_vector& vars, expr_ref_vector& lits) {
|
bool operator()(model& model, app* v, app_ref_vector& vars, expr_ref_vector& lits) {
|
||||||
app_ref_vector vs(m);
|
app_ref_vector vs(m);
|
||||||
vs.push_back(v);
|
vs.push_back(v);
|
||||||
vector<def> defs;
|
vector<def> defs;
|
||||||
return project(model, vs, lits, defs, false) && vs.empty();
|
return project(model, vs, lits, defs, false) && vs.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef opt::model_based_opt::var var;
|
typedef opt::model_based_opt::var var;
|
||||||
|
@ -267,10 +283,10 @@ namespace mbp {
|
||||||
|
|
||||||
bool project(model& model, app_ref_vector& vars, expr_ref_vector& fmls, vector<def>& result, bool compute_def) {
|
bool project(model& model, app_ref_vector& vars, expr_ref_vector& fmls, vector<def>& result, bool compute_def) {
|
||||||
bool has_arith = false;
|
bool has_arith = false;
|
||||||
for (expr* v : vars)
|
for (expr* v : vars)
|
||||||
has_arith |= is_arith(v);
|
has_arith |= is_arith(v);
|
||||||
if (!has_arith)
|
if (!has_arith)
|
||||||
return true;
|
return true;
|
||||||
model_evaluator eval(model);
|
model_evaluator eval(model);
|
||||||
TRACE("qe", tout << model;);
|
TRACE("qe", tout << model;);
|
||||||
eval.set_model_completion(true);
|
eval.set_model_completion(true);
|
||||||
|
@ -281,7 +297,7 @@ namespace mbp {
|
||||||
expr_ref_vector pinned(m);
|
expr_ref_vector pinned(m);
|
||||||
unsigned j = 0;
|
unsigned j = 0;
|
||||||
TRACE("qe", tout << "vars: " << vars << "\n";
|
TRACE("qe", tout << "vars: " << vars << "\n";
|
||||||
for (expr* f : fmls) tout << mk_pp(f, m) << " := " << model(f) << "\n";);
|
for (expr* f : fmls) tout << mk_pp(f, m) << " := " << model(f) << "\n";);
|
||||||
for (unsigned i = 0; i < fmls.size(); ++i) {
|
for (unsigned i = 0; i < fmls.size(); ++i) {
|
||||||
expr* fml = fmls.get(i);
|
expr* fml = fmls.get(i);
|
||||||
if (!linearize(mbo, eval, fml, fmls, tids)) {
|
if (!linearize(mbo, eval, fml, fmls, tids)) {
|
||||||
|
@ -294,8 +310,8 @@ namespace mbp {
|
||||||
}
|
}
|
||||||
fmls.shrink(j);
|
fmls.shrink(j);
|
||||||
TRACE("qe", tout << "formulas\n" << fmls << "\n";
|
TRACE("qe", tout << "formulas\n" << fmls << "\n";
|
||||||
for (auto const& [e, id] : tids)
|
for (auto const& [e, id] : tids)
|
||||||
tout << mk_pp(e, m) << " -> " << id << "\n";);
|
tout << mk_pp(e, m) << " -> " << id << "\n";);
|
||||||
|
|
||||||
// fmls holds residue,
|
// fmls holds residue,
|
||||||
// mbo holds linear inequalities that are in scope
|
// mbo holds linear inequalities that are in scope
|
||||||
|
@ -306,7 +322,7 @@ namespace mbp {
|
||||||
// return those to fmls.
|
// return those to fmls.
|
||||||
|
|
||||||
expr_mark var_mark, fmls_mark;
|
expr_mark var_mark, fmls_mark;
|
||||||
for (app * v : vars) {
|
for (app* v : vars) {
|
||||||
var_mark.mark(v);
|
var_mark.mark(v);
|
||||||
if (is_arith(v) && !tids.contains(v)) {
|
if (is_arith(v) && !tids.contains(v)) {
|
||||||
rational r;
|
rational r;
|
||||||
|
@ -321,60 +337,70 @@ namespace mbp {
|
||||||
}
|
}
|
||||||
|
|
||||||
// bail on variables in non-linear sub-terms
|
// bail on variables in non-linear sub-terms
|
||||||
|
auto is_pure = [&](expr* e) {
|
||||||
|
expr* x, * y;
|
||||||
|
rational r;
|
||||||
|
if (a.is_mod(e, x, y) && a.is_numeral(y))
|
||||||
|
return true;
|
||||||
|
if (a.is_idiv(e, x, y) && a.is_numeral(y, r) && r > 0)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
for (auto& kv : tids) {
|
for (auto& kv : tids) {
|
||||||
expr* e = kv.m_key;
|
expr* e = kv.m_key;
|
||||||
if (is_arith(e) && !var_mark.is_marked(e))
|
if (is_arith(e) && !is_pure(e) && !var_mark.is_marked(e))
|
||||||
mark_rec(fmls_mark, e);
|
mark_rec(fmls_mark, e);
|
||||||
}
|
}
|
||||||
if (m_check_purified) {
|
if (m_check_purified) {
|
||||||
for (expr* fml : fmls)
|
for (expr* fml : fmls)
|
||||||
mark_rec(fmls_mark, fml);
|
mark_rec(fmls_mark, fml);
|
||||||
for (auto& kv : tids) {
|
for (auto& kv : tids) {
|
||||||
expr* e = kv.m_key;
|
expr* e = kv.m_key;
|
||||||
if (!var_mark.is_marked(e))
|
if (!var_mark.is_marked(e) && !is_pure(e))
|
||||||
mark_rec(fmls_mark, e);
|
mark_rec(fmls_mark, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr_vector<expr> index2expr;
|
ptr_vector<expr> index2expr;
|
||||||
for (auto& kv : tids)
|
for (auto& kv : tids)
|
||||||
index2expr.setx(kv.m_value, kv.m_key, nullptr);
|
index2expr.setx(kv.m_value, kv.m_key, nullptr);
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
unsigned_vector real_vars;
|
unsigned_vector real_vars;
|
||||||
for (app* v : vars) {
|
for (app* v : vars) {
|
||||||
if (is_arith(v) && !fmls_mark.is_marked(v))
|
if (is_arith(v) && !fmls_mark.is_marked(v))
|
||||||
real_vars.push_back(tids.find(v));
|
real_vars.push_back(tids.find(v));
|
||||||
else
|
else
|
||||||
vars[j++] = v;
|
vars[j++] = v;
|
||||||
}
|
}
|
||||||
vars.shrink(j);
|
vars.shrink(j);
|
||||||
|
|
||||||
TRACE("qe", tout << "remaining vars: " << vars << "\n";
|
TRACE("qe", tout << "remaining vars: " << vars << "\n";
|
||||||
for (unsigned v : real_vars) tout << "v" << v << " " << mk_pp(index2expr[v], m) << "\n";
|
for (unsigned v : real_vars) tout << "v" << v << " " << mk_pp(index2expr[v], m) << "\n";
|
||||||
mbo.display(tout););
|
mbo.display(tout););
|
||||||
vector<opt::model_based_opt::def> defs = mbo.project(real_vars.size(), real_vars.data(), compute_def);
|
vector<opt::model_based_opt::def> defs = mbo.project(real_vars.size(), real_vars.data(), compute_def);
|
||||||
|
|
||||||
vector<row> rows;
|
vector<row> rows;
|
||||||
mbo.get_live_rows(rows);
|
mbo.get_live_rows(rows);
|
||||||
rows2fmls(rows, index2expr, fmls);
|
rows2fmls(rows, index2expr, fmls);
|
||||||
TRACE("qe", mbo.display(tout << "mbo result\n");
|
TRACE("qe", mbo.display(tout << "mbo result\n");
|
||||||
for (auto const& d : defs) tout << "def: " << d << "\n";
|
for (auto const& d : defs) tout << "def: " << d << "\n";
|
||||||
tout << fmls << "\n";);
|
tout << fmls << "\n";);
|
||||||
|
|
||||||
if (compute_def)
|
if (compute_def)
|
||||||
optdefs2mbpdef(defs, index2expr, real_vars, result);
|
optdefs2mbpdef(defs, index2expr, real_vars, result);
|
||||||
if (m_apply_projection && !apply_projection(eval, result, fmls))
|
if (m_apply_projection && !apply_projection(eval, result, fmls))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
TRACE("qe",
|
TRACE("qe",
|
||||||
for (auto const& [v, t] : result)
|
for (auto const& [v, t] : result)
|
||||||
tout << v << " := " << t << "\n";
|
tout << v << " := " << t << "\n";
|
||||||
for (auto* f : fmls)
|
for (auto* f : fmls)
|
||||||
tout << mk_pp(f, m) << " := " << eval(f) << "\n";
|
tout << mk_pp(f, m) << " := " << eval(f) << "\n";
|
||||||
tout << "fmls:" << fmls << "\n";);
|
tout << "fmls:" << fmls << "\n";);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void optdefs2mbpdef(vector<opt::model_based_opt::def> const& defs, ptr_vector<expr> const& index2expr, unsigned_vector const& real_vars, vector<def>& result) {
|
void optdefs2mbpdef(vector<opt::model_based_opt::def> const& defs, ptr_vector<expr> const& index2expr, unsigned_vector const& real_vars, vector<def>& result) {
|
||||||
SASSERT(defs.size() == real_vars.size());
|
SASSERT(defs.size() == real_vars.size());
|
||||||
|
@ -384,31 +410,92 @@ namespace mbp {
|
||||||
bool is_int = a.is_int(x);
|
bool is_int = a.is_int(x);
|
||||||
expr_ref_vector ts(m);
|
expr_ref_vector ts(m);
|
||||||
expr_ref t(m);
|
expr_ref t(m);
|
||||||
for (var const& v : d.m_vars)
|
for (var const& v : d.m_vars)
|
||||||
ts.push_back(var2expr(index2expr, v));
|
ts.push_back(var2expr(index2expr, v));
|
||||||
if (!d.m_coeff.is_zero())
|
if (!d.m_coeff.is_zero())
|
||||||
ts.push_back(a.mk_numeral(d.m_coeff, is_int));
|
ts.push_back(a.mk_numeral(d.m_coeff, is_int));
|
||||||
if (ts.empty())
|
if (ts.empty())
|
||||||
ts.push_back(a.mk_numeral(rational(0), is_int));
|
ts.push_back(a.mk_numeral(rational(0), is_int));
|
||||||
t = mk_add(ts);
|
t = mk_add(ts);
|
||||||
if (!d.m_div.is_one() && is_int)
|
if (!d.m_div.is_one() && is_int)
|
||||||
t = a.mk_idiv(t, a.mk_numeral(d.m_div, is_int));
|
t = a.mk_idiv(t, a.mk_numeral(d.m_div, is_int));
|
||||||
else if (!d.m_div.is_one() && !is_int)
|
else if (!d.m_div.is_one() && !is_int)
|
||||||
t = a.mk_div(t, a.mk_numeral(d.m_div, is_int));
|
t = a.mk_div(t, a.mk_numeral(d.m_div, is_int));
|
||||||
result.push_back(def(expr_ref(x, m), t));
|
result.push_back(def(expr_ref(x, m), t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void rows2fmls(vector<row> const& rows, ptr_vector<expr> const& index2expr, expr_ref_vector& fmls) {
|
expr_ref id2expr(u_map<row> const& def_vars, ptr_vector<expr> const& index2expr, unsigned id) {
|
||||||
for (row const& r : rows) {
|
row r;
|
||||||
expr_ref_vector ts(m);
|
if (def_vars.find(id, r))
|
||||||
expr_ref t(m), s(m), val(m);
|
return row2expr(def_vars, index2expr, r);
|
||||||
if (r.m_vars.empty()) {
|
return expr_ref(index2expr[id], m);
|
||||||
|
}
|
||||||
|
|
||||||
|
expr_ref row2expr(u_map<row> const& def_vars, ptr_vector<expr> const& index2expr, row const& r) {
|
||||||
|
expr_ref_vector ts(m);
|
||||||
|
expr_ref t(m);
|
||||||
|
rational n;
|
||||||
|
for (var const& v : r.m_vars) {
|
||||||
|
t = id2expr(def_vars, index2expr, v.m_id);
|
||||||
|
if (a.is_numeral(t, n) && n == 0)
|
||||||
continue;
|
continue;
|
||||||
|
else if (a.is_numeral(t, n))
|
||||||
|
t = a.mk_numeral(v.m_coeff * n, a.is_int(t));
|
||||||
|
else if (!v.m_coeff.is_one())
|
||||||
|
t = a.mk_mul(a.mk_numeral(v.m_coeff, a.is_int(t)), t);
|
||||||
|
ts.push_back(t);
|
||||||
|
}
|
||||||
|
switch (r.m_type) {
|
||||||
|
case opt::t_mod:
|
||||||
|
if (ts.empty()) {
|
||||||
|
t = a.mk_int(mod(r.m_coeff, r.m_mod));
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
if (r.m_vars.size() == 1 && r.m_vars[0].m_coeff.is_neg() && r.m_type != opt::t_mod) {
|
ts.push_back(a.mk_int(r.m_coeff));
|
||||||
|
t = mk_add(ts);
|
||||||
|
t = a.mk_mod(t, a.mk_int(r.m_mod));
|
||||||
|
return t;
|
||||||
|
case opt::t_div:
|
||||||
|
if (ts.empty()) {
|
||||||
|
t = a.mk_int(div(r.m_coeff, r.m_mod));
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
ts.push_back(a.mk_int(r.m_coeff));
|
||||||
|
t = mk_add(ts);
|
||||||
|
t = a.mk_idiv(t, a.mk_int(r.m_mod));
|
||||||
|
return t;
|
||||||
|
case opt::t_divides:
|
||||||
|
ts.push_back(a.mk_int(r.m_coeff));
|
||||||
|
return mk_add(ts);
|
||||||
|
default:
|
||||||
|
return mk_add(ts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void rows2fmls(vector<row> const& rows, ptr_vector<expr> const& index2expr, expr_ref_vector& fmls) {
|
||||||
|
|
||||||
|
u_map<row> def_vars;
|
||||||
|
for (row const& r : rows) {
|
||||||
|
if (r.m_type == opt::t_mod)
|
||||||
|
def_vars.insert(r.m_id, r);
|
||||||
|
else if (r.m_type == opt::t_div)
|
||||||
|
def_vars.insert(r.m_id, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (row const& r : rows) {
|
||||||
|
expr_ref t(m), s(m), val(m);
|
||||||
|
|
||||||
|
if (r.m_vars.empty())
|
||||||
|
continue;
|
||||||
|
if (r.m_type == opt::t_mod)
|
||||||
|
continue;
|
||||||
|
if (r.m_type == opt::t_div)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (r.m_vars.size() == 1 && r.m_vars[0].m_coeff.is_neg() && r.m_type != opt::t_divides) {
|
||||||
var const& v = r.m_vars[0];
|
var const& v = r.m_vars[0];
|
||||||
t = index2expr[v.m_id];
|
t = id2expr(def_vars, index2expr, v.m_id);
|
||||||
if (!v.m_coeff.is_minus_one()) {
|
if (!v.m_coeff.is_minus_one()) {
|
||||||
t = a.mk_mul(a.mk_numeral(-v.m_coeff, a.is_int(t)), t);
|
t = a.mk_mul(a.mk_numeral(-v.m_coeff, a.is_int(t)), t);
|
||||||
}
|
}
|
||||||
|
@ -422,24 +509,14 @@ namespace mbp {
|
||||||
fmls.push_back(t);
|
fmls.push_back(t);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (var const& v : r.m_vars) {
|
t = row2expr(def_vars, index2expr, r);
|
||||||
t = index2expr[v.m_id];
|
|
||||||
if (!v.m_coeff.is_one()) {
|
|
||||||
t = a.mk_mul(a.mk_numeral(v.m_coeff, a.is_int(t)), t);
|
|
||||||
}
|
|
||||||
ts.push_back(t);
|
|
||||||
}
|
|
||||||
t = mk_add(ts);
|
|
||||||
s = a.mk_numeral(-r.m_coeff, r.m_coeff.is_int() && a.is_int(t));
|
s = a.mk_numeral(-r.m_coeff, r.m_coeff.is_int() && a.is_int(t));
|
||||||
switch (r.m_type) {
|
switch (r.m_type) {
|
||||||
case opt::t_lt: t = a.mk_lt(t, s); break;
|
case opt::t_lt: t = a.mk_lt(t, s); break;
|
||||||
case opt::t_le: t = a.mk_le(t, s); break;
|
case opt::t_le: t = a.mk_le(t, s); break;
|
||||||
case opt::t_eq: t = a.mk_eq(t, s); break;
|
case opt::t_eq: t = a.mk_eq(t, s); break;
|
||||||
case opt::t_mod:
|
case opt::t_divides:
|
||||||
if (!r.m_coeff.is_zero()) {
|
t = a.mk_eq(a.mk_mod(t, a.mk_int(r.m_mod)), a.mk_int(0));
|
||||||
t = a.mk_sub(t, s);
|
|
||||||
}
|
|
||||||
t = a.mk_eq(a.mk_mod(t, a.mk_numeral(r.m_mod, true)), a.mk_int(0));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
fmls.push_back(t);
|
fmls.push_back(t);
|
||||||
|
@ -468,11 +545,11 @@ namespace mbp {
|
||||||
SASSERT(validate_model(eval, fmls0));
|
SASSERT(validate_model(eval, fmls0));
|
||||||
|
|
||||||
// extract linear constraints
|
// extract linear constraints
|
||||||
|
|
||||||
for (expr * fml : fmls) {
|
for (expr* fml : fmls) {
|
||||||
linearize(mbo, eval, fml, fmls, tids);
|
linearize(mbo, eval, fml, fmls, tids);
|
||||||
}
|
}
|
||||||
|
|
||||||
// find optimal value
|
// find optimal value
|
||||||
value = mbo.maximize();
|
value = mbo.maximize();
|
||||||
|
|
||||||
|
@ -543,7 +620,7 @@ namespace mbp {
|
||||||
}
|
}
|
||||||
CTRACE("qe", kv.m_value.is_zero(), tout << mk_pp(v, m) << " has coefficeint 0\n";);
|
CTRACE("qe", kv.m_value.is_zero(), tout << mk_pp(v, m) << " has coefficeint 0\n";);
|
||||||
if (!kv.m_value.is_zero()) {
|
if (!kv.m_value.is_zero()) {
|
||||||
coeffs.push_back(var(id, kv.m_value));
|
coeffs.push_back(var(id, kv.m_value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -571,7 +648,7 @@ namespace mbp {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
arith_project_plugin::arith_project_plugin(ast_manager& m):project_plugin(m) {
|
arith_project_plugin::arith_project_plugin(ast_manager& m) :project_plugin(m) {
|
||||||
m_imp = alloc(imp, m);
|
m_imp = alloc(imp, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue