3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-13 20:38:43 +00:00

fix remove lar_solver::add_constraint

Signed-off-by: Lev Nachmanson <levnach@hotmail.com>
This commit is contained in:
Lev Nachmanson 2019-08-01 17:28:49 -07:00
parent 7cd90537c3
commit 3c5b1086a1
8 changed files with 164 additions and 122 deletions

View file

@ -204,9 +204,22 @@ interv horner::interval_of_mul(const nex& e) {
return a;
}
bool horner::find_term_expr(rational& a, const lp::lar_term * &t, rational& b) const {
return false;
}
interv horner::interval_of_sum(const nex& e) {
TRACE("nla_horner_details", tout << "e=" << e << "\n";);
SASSERT(e.is_sum());
if (e.sum_is_linear()) {
const lp::lar_term * t;
rational a,b;
if (find_term_expr(a, t, b)) {
//todo create interval from a*t + b
SASSERT(false);
}
}
auto & es = e.children();
interv a = interval_of_expr(es[0]);
if (m_intervals.is_inf(a)) {

View file

@ -49,5 +49,6 @@ public:
template <typename T> // T has an iterator of (coeff(), var())
bool row_has_monomial_to_refine(const T&) const;
bool find_term_expr(rational& a, const lp::lar_term * & t, rational& b) const;
}; // end of horner
}

View file

@ -1941,15 +1941,6 @@ void lar_solver::add_var_bound_on_constraint_for_term(var_index j, lconstraint_k
}
}
constraint_index lar_solver::add_constraint(const vector<std::pair<mpq, var_index>>& left_side_with_terms, lconstraint_kind kind_par, const mpq& right_side_parm) {
vector<std::pair<mpq, var_index>> left_side;
substitute_terms_in_linear_expression(left_side_with_terms, left_side);
unsigned term_index = add_term(left_side, -1);
constraint_index ci = m_constraints.size();
add_var_bound_on_constraint_for_term(term_index, kind_par, right_side_parm, ci);
return ci;
}
void lar_solver::add_constraint_from_term_and_create_new_column_row(unsigned term_j, const lar_term* term,
lconstraint_kind kind, const mpq & right_side) {

View file

@ -49,14 +49,14 @@ namespace lp {
class lar_solver : public column_namer {
struct term_hasher {
std::size_t operator()(const lar_term *t) const
std::size_t operator()(const lar_term &t) const
{
using std::size_t;
using std::hash;
using std::string;
size_t seed = 0;
int i = 0;
for (const auto& p : t->coeffs()) {
for (const auto& p : t.coeffs()) {
hash_combine(seed, p.m_key);
hash_combine(seed, p.m_value);
if (i++ > 10)
@ -66,31 +66,14 @@ class lar_solver : public column_namer {
}
};
struct term_ls_comparer {
bool operator()(const lar_term *a, const lar_term* b) const
struct term_comparer {
bool operator()(const lar_term &a, const lar_term& b) const
{
return a->coeffs() == b->coeffs();
// // a is contained in b
// for (auto & p : a->coeffs()) {
// auto t = b->coeffs().find_iterator(p.m_key);
// if (t == b->coeffs().end())
// return false;
// if (p.m_value != t->m_value)
// return false;
// }
// // zz is contained in b
// for (auto & p : b->coeffs()) {
// auto t = a->coeffs().find_iterator(p.m_key);
// if (t == a->coeffs().end())
// return false;
// if (p.m_value != t->m_value)
// return false;
// }
// return true;
return a.coeffs() == b.coeffs();
}
};
std::unordered_set<lar_term*, term_hasher, term_ls_comparer> m_set_of_terms;
std::unordered_map<lar_term, unsigned, term_hasher, term_comparer> m_normalized_terms_to_columns;
//////////////////// fields //////////////////////////
@ -464,7 +447,6 @@ public:
bool all_constrained_variables_are_registered(const vector<std::pair<mpq, var_index>>& left_side);
constraint_index add_constraint(const vector<std::pair<mpq, var_index>>& left_side_with_terms, lconstraint_kind kind_par, const mpq& right_side_parm);
bool all_constraints_hold() const;
bool constraint_holds(const lar_base_constraint & constr, std::unordered_map<var_index, mpq> & var_map) const;
bool the_relations_are_of_same_type(const vector<std::pair<mpq, unsigned>> & evidence, lconstraint_kind & the_kind_of_sum) const;

View file

@ -816,7 +816,7 @@ public:
return ret;
}
void fill_lar_solver_on_row(row * row, lar_solver *solver) {
void fill_lar_solver_on_row(row * row, lar_solver *solver, int row_index) {
if (row->m_name != m_cost_row_name) {
auto kind = get_lar_relation_from_row(row->m_type);
vector<std::pair<mpq, var_index>> ls;
@ -824,7 +824,8 @@ public:
var_index i = solver->add_var(get_var_index(s.first), false);
ls.push_back(std::make_pair(s.second, i));
}
solver->add_constraint(ls, kind, row->m_right_side);
unsigned j = solver->add_term(ls, row_index);
solver->add_var_bound(j, kind, row->m_right_side);
} else {
// ignore the cost row
}
@ -832,30 +833,26 @@ public:
void fill_lar_solver_on_rows(lar_solver * solver) {
int row_index = 0;
for (auto row_it : m_rows) {
fill_lar_solver_on_row(row_it.second, solver);
fill_lar_solver_on_row(row_it.second, solver, row_index++);
}
}
void create_low_constraint_for_var(column* col, bound * b, lar_solver *solver) {
vector<std::pair<mpq, var_index>> ls;
var_index i = solver->add_var(col->m_index, false);
ls.push_back(std::make_pair(numeric_traits<T>::one(), i));
solver->add_constraint(ls, GE, b->m_low);
solver->add_var_bound(i, GE, b->m_low);
}
void create_upper_constraint_for_var(column* col, bound * b, lar_solver *solver) {
var_index i = solver->add_var(col->m_index, false);
vector<std::pair<mpq, var_index>> ls;
ls.push_back(std::make_pair(numeric_traits<T>::one(), i));
solver->add_constraint(ls, LE, b->m_upper);
solver->add_var_bound(i, LE, b->m_upper);
}
void create_equality_contraint_for_var(column* col, bound * b, lar_solver *solver) {
var_index i = solver->add_var(col->m_index, false);
vector<std::pair<mpq, var_index>> ls;
ls.push_back(std::make_pair(numeric_traits<T>::one(), i));
solver->add_constraint(ls, EQ, b->m_fixed_value);
solver->add_var_bound(i, LE, b->m_fixed_value);
solver->add_var_bound(i, GE, b->m_fixed_value);
}
void fill_lar_solver_on_columns(lar_solver * solver) {

View file

@ -376,6 +376,47 @@ public:
return *this;
}
bool sum_is_linear() const {
SASSERT(is_sum());
int degree = 0;
for (auto & e : children()) {
int d = e.get_degree();
if (d > 1)
return false;
if (d > degree)
degree = d;
}
return degree == 1;
}
int get_degree() const {
switch (type()) {
case expr_type::SUM: {
int degree = 0;
for (auto & e : children()) {
degree = std::max(degree, e.get_degree());
}
return degree;
}
case expr_type::MUL: {
int degree = 0;
for (auto & e : children()) {
degree += e.get_degree();
}
return degree;
}
case expr_type::VAR:
return 1;
case expr_type::SCALAR:
return 0;
case expr_type::UNDEF:
default:
UNREACHABLE();
break;
}
return 0;
}
};
/*

View file

@ -2730,7 +2730,10 @@ void test_term() {
vector<std::pair<mpq, var_index>> term_one;
term_one.push_back(std::make_pair(mpq(1), one));
solver.add_constraint(term_one, lconstraint_kind::EQ, mpq(0));
int ti = 0;
unsigned j = solver.add_term(term_one, ti++);
solver.add_var_bound(j, lconstraint_kind::LE, mpq(0));
solver.add_var_bound(j, lconstraint_kind::GE, mpq(0));
vector<std::pair<mpq, var_index>> term_ls;
term_ls.push_back(std::pair<mpq, var_index>(mpq(1), x));
@ -2742,13 +2745,16 @@ void test_term() {
ls.push_back(std::pair<mpq, var_index>(mpq(1), x));
ls.push_back(std::pair<mpq, var_index>(mpq(1), y));
ls.push_back(std::pair<mpq, var_index>(mpq(1), z));
solver.add_constraint(ls, lconstraint_kind::EQ, mpq(0));
j = solver.add_term(ls, ti++);
solver.add_var_bound(j , lconstraint_kind::LE, mpq(0));
solver.add_var_bound(j , lconstraint_kind::GE, mpq(0));
ls.clear();
ls.push_back(std::pair<mpq, var_index>(mpq(1), x));
solver.add_constraint(ls, lconstraint_kind::LT, mpq(0));
j = solver.add_term(ls, ti++);
solver.add_var_bound(j, lconstraint_kind::LT, mpq(0));
ls.push_back(std::pair<mpq, var_index>(mpq(2), y));
solver.add_constraint(ls, lconstraint_kind::GT, mpq(0));
j = solver.add_term(ls, ti++);
solver.add_var_bound(j, lconstraint_kind::GT, mpq(0));
auto status = solver.solve();
std::cout << lp_status_to_string(status) << std::endl;
std::unordered_map<var_index, mpq> model;
@ -2773,10 +2779,13 @@ void test_evidence_for_total_inf_simple(argument_parser & args_parser) {
ls.push_back(std::pair<mpq, var_index>(mpq(1), x));
ls.push_back(std::pair<mpq, var_index>(mpq(1), y));
solver.add_constraint(ls, GE, mpq(1));
unsigned j = solver.add_term(ls, 1);
solver.add_var_bound(j, GE, mpq(1));
ls.pop_back();
ls.push_back(std::pair<mpq, var_index>(- mpq(1), y));
solver.add_constraint(ls, lconstraint_kind::GE, mpq(0));
j = solver.add_term(ls, 2);
solver.add_var_bound(j, GE, mpq(0));
auto status = solver.solve();
std::cout << lp_status_to_string(status) << std::endl;
std::unordered_map<var_index, mpq> model;
@ -2813,21 +2822,21 @@ void test_bound_propagation_one_small_sample1() {
coeffs.clear();
coeffs.push_back(std::pair<mpq, var_index>(mpq(1), a));
coeffs.push_back(std::pair<mpq, var_index>(mpq(-1), b));
ls.add_constraint(coeffs, LE, zero_of_type<mpq>());
coeffs.clear();
coeffs.push_back(std::pair<mpq, var_index>(mpq(1), b));
coeffs.push_back(std::pair<mpq, var_index>(mpq(-1), c));
ls.add_constraint(coeffs, LE, zero_of_type<mpq>());
vector<implied_bound> ev;
ls.add_var_bound(a, LE, mpq(1));
ls.solve();
my_bound_propagator bp(ls);
ls.propagate_bounds_for_touched_rows(bp);
std::cout << " bound ev from test_bound_propagation_one_small_sample1" << std::endl;
for (auto & be : bp.m_ibounds) {
std::cout << "bound\n";
ls.print_implied_bound(be, std::cout);
}
// ls.add_constraint(coeffs, LE, zero_of_type<mpq>());
// coeffs.clear();
// coeffs.push_back(std::pair<mpq, var_index>(mpq(1), b));
// coeffs.push_back(std::pair<mpq, var_index>(mpq(-1), c));
// ls.add_constraint(coeffs, LE, zero_of_type<mpq>());
// vector<implied_bound> ev;
// ls.add_var_bound(a, LE, mpq(1));
// ls.solve();
// my_bound_propagator bp(ls);
// ls.propagate_bounds_for_touched_rows(bp);
// std::cout << " bound ev from test_bound_propagation_one_small_sample1" << std::endl;
// for (auto & be : bp.m_ibounds) {
// std::cout << "bound\n";
// ls.print_implied_bound(be, std::cout);
// } // todo: restore test
}
void test_bound_propagation_one_small_samples() {
@ -2870,12 +2879,13 @@ void test_bound_propagation_one_row() {
vector<std::pair<mpq, var_index>> c;
c.push_back(std::pair<mpq, var_index>(mpq(1), x0));
c.push_back(std::pair<mpq, var_index>(mpq(-1), x1));
ls.add_constraint(c, EQ, one_of_type<mpq>());
vector<implied_bound> ev;
ls.add_var_bound(x0, LE, mpq(1));
ls.solve();
my_bound_propagator bp(ls);
ls.propagate_bounds_for_touched_rows(bp);
// todo : restore test
// ls.add_constraint(c, EQ, one_of_type<mpq>());
// vector<implied_bound> ev;
// ls.add_var_bound(x0, LE, mpq(1));
// ls.solve();
// my_bound_propagator bp(ls);
// ls.propagate_bounds_for_touched_rows(bp);
}
void test_bound_propagation_one_row_with_bounded_vars() {
lar_solver ls;
@ -2884,14 +2894,15 @@ void test_bound_propagation_one_row_with_bounded_vars() {
vector<std::pair<mpq, var_index>> c;
c.push_back(std::pair<mpq, var_index>(mpq(1), x0));
c.push_back(std::pair<mpq, var_index>(mpq(-1), x1));
ls.add_constraint(c, EQ, one_of_type<mpq>());
vector<implied_bound> ev;
ls.add_var_bound(x0, GE, mpq(-3));
ls.add_var_bound(x0, LE, mpq(3));
ls.add_var_bound(x0, LE, mpq(1));
ls.solve();
my_bound_propagator bp(ls);
ls.propagate_bounds_for_touched_rows(bp);
// todo: restore test
// ls.add_constraint(c, EQ, one_of_type<mpq>());
// vector<implied_bound> ev;
// ls.add_var_bound(x0, GE, mpq(-3));
// ls.add_var_bound(x0, LE, mpq(3));
// ls.add_var_bound(x0, LE, mpq(1));
// ls.solve();
// my_bound_propagator bp(ls);
// ls.propagate_bounds_for_touched_rows(bp);
}
void test_bound_propagation_one_row_mixed() {
lar_solver ls;
@ -2900,12 +2911,13 @@ void test_bound_propagation_one_row_mixed() {
vector<std::pair<mpq, var_index>> c;
c.push_back(std::pair<mpq, var_index>(mpq(1), x0));
c.push_back(std::pair<mpq, var_index>(mpq(-1), x1));
ls.add_constraint(c, EQ, one_of_type<mpq>());
vector<implied_bound> ev;
ls.add_var_bound(x1, LE, mpq(1));
ls.solve();
my_bound_propagator bp(ls);
ls.propagate_bounds_for_touched_rows(bp);
// todo: restore test
// ls.add_constraint(c, EQ, one_of_type<mpq>());
// vector<implied_bound> ev;
// ls.add_var_bound(x1, LE, mpq(1));
// ls.solve();
// my_bound_propagator bp(ls);
// ls.propagate_bounds_for_touched_rows(bp);
}
void test_bound_propagation_two_rows() {
@ -2917,18 +2929,19 @@ void test_bound_propagation_two_rows() {
c.push_back(std::pair<mpq, var_index>(mpq(1), x));
c.push_back(std::pair<mpq, var_index>(mpq(2), y));
c.push_back(std::pair<mpq, var_index>(mpq(3), z));
ls.add_constraint(c, GE, one_of_type<mpq>());
c.clear();
c.push_back(std::pair<mpq, var_index>(mpq(3), x));
c.push_back(std::pair<mpq, var_index>(mpq(2), y));
c.push_back(std::pair<mpq, var_index>(mpq(y), z));
ls.add_constraint(c, GE, one_of_type<mpq>());
ls.add_var_bound(x, LE, mpq(2));
vector<implied_bound> ev;
ls.add_var_bound(y, LE, mpq(1));
ls.solve();
my_bound_propagator bp(ls);
ls.propagate_bounds_for_touched_rows(bp);
// todo: restore test
// ls.add_constraint(c, GE, one_of_type<mpq>());
// c.clear();
// c.push_back(std::pair<mpq, var_index>(mpq(3), x));
// c.push_back(std::pair<mpq, var_index>(mpq(2), y));
// c.push_back(std::pair<mpq, var_index>(mpq(y), z));
// ls.add_constraint(c, GE, one_of_type<mpq>());
// ls.add_var_bound(x, LE, mpq(2));
// vector<implied_bound> ev;
// ls.add_var_bound(y, LE, mpq(1));
// ls.solve();
// my_bound_propagator bp(ls);
// ls.propagate_bounds_for_touched_rows(bp);
}
void test_total_case_u() {
@ -2941,14 +2954,15 @@ void test_total_case_u() {
c.push_back(std::pair<mpq, var_index>(mpq(1), x));
c.push_back(std::pair<mpq, var_index>(mpq(2), y));
c.push_back(std::pair<mpq, var_index>(mpq(3), z));
ls.add_constraint(c, LE, one_of_type<mpq>());
ls.add_var_bound(x, GE, zero_of_type<mpq>());
ls.add_var_bound(y, GE, zero_of_type<mpq>());
vector<implied_bound> ev;
ls.add_var_bound(z, GE, zero_of_type<mpq>());
ls.solve();
my_bound_propagator bp(ls);
ls.propagate_bounds_for_touched_rows(bp);
// todo: restore test
// ls.add_constraint(c, LE, one_of_type<mpq>());
// ls.add_var_bound(x, GE, zero_of_type<mpq>());
// ls.add_var_bound(y, GE, zero_of_type<mpq>());
// vector<implied_bound> ev;
// ls.add_var_bound(z, GE, zero_of_type<mpq>());
// ls.solve();
// my_bound_propagator bp(ls);
// ls.propagate_bounds_for_touched_rows(bp);
}
bool contains_j_kind(unsigned j, lconstraint_kind kind, const mpq & rs, const vector<implied_bound> & ev) {
for (auto & e : ev) {
@ -2967,17 +2981,18 @@ void test_total_case_l(){
c.push_back(std::pair<mpq, var_index>(mpq(1), x));
c.push_back(std::pair<mpq, var_index>(mpq(2), y));
c.push_back(std::pair<mpq, var_index>(mpq(3), z));
ls.add_constraint(c, GE, one_of_type<mpq>());
ls.add_var_bound(x, LE, one_of_type<mpq>());
ls.add_var_bound(y, LE, one_of_type<mpq>());
ls.settings().presolve_with_double_solver_for_lar = true;
vector<implied_bound> ev;
ls.add_var_bound(z, LE, zero_of_type<mpq>());
ls.solve();
my_bound_propagator bp(ls);
ls.propagate_bounds_for_touched_rows(bp);
lp_assert(ev.size() == 4);
lp_assert(contains_j_kind(x, GE, - one_of_type<mpq>(), ev));
// todo: restore test
// ls.add_constraint(c, GE, one_of_type<mpq>());
// ls.add_var_bound(x, LE, one_of_type<mpq>());
// ls.add_var_bound(y, LE, one_of_type<mpq>());
// ls.settings().presolve_with_double_solver_for_lar = true;
// vector<implied_bound> ev;
// ls.add_var_bound(z, LE, zero_of_type<mpq>());
// ls.solve();
// my_bound_propagator bp(ls);
// ls.propagate_bounds_for_touched_rows(bp);
// lp_assert(ev.size() == 4);
// lp_assert(contains_j_kind(x, GE, - one_of_type<mpq>(), ev));
}
void test_bound_propagation() {
test_total_case_u();

View file

@ -386,17 +386,19 @@ namespace lp {
return ret;
}
void add_constraint_to_solver(lar_solver * solver, formula_constraint & fc) {
void add_constraint_to_solver(lar_solver * solver, formula_constraint & fc, unsigned i) {
vector<std::pair<mpq, var_index>> ls;
for (auto & it : fc.m_coeffs) {
ls.push_back(std::make_pair(it.first, solver->add_var(register_name(it.second), false)));
}
solver->add_constraint(ls, fc.m_kind, fc.m_right_side);
unsigned j = solver->add_term(ls, i);
solver->add_var_bound(j, fc.m_kind, fc.m_right_side);
}
void fill_lar_solver(lar_solver * solver) {
unsigned i = 0;
for (formula_constraint & fc : m_constraints)
add_constraint_to_solver(solver, fc);
add_constraint_to_solver(solver, fc, i++);
}