3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-06 17:44:08 +00:00
z3/src/math/lp/monomial_bounds.cpp
Nikolaj Bjorner 33042268b5 bounds propagation functionality
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-05-13 13:36:22 -07:00

140 lines
5.1 KiB
C++

/*++
Copyright (c) 2020 Microsoft Corporation
Author:
Nikolaj Bjorner (nbjorner)
Lev Nachmanson (levnach)
--*/
#pragma once
#include "math/lp/monomial_bounds.h"
#include "math/lp/nla_core.h"
#include "math/lp/nla_intervals.h"
namespace nla {
monomial_bounds::monomial_bounds(core* c):common(c) {}
bool monomial_bounds::operator()() {
bool propagated = false;
for (lpvar v : c().m_to_refine) {
monic const& m = c().emons()[v];
if (propagate(m))
propagated = true;
}
return propagated;
}
void monomial_bounds::compute_product(unsigned start, monic const& m, scoped_dep_interval& product) {
auto & intervals = c().m_intervals;
auto & dep_intervals = intervals.get_dep_intervals();
scoped_dep_interval vi(dep_intervals);
for (unsigned i = start; i < m.size(); ) {
lpvar v = m.vars()[i];
unsigned power = 1;
var2interval(v, vi);
++i;
for (; i < m.size() && m.vars()[i] == v; ++i) {
++power;
}
dep_intervals.power<dep_intervals::with_deps>(vi, power, vi);
dep_intervals.mul<dep_intervals::with_deps>(product, vi, product);
}
}
bool monomial_bounds::propagate_value(dep_interval& range, lpvar v) {
auto & intervals = c().m_intervals;
auto & dep_intervals = intervals.get_dep_intervals();
if (dep_intervals.is_below(range, c().val(v))) {
lp::explanation ex;
dep_intervals.get_upper_dep(range, ex);
new_lemma lemma(c(), "propagate up - upper bound of range is below value");
lemma &= ex;
auto const& upper = dep_intervals.upper(range);
auto cmp = dep_intervals.upper_is_open(range) ? llc::LT : llc::LE;
lemma |= ineq(v, cmp, upper);
return true;
}
else if (dep_intervals.is_above(range, c().val(v))) {
lp::explanation ex;
dep_intervals.get_lower_dep(range, ex);
new_lemma lemma(c(), "propagate up - lower bound of range is above value");
lemma &= ex;
auto const& lower = dep_intervals.upper(range);
auto cmp = dep_intervals.lower_is_open(range) ? llc::GT : llc::GE;
lemma |= ineq(v, cmp, lower);
return true;
}
else {
return false;
}
}
void monomial_bounds::var2interval(lpvar v, scoped_dep_interval& i) {
auto & intervals = c().m_intervals;
auto & dep_intervals = intervals.get_dep_intervals();
lp::constraint_index ci;
rational bound;
bool is_strict;
if (c().has_lower_bound(v, ci, bound, is_strict)) {
dep_intervals.set_lower_is_open(i, is_strict);
dep_intervals.set_lower(i, bound);
dep_intervals.set_lower_dep(i, dep_intervals.mk_leaf(ci));
}
else {
dep_intervals.set_lower_is_inf(i, true);
}
if (c().has_upper_bound(v, ci, bound, is_strict)) {
dep_intervals.set_upper_is_open(i, is_strict);
dep_intervals.set_upper(i, bound);
dep_intervals.set_upper_dep(i, dep_intervals.mk_leaf(ci));
}
else {
dep_intervals.set_upper_is_inf(i, true);
}
}
bool monomial_bounds::propagate(monic const& m) {
auto & intervals = c().m_intervals;
auto & dep_intervals = intervals.get_dep_intervals();
scoped_dep_interval product(dep_intervals);
scoped_dep_interval vi(dep_intervals), mi(dep_intervals);
scoped_dep_interval other_product(dep_intervals);
var2interval(m.var(), mi);
if (dep_intervals.lower_is_inf(mi) && dep_intervals.upper_is_inf(mi))
return false;
dep_intervals.set_value(product, rational::one());
for (unsigned i = 0; i < m.size(); ) {
lpvar v = m.vars()[i];
++i;
unsigned power = 1;
for (; i < m.size() && v == m.vars()[i]; ++i)
++power;
var2interval(v, vi);
dep_intervals.power<dep_intervals::with_deps>(vi, power, vi);
if (power == 1) {
dep_intervals.set<dep_intervals::with_deps>(other_product, product);
compute_product(i, m, other_product);
if (propagate_down(m, mi, v, other_product))
return true;
}
dep_intervals.mul<dep_intervals::with_deps>(product, vi, product);
}
return propagate_value(product, m.var());
}
bool monomial_bounds::propagate_down(monic const& m, dep_interval& mi, lpvar v, dep_interval& product) {
auto & intervals = c().m_intervals;
auto & dep_intervals = intervals.get_dep_intervals();
if (!dep_intervals.separated_from_zero(product))
return false;
scoped_dep_interval range(dep_intervals);
dep_intervals.set<dep_intervals::with_deps>(range, mi);
dep_intervals.div<dep_intervals::with_deps>(range, product, range);
return propagate_value(range, v);
}
}