mirror of
https://github.com/Z3Prover/z3
synced 2025-04-22 16:45:31 +00:00
working on hilbert basis
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
commit
aaf0c16e08
9 changed files with 480 additions and 450 deletions
|
@ -619,6 +619,8 @@ namespace z3 {
|
|||
a.check_error();
|
||||
return expr(a.ctx(), r);
|
||||
}
|
||||
friend expr implies(expr const & a, bool b) { return implies(a, a.ctx().bool_val(b)); }
|
||||
friend expr implies(bool a, expr const & b) { return implies(b.ctx().bool_val(a), b); }
|
||||
|
||||
/**
|
||||
\brief Create the if-then-else expression <tt>ite(c, t, e)</tt>
|
||||
|
@ -758,7 +760,7 @@ namespace z3 {
|
|||
return expr(a.ctx(), r);
|
||||
}
|
||||
friend expr operator-(expr const & a, int b) { return a - a.ctx().num_val(b, a.get_sort()); }
|
||||
friend expr operator-(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) - a; }
|
||||
friend expr operator-(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) - b; }
|
||||
|
||||
friend expr operator<=(expr const & a, expr const & b) {
|
||||
check_context(a, b);
|
||||
|
@ -777,7 +779,7 @@ namespace z3 {
|
|||
return expr(a.ctx(), r);
|
||||
}
|
||||
friend expr operator<=(expr const & a, int b) { return a <= a.ctx().num_val(b, a.get_sort()); }
|
||||
friend expr operator<=(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) <= a; }
|
||||
friend expr operator<=(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) <= b; }
|
||||
|
||||
friend expr operator>=(expr const & a, expr const & b) {
|
||||
check_context(a, b);
|
||||
|
@ -796,7 +798,7 @@ namespace z3 {
|
|||
return expr(a.ctx(), r);
|
||||
}
|
||||
friend expr operator>=(expr const & a, int b) { return a >= a.ctx().num_val(b, a.get_sort()); }
|
||||
friend expr operator>=(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) >= a; }
|
||||
friend expr operator>=(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) >= b; }
|
||||
|
||||
friend expr operator<(expr const & a, expr const & b) {
|
||||
check_context(a, b);
|
||||
|
@ -815,7 +817,7 @@ namespace z3 {
|
|||
return expr(a.ctx(), r);
|
||||
}
|
||||
friend expr operator<(expr const & a, int b) { return a < a.ctx().num_val(b, a.get_sort()); }
|
||||
friend expr operator<(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) < a; }
|
||||
friend expr operator<(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) < b; }
|
||||
|
||||
friend expr operator>(expr const & a, expr const & b) {
|
||||
check_context(a, b);
|
||||
|
@ -834,7 +836,7 @@ namespace z3 {
|
|||
return expr(a.ctx(), r);
|
||||
}
|
||||
friend expr operator>(expr const & a, int b) { return a > a.ctx().num_val(b, a.get_sort()); }
|
||||
friend expr operator>(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) > a; }
|
||||
friend expr operator>(int a, expr const & b) { return b.ctx().num_val(a, b.get_sort()) > b; }
|
||||
|
||||
friend expr operator&(expr const & a, expr const & b) { check_context(a, b); Z3_ast r = Z3_mk_bvand(a.ctx(), a, b); return expr(a.ctx(), r); }
|
||||
friend expr operator&(expr const & a, int b) { return a & a.ctx().num_val(b, a.get_sort()); }
|
||||
|
@ -888,31 +890,31 @@ namespace z3 {
|
|||
*/
|
||||
inline expr ule(expr const & a, expr const & b) { return to_expr(a.ctx(), Z3_mk_bvule(a.ctx(), a, b)); }
|
||||
inline expr ule(expr const & a, int b) { return ule(a, a.ctx().num_val(b, a.get_sort())); }
|
||||
inline expr ule(int a, expr const & b) { return ule(b.ctx().num_val(a, b.get_sort()), a); }
|
||||
inline expr ule(int a, expr const & b) { return ule(b.ctx().num_val(a, b.get_sort()), b); }
|
||||
/**
|
||||
\brief unsigned less than operator for bitvectors.
|
||||
*/
|
||||
inline expr ult(expr const & a, expr const & b) { return to_expr(a.ctx(), Z3_mk_bvult(a.ctx(), a, b)); }
|
||||
inline expr ult(expr const & a, int b) { return ult(a, a.ctx().num_val(b, a.get_sort())); }
|
||||
inline expr ult(int a, expr const & b) { return ult(b.ctx().num_val(a, b.get_sort()), a); }
|
||||
inline expr ult(int a, expr const & b) { return ult(b.ctx().num_val(a, b.get_sort()), b); }
|
||||
/**
|
||||
\brief unsigned greater than or equal to operator for bitvectors.
|
||||
*/
|
||||
inline expr uge(expr const & a, expr const & b) { return to_expr(a.ctx(), Z3_mk_bvuge(a.ctx(), a, b)); }
|
||||
inline expr uge(expr const & a, int b) { return uge(a, a.ctx().num_val(b, a.get_sort())); }
|
||||
inline expr uge(int a, expr const & b) { return uge(b.ctx().num_val(a, b.get_sort()), a); }
|
||||
inline expr uge(int a, expr const & b) { return uge(b.ctx().num_val(a, b.get_sort()), b); }
|
||||
/**
|
||||
\brief unsigned greater than operator for bitvectors.
|
||||
*/
|
||||
inline expr ugt(expr const & a, expr const & b) { return to_expr(a.ctx(), Z3_mk_bvugt(a.ctx(), a, b)); }
|
||||
inline expr ugt(expr const & a, int b) { return ugt(a, a.ctx().num_val(b, a.get_sort())); }
|
||||
inline expr ugt(int a, expr const & b) { return ugt(b.ctx().num_val(a, b.get_sort()), a); }
|
||||
inline expr ugt(int a, expr const & b) { return ugt(b.ctx().num_val(a, b.get_sort()), b); }
|
||||
/**
|
||||
\brief unsigned division operator for bitvectors.
|
||||
*/
|
||||
inline expr udiv(expr const & a, expr const & b) { return to_expr(a.ctx(), Z3_mk_bvudiv(a.ctx(), a, b)); }
|
||||
inline expr udiv(expr const & a, int b) { return udiv(a, a.ctx().num_val(b, a.get_sort())); }
|
||||
inline expr udiv(int a, expr const & b) { return udiv(b.ctx().num_val(a, b.get_sort()), a); }
|
||||
inline expr udiv(int a, expr const & b) { return udiv(b.ctx().num_val(a, b.get_sort()), b); }
|
||||
|
||||
// Basic functions for creating quantified formulas.
|
||||
// The C API should be used for creating quantifiers with patterns, weights, many variables, etc.
|
||||
|
|
|
@ -1849,9 +1849,9 @@ void ast_manager::check_sort(func_decl const * decl, unsigned num_args, expr * c
|
|||
sort * given = get_sort(args[i]);
|
||||
if (!compatible_sorts(expected, given)) {
|
||||
std::ostringstream buff;
|
||||
buff << "Invalid function application for " << decl->get_name() << ". ";
|
||||
buff << "Sort mismatch on argument at position " << (i+1) << ". ";
|
||||
buff << "Expected: " << mk_pp(expected, m) << " but given " << mk_pp(given, m);
|
||||
buff << "invalid function application for " << decl->get_name() << ", ";
|
||||
buff << "sort mismatch on argument at position " << (i+1) << ", ";
|
||||
buff << "expected " << mk_pp(expected, m) << " but given " << mk_pp(given, m);
|
||||
throw ast_exception(buff.str().c_str());
|
||||
}
|
||||
}
|
||||
|
@ -1865,9 +1865,9 @@ void ast_manager::check_sort(func_decl const * decl, unsigned num_args, expr * c
|
|||
sort * given = get_sort(args[i]);
|
||||
if (!compatible_sorts(expected, given)) {
|
||||
std::ostringstream buff;
|
||||
buff << "Invalid function application for " << decl->get_name() << ". ";
|
||||
buff << "Sort mismatch on argument at position " << (i+1) << ". ";
|
||||
buff << "Expected: " << mk_pp(expected, m) << " but given " << mk_pp(given, m);
|
||||
buff << "invalid function application for " << decl->get_name() << ", ";
|
||||
buff << "sort mismatch on argument at position " << (i+1) << ", ";
|
||||
buff << "expected " << mk_pp(expected, m) << " but given " << mk_pp(given, m);
|
||||
throw ast_exception(buff.str().c_str());
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -34,11 +34,9 @@ public:
|
|||
typedef rational numeral;
|
||||
typedef vector<numeral> num_vector;
|
||||
private:
|
||||
class rational_heap;
|
||||
class value_index;
|
||||
class index;
|
||||
class passive;
|
||||
class weight_map;
|
||||
struct offset_t {
|
||||
unsigned m_offset;
|
||||
offset_t(unsigned o) : m_offset(o) {}
|
||||
|
@ -59,25 +57,28 @@ private:
|
|||
numeral* m_values;
|
||||
public:
|
||||
values(numeral* v):m_values(v) {}
|
||||
numeral& value() { return m_values[0]; } // value of a*x
|
||||
numeral& weight() { return m_values[0]; } // value of a*x
|
||||
numeral& operator[](unsigned i) { return m_values[i+1]; } // value of x_i
|
||||
numeral const& value() const { return m_values[0]; } // value of a*x
|
||||
numeral const& weight() const { return m_values[0]; } // value of a*x
|
||||
numeral const& operator[](unsigned i) const { return m_values[i+1]; } // value of x_i
|
||||
};
|
||||
|
||||
vector<num_vector> m_ineqs;
|
||||
num_vector m_store;
|
||||
svector<offset_t> m_basis;
|
||||
svector<offset_t> m_free_list;
|
||||
svector<offset_t> m_active;
|
||||
svector<offset_t> m_zero;
|
||||
volatile bool m_cancel;
|
||||
vector<num_vector> m_ineqs; // set of asserted inequalities
|
||||
svector<bool> m_iseq; // inequalities that are equalities
|
||||
num_vector m_store; // store of vectors
|
||||
svector<offset_t> m_basis; // vector of current basis
|
||||
svector<offset_t> m_free_list; // free list of unused storage
|
||||
svector<offset_t> m_active; // active set
|
||||
svector<offset_t> m_zero; // zeros
|
||||
passive* m_passive; // passive set
|
||||
volatile bool m_cancel;
|
||||
stats m_stats;
|
||||
index* m_index;
|
||||
passive* m_passive;
|
||||
index* m_index; // index of generated vectors
|
||||
unsigned_vector m_ints; // indices that can be both positive and negative
|
||||
unsigned m_current_ineq;
|
||||
class iterator {
|
||||
hilbert_basis const& hb;
|
||||
unsigned m_idx;
|
||||
unsigned m_idx;
|
||||
public:
|
||||
iterator(hilbert_basis const& hb, unsigned idx): hb(hb), m_idx(idx) {}
|
||||
offset_t operator*() const { return hb.m_basis[m_idx]; }
|
||||
|
@ -89,10 +90,17 @@ private:
|
|||
|
||||
static offset_t mk_invalid_offset();
|
||||
static bool is_invalid_offset(offset_t offs);
|
||||
lbool saturate(num_vector const& ineq);
|
||||
lbool saturate(num_vector const& ineq, bool is_eq);
|
||||
void init_basis();
|
||||
void select_inequality();
|
||||
unsigned get_num_nonzeros(num_vector const& ineq);
|
||||
unsigned get_ineq_product(num_vector const& ineq);
|
||||
|
||||
void add_unit_vector(unsigned i, numeral const& e);
|
||||
unsigned get_num_vars() const;
|
||||
void set_eval(values& val, num_vector const& ineq) const;
|
||||
numeral get_weight(values& val, num_vector const& ineq) const;
|
||||
bool is_geq(values const& v, values const& w) const;
|
||||
bool is_abs_geq(numeral const& v, numeral const& w) const;
|
||||
bool is_subsumed(offset_t idx);
|
||||
bool is_subsumed(offset_t i, offset_t j) const;
|
||||
void recycle(offset_t idx);
|
||||
|
@ -108,7 +116,7 @@ private:
|
|||
|
||||
void display(std::ostream& out, offset_t o) const;
|
||||
void display(std::ostream& out, values const & v) const;
|
||||
void display_ineq(std::ostream& out, num_vector const& v) const;
|
||||
void display_ineq(std::ostream& out, num_vector const& v, bool is_eq) const;
|
||||
|
||||
public:
|
||||
|
||||
|
@ -131,6 +139,8 @@ public:
|
|||
void add_le(num_vector const& v, numeral const& b);
|
||||
void add_eq(num_vector const& v, numeral const& b);
|
||||
|
||||
void set_is_int(unsigned var_index);
|
||||
|
||||
lbool saturate();
|
||||
|
||||
void set_cancel(bool f) { m_cancel = f; }
|
||||
|
|
|
@ -74,6 +74,14 @@ static void gorrila_test(unsigned seed, unsigned n, unsigned k, unsigned bound,
|
|||
saturate_basis(hb);
|
||||
}
|
||||
|
||||
static vector<rational> vec(int i, int j) {
|
||||
vector<rational> nv;
|
||||
nv.resize(2);
|
||||
nv[0] = rational(i);
|
||||
nv[1] = rational(j);
|
||||
return nv;
|
||||
}
|
||||
|
||||
static vector<rational> vec(int i, int j, int k) {
|
||||
vector<rational> nv;
|
||||
nv.resize(3);
|
||||
|
@ -243,25 +251,37 @@ static void tst11() {
|
|||
saturate_basis(hb);
|
||||
}
|
||||
|
||||
static void tst12() {
|
||||
hilbert_basis hb;
|
||||
hb.add_le(vec(1, 0), R(1));
|
||||
hb.add_le(vec(0, 1), R(1));
|
||||
saturate_basis(hb);
|
||||
}
|
||||
|
||||
void tst_hilbert_basis() {
|
||||
std::cout << "hilbert basis test\n";
|
||||
#if 0
|
||||
tst1();
|
||||
tst2();
|
||||
tst3();
|
||||
tst4();
|
||||
tst5();
|
||||
tst6();
|
||||
tst7();
|
||||
tst8();
|
||||
tst9();
|
||||
tst10();
|
||||
tst11();
|
||||
gorrila_test(0, 4, 3, 20, 5);
|
||||
gorrila_test(1, 4, 3, 20, 5);
|
||||
gorrila_test(2, 4, 3, 20, 5);
|
||||
gorrila_test(0, 4, 2, 20, 5);
|
||||
gorrila_test(0, 4, 2, 20, 5);
|
||||
#endif
|
||||
gorrila_test(0, 10, 7, 20, 11);
|
||||
tst12();
|
||||
return;
|
||||
|
||||
if (true) {
|
||||
tst1();
|
||||
tst2();
|
||||
tst3();
|
||||
tst4();
|
||||
tst5();
|
||||
tst6();
|
||||
tst7();
|
||||
tst8();
|
||||
tst9();
|
||||
tst10();
|
||||
tst11();
|
||||
gorrila_test(0, 4, 3, 20, 5);
|
||||
gorrila_test(1, 4, 3, 20, 5);
|
||||
gorrila_test(2, 4, 3, 20, 5);
|
||||
gorrila_test(0, 4, 2, 20, 5);
|
||||
gorrila_test(0, 4, 2, 20, 5);
|
||||
}
|
||||
else {
|
||||
gorrila_test(0, 10, 7, 20, 11);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue