3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-14 04:48:45 +00:00
z3/src/test/simplex.cpp
Arie Gurfinkel d2b618df23
Spacer Global Guidance (#6026)
* Make spacer_sem_matcher::reset() public

* Add .clang-format for src/muz/spacer

* Mark substitution::get_bindings() as const

* Fix in spacer_antiunify

* Various helper methods in spacer_util

Minor functions to compute number of free variables, detect presence of certain
sub-expressions, etc.

The diff is ugly because of clang-format

* Add spacer_cluster for clustering lemmas

A cluster of lemmas is a set of lemmas that are all instances of the same
pattern, where a pattern is a qff formula with free variables.

Currently, the instances are required to be explicit, that is, they are all
obtained by substituting concrete values (i.e., numbers) for free variables of
the pattern.

Lemmas are clustered in cluster_db in each predicate transformer.

* Integrate spacer_cluster into spacer_context

* Custom clang-format pragmas for spacer_context

spacer_context.(cpp|h) are large and have inconsistent formatting. Disable
clang-format for them until merge with main z3 branch and re-format.

* Computation of convex closure and matrix kernel

Various LA functions. The implementations are somewhat preliminary.

Convex closure is simplemented via syntactic convex closure procedure.
Kernel computation considers many common cases.

spacer_arith_kernel_sage implements kernel computation by call external
Sage binary. It is used only for debugging and experiments. There is no
link dependence on Sage. If desired, it can be removed.

* Add spacer_concretize

* Utility methods for spacer conjecture rule

* Add spacer_expand_bnd_generalizer

Generalizes arithmetic inequality literals of the form x <= c,
by changing constant c to other constants found in the problem.

* Add spacer_global_generalizer

Global generalizer checks every new lemma against a cluster
of previously learned lemmas, and, if possible, conjectures
a new pob, that, when blocked, generalizes multiple existing
lemmas.

* Remove fp.spacer.print_json option

The option is used to dump state of spacer into json for debugging.

It has been replaced by `fp.spacer.trace_file` that allows dumping an execution
of spacer. The json file can be reconstructed from the trace file elsewhere.

* Workaround for segfault in spacer_proof_utils

Issue #3 in hgvk94/z3

Segfault in some proof reduction. Avoid by bailing out on reduction.

* Revert bug for incomplete models

* Use local fresh variables in spacer_global_generalizer

* Cleanup of spacer_convex_closure

* Allow arbitrary expressions to name cols in convex_closure

* WIP: convex closure

* WIP: convex closure

* Fix bindings order in spacer_global_generalizer

The matcher creates substitution using std_order, which is
reverse of expected order (variable 0 is last). Adjust the code
appropriately for that.

* Increase verbosity level for smt_context stats

* Dead code in qe_mbp

* bug fixes in spacer_global_generalizer::subsumer

* Partially remove dependence of size of m_alphas

I want m_alphas to potentially be greater than currently used alpha variables.
This is helpful for reusing them across multiple calls to convex closure

* Subtle bug in kernel computation

Coefficient was being passed by reference and, therefore, was
being changed indirectly.

In the process, updated the code to be more generic to avoid rational
computation in the middle of matrix manipulation.

* another test for sparse_matrix_ops::kernel

* Implementation of matrix kernel using Fraction Free Elimination

Ensures that the kernel is int for int matrices. All divisions are exact.

* clang-format sparse_matrix_ops.h

* another implementation of ffe kernel in sparse_matrix_ops

* Re-do arith_kernel and convex_closure

* update spacer_global_generalization for new subsumer

* remove spacer.gg.use_sage parameter

* cleanup of spacer_global_generalizer

* Removed dependency on sage

* fix in spacer_convex_closure

* spacer_sem_matcher: consider an additional semantic matching

disabled until it is shown useful

* spacer_global_generalizer: improve do_conjecture

 - if conjecture does not apply to pob, use lemma instead
 - better normalization
 - improve debug prints

* spacer_conjecture: formatting

* spacer_cluster: improve debug prints

* spacer_context: improve debug prints

* spacer_context: re-queue may pobs

enabled even if global re-queue is disabled

* spacer_cluster print formatting

* reset methods on pob

* cleanup of print and local variable names

* formatting

* reset generalization data once it has been used

* refactored extra pob creation during global guidance

* fix bug copying sparse matrix into spacer matrix

* bug fix in spacer_convex_closure

* formatting change in spacer_context

* spacer_cluster: get_min_lvl

chose level based on pob as well as lemmas

* spacer_context: add desired_level to pob

desired_level indicates at which level pob should be proved.
A pob will be pushed to desired_level if necessary

* spacer_context: renamed subsume stats

the name of success/failed was switched

* spacer_convex_closure: fix prototype of is_congruent_mod()

* spacer_convex_closure: hacks in infer_div_pred()

* spacer_util: do not expand literals with mod

By default, equality literal t=p is expanded into t<=p && t>=p

Disable the expansion in case t contains 'mod' operator since such
expansion is usually not helpful for divisibility

* spacer_util: rename m_util into m_arith

* spacer_util: cleanup normalize()

* spacer_util: formatting

* spacer_context: formatting cleanup on subsume and conjecture

* spacer_context: fix handling may pobs when abs_weakness is enabled

A pob might be undef, so weakness must be bumped up

* spacer_arith_kernel: enhance debug print

* spacer_global_generalizer: improve matching on conjecture

* spacer_global_generalizer: set desired level on conjecture pob

* spacer_global_generalizer: debug print

* spacer_global_generalizer: set min level on new pobs

the new level should not be higher than the pob that was generalized

* spacer_global_generalizer: do no re-create closed pobs

If a generalized pob exist and closed, do not re-create it.

* spacer_context: normalize twice

* spacer_context: forward propagate only same kind of pobs

* sketch of inductive generalizer

A better implementation of inductive generalizer that in addition to dropping
literals also attempts to weaken them.

Current implementation is a sketch to be extended based on examples/requirements.

* fix ordering in spacer_cluster_util

* fix resetting of substitution matcher in spacer_conjecture

Old code would forget to reset the substitution provided to the sem_matcher.
Thus, if the substitution was matched once (i.e., one literal of interest is
found), no other literal would be matched.

* add spacer_util is_normalized() method

used for debugging only

* simplify normalization of pob expressions

pob expressions are normalized to increase syntactic matching.
Some of the normalization rules seem out of place, so removing them for now.

* fix in spacer_global_generalizer

If conjecture fails, do not try other generalization strategies -- they will not apply.

* fix in spacer_context

do not check that may pob is blocked by existing lemmas.
It is likely to be blocked. Our goal is to block it again and generalize
to a new lemma.

This can be further improved by moving directly to generalization when pob is
blocked by existing lemmas...

Co-authored-by: hgvk94 <hgvk94@gmail.com>
2022-08-30 15:47:00 -07:00

232 lines
5.4 KiB
C++

/*++
Copyright (c) 2015 Microsoft Corporation
--*/
#include "math/simplex/sparse_matrix_def.h"
#include "math/simplex/simplex.h"
#include "math/simplex/simplex_def.h"
#include "util/mpq_inf.h"
#include "util/vector.h"
#include "util/rational.h"
#include "util/rlimit.h"
#include <iostream>
#define R rational
typedef simplex::simplex<simplex::mpz_ext> Simplex;
typedef simplex::sparse_matrix<simplex::mpz_ext> sparse_matrix;
typedef simplex::sparse_matrix<simplex::mpq_ext> qmatrix;
static vector<R> vec(int i, int j) {
vector<R> nv;
nv.resize(2);
nv[0] = R(i);
nv[1] = R(j);
return nv;
}
static vector<R> vec(int i, int j, int k) {
vector<R> nv = vec(i, j);
nv.push_back(R(k));
return nv;
}
static vector<R> vec(int i, int j, int k, int l) {
vector<R> nv = vec(i, j, k);
nv.push_back(R(l));
return nv;
}
static vector<R> vec(int i, int j, int k, int l, int x) {
vector<R> nv = vec(i, j, k, l);
nv.push_back(R(x));
return nv;
}
static vector<R> vec(int i, int j, int k, int l, int x, int y) {
vector<R> nv = vec(i, j, k, l, x);
nv.push_back(R(y));
return nv;
}
// static vector<R> vec(int i, int j, int k, int l, int x, int y, int z) {
// vector<R> nv = vec(i, j, k, l, x, y);
// nv.push_back(R(z));
// return nv;
// }
void add_row(Simplex& S, vector<R> const& _v, R const& _b, bool is_eq = false) {
unsynch_mpz_manager m;
unsigned_vector vars;
vector<R> v(_v);
R b(_b);
R l(denominator(b));
scoped_mpz_vector coeffs(m);
for (unsigned i = 0; i < v.size(); ++i) {
l = lcm(l, denominator(v[i]));
vars.push_back(i);
S.ensure_var(i);
}
b *= l;
b.neg();
for (unsigned i = 0; i < v.size(); ++i) {
v[i] *= l;
coeffs.push_back(v[i].to_mpq().numerator());
}
unsigned nv = S.get_num_vars();
vars.push_back(nv);
vars.push_back(nv+1);
S.ensure_var(nv);
S.ensure_var(nv+1);
coeffs.push_back(mpz(-1));
coeffs.push_back(b.to_mpq().numerator());
mpq_inf one(mpq(1),mpq(0));
mpq_inf zero(mpq(0),mpq(0));
ENSURE(vars.size() == coeffs.size());
S.set_lower(nv, zero);
if (is_eq) S.set_upper(nv, zero);
S.set_lower(nv+1, one);
S.set_upper(nv+1, one);
S.add_row(nv, coeffs.size(), vars.data(), coeffs.data());
}
static void feas(Simplex& S) {
S.display(std::cout);
lbool is_sat = S.make_feasible();
std::cout << "feasible: " << is_sat << "\n";
S.display(std::cout);
}
static void test1() {
reslimit rl;
Simplex S(rl);
add_row(S, vec(1,0), R(1));
add_row(S, vec(0,1), R(1));
add_row(S, vec(1,1), R(1));
feas(S);
}
static void test2() {
reslimit rl; Simplex S(rl);
add_row(S, vec(1, 0), R(1));
add_row(S, vec(0, 1), R(1));
add_row(S, vec(1, 1), R(1), true);
feas(S);
}
static void test3() {
reslimit rl; Simplex S(rl);
add_row(S, vec(-1, 0), R(-1));
add_row(S, vec(0, -1), R(-1));
add_row(S, vec(1, 1), R(1), true);
feas(S);
}
static void test4() {
reslimit rl; Simplex S(rl);
add_row(S, vec(1, 0), R(1));
add_row(S, vec(0, -1), R(-1));
add_row(S, vec(1, 1), R(1), true);
feas(S);
}
static void add(qmatrix& m, vector<R> const& v) {
m.ensure_var(v.size()-1);
auto r = m.mk_row();
for (unsigned u = 0; u < v.size(); ++u)
m.add_var(r, v[u].to_mpq(), u);
}
static void test5() {
unsynch_mpq_manager m;
qmatrix M(m);
add(M, vec(1, 0, -3, 0, 2, -8));
add(M, vec(0, 1, 5, 0, -1, 4));
add(M, vec(0, 0, 0, 1, 7, -9));
add(M, vec(0, 0, 0, 0, 0, 0));
M.display(std::cout);
vector<vector<rational>> K;
kernel_ffe(M, K);
std::cout << "after\n";
for (auto const& v : K)
std::cout << v << "\n";
M.display(std::cout);
}
static void test6() {
unsynch_mpq_manager m;
qmatrix M(m);
add(M, vec(-1, 2, 1));
add(M, vec(0, 1, 1));
M.display(std::cout);
vector<vector<rational>> K;
kernel_ffe(M, K);
std::cout << "Kernel:\n";
for (auto const &v : K)
std::cout << v << "\n";
std::cout << "matrix after\n";
M.display(std::cout);
}
static void test7() {
unsynch_mpq_manager m;
qmatrix M(m);
add(M, vec(1, 2, 3, 4, 10));
add(M, vec(2, 2, 3, 4, 11));
add(M, vec(3, 3, 3, 4, 13));
add(M, vec(9, 8, 7, 6, 30));
M.display(std::cout);
vector<vector<rational>> K;
kernel_ffe(M, K);
std::cout << "Kernel:\n";
for (auto const &v : K)
std::cout << v << "\n";
std::cout << "matrix after\n";
M.display(std::cout);
}
void tst_simplex() {
reslimit rl; Simplex S(rl);
std::cout << "simplex\n";
lbool is_sat = S.make_feasible();
std::cout << "feasible: " << is_sat << "\n";
unsynch_mpz_manager m;
unsynch_mpq_inf_manager em;
scoped_mpz_vector coeffs(m);
svector<unsigned> vars;
for (unsigned i = 0; i < 5; ++i) {
S.ensure_var(i);
vars.push_back(i);
coeffs.push_back(mpz(i+1));
}
// Simplex::row r = S.add_row(1, coeffs.size(), vars.c_ptr(), coeffs.c_ptr());
is_sat = S.make_feasible();
std::cout << "feasible: " << is_sat << "\n";
S.display(std::cout);
_scoped_numeral<unsynch_mpq_inf_manager> num(em);
num = std::make_pair(mpq(1), mpq(0));
S.set_lower(0, num);
S.set_upper(0, num);
is_sat = S.make_feasible();
std::cout << "feasible: " << is_sat << "\n";
S.display(std::cout);
test1();
test2();
test3();
test4();
test5();
test6();
test7();
}