mirror of
https://github.com/Z3Prover/z3
synced 2025-04-23 17:15:31 +00:00
moving to rational coefficients
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
e44db06bb7
commit
97dfb6d521
7 changed files with 261 additions and 119 deletions
|
@ -21,12 +21,13 @@ Revision History:
|
|||
|
||||
pb_decl_plugin::pb_decl_plugin():
|
||||
m_at_most_sym("at-most"),
|
||||
m_at_least_sym("at-least"),
|
||||
m_pble_sym("pble"),
|
||||
m_pbge_sym("pbge")
|
||||
{}
|
||||
|
||||
func_decl * pb_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters,
|
||||
unsigned arity, sort * const * domain, sort * range) {
|
||||
unsigned arity, sort * const * domain, sort * range) {
|
||||
SASSERT(m_manager);
|
||||
ast_manager& m = *m_manager;
|
||||
for (unsigned i = 0; i < arity; ++i) {
|
||||
|
@ -34,37 +35,42 @@ func_decl * pb_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, p
|
|||
m.raise_exception("invalid non-Boolean sort applied to 'at-most'");
|
||||
}
|
||||
}
|
||||
symbol sym;
|
||||
switch(k) {
|
||||
case OP_AT_LEAST_K: sym = m_at_least_sym; break;
|
||||
case OP_AT_MOST_K: sym = m_at_most_sym; break;
|
||||
case OP_PB_LE: sym = m_pble_sym; break;
|
||||
case OP_PB_GE: sym = m_pbge_sym; break;
|
||||
default: break;
|
||||
}
|
||||
switch(k) {
|
||||
case OP_AT_LEAST_K:
|
||||
case OP_AT_MOST_K: {
|
||||
if (num_parameters != 1 || !parameters[0].is_int() || parameters[0].get_int() < 0) {
|
||||
m.raise_exception("function 'at-most' expects one non-negative integer parameter");
|
||||
m.raise_exception("function expects one non-negative integer parameter");
|
||||
}
|
||||
func_decl_info info(m_family_id, OP_AT_MOST_K, 1, parameters);
|
||||
return m.mk_func_decl(m_at_most_sym, arity, domain, m.mk_bool_sort(), info);
|
||||
func_decl_info info(m_family_id, k, 1, parameters);
|
||||
return m.mk_func_decl(sym, arity, domain, m.mk_bool_sort(), info);
|
||||
}
|
||||
case OP_PB_GE:
|
||||
case OP_PB_LE: {
|
||||
if (num_parameters != 1 + arity || !parameters[0].is_int()) {
|
||||
m.raise_exception("function 'pble' expects arity+1 integer parameters");
|
||||
if (num_parameters != 1 + arity) {
|
||||
m.raise_exception("function expects arity+1 rational parameters");
|
||||
}
|
||||
for (unsigned i = 1; i < num_parameters; ++i) {
|
||||
if (!parameters[i].is_int()) {
|
||||
vector<parameter> params;
|
||||
for (unsigned i = 0; i < num_parameters; ++i) {
|
||||
if (parameters[i].is_int()) {
|
||||
params.push_back(parameter(rational(parameters[i].get_int())));
|
||||
}
|
||||
else if (parameters[i].is_rational()) {
|
||||
params.push_back(parameter(parameters[i].get_rational()));
|
||||
}
|
||||
else {
|
||||
m.raise_exception("function 'pble' expects arity+1 integer parameters");
|
||||
}
|
||||
}
|
||||
func_decl_info info(m_family_id, OP_PB_LE, num_parameters, parameters);
|
||||
return m.mk_func_decl(m_pble_sym, arity, domain, m.mk_bool_sort(), info);
|
||||
}
|
||||
case OP_PB_GE: {
|
||||
if (num_parameters != 1 + arity || !parameters[0].is_int()) {
|
||||
m.raise_exception("function 'pbge' expects arity+1 integer parameters");
|
||||
}
|
||||
for (unsigned i = 1; i < num_parameters; ++i) {
|
||||
if (!parameters[i].is_int()) {
|
||||
m.raise_exception("function 'pbge' expects arity+1 integer parameters");
|
||||
}
|
||||
}
|
||||
func_decl_info info(m_family_id, OP_PB_GE, num_parameters, parameters);
|
||||
return m.mk_func_decl(m_pbge_sym, arity, domain, m.mk_bool_sort(), info);
|
||||
func_decl_info info(m_family_id, k, num_parameters, params.c_ptr());
|
||||
return m.mk_func_decl(sym, arity, domain, m.mk_bool_sort(), info);
|
||||
}
|
||||
default:
|
||||
UNREACHABLE();
|
||||
|
@ -80,7 +86,7 @@ void pb_decl_plugin::get_op_names(svector<builtin_name> & op_names, symbol const
|
|||
}
|
||||
}
|
||||
|
||||
app * pb_util::mk_le(unsigned num_args, int const * coeffs, expr * const * args, int k) {
|
||||
app * pb_util::mk_le(unsigned num_args, rational const * coeffs, expr * const * args, rational const& k) {
|
||||
vector<parameter> params;
|
||||
params.push_back(parameter(k));
|
||||
for (unsigned i = 0; i < num_args; ++i) {
|
||||
|
@ -89,7 +95,7 @@ app * pb_util::mk_le(unsigned num_args, int const * coeffs, expr * const * args,
|
|||
return m.mk_app(m_fid, OP_PB_LE, params.size(), params.c_ptr(), num_args, args, m.mk_bool_sort());
|
||||
}
|
||||
|
||||
app * pb_util::mk_ge(unsigned num_args, int const * coeffs, expr * const * args, int k) {
|
||||
app * pb_util::mk_ge(unsigned num_args, rational const * coeffs, expr * const * args, rational const& k) {
|
||||
vector<parameter> params;
|
||||
params.push_back(parameter(k));
|
||||
for (unsigned i = 0; i < num_args; ++i) {
|
||||
|
@ -105,13 +111,11 @@ app * pb_util::mk_at_most_k(unsigned num_args, expr * const * args, unsigned k)
|
|||
return m.mk_app(m_fid, OP_AT_MOST_K, 1, ¶m, num_args, args, m.mk_bool_sort());
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool pb_util::is_at_most_k(app *a) const {
|
||||
return is_app_of(a, m_fid, OP_AT_MOST_K);
|
||||
}
|
||||
|
||||
bool pb_util::is_at_most_k(app *a, unsigned& k) const {
|
||||
bool pb_util::is_at_most_k(app *a, rational& k) const {
|
||||
if (is_at_most_k(a)) {
|
||||
k = get_k(a);
|
||||
return true;
|
||||
|
@ -121,9 +125,35 @@ bool pb_util::is_at_most_k(app *a, unsigned& k) const {
|
|||
}
|
||||
}
|
||||
|
||||
int pb_util::get_k(app *a) const {
|
||||
SASSERT(is_at_most_k(a) || is_le(a) || is_ge(a));
|
||||
return a->get_decl()->get_parameter(0).get_int();
|
||||
|
||||
app * pb_util::mk_at_least_k(unsigned num_args, expr * const * args, unsigned k) {
|
||||
parameter param(k);
|
||||
return m.mk_app(m_fid, OP_AT_LEAST_K, 1, ¶m, num_args, args, m.mk_bool_sort());
|
||||
}
|
||||
|
||||
bool pb_util::is_at_least_k(app *a) const {
|
||||
return is_app_of(a, m_fid, OP_AT_LEAST_K);
|
||||
}
|
||||
|
||||
bool pb_util::is_at_least_k(app *a, rational& k) const {
|
||||
if (is_at_least_k(a)) {
|
||||
k = get_k(a);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
rational pb_util::get_k(app *a) const {
|
||||
parameter const& p = a->get_decl()->get_parameter(0);
|
||||
if (is_at_most_k(a) || is_at_least_k(a)) {
|
||||
return rational(p.get_int());
|
||||
}
|
||||
else {
|
||||
SASSERT(is_le(a) || is_ge(a));
|
||||
return p.get_rational();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -131,7 +161,7 @@ bool pb_util::is_le(app *a) const {
|
|||
return is_app_of(a, m_fid, OP_PB_LE);
|
||||
}
|
||||
|
||||
bool pb_util::is_le(app* a, int& k) const {
|
||||
bool pb_util::is_le(app* a, rational& k) const {
|
||||
if (is_le(a)) {
|
||||
k = get_k(a);
|
||||
return true;
|
||||
|
@ -145,7 +175,7 @@ bool pb_util::is_ge(app *a) const {
|
|||
return is_app_of(a, m_fid, OP_PB_GE);
|
||||
}
|
||||
|
||||
bool pb_util::is_ge(app* a, int& k) const {
|
||||
bool pb_util::is_ge(app* a, rational& k) const {
|
||||
if (is_ge(a)) {
|
||||
k = get_k(a);
|
||||
return true;
|
||||
|
@ -155,13 +185,13 @@ bool pb_util::is_ge(app* a, int& k) const {
|
|||
}
|
||||
}
|
||||
|
||||
int pb_util::get_coeff(app* a, unsigned index) {
|
||||
if (is_at_most_k(a)) {
|
||||
return 1;
|
||||
rational pb_util::get_coeff(app* a, unsigned index) {
|
||||
if (is_at_most_k(a) || is_at_least_k(a)) {
|
||||
return rational::one();
|
||||
}
|
||||
SASSERT(is_le(a) || is_ge(a));
|
||||
SASSERT(1 + index < a->get_decl()->get_num_parameters());
|
||||
return a->get_decl()->get_parameter(index + 1).get_int();
|
||||
return a->get_decl()->get_parameter(index + 1).get_rational();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@ hence:
|
|||
|
||||
enum pb_op_kind {
|
||||
OP_AT_MOST_K, // at most K Booleans are true.
|
||||
OP_AT_LEAST_K, // at least K Booleans are true.
|
||||
OP_PB_LE, // pseudo-Boolean <= (generalizes at_most_k)
|
||||
OP_PB_GE, // pseudo-Boolean >=
|
||||
LAST_PB_OP
|
||||
|
@ -39,11 +40,13 @@ enum pb_op_kind {
|
|||
|
||||
class pb_decl_plugin : public decl_plugin {
|
||||
symbol m_at_most_sym;
|
||||
symbol m_at_least_sym;
|
||||
symbol m_pble_sym;
|
||||
symbol m_pbge_sym;
|
||||
func_decl * mk_at_most(unsigned arity, unsigned k);
|
||||
func_decl * mk_le(unsigned arity, int const* coeffs, int k);
|
||||
func_decl * mk_ge(unsigned arity, int const* coeffs, int k);
|
||||
func_decl * mk_at_least(unsigned arity, unsigned k);
|
||||
func_decl * mk_le(unsigned arity, rational const* coeffs, int k);
|
||||
func_decl * mk_ge(unsigned arity, rational const* coeffs, int k);
|
||||
public:
|
||||
pb_decl_plugin();
|
||||
virtual ~pb_decl_plugin() {}
|
||||
|
@ -76,16 +79,19 @@ public:
|
|||
ast_manager & get_manager() const { return m; }
|
||||
family_id get_family_id() const { return m_fid; }
|
||||
app * mk_at_most_k(unsigned num_args, expr * const * args, unsigned k);
|
||||
app * mk_le(unsigned num_args, int const * coeffs, expr * const * args, int k);
|
||||
app * mk_ge(unsigned num_args, int const * coeffs, expr * const * args, int k);
|
||||
app * mk_at_least_k(unsigned num_args, expr * const * args, unsigned k);
|
||||
app * mk_le(unsigned num_args, rational const * coeffs, expr * const * args, rational const& k);
|
||||
app * mk_ge(unsigned num_args, rational const * coeffs, expr * const * args, rational const& k);
|
||||
bool is_at_most_k(app *a) const;
|
||||
bool is_at_most_k(app *a, unsigned& k) const;
|
||||
int get_k(app *a) const;
|
||||
bool is_at_most_k(app *a, rational& k) const;
|
||||
bool is_at_least_k(app *a) const;
|
||||
bool is_at_least_k(app *a, rational& k) const;
|
||||
rational get_k(app *a) const;
|
||||
bool is_le(app *a) const;
|
||||
bool is_le(app* a, int& k) const;
|
||||
bool is_le(app* a, rational& k) const;
|
||||
bool is_ge(app* a) const;
|
||||
bool is_ge(app* a, int& k) const;
|
||||
int get_coeff(app* a, unsigned index);
|
||||
bool is_ge(app* a, rational& k) const;
|
||||
rational get_coeff(app* a, unsigned index);
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue