mirror of
https://github.com/Z3Prover/z3
synced 2025-08-16 16:27:11 +00:00
use dep_intervals inside of nla_intervals
Signed-off-by: Lev Nachmanson <levnach@hotmail.com>
This commit is contained in:
parent
ee255ef8b3
commit
22bec010ba
10 changed files with 260 additions and 667 deletions
|
@ -4,19 +4,7 @@
|
|||
#include "util/mpq.h"
|
||||
|
||||
namespace nla {
|
||||
|
||||
|
||||
typedef intervals::interval interv;
|
||||
typedef enum intervals::with_deps_t e_with_deps;
|
||||
|
||||
void intervals::set_interval_for_scalar(interv& a, const rational& v) {
|
||||
set_lower(a, v);
|
||||
set_upper(a, v);
|
||||
set_lower_is_open(a, false);
|
||||
set_lower_is_inf(a, false);
|
||||
set_upper_is_open(a, false);
|
||||
set_upper_is_inf(a, false);
|
||||
}
|
||||
typedef enum dep_intervals::with_deps_t e_with_deps;
|
||||
|
||||
const nex* intervals::get_inf_interval_child(const nex_sum& e) const {
|
||||
for (auto * c : e) {
|
||||
|
@ -68,7 +56,7 @@ const nex* intervals::get_zero_interval_child(const nex_mul& e) const {
|
|||
|
||||
std::ostream & intervals::print_dependencies(u_dependency* deps , std::ostream& out) const {
|
||||
svector<lp::constraint_index> expl;
|
||||
m_dep_manager.linearize(deps, expl);
|
||||
m_dep_intervals.linearize(deps, expl);
|
||||
{
|
||||
lp::explanation e(expl);
|
||||
if (!expl.empty()) {
|
||||
|
@ -84,15 +72,15 @@ std::ostream & intervals::print_dependencies(u_dependency* deps , std::ostream&
|
|||
// return true iff the interval of n is does not contain 0
|
||||
bool intervals::check_nex(const nex* n, u_dependency* initial_deps) {
|
||||
m_core->lp_settings().stats().m_cross_nested_forms++;
|
||||
auto i = interval_of_expr<without_deps>(n, 1);
|
||||
if (!separated_from_zero(i)) {
|
||||
reset();
|
||||
auto i = interval_of_expr<e_with_deps::without_deps>(n, 1);
|
||||
if (!m_dep_intervals.separated_from_zero(i)) {
|
||||
m_dep_intervals.reset();
|
||||
return false;
|
||||
}
|
||||
auto interv_wd = interval_of_expr<with_deps>(n, 1);
|
||||
auto interv_wd = interval_of_expr<e_with_deps::with_deps>(n, 1);
|
||||
TRACE("grobner", tout << "conflict: interv_wd = "; display(tout, interv_wd ) <<"expr = " << *n << "\n, initial deps\n"; print_dependencies(initial_deps, tout););
|
||||
check_interval_for_conflict_on_zero(interv_wd, initial_deps);
|
||||
reset(); // clean the memory allocated by the interval bound dependencies
|
||||
m_dep_intervals.check_interval_for_conflict_on_zero(interv_wd, initial_deps);
|
||||
m_dep_intervals.reset(); // clean the memory allocated by the interval bound dependencies
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -175,113 +163,59 @@ lpvar intervals::find_term_column(const lp::lar_term & norm_t, rational& a) cons
|
|||
return -1;
|
||||
}
|
||||
|
||||
void intervals::set_zero_interval_with_explanation(interval& i, const lp::explanation& exp) const {
|
||||
void intervals::set_zero_interval_with_explanation(interval& i, const lp::explanation& exp) {
|
||||
auto val = rational(0);
|
||||
m_config.set_lower(i, val);
|
||||
m_config.set_lower_is_open(i, false);
|
||||
m_config.set_lower_is_inf(i, false);
|
||||
m_config.set_upper(i, val);
|
||||
m_config.set_upper_is_open(i, false);
|
||||
m_config.set_upper_is_inf(i, false);
|
||||
m_dep_intervals.set_lower(i, val);
|
||||
m_dep_intervals.set_lower_is_open(i, false);
|
||||
m_dep_intervals.set_lower_is_inf(i, false);
|
||||
m_dep_intervals.set_upper(i, val);
|
||||
m_dep_intervals.set_upper_is_open(i, false);
|
||||
m_dep_intervals.set_upper_is_inf(i, false);
|
||||
i.m_lower_dep = i.m_upper_dep = mk_dep(exp);
|
||||
}
|
||||
|
||||
void intervals::set_zero_interval(interval& i) const {
|
||||
void intervals::set_zero_interval(interval& i) {
|
||||
auto val = rational(0);
|
||||
m_config.set_lower(i, val);
|
||||
m_config.set_lower_is_open(i, false);
|
||||
m_config.set_lower_is_inf(i, false);
|
||||
m_config.set_upper(i, val);
|
||||
m_config.set_upper_is_open(i, false);
|
||||
m_config.set_upper_is_inf(i, false);
|
||||
m_dep_intervals.set_lower(i, val);
|
||||
m_dep_intervals.set_lower_is_open(i, false);
|
||||
m_dep_intervals.set_lower_is_inf(i, false);
|
||||
m_dep_intervals.set_upper(i, val);
|
||||
m_dep_intervals.set_upper_is_open(i, false);
|
||||
m_dep_intervals.set_upper_is_inf(i, false);
|
||||
}
|
||||
|
||||
void intervals::set_zero_interval_deps_for_mult(interval& a) {
|
||||
a.m_lower_dep = m_dep_manager.mk_join(a.m_lower_dep, a.m_upper_dep);
|
||||
a.m_lower_dep = mk_join(a.m_lower_dep, a.m_upper_dep);
|
||||
a.m_upper_dep = a.m_lower_dep;
|
||||
}
|
||||
|
||||
bool intervals::separated_from_zero_on_lower(const interval& i) const {
|
||||
if (lower_is_inf(i))
|
||||
return false;
|
||||
if (unsynch_mpq_manager::is_neg(lower(i)))
|
||||
return false;
|
||||
if (unsynch_mpq_manager::is_zero(lower(i)) && !m_config.lower_is_open(i))
|
||||
return false;
|
||||
return true;
|
||||
u_dependency *intervals::mk_dep(lp::constraint_index ci) {
|
||||
return m_dep_intervals.mk_leaf(ci);
|
||||
}
|
||||
|
||||
bool intervals::separated_from_zero_on_upper(const interval& i) const {
|
||||
if (upper_is_inf(i))
|
||||
return false;
|
||||
if (unsynch_mpq_manager::is_pos(upper(i)))
|
||||
return false;
|
||||
if (unsynch_mpq_manager::is_zero(upper(i)) && !m_config.upper_is_open(i))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool intervals::check_interval_for_conflict_on_zero(const interval & i, u_dependency* dep) {
|
||||
return check_interval_for_conflict_on_zero_lower(i, dep) || check_interval_for_conflict_on_zero_upper(i, dep);
|
||||
}
|
||||
|
||||
bool intervals::check_interval_for_conflict_on_zero_upper(
|
||||
const interval & i,
|
||||
u_dependency* dep) {
|
||||
if (!separated_from_zero_on_upper(i))
|
||||
return false;
|
||||
|
||||
TRACE("grobner", display(tout, i););
|
||||
m_core->add_empty_lemma();
|
||||
svector<lp::constraint_index> expl;
|
||||
dep = m_dep_manager.mk_join(dep, i.m_upper_dep);
|
||||
m_dep_manager.linearize(dep, expl);
|
||||
m_core->current_expl().add_expl(expl);
|
||||
TRACE("nla_solver", m_core->print_lemma(tout););
|
||||
return true;
|
||||
}
|
||||
|
||||
bool intervals::check_interval_for_conflict_on_zero_lower(const interval & i, u_dependency* dep) {
|
||||
if (!separated_from_zero_on_lower(i)) {
|
||||
return false;
|
||||
}
|
||||
TRACE("grobner", display(tout, i););
|
||||
m_core->add_empty_lemma();
|
||||
svector<lp::constraint_index> expl;
|
||||
dep = m_dep_manager.mk_join(dep, i.m_lower_dep);
|
||||
m_dep_manager.linearize(dep, expl);
|
||||
m_core->current_expl().add_expl(expl);
|
||||
TRACE("nla_solver", m_core->print_lemma(tout););
|
||||
return true;
|
||||
}
|
||||
|
||||
u_dependency *intervals::mk_dep(lp::constraint_index ci) const {
|
||||
return m_dep_manager.mk_leaf(ci);
|
||||
}
|
||||
|
||||
u_dependency *intervals::mk_dep(const lp::explanation& expl) const {
|
||||
u_dependency *intervals::mk_dep(const lp::explanation& expl) {
|
||||
u_dependency * r = nullptr;
|
||||
for (auto p : expl) {
|
||||
if (r == nullptr) {
|
||||
r = m_dep_manager.mk_leaf(p.second);
|
||||
r = m_dep_intervals.mk_leaf(p.second);
|
||||
} else {
|
||||
r = m_dep_manager.mk_join(r, m_dep_manager.mk_leaf(p.second));
|
||||
r = m_dep_intervals.mk_join(r, m_dep_intervals.mk_leaf(p.second));
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
std::ostream& intervals::display(std::ostream& out, const interval& i) const {
|
||||
if (m_imanager.lower_is_inf(i)) {
|
||||
if (m_dep_intervals.lower_is_inf(i)) {
|
||||
out << "(-oo";
|
||||
} else {
|
||||
out << (m_imanager.lower_is_open(i)? "(":"[") << rational(m_imanager.lower(i));
|
||||
out << (m_dep_intervals.lower_is_open(i)? "(":"[") << rational(m_dep_intervals.lower(i));
|
||||
}
|
||||
out << ",";
|
||||
if (m_imanager.upper_is_inf(i)) {
|
||||
if (m_dep_intervals.upper_is_inf(i)) {
|
||||
out << "oo)";
|
||||
} else {
|
||||
out << rational(m_imanager.upper(i)) << (m_imanager.upper_is_open(i)? ")":"]");
|
||||
out << rational(m_dep_intervals.upper(i)) << (m_dep_intervals.upper_is_open(i)? ")":"]");
|
||||
}
|
||||
svector<lp::constraint_index> expl;
|
||||
if (i.m_lower_dep) {
|
||||
|
@ -296,37 +230,37 @@ std::ostream& intervals::display(std::ostream& out, const interval& i) const {
|
|||
}
|
||||
|
||||
template <e_with_deps wd>
|
||||
void intervals::set_var_interval(lpvar v, interval& b) const {
|
||||
void intervals::set_var_interval(lpvar v, interval& b) {
|
||||
TRACE("nla_intervals_details", m_core->print_var(v, tout) << "\n";);
|
||||
lp::constraint_index ci;
|
||||
rational val;
|
||||
bool is_strict;
|
||||
if (ls().has_lower_bound(v, ci, val, is_strict)) {
|
||||
m_config.set_lower(b, val);
|
||||
m_config.set_lower_is_open(b, is_strict);
|
||||
m_config.set_lower_is_inf(b, false);
|
||||
if (wd == with_deps) b.m_lower_dep = mk_dep(ci);
|
||||
m_dep_intervals.set_lower(b, val);
|
||||
m_dep_intervals.set_lower_is_open(b, is_strict);
|
||||
m_dep_intervals.set_lower_is_inf(b, false);
|
||||
if (wd == e_with_deps::with_deps) b.m_lower_dep = mk_dep(ci);
|
||||
}
|
||||
else {
|
||||
m_config.set_lower_is_open(b, true);
|
||||
m_config.set_lower_is_inf(b, true);
|
||||
if (wd == with_deps) b.m_lower_dep = nullptr;
|
||||
m_dep_intervals.set_lower_is_open(b, true);
|
||||
m_dep_intervals.set_lower_is_inf(b, true);
|
||||
if (wd == e_with_deps::with_deps) b.m_lower_dep = nullptr;
|
||||
}
|
||||
if (ls().has_upper_bound(v, ci, val, is_strict)) {
|
||||
m_config.set_upper(b, val);
|
||||
m_config.set_upper_is_open(b, is_strict);
|
||||
m_config.set_upper_is_inf(b, false);
|
||||
if (wd == with_deps) b.m_upper_dep = mk_dep(ci);
|
||||
m_dep_intervals.set_upper(b, val);
|
||||
m_dep_intervals.set_upper_is_open(b, is_strict);
|
||||
m_dep_intervals.set_upper_is_inf(b, false);
|
||||
if (wd == e_with_deps::with_deps) b.m_upper_dep = mk_dep(ci);
|
||||
}
|
||||
else {
|
||||
m_config.set_upper_is_open(b, true);
|
||||
m_config.set_upper_is_inf(b, true);
|
||||
if (wd == with_deps) b.m_upper_dep = nullptr;
|
||||
m_dep_intervals.set_upper_is_open(b, true);
|
||||
m_dep_intervals.set_upper_is_inf(b, true);
|
||||
if (wd == e_with_deps::with_deps) b.m_upper_dep = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template <e_with_deps wd>
|
||||
bool intervals::interval_from_term(const nex& e, interv& i) const {
|
||||
bool intervals::interval_from_term(const nex& e, interval& i) {
|
||||
rational a, b;
|
||||
lp::lar_term norm_t = expression_to_normalized_term(&e.to_sum(), a, b);
|
||||
lp::explanation exp;
|
||||
|
@ -339,11 +273,11 @@ bool intervals::interval_from_term(const nex& e, interv& i) const {
|
|||
if (j + 1 == 0)
|
||||
return false;
|
||||
|
||||
set_var_interval<without_deps>(j, i);
|
||||
interv bi;
|
||||
mul<wd>(a, i, bi);
|
||||
add(b, bi);
|
||||
set<wd>(i, bi);
|
||||
set_var_interval<e_with_deps::without_deps>(j, i);
|
||||
interval bi;
|
||||
m_dep_intervals.mul<wd>(a, i, bi);
|
||||
m_dep_intervals.add(b, bi);
|
||||
m_dep_intervals.set<wd>(i, bi);
|
||||
|
||||
TRACE("nla_intervals",
|
||||
m_core->m_lar_solver.print_column_info(j, tout) << "\n";
|
||||
|
@ -353,27 +287,27 @@ bool intervals::interval_from_term(const nex& e, interv& i) const {
|
|||
}
|
||||
|
||||
template <e_with_deps wd>
|
||||
interv intervals::interval_of_sum_no_term(const nex_sum& e) {
|
||||
intervals::interval intervals::interval_of_sum_no_term(const nex_sum& e) {
|
||||
const nex* inf_e = get_inf_interval_child(e);
|
||||
if (inf_e) {
|
||||
return interv();
|
||||
return interval();
|
||||
}
|
||||
interv a = interval_of_expr<wd>(e[0], 1);
|
||||
interval a = interval_of_expr<wd>(e[0], 1);
|
||||
for (unsigned k = 1; k < e.size(); k++) {
|
||||
TRACE("nla_intervals_details_sum", tout << "e[" << k << "]= " << *e[k] << "\n";);
|
||||
interv b = interval_of_expr<wd>(e[k], 1);
|
||||
interv c;
|
||||
interval b = interval_of_expr<wd>(e[k], 1);
|
||||
interval c;
|
||||
|
||||
TRACE("nla_intervals_details_sum", tout << "a = "; display(tout, a) << "\nb = "; display(tout, b) << "\n";);
|
||||
if (wd == with_deps) {
|
||||
if (wd == e_with_deps::with_deps) {
|
||||
interval_deps_combine_rule combine_rule;
|
||||
add(a, b, c, combine_rule);
|
||||
combine_deps(a, b, combine_rule, c);
|
||||
m_dep_intervals.add(a, b, c, combine_rule);
|
||||
m_dep_intervals.combine_deps(a, b, combine_rule, c);
|
||||
}
|
||||
else {
|
||||
add(a, b, c);
|
||||
m_dep_intervals.add(a, b, c);
|
||||
}
|
||||
set<wd>(a, c);
|
||||
m_dep_intervals.set<wd>(a, c);
|
||||
TRACE("nla_intervals_details_sum", tout << *e[k] << ", ";
|
||||
display(tout, a); tout << "\n";);
|
||||
}
|
||||
|
@ -383,46 +317,16 @@ interv intervals::interval_of_sum_no_term(const nex_sum& e) {
|
|||
}
|
||||
|
||||
template <e_with_deps wd>
|
||||
void intervals::update_upper_for_intersection(const interval& a, const interval& b, interval& i) const {
|
||||
if (a.m_upper_inf) {
|
||||
if (b.m_upper_inf)
|
||||
return;
|
||||
copy_upper_bound<wd>(b, i);
|
||||
return;
|
||||
}
|
||||
if (b.m_upper_inf) {
|
||||
SASSERT(!a.m_upper_inf);
|
||||
copy_upper_bound<wd>(a, i);
|
||||
return;
|
||||
}
|
||||
if (m_num_manager.gt(a.m_upper, b.m_upper)) {
|
||||
copy_upper_bound<wd>(b, i);
|
||||
return;
|
||||
}
|
||||
if (m_num_manager.lt(a.m_upper, b.m_upper)) {
|
||||
copy_upper_bound<wd>(a, i);
|
||||
return;
|
||||
}
|
||||
SASSERT(m_num_manager.eq(a.m_upper, b.m_upper));
|
||||
if (a.m_upper_open) { // we might consider to look at b.m_upper_open too here
|
||||
copy_upper_bound<wd>(a, i);
|
||||
return;
|
||||
}
|
||||
|
||||
copy_upper_bound<wd>(b, i);
|
||||
}
|
||||
|
||||
template <e_with_deps wd>
|
||||
interv intervals::interval_of_sum(const nex_sum& e) {
|
||||
intervals::interval intervals::interval_of_sum(const nex_sum& e) {
|
||||
TRACE("nla_intervals_details", tout << "e=" << e << "\n";);
|
||||
interv i_e = interval_of_sum_no_term<wd>(e);
|
||||
interval i_e = interval_of_sum_no_term<wd>(e);
|
||||
if (e.is_a_linear_term()) {
|
||||
SASSERT(e.is_sum() && e.size() > 1);
|
||||
interv i_from_term;
|
||||
interval i_from_term;
|
||||
if (interval_from_term<wd>(e, i_from_term)) {
|
||||
interv r = intersect<wd>(i_e, i_from_term);
|
||||
interval r = m_dep_intervals.intersect<wd>(i_e, i_from_term);
|
||||
TRACE("nla_intervals_details", tout << "intersection="; display(tout, r) << "\n";);
|
||||
if (is_empty(r)) {
|
||||
if (m_dep_intervals.is_empty(r)) {
|
||||
SASSERT(false); // not implemented
|
||||
}
|
||||
return r;
|
||||
|
@ -433,35 +337,35 @@ interv intervals::interval_of_sum(const nex_sum& e) {
|
|||
}
|
||||
|
||||
template <e_with_deps wd>
|
||||
interv intervals::interval_of_mul(const nex_mul& e) {
|
||||
intervals::interval intervals::interval_of_mul(const nex_mul& e) {
|
||||
TRACE("nla_intervals_details", tout << "e = " << e << "\n";);
|
||||
const nex* zero_interval_child = get_zero_interval_child(e);
|
||||
if (zero_interval_child) {
|
||||
interv a = interval_of_expr<wd>(zero_interval_child, 1);
|
||||
if(wd == with_deps)
|
||||
interval a = interval_of_expr<wd>(zero_interval_child, 1);
|
||||
if(wd == e_with_deps::with_deps)
|
||||
set_zero_interval_deps_for_mult(a);
|
||||
TRACE("nla_intervals_details", tout << "zero_interval_child = " << *zero_interval_child << std::endl << "a = "; display(tout, a); );
|
||||
return a;
|
||||
}
|
||||
|
||||
interv a;
|
||||
set_interval_for_scalar(a, e.coeff());
|
||||
interval a;
|
||||
m_dep_intervals.set_interval_for_scalar(a, e.coeff());
|
||||
TRACE("nla_intervals_details", tout << "a = "; display(tout, a); );
|
||||
for (const auto& ep : e) {
|
||||
interv b = interval_of_expr<wd>(ep.e(), ep.pow());
|
||||
interval b = interval_of_expr<wd>(ep.e(), ep.pow());
|
||||
TRACE("nla_intervals_details", tout << "ep = " << ep << ", "; display(tout, b); );
|
||||
interv c;
|
||||
if (wd == with_deps) {
|
||||
interval c;
|
||||
if (wd == e_with_deps::with_deps) {
|
||||
interval_deps_combine_rule comb_rule;
|
||||
mul_two_intervals(a, b, c, comb_rule);
|
||||
m_dep_intervals.mul(a, b, c, comb_rule);
|
||||
TRACE("nla_intervals_details", tout << "c before combine_deps() "; display(tout, c););
|
||||
combine_deps(a, b, comb_rule, c);
|
||||
m_dep_intervals.combine_deps(a, b, comb_rule, c);
|
||||
} else {
|
||||
mul_two_intervals(a, b, c);
|
||||
m_dep_intervals.mul(a, b, c);
|
||||
}
|
||||
TRACE("nla_intervals_details", tout << "a "; display(tout, a););
|
||||
TRACE("nla_intervals_details", tout << "c "; display(tout, c););
|
||||
set<wd>(a, c);
|
||||
m_dep_intervals.set<wd>(a, c);
|
||||
TRACE("nla_intervals_details", tout << "part mult "; display(tout, a););
|
||||
}
|
||||
TRACE("nla_intervals_details", tout << "e=" << e << "\n";
|
||||
|
@ -470,31 +374,31 @@ interv intervals::interval_of_mul(const nex_mul& e) {
|
|||
}
|
||||
|
||||
template <e_with_deps wd>
|
||||
interv intervals::interval_of_expr(const nex* e, unsigned p) {
|
||||
interv a;
|
||||
intervals::interval intervals::interval_of_expr(const nex* e, unsigned p) {
|
||||
interval a;
|
||||
switch (e->type()) {
|
||||
case expr_type::SCALAR:
|
||||
set_interval_for_scalar(a, to_scalar(e)->value());
|
||||
m_dep_intervals.set_interval_for_scalar(a, to_scalar(e)->value());
|
||||
if (p != 1) {
|
||||
return power<wd>(a, p);
|
||||
return m_dep_intervals.power<wd>(a, p);
|
||||
}
|
||||
return a;
|
||||
case expr_type::SUM: {
|
||||
interv b = interval_of_sum<wd>(e->to_sum());
|
||||
interval b = interval_of_sum<wd>(e->to_sum());
|
||||
if (p != 1)
|
||||
return power<wd>(b, p);
|
||||
return m_dep_intervals.power<wd>(b, p);
|
||||
return b;
|
||||
}
|
||||
case expr_type::MUL: {
|
||||
interv b = interval_of_mul<wd>(e->to_mul());
|
||||
interval b = interval_of_mul<wd>(e->to_mul());
|
||||
if (p != 1)
|
||||
return power<wd>(b, p);
|
||||
return m_dep_intervals.power<wd>(b, p);
|
||||
return b;
|
||||
}
|
||||
case expr_type::VAR:
|
||||
set_var_interval<wd>(e->to_var().var(), a);
|
||||
if (p != 1)
|
||||
return power<wd>(a, p);;
|
||||
return m_dep_intervals.power<wd>(a, p);;
|
||||
return a;
|
||||
default:
|
||||
TRACE("nla_intervals_details", tout << e->type() << "\n";);
|
||||
|
@ -511,5 +415,3 @@ const lp::lar_solver& intervals::ls() const { return m_core->m_lar_solver; }
|
|||
|
||||
} // end of nla namespace
|
||||
|
||||
// instantiate the template
|
||||
template class interval_manager<nla::intervals::im_config>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue