3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-08 10:25:18 +00:00

patching merge (#6780)

* patching merge

* fix the format and some warnings

Signed-off-by: Lev Nachmanson <levnach@hotmail.com>

* a fix in the delta calculation

* test patching

* try a new version of get_patching_deltas

Signed-off-by: Lev Nachmanson <levnach@hotmail.com>

* remove dead code from lp_tst and try optimizing patching

* add comments, replace VERIFY with lp_assert

Signed-off-by: Lev Nachmanson <levnach@hotmail.com>

* cleanup

---------

Signed-off-by: Lev Nachmanson <levnach@hotmail.com>
This commit is contained in:
Lev Nachmanson 2023-06-27 17:53:27 -07:00 committed by GitHub
parent b2c035ea3f
commit 30a2ced9aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1989 additions and 1800 deletions

View file

@ -0,0 +1,4 @@
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
NamespaceIndentation: All

View file

@ -48,6 +48,15 @@ public:
SASSERT(m_vector.empty());
m_set.insert(j);
}
void remove(constraint_index j) {
m_set.remove(j);
unsigned i = 0;
for (auto& p : m_vector)
if (p.first != j)
m_vector[i++] = p;
m_vector.shrink(i);
}
void add_expl(const explanation& e) {
if (e.m_vector.empty()) {

View file

@ -2,8 +2,7 @@
Copyright (c) 2017 Microsoft Corporation
Author: Lev Nachmanson
*/
#include <utility>
// clang-format off
#include "math/lp/int_solver.h"
#include "math/lp/lar_solver.h"
#include "math/lp/lp_utils.h"
@ -14,56 +13,216 @@
namespace lp {
int_solver::patcher::patcher(int_solver& lia):
lia(lia),
lra(lia.lra),
lrac(lia.lrac),
m_num_nbasic_patches(0),
m_patch_cost(0),
m_next_patch(0),
m_delay(0)
{}
int_solver::patcher::patcher(int_solver& lia):
lia(lia),
lra(lia.lra),
lrac(lia.lrac)
{}
void int_solver::patcher::remove_fixed_vars_from_base() {
unsigned num = lra.A_r().column_count();
for (unsigned v = 0; v < num; v++) {
if (!lia.is_base(v) || !lia.is_fixed(v))
continue;
auto const & r = lra.basic2row(v);
for (auto const& c : r) {
if (c.var() != v && !lia.is_fixed(c.var())) {
lra.pivot(c.var(), v);
break;
}
}
}
}
bool int_solver::patcher::should_apply() {
#if 1
return true;
#else
if (m_delay == 0) {
unsigned int_solver::patcher::count_non_int() {
unsigned non_int = 0;
for (auto j : lra.r_basis())
if (lra.column_is_int(j) && !lra.column_value_is_int(j))
++non_int;
return non_int;
}
lia_move int_solver::patcher::patch_basic_columns() {
remove_fixed_vars_from_base();
lia.settings().stats().m_patches++;
lp_assert(lia.is_feasible());
// unsigned non_int_before, non_int_after;
// non_int_before = count_non_int();
// unsigned num = lra.A_r().column_count();
for (unsigned j : lra.r_basis())
if (!lra.get_value(j).is_int())
patch_basic_column(j);
// non_int_after = count_non_int();
// verbose_stream() << non_int_before << " -> " << non_int_after << "\n";
if (!lia.has_inf_int()) {
lia.settings().stats().m_patches_success++;
return lia_move::sat;
}
return lia_move::undef;
}
// clang-format on
/**
* \brief find integral and minimal, in the absolute values, deltas such that x - alpha*delta is integral too.
*/
bool get_patching_deltas(const rational& x, const rational& alpha,
rational& delta_0, rational& delta_1) {
auto a1 = numerator(alpha);
auto a2 = denominator(alpha);
auto x1 = numerator(x);
auto x2 = denominator(x);
if (!divides(x2, a2))
return false;
// delta has to be integral.
// We need to find delta such that x1/x2 + (a1/a2)*delta is integral (we are going to flip the delta sign later).
// Then a2*x1/x2 + a1*delta is integral, but x2 and x1 are coprime:
// that means that t = a2/x2 is
// integral. We established that a2 = x2*t Then x1 + a1*delta*(x2/a2) = x1
// + a1*(delta/t) is integral. Taking into account that t and a1 are
// coprime we have delta = t*k, where k is an integer.
rational t = a2 / x2;
// std::cout << "t = " << t << std::endl;
// Now we have x1/x2 + (a1/x2)*k is integral, or (x1 + a1*k)/x2 is integral.
// It is equivalent to x1 + a1*k = x2*m, where m is an integer
// We know that a2 and a1 are coprime, and x2 divides a2, so x2 and a1 are
// coprime. We can find u and v such that u*a1 + v*x2 = 1.
rational u, v;
gcd(a1, x2, u, v);
lp_assert(gcd(a1, x2, u, v).is_one());
// std::cout << "u = " << u << ", v = " << v << std::endl;
// std::cout << "x= " << (x1 / x2) << std::endl;
// std::cout << "x + (a1 / a2) * (-u * t) * x1 = "
// << x + (a1 / a2) * (-u * t) * x1 << std::endl;
lp_assert((x + (a1 / a2) * (-u * t) * x1).is_int());
// 1 = (u- l*x2 ) * a1 + (v + l*a1)*x2, for every integer l.
rational l_low, l_high;
auto sign = u.is_pos() ? 1 : -1;
auto p = sign * floor(abs(u / x2));
auto p_ = p + sign;
lp_assert(p * x2 <= u && u <= p_ * x2 || p * x2 >= u && u >= p_ * x2);
// std::cout << "u = " << u << ", v = " << v << std::endl;
// std::cout << "p = " << p << ", p_ = " << p_ << std::endl;
// std::cout << "u - p*x2 = " << u - p * x2 << ", u - p_*x2 = " << u - p_ * x2
// << std::endl;
mpq d_0 = (u - p * x2) * t * x1;
mpq d_1 = (u - p_ * x2) * t * x1;
if (abs(d_0) < abs(d_1)) {
delta_0 = d_0;
delta_1 = d_1;
} else {
delta_0 = d_1;
delta_1 = d_0;
}
return true;
// std::cout << "delta_0 = " << delta_0 << std::endl;
// std::cout << "delta_1 = " << delta_1 << std::endl;
}
// clang-format off
/**
* \brief try to patch the basic column v
*/
bool int_solver::patcher::patch_basic_column_on_row_cell(unsigned v, row_cell<mpq> const& c) {
if (v == c.var())
return false;
if (!lra.column_is_int(c.var())) // could use real to patch integer
return false;
if (c.coeff().is_int())
return false;
mpq a = fractional_part(c.coeff());
mpq r = fractional_part(lra.get_value(v));
lp_assert(0 < r && r < 1);
lp_assert(0 < a && a < 1);
mpq delta_0, delta_1;
if (!get_patching_deltas(r, a, delta_0, delta_1))
return false;
if (try_patch_column(v, c.var(), delta_0))
return true;
if (try_patch_column(v, c.var(), delta_1))
return true;
return false;
}
bool int_solver::patcher::try_patch_column(unsigned v, unsigned j, mpq const& delta) {
const auto & A = lra.A_r();
if (delta < 0) {
if (lia.has_lower(j) && lia.get_value(j) + impq(delta) < lra.get_lower_bound(j))
return false;
}
else {
if (lia.has_upper(j) && lia.get_value(j) + impq(delta) > lra.get_upper_bound(j))
return false;
}
for (auto const& c : A.column(j)) {
unsigned row_index = c.var();
unsigned i = lrac.m_r_basis[row_index];
auto old_val = lia.get_value(i);
auto new_val = old_val - impq(c.coeff()*delta);
if (lia.has_lower(i) && new_val < lra.get_lower_bound(i))
return false;
if (lia.has_upper(i) && new_val > lra.get_upper_bound(i))
return false;
if (old_val.is_int() && !new_val.is_int()){
return false; // do not waste resources on this case
}
lp_assert(i != v || new_val.is_int())
}
lra.set_value_for_nbasic_column(j, lia.get_value(j) + impq(delta));
return true;
}
--m_delay;
return false;
#endif
}
void int_solver::patcher::patch_basic_column(unsigned v) {
SASSERT(!lia.is_fixed(v));
for (auto const& c : lra.basic2row(v))
if (patch_basic_column_on_row_cell(v, c))
return;
}
lia_move int_solver::patcher::operator()() {
return patch_nbasic_columns();
}
lia_move int_solver::patcher::patch_nbasic_columns() {
remove_fixed_vars_from_base();
lia.settings().stats().m_patches++;
lp_assert(lia.is_feasible());
m_patch_success = 0;
m_patch_fail = 0;
m_num_ones = 0;
m_num_divides = 0;
//unsigned non_int_before = count_non_int();
lia_move int_solver::patcher::patch_nbasic_columns() {
lia.settings().stats().m_patches++;
lp_assert(lia.is_feasible());
m_num_nbasic_patches = 0;
m_patch_cost = 0;
for (unsigned j : lia.lrac.m_r_nbasis) {
patch_nbasic_column(j);
unsigned num = lra.A_r().column_count();
for (unsigned v = 0; v < num; v++) {
if (lia.is_base(v))
continue;
patch_nbasic_column(v);
}
unsigned num_fixed = 0;
for (unsigned v = 0; v < num; v++)
if (lia.is_fixed(v))
++num_fixed;
lp_assert(lia.is_feasible());
//verbose_stream() << "patch " << m_patch_success << " fails " << m_patch_fail << " ones " << m_num_ones << " divides " << m_num_divides << " num fixed " << num_fixed << "\n";
//lra.display(verbose_stream());
//exit(0);
//unsigned non_int_after = count_non_int();
// verbose_stream() << non_int_before << " -> " << non_int_after << "\n";
if (!lia.has_inf_int()) {
lia.settings().stats().m_patches_success++;
return lia_move::sat;
}
return lia_move::undef;
}
lp_assert(lia.is_feasible());
if (!lia.has_inf_int()) {
lia.settings().stats().m_patches_success++;
m_delay = 0;
m_next_patch = 0;
return lia_move::sat;
}
if (m_patch_cost > 0 && m_num_nbasic_patches * 10 < m_patch_cost) {
m_delay = std::min(20u, m_next_patch++);
}
else {
m_delay = 0;
m_next_patch = 0;
}
return lia_move::undef;
}
void int_solver::patcher::patch_nbasic_column(unsigned j) {
impq & val = lrac.m_r_x[j];
@ -71,17 +230,48 @@ void int_solver::patcher::patch_nbasic_column(unsigned j) {
impq l, u;
mpq m;
bool has_free = lia.get_freedom_interval_for_column(j, inf_l, l, inf_u, u, m);
m_patch_cost += lra.A_r().number_of_non_zeroes_in_column(j);
if (!has_free) {
if (!has_free)
return;
}
bool m_is_one = m.is_one();
bool val_is_int = lia.value_is_int(j);
#if 0
const auto & A = lra.A_r();
#endif
// check whether value of j is already a multiple of m.
if (val_is_int && (m_is_one || (val.x / m).is_int())) {
if (m_is_one)
++m_num_ones;
else
++m_num_divides;
#if 0
for (auto c : A.column(j)) {
unsigned row_index = c.var();
unsigned i = lrac.m_r_basis[row_index];
if (!lia.get_value(i).is_int() ||
(lia.has_lower(i) && lia.get_value(i) < lra.get_lower_bound(i)) ||
(lia.has_upper(i) && lia.get_value(i) > lra.get_upper_bound(i))) {
verbose_stream() << "skip " << j << " " << m << ": ";
lia.display_row(verbose_stream(), A.m_rows[row_index]);
verbose_stream() << "\n";
}
}
#endif
return;
}
#if 0
if (!m_is_one) {
// lia.display_column(verbose_stream(), j);
for (auto c : A.column(j)) {
continue;
unsigned row_index = c.var();
lia.display_row(verbose_stream(), A.m_rows[row_index]);
verbose_stream() << "\n";
}
}
#endif
TRACE("patch_int",
tout << "TARGET j" << j << " -> [";
if (inf_l) tout << "-oo"; else tout << l;
@ -89,9 +279,33 @@ void int_solver::patcher::patch_nbasic_column(unsigned j) {
if (inf_u) tout << "oo"; else tout << u;
tout << "]";
tout << ", m: " << m << ", val: " << val << ", is_int: " << lra.column_is_int(j) << "\n";);
#if 0
verbose_stream() << "path " << m << " ";
if (!inf_l) verbose_stream() << "infl " << l.x << " ";
if (!inf_u) verbose_stream() << "infu " << u.x << " ";
verbose_stream() << "\n";
if (m.is_big() || (!inf_l && l.x.is_big()) || (!inf_u && u.x.is_big())) {
return;
}
#endif
#if 0
verbose_stream() << "TARGET v" << j << " -> [";
if (inf_l) verbose_stream() << "-oo"; else verbose_stream() << ceil(l.x) << " " << l << "\n";
verbose_stream() << ", ";
if (inf_u) verbose_stream() << "oo"; else verbose_stream() << floor(u.x) << " " << u << "\n";
verbose_stream() << "]";
verbose_stream() << ", m: " << m << ", val: " << val << ", is_int: " << lra.column_is_int(j) << "\n";
#endif
#if 0
if (!inf_l)
l = impq(ceil(l));
if (!inf_u)
u = impq(floor(u));
#endif
if (!inf_l) {
l = impq(m_is_one ? ceil(l) : m * ceil(l / m));
if (inf_u || l <= u) {
@ -99,8 +313,23 @@ void int_solver::patcher::patch_nbasic_column(unsigned j) {
lra.set_value_for_nbasic_column(j, l);
}
else {
--m_num_nbasic_patches;
//verbose_stream() << "fail: " << j << " " << m << "\n";
++m_patch_fail;
TRACE("patch_int", tout << "not patching " << l << "\n";);
#if 0
verbose_stream() << "not patched\n";
for (auto c : A.column(j)) {
unsigned row_index = c.var();
unsigned i = lrac.m_r_basis[row_index];
if (!lia.get_value(i).is_int() ||
(lia.has_lower(i) && lia.get_value(i) < lra.get_lower_bound(i)) ||
(lia.has_upper(i) && lia.get_value(i) > lra.get_upper_bound(i))) {
lia.display_row(verbose_stream(), A.m_rows[row_index]);
verbose_stream() << "\n";
}
}
#endif
return;
}
}
else if (!inf_u) {
@ -112,7 +341,21 @@ void int_solver::patcher::patch_nbasic_column(unsigned j) {
lra.set_value_for_nbasic_column(j, impq(0));
TRACE("patch_int", tout << "patching with 0\n";);
}
++m_num_nbasic_patches;
++m_patch_success;
#if 0
verbose_stream() << "patched " << j << "\n";
for (auto c : A.column(j)) {
unsigned row_index = c.var();
unsigned i = lrac.m_r_basis[row_index];
if (!lia.get_value(i).is_int() ||
(lia.has_lower(i) && lia.get_value(i) < lra.get_lower_bound(i)) ||
(lia.has_upper(i) && lia.get_value(i) > lra.get_upper_bound(i))) {
lia.display_row(verbose_stream(), A.m_rows[row_index]);
verbose_stream() << "\n";
}
}
#endif
}
int_solver::int_solver(lar_solver& lar_slv) :
@ -326,7 +569,7 @@ static void set_upper(impq & u, bool & inf_u, impq const & v) {
// this function assumes that all basic columns dependend on j are feasible
bool int_solver::get_freedom_interval_for_column(unsigned j, bool & inf_l, impq & l, bool & inf_u, impq & u, mpq & m) {
if (lrac.m_r_heading[j] >= 0) // the basic var
if (lrac.m_r_heading[j] >= 0 || is_fixed(j)) // basic or fixed var
return false;
TRACE("random_update", display_column(tout, j) << ", is_int = " << column_is_int(j) << "\n";);
@ -361,10 +604,9 @@ bool int_solver::get_freedom_interval_for_column(unsigned j, bool & inf_l, impq
unsigned i = lrac.m_r_basis[row_index];
impq const & xi = get_value(i);
lp_assert(lrac.m_r_solver.column_is_feasible(i));
if (column_is_int(i) && !a.is_int())
if (column_is_int(i) && !a.is_int() && xi.is_int())
m = lcm(m, denominator(a));
if (!inf_l && !inf_u) {
if (l == u)
continue;
@ -372,15 +614,15 @@ bool int_solver::get_freedom_interval_for_column(unsigned j, bool & inf_l, impq
if (a.is_neg()) {
if (has_lower(i))
set_lower(l, inf_l, delta(a, xi, lrac.m_r_lower_bounds()[i]));
set_lower(l, inf_l, delta(a, xi, lra.get_lower_bound(i)));
if (has_upper(i))
set_upper(u, inf_u, delta(a, xi, lrac.m_r_upper_bounds()[i]));
set_upper(u, inf_u, delta(a, xi, lra.get_upper_bound(i)));
}
else {
if (has_upper(i))
set_lower(l, inf_l, delta(a, xi, lrac.m_r_upper_bounds()[i]));
set_lower(l, inf_l, delta(a, xi, lra.get_upper_bound(i)));
if (has_lower(i))
set_upper(u, inf_u, delta(a, xi, lrac.m_r_lower_bounds()[i]));
set_upper(u, inf_u, delta(a, xi, lra.get_lower_bound(i)));
}
}
@ -479,14 +721,11 @@ bool int_solver::at_upper(unsigned j) const {
}
std::ostream & int_solver::display_row(std::ostream & out, lp::row_strip<rational> const & row) const {
bool first = true;
bool first = true;
auto & rslv = lrac.m_r_solver;
for (const auto &c : row)
{
if (is_fixed(c.var()))
{
if (!get_value(c.var()).is_zero())
{
for (const auto &c : row) {
if (is_fixed(c.var())) {
if (!get_value(c.var()).is_zero()) {
impq val = get_value(c.var()) * c.coeff();
if (!first && val.is_pos())
out << "+";
@ -505,17 +744,11 @@ for (const auto &c : row)
}
else if (c.coeff().is_minus_one())
out << "-";
else
{
if (c.coeff().is_pos())
{
if (!first)
out << "+";
}
else {
if (c.coeff().is_pos() && !first)
out << "+";
if (c.coeff().is_big())
{
out << " b*";
}
else
out << c.coeff();
}
@ -523,8 +756,7 @@ for (const auto &c : row)
first = false;
}
out << "\n";
for (const auto &c : row)
{
for (const auto &c : row) {
if (is_fixed(c.var()))
continue;
rslv.print_column_info(c.var(), out);
@ -533,14 +765,13 @@ for (const auto &c : row)
}
return out;
}
std::ostream& int_solver::display_row_info(std::ostream & out, unsigned row_index) const {
auto & rslv = lrac.m_r_solver;
auto const& row = rslv.m_A.m_rows[row_index];
return display_row(out, row);
}
bool int_solver::shift_var(unsigned j, unsigned range) {
if (is_fixed(j) || is_base(j))
return false;
@ -549,11 +780,13 @@ bool int_solver::shift_var(unsigned j, unsigned range) {
bool inf_l = false, inf_u = false;
impq l, u;
mpq m;
VERIFY(get_freedom_interval_for_column(j, inf_l, l, inf_u, u, m) || settings().get_cancel_flag());
if (!get_freedom_interval_for_column(j, inf_l, l, inf_u, u, m))
return false;
if (settings().get_cancel_flag())
return false;
const impq & x = get_value(j);
// x, the value of j column, might be shifted on a multiple of m
if (inf_l && inf_u) {
impq new_val = m * impq(random() % (range + 1)) + x;
lra.set_value_for_nbasic_column(j, new_val);
@ -570,6 +803,7 @@ bool int_solver::shift_var(unsigned j, unsigned range) {
if (!inf_l && !inf_u && l >= u)
return false;
if (inf_u) {
SASSERT(!inf_l);
impq new_val = x + m * impq(random() % (range + 1));
@ -640,9 +874,6 @@ int int_solver::select_int_infeasible_var() {
unsigned n = 0;
lar_core_solver & lcs = lra.m_mpq_lar_core_solver;
unsigned prev_usage = 0; // to quiet down the compile
unsigned k = 0;
unsigned usage;
unsigned j;
enum state { small_box, is_small_value, any_value, not_found };
state st = not_found;
@ -650,11 +881,10 @@ int int_solver::select_int_infeasible_var() {
// 1. small box
// 2. small value
// 3. any value
for (; k < lra.r_basis().size(); k++) {
j = lra.r_basis()[k];
for (unsigned j : lra.r_basis()) {
if (!column_is_int_inf(j))
continue;
usage = lra.usage_in_terms(j);
unsigned usage = lra.usage_in_terms(j);
if (is_boxed(j) && (new_range = lcs.m_r_upper_bounds()[j].x - lcs.m_r_lower_bounds()[j].x - rational(2*usage)) <= small_value) {
SASSERT(!is_fixed(j));
if (st != small_box) {
@ -688,12 +918,12 @@ int int_solver::select_int_infeasible_var() {
continue;
SASSERT(st == not_found || st == any_value);
st = any_value;
if (n == 0 /*|| usage > prev_usage*/) {
if (n == 0 || usage > prev_usage) {
result = j;
prev_usage = usage;
n = 1;
}
else if (usage > 0 && /*usage == prev_usage && */ (random() % (++n) == 0))
else if (usage > 0 && usage == prev_usage && (random() % (++n) == 0))
result = j;
}

View file

@ -17,6 +17,7 @@ Revision History:
--*/
// clang-format off
#pragma once
#include "math/lp/lp_settings.h"
#include "math/lp/static_matrix.h"
@ -44,17 +45,23 @@ class int_solver {
int_solver& lia;
lar_solver& lra;
lar_core_solver& lrac;
unsigned m_num_nbasic_patches;
unsigned m_patch_cost;
unsigned m_next_patch;
unsigned m_delay;
unsigned m_patch_success = 0;
unsigned m_patch_fail = 0;
unsigned m_num_ones = 0;
unsigned m_num_divides = 0;
public:
patcher(int_solver& lia);
bool should_apply();
lia_move operator()();
bool should_apply() const { return true; }
lia_move operator()() { return patch_basic_columns(); }
void patch_nbasic_column(unsigned j);
bool patch_basic_column_on_row_cell(unsigned v, row_cell<mpq> const& c);
void patch_basic_column(unsigned j);
bool try_patch_column(unsigned v, unsigned j, mpq const& delta);
unsigned count_non_int();
private:
void remove_fixed_vars_from_base();
lia_move patch_nbasic_columns();
lia_move patch_basic_columns();
};
lar_solver& lra;

View file

@ -93,6 +93,8 @@ public:
void solve();
void pivot(int entering, int leaving) { m_r_solver.pivot(entering, leaving); }
bool lower_bounds_are_set() const { return true; }
const indexed_vector<mpq> & get_pivot_row() const {

View file

@ -2,7 +2,7 @@
Copyright (c) 2017 Microsoft Corporation
Author: Nikolaj Bjorner, Lev Nachmanson
*/
// clang-format off
#include "math/lp/lar_solver.h"
#include "smt/params/smt_params_helper.hpp"
@ -41,7 +41,6 @@ namespace lp {
for (auto t : m_terms)
delete t;
}
bool lar_solver::sizes_are_correct() const {
lp_assert(A_r().column_count() == m_mpq_lar_core_solver.m_r_solver.m_column_types.size());
@ -50,7 +49,6 @@ namespace lp {
return true;
}
std::ostream& lar_solver::print_implied_bound(const implied_bound& be, std::ostream& out) const {
out << "implied bound\n";
unsigned v = be.m_j;
@ -1354,8 +1352,8 @@ namespace lp {
}
bool lar_solver::term_is_int(const vector<std::pair<mpq, unsigned int>>& coeffs) const {
for (auto const& p : coeffs)
if (!(column_is_int(p.second) && p.first.is_int()))
for (auto const& [coeff, v] : coeffs)
if (!(column_is_int(v) && coeff.is_int()))
return false;
return true;
}
@ -1386,7 +1384,7 @@ namespace lp {
// the lower/upper bound is not strict.
// the LP obtained by making the bound strict is infeasible
// -> the column has to be fixed
bool lar_solver::is_fixed_at_bound(column_index const& j) {
bool lar_solver::is_fixed_at_bound(column_index const& j, vector<std::tuple<explanation, column_index, bool, mpq>>& bounds) {
if (column_is_fixed(j))
return false;
mpq val;
@ -1395,50 +1393,56 @@ namespace lp {
lp::lconstraint_kind k;
if (column_has_upper_bound(j) &&
get_upper_bound(j).x == val) {
verbose_stream() << "check upper " << j << "\n";
push();
if (column_is_int(j))
k = LE, val -= 1;
else
k = LT;
auto ci = mk_var_bound(j, k, val);
update_column_type_and_bound(j, k, val, ci);
push();
k = column_is_int(j) ? LE : LT;
auto ci = mk_var_bound(j, k, column_is_int(j) ? val - 1 : val);
update_column_type_and_bound(j, k, column_is_int(j) ? val - 1 : val, ci);
auto st = find_feasible_solution();
bool infeasible = st == lp_status::INFEASIBLE;
if (infeasible) {
explanation exp;
get_infeasibility_explanation(exp);
unsigned_vector cis;
exp.remove(ci);
verbose_stream() << "tight upper bound " << j << " " << val << "\n";
bounds.push_back({exp, j, true, val});
}
pop(1);
return st == lp_status::INFEASIBLE;
return infeasible;
}
if (column_has_lower_bound(j) &&
get_lower_bound(j).x == val) {
verbose_stream() << "check lower " << j << "\n";
push();
if (column_is_int(j))
k = GE, val += 1;
else
k = GT;
auto ci = mk_var_bound(j, k, val);
update_column_type_and_bound(j, k, val, ci);
k = column_is_int(j) ? GE : GT;
auto ci = mk_var_bound(j, k, column_is_int(j) ? val + 1 : val);
update_column_type_and_bound(j, k, column_is_int(j) ? val + 1 : val, ci);
auto st = find_feasible_solution();
bool infeasible = st == lp_status::INFEASIBLE;
if (infeasible) {
explanation exp;
get_infeasibility_explanation(exp);
exp.remove(ci);
verbose_stream() << "tight lower bound " << j << " " << val << "\n";
bounds.push_back({exp, j, false, val});
}
pop(1);
return st == lp_status::INFEASIBLE;
return infeasible;
}
return false;
}
bool lar_solver::has_fixed_at_bound() {
bool lar_solver::has_fixed_at_bound(vector<std::tuple<explanation, column_index, bool, mpq>>& bounds) {
verbose_stream() << "has-fixed-at-bound\n";
unsigned num_fixed = 0;
for (unsigned j = 0; j < A_r().m_columns.size(); ++j) {
auto ci = column_index(j);
if (is_fixed_at_bound(ci)) {
++num_fixed;
if (is_fixed_at_bound(ci, bounds))
verbose_stream() << "fixed " << j << "\n";
}
}
verbose_stream() << "num fixed " << num_fixed << "\n";
if (num_fixed > 0)
verbose_stream() << "num fixed " << bounds.size() << "\n";
if (!bounds.empty())
find_feasible_solution();
return num_fixed > 0;
return !bounds.empty();
}
@ -1617,6 +1621,18 @@ namespace lp {
return ret;
}
/**
* \brief ensure there is a column index corresponding to vi
* If vi is already a column, just return vi
* If vi is for a term, then create a row that uses the term.
*/
var_index lar_solver::ensure_column(var_index vi) {
if (lp::tv::is_term(vi))
return to_column(vi);
else
return vi;
}
void lar_solver::add_row_from_term_no_constraint(const lar_term* term, unsigned term_ext_index) {
TRACE("dump_terms", print_term(*term, tout) << std::endl;);

View file

@ -18,29 +18,30 @@
--*/
#pragma once
#include "util/vector.h"
#include <utility>
#include "util/debug.h"
#include "util/buffer.h"
#include <algorithm>
#include <functional>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <stack>
#include <functional>
#include <utility>
#include "math/lp/bound_analyzer_on_row.h"
#include "math/lp/implied_bound.h"
#include "math/lp/int_solver.h"
#include "math/lp/lar_constraints.h"
#include "math/lp/lar_core_solver.h"
#include "math/lp/numeric_pair.h"
#include "math/lp/lp_primal_core_solver.h"
#include "math/lp/random_updater.h"
#include "util/stacked_value.h"
#include "math/lp/stacked_vector.h"
#include "math/lp/implied_bound.h"
#include "math/lp/bound_analyzer_on_row.h"
#include "math/lp/int_solver.h"
#include "math/lp/nra_solver.h"
#include "math/lp/lp_types.h"
#include "math/lp/lp_bound_propagator.h"
#include "math/lp/lp_primal_core_solver.h"
#include "math/lp/lp_types.h"
#include "math/lp/nra_solver.h"
#include "math/lp/numeric_pair.h"
#include "math/lp/random_updater.h"
#include "math/lp/stacked_vector.h"
#include "util/buffer.h"
#include "util/debug.h"
#include "util/stacked_value.h"
#include "util/vector.h"
namespace lp {
@ -48,10 +49,9 @@ class int_branch;
class int_solver;
class lar_solver : public column_namer {
struct term_hasher {
std::size_t operator()(const lar_term &t) const
{
using std::size_t;
std::size_t operator()(const lar_term& t) const {
using std::hash;
using std::size_t;
using std::string;
size_t seed = 0;
int i = 0;
@ -66,110 +66,107 @@ class lar_solver : public column_namer {
};
struct term_comparer {
bool operator()(const lar_term &a, const lar_term& b) const
{
return a == b;
bool operator()(const lar_term& a, const lar_term& b) const {
return a == b;
}
};
//////////////////// fields //////////////////////////
lp_settings m_settings;
lp_status m_status = lp_status::UNKNOWN;
stacked_value<simplex_strategy_enum> m_simplex_strategy;
lp_settings m_settings;
lp_status m_status = lp_status::UNKNOWN;
stacked_value<simplex_strategy_enum> m_simplex_strategy;
// such can be found at the initialization step: u < l
stacked_value<int> m_crossed_bounds_column;
lar_core_solver m_mpq_lar_core_solver;
int_solver * m_int_solver = nullptr;
bool m_need_register_terms = false;
var_register m_var_register;
var_register m_term_register;
stacked_vector<ul_pair> m_columns_to_ul_pairs;
constraint_set m_constraints;
stacked_value<int> m_crossed_bounds_column;
lar_core_solver m_mpq_lar_core_solver;
int_solver* m_int_solver = nullptr;
bool m_need_register_terms = false;
var_register m_var_register;
var_register m_term_register;
stacked_vector<ul_pair> m_columns_to_ul_pairs;
constraint_set m_constraints;
// the set of column indices j such that bounds have changed for j
u_set m_columns_with_changed_bounds;
u_set m_rows_with_changed_bounds;
unsigned_vector m_row_bounds_to_replay;
u_set m_basic_columns_with_changed_cost;
u_set m_columns_with_changed_bounds;
u_set m_rows_with_changed_bounds;
unsigned_vector m_row_bounds_to_replay;
u_set m_basic_columns_with_changed_cost;
// these are basic columns with the value changed, so the corresponding row in the tableau
// does not sum to zero anymore
u_set m_incorrect_columns;
u_set m_incorrect_columns;
// copy of m_r_solver.inf_heap()
unsigned_vector m_inf_index_copy;
stacked_value<unsigned> m_term_count;
vector<lar_term*> m_terms;
indexed_vector<mpq> m_column_buffer;
unsigned_vector m_inf_index_copy;
stacked_value<unsigned> m_term_count;
vector<lar_term*> m_terms;
indexed_vector<mpq> m_column_buffer;
std::unordered_map<lar_term, std::pair<mpq, unsigned>, term_hasher, term_comparer>
m_normalized_terms_to_columns;
vector<impq> m_backup_x;
stacked_vector<unsigned> m_usage_in_terms;
m_normalized_terms_to_columns;
vector<impq> m_backup_x;
stacked_vector<unsigned> m_usage_in_terms;
// ((x[j], is_int(j))->j) for fixed j, used in equalities propagation
// maps values to integral fixed vars
map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>> m_fixed_var_table_int;
map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>> m_fixed_var_table_int;
// maps values to non-integral fixed vars
map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>> m_fixed_var_table_real;
map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>> m_fixed_var_table_real;
// end of fields
////////////////// methods ////////////////////////////////
static bool valid_index(unsigned j) { return static_cast<int>(j) >= 0;}
const lar_term & get_term(unsigned j) const;
static bool valid_index(unsigned j) { return static_cast<int>(j) >= 0; }
const lar_term& get_term(unsigned j) const;
bool row_has_a_big_num(unsigned i) const;
// init region
bool strategy_is_undecided() const;
void register_new_ext_var_index(unsigned ext_v, bool is_int);
bool term_is_int(const lar_term * t) const;
bool term_is_int(const vector<std::pair<mpq, unsigned int>> & coeffs) const;
bool term_is_int(const lar_term* t) const;
bool term_is_int(const vector<std::pair<mpq, unsigned int>>& coeffs) const;
void add_non_basic_var_to_core_fields(unsigned ext_j, bool is_int);
void add_new_var_to_core_fields_for_mpq(bool register_in_basis);
mpq adjust_bound_for_int(lpvar j, lconstraint_kind&, const mpq&);
// terms
bool all_vars_are_registered(const vector<std::pair<mpq, var_index>> & coeffs);
var_index add_term_undecided(const vector<std::pair<mpq, var_index>> & coeffs);
bool term_coeffs_are_ok(const vector<std::pair<mpq, var_index>> & coeffs);
bool all_vars_are_registered(const vector<std::pair<mpq, var_index>>& coeffs);
var_index add_term_undecided(const vector<std::pair<mpq, var_index>>& coeffs);
bool term_coeffs_are_ok(const vector<std::pair<mpq, var_index>>& coeffs);
void push_term(lar_term* t);
void add_row_from_term_no_constraint(const lar_term * term, unsigned term_ext_index);
void add_row_from_term_no_constraint(const lar_term* term, unsigned term_ext_index);
void add_basic_var_to_core_fields();
bool compare_values(impq const& lhs, lconstraint_kind k, const mpq & rhs);
bool compare_values(impq const& lhs, lconstraint_kind k, const mpq& rhs);
inline void clear_columns_with_changed_bounds() { m_columns_with_changed_bounds.clear(); }
inline void increase_by_one_columns_with_changed_bounds() { m_columns_with_changed_bounds.increase_size_by_one(); }
inline void insert_to_columns_with_changed_bounds(unsigned j) { m_columns_with_changed_bounds.insert(j); }
void update_column_type_and_bound_check_on_equal(unsigned j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index, unsigned&);
void update_column_type_and_bound(unsigned j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index);
void update_column_type_and_bound_with_ub(var_index j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index);
void update_column_type_and_bound_with_no_ub(var_index j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index);
void update_bound_with_ub_lb(var_index j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index);
void update_bound_with_no_ub_lb(var_index j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index);
void update_bound_with_ub_no_lb(var_index j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index);
void update_bound_with_no_ub_no_lb(var_index j, lconstraint_kind kind, const mpq & right_side, constraint_index constr_index);
void update_column_type_and_bound_check_on_equal(unsigned j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index, unsigned&);
void update_column_type_and_bound(unsigned j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index);
void update_column_type_and_bound_with_ub(var_index j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index);
void update_column_type_and_bound_with_no_ub(var_index j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index);
void update_bound_with_ub_lb(var_index j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index);
void update_bound_with_no_ub_lb(var_index j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index);
void update_bound_with_ub_no_lb(var_index j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index);
void update_bound_with_no_ub_no_lb(var_index j, lconstraint_kind kind, const mpq& right_side, constraint_index constr_index);
void register_in_fixed_var_table(unsigned, unsigned&);
void remove_non_fixed_from_fixed_var_table();
constraint_index add_var_bound_on_constraint_for_term(var_index j, lconstraint_kind kind, const mpq & right_side);
constraint_index add_var_bound_on_constraint_for_term(var_index j, lconstraint_kind kind, const mpq& right_side);
inline void set_infeasible_column(unsigned j) {
set_status(lp_status::INFEASIBLE);
m_crossed_bounds_column = j;
}
constraint_index add_constraint_from_term_and_create_new_column_row(unsigned term_j, const lar_term* term,
lconstraint_kind kind, const mpq & right_side);
lconstraint_kind kind, const mpq& right_side);
unsigned row_of_basic_column(unsigned) const;
void decide_on_strategy_and_adjust_initial_state();
void adjust_initial_state();
void adjust_initial_state_for_tableau_rows();
bool sizes_are_correct() const;
bool implied_bound_is_correctly_explained(implied_bound const & be, const vector<std::pair<mpq, unsigned>> & explanation) const;
bool implied_bound_is_correctly_explained(implied_bound const& be, const vector<std::pair<mpq, unsigned>>& explanation) const;
void substitute_basis_var_in_terms_for_row(unsigned i);
template <typename T>
unsigned calculate_implied_bounds_for_row(unsigned row_index, lp_bound_propagator<T> & bp) {
if (A_r().m_rows[row_index].size() > settings().max_row_length_for_bound_propagation || row_has_a_big_num(row_index))
unsigned calculate_implied_bounds_for_row(unsigned row_index, lp_bound_propagator<T>& bp) {
if (A_r().m_rows[row_index].size() > settings().max_row_length_for_bound_propagation || row_has_a_big_num(row_index))
return 0;
return bound_analyzer_on_row<row_strip<mpq>, lp_bound_propagator<T>>::analyze_row(
A_r().m_rows[row_index],
null_ci,
@ -177,29 +174,28 @@ class lar_solver : public column_namer {
row_index,
bp);
}
static void clean_popped_elements_for_heap(unsigned n, lpvar_heap& set);
static void clean_popped_elements(unsigned n, u_set& set);
bool maximize_term_on_tableau(const lar_term & term,
impq &term_max);
bool maximize_term_on_tableau(const lar_term& term,
impq& term_max);
bool costs_are_zeros_for_r_solver() const;
bool reduced_costs_are_zeroes_for_r_solver() const;
void set_costs_to_zero(const lar_term & term);
void prepare_costs_for_r_solver(const lar_term & term);
bool maximize_term_on_corrected_r_solver(lar_term & term, impq &term_max);
void set_costs_to_zero(const lar_term& term);
void prepare_costs_for_r_solver(const lar_term& term);
bool maximize_term_on_corrected_r_solver(lar_term& term, impq& term_max);
void pop_core_solver_params();
void pop_core_solver_params(unsigned k);
void set_upper_bound_witness(var_index j, constraint_index ci);
void set_lower_bound_witness(var_index j, constraint_index ci);
void substitute_terms_in_linear_expression( const vector<std::pair<mpq, var_index>>& left_side_with_terms,
vector<std::pair<mpq, var_index>> &left_side) const;
void substitute_terms_in_linear_expression(const vector<std::pair<mpq, var_index>>& left_side_with_terms,
vector<std::pair<mpq, var_index>>& left_side) const;
void detect_rows_of_bound_change_column_for_nbasic_column_tableau(unsigned j);
bool use_tableau_costs() const;
bool tableau_with_costs() const;
bool costs_are_used() const;
void change_basic_columns_dependend_on_a_given_nb_column(unsigned j, const numeric_pair<mpq> & delta);
void change_basic_columns_dependend_on_a_given_nb_column(unsigned j, const numeric_pair<mpq>& delta);
void update_x_and_inf_costs_for_column_with_changed_bounds(unsigned j);
unsigned num_changed_bounds() const { return m_rows_with_changed_bounds.size(); }
void insert_row_with_changed_bounds(unsigned rid);
@ -211,19 +207,19 @@ class lar_solver : public column_namer {
numeric_pair<mpq> get_basic_var_value_from_row(unsigned i);
bool all_constrained_variables_are_registered(const vector<std::pair<mpq, var_index>>& left_side);
bool all_constraints_hold() const;
bool constraint_holds(const lar_base_constraint & constr, std::unordered_map<var_index, mpq> & var_map) const;
static void register_in_map(std::unordered_map<var_index, mpq> & coeffs, const lar_base_constraint & cn, const mpq & a);
static void register_monoid_in_map(std::unordered_map<var_index, mpq> & coeffs, const mpq & a, unsigned j);
bool the_left_sides_sum_to_zero(const vector<std::pair<mpq, unsigned>> & evidence) const;
bool constraint_holds(const lar_base_constraint& constr, std::unordered_map<var_index, mpq>& var_map) const;
static void register_in_map(std::unordered_map<var_index, mpq>& coeffs, const lar_base_constraint& cn, const mpq& a);
static void register_monoid_in_map(std::unordered_map<var_index, mpq>& coeffs, const mpq& a, unsigned j);
bool the_left_sides_sum_to_zero(const vector<std::pair<mpq, unsigned>>& evidence) const;
bool explanation_is_correct(explanation&) const;
bool inf_explanation_is_correct() const;
mpq sum_of_right_sides_of_explanation(explanation &) const;
mpq sum_of_right_sides_of_explanation(explanation&) const;
void get_infeasibility_explanation_for_inf_sign(
explanation & exp,
const vector<std::pair<mpq, unsigned>> & inf_row,
explanation& exp,
const vector<std::pair<mpq, unsigned>>& inf_row,
int inf_sign) const;
mpq get_left_side_val(const lar_base_constraint & cns, const std::unordered_map<var_index, mpq> & var_map) const;
void fill_var_set_for_random_update(unsigned sz, var_index const * vars, vector<unsigned>& column_list);
mpq get_left_side_val(const lar_base_constraint& cns, const std::unordered_map<var_index, mpq>& var_map) const;
void fill_var_set_for_random_update(unsigned sz, var_index const* vars, vector<unsigned>& column_list);
bool column_represents_row_in_tableau(unsigned j);
void make_sure_that_the_bottom_right_elem_not_zero_in_tableau(unsigned i, unsigned j);
void remove_last_row_and_column_from_tableau(unsigned j);
@ -235,22 +231,22 @@ class lar_solver : public column_namer {
void clean_inf_heap_of_r_solver_after_pop();
inline bool column_value_is_integer(unsigned j) const { return get_column_value(j).is_int(); }
bool model_is_int_feasible() const;
bool bound_is_integer_for_integer_column(unsigned j, const mpq & right_side) const;
inline lar_core_solver & get_core_solver() { return m_mpq_lar_core_solver; }
bool bound_is_integer_for_integer_column(unsigned j, const mpq& right_side) const;
inline lar_core_solver& get_core_solver() { return m_mpq_lar_core_solver; }
var_index to_column(unsigned ext_j) const;
void fix_terms_with_rounded_columns();
bool remove_from_basis(unsigned);
lar_term get_term_to_maximize(unsigned ext_j) const;
bool sum_first_coords(const lar_term& t, mpq & val) const;
bool sum_first_coords(const lar_term& t, mpq& val) const;
void register_normalized_term(const lar_term&, lpvar);
void deregister_normalized_term(const lar_term&);
mutable std::unordered_set<impq> m_set_of_different_pairs;
mutable std::unordered_set<mpq> m_set_of_different_singles;
mutable std::unordered_set<mpq> m_set_of_different_singles;
mutable mpq m_delta;
public:
public:
// this function just looks at the status
bool is_feasible() const;
@ -258,7 +254,6 @@ public:
return m_fixed_var_table_int;
}
const map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>>& fixed_var_table_real() const {
return m_fixed_var_table_real;
}
@ -267,11 +262,12 @@ public:
return m_fixed_var_table_real;
}
bool find_in_fixed_tables(const rational& mpq, bool is_int, unsigned & j) const {
return is_int? fixed_var_table_int().find(mpq, j) : fixed_var_table_real().find(mpq, j);
bool find_in_fixed_tables(const rational& mpq, bool is_int, unsigned& j) const {
return is_int ? fixed_var_table_int().find(mpq, j) : fixed_var_table_real().find(mpq, j);
}
template <typename T> void remove_non_fixed_from_table(T&);
template <typename T>
void remove_non_fixed_from_table(T&);
unsigned external_to_column_index(unsigned) const;
@ -284,38 +280,38 @@ public:
inline void set_column_value_test(unsigned j, const impq& v) {
set_column_value(j, v);
}
var_index add_named_var(unsigned ext_j, bool is_integer, const std::string&);
lp_status maximize_term(unsigned j_or_term, impq &term_max);
lp_status maximize_term(unsigned j_or_term, impq& term_max);
inline core_solver_pretty_printer<lp::mpq, lp::impq> pp(std::ostream& out) const {
return core_solver_pretty_printer<lp::mpq, lp::impq>(m_mpq_lar_core_solver.m_r_solver, out);
inline core_solver_pretty_printer<lp::mpq, lp::impq> pp(std::ostream& out) const {
return core_solver_pretty_printer<lp::mpq, lp::impq>(m_mpq_lar_core_solver.m_r_solver, out);
}
void get_infeasibility_explanation(explanation &) const;
void get_infeasibility_explanation(explanation&) const;
inline void backup_x() { m_backup_x = m_mpq_lar_core_solver.m_r_x; }
inline void restore_x() { m_mpq_lar_core_solver.m_r_x = m_backup_x; }
template <typename T>
void explain_implied_bound(const implied_bound & ib, lp_bound_propagator<T> & bp) {
void explain_implied_bound(const implied_bound& ib, lp_bound_propagator<T>& bp) {
unsigned i = ib.m_row_or_term_index;
int bound_sign = (ib.m_is_lower_bound ? 1 : -1);
int j_sign = (ib.m_coeff_before_j_is_pos ? 1 : -1) * bound_sign;
unsigned bound_j = ib.m_j;
if (tv::is_term(bound_j))
if (tv::is_term(bound_j))
bound_j = m_var_register.external_to_local(bound_j);
for (auto const& r : get_row(i)) {
unsigned j = r.var();
if (j == bound_j)
if (j == bound_j)
continue;
mpq const& a = r.coeff();
int a_sign = is_pos(a) ? 1 : -1;
int sign = j_sign * a_sign;
const ul_pair & ul = m_columns_to_ul_pairs[j];
const ul_pair& ul = m_columns_to_ul_pairs[j];
auto witness = sign > 0 ? ul.upper_bound_witness() : ul.lower_bound_witness();
lp_assert(is_valid(witness));
bp.consume(a, witness);
@ -329,13 +325,13 @@ public:
}
// lp_assert(implied_bound_is_correctly_explained(ib, explanation)); }
constraint_index mk_var_bound(var_index j, lconstraint_kind kind, const mpq & right_side);
constraint_index mk_var_bound(var_index j, lconstraint_kind kind, const mpq& right_side);
void activate_check_on_equal(constraint_index, var_index&);
void activate(constraint_index);
void random_update(unsigned sz, var_index const * vars);
void random_update(unsigned sz, var_index const* vars);
void mark_rows_for_bound_prop(lpvar j);
template <typename T>
void propagate_bounds_for_touched_rows(lp_bound_propagator<T> & bp) {
void propagate_bounds_for_touched_rows(lp_bound_propagator<T>& bp) {
unsigned num_prop = 0;
for (unsigned i : m_rows_with_changed_bounds) {
num_prop += calculate_implied_bounds_for_row(i, bp);
@ -349,7 +345,7 @@ public:
bp.clear_for_eq();
for (unsigned i : m_rows_with_changed_bounds) {
unsigned offset_eqs = stats().m_offset_eqs;
bp.cheap_eq_tree(i);
bp.cheap_eq_tree(i);
if (settings().get_cancel_flag())
return;
if (stats().m_offset_eqs > offset_eqs)
@ -360,75 +356,81 @@ public:
}
template <typename T>
void check_missed_propagations(lp_bound_propagator<T> & bp) {
for (unsigned i = 0; i < A_r().row_count(); i++)
if (!m_rows_with_changed_bounds.contains(i))
void check_missed_propagations(lp_bound_propagator<T>& bp) {
for (unsigned i = 0; i < A_r().row_count(); i++)
if (!m_rows_with_changed_bounds.contains(i))
if (0 < calculate_implied_bounds_for_row(i, bp)) {
verbose_stream() << i << ": " << get_row(i) << "\n";
}
}
bool is_fixed_at_bound(column_index const& j);
bool has_fixed_at_bound();
bool is_fixed(column_index const& j) const { return column_is_fixed(j); }
bool is_fixed_at_bound(column_index const& j, vector<std::tuple<explanation, column_index, bool, mpq>>& bounds);
bool has_fixed_at_bound(vector<std::tuple<explanation, column_index, bool, mpq>>& bounds);
bool is_fixed(column_index const& j) const { return column_is_fixed(j); }
inline column_index to_column_index(unsigned v) const { return column_index(external_to_column_index(v)); }
bool external_is_used(unsigned) const;
void pop(unsigned k);
bool compare_values(var_index j, lconstraint_kind kind, const mpq & right_side);
var_index add_term(const vector<std::pair<mpq, var_index>> & coeffs, unsigned ext_i);
bool compare_values(var_index j, lconstraint_kind kind, const mpq& right_side);
var_index add_term(const vector<std::pair<mpq, var_index>>& coeffs, unsigned ext_i);
void register_existing_terms();
constraint_index add_var_bound(var_index, lconstraint_kind, const mpq &);
constraint_index add_var_bound_check_on_equal(var_index, lconstraint_kind, const mpq &, var_index&);
var_index ensure_column(var_index vi);
constraint_index add_var_bound(var_index, lconstraint_kind, const mpq&);
constraint_index add_var_bound_check_on_equal(var_index, lconstraint_kind, const mpq&, var_index&);
var_index add_var(unsigned ext_j, bool is_integer);
void set_cut_strategy(unsigned cut_frequency);
inline unsigned column_count() const { return A_r().column_count(); }
inline var_index local_to_external(var_index idx) const {
return tv::is_term(idx)?
m_term_register.local_to_external(idx) : m_var_register.local_to_external(idx);
return tv::is_term(idx) ? m_term_register.local_to_external(idx) : m_var_register.local_to_external(idx);
}
bool column_corresponds_to_term(unsigned) const;
const lar_term& column_to_term(unsigned j) const {
SASSERT(column_corresponds_to_term(j));
return get_term(column2tv(to_column_index(j)));
}
inline unsigned row_count() const { return A_r().row_count(); }
bool var_is_registered(var_index vj) const;
void clear_inf_heap() {
m_mpq_lar_core_solver.m_r_solver.inf_heap().clear();
m_mpq_lar_core_solver.m_r_solver.inf_heap().clear();
}
inline void remove_column_from_inf_heap(unsigned j) {
inline void remove_column_from_inf_set(unsigned j) {
m_mpq_lar_core_solver.m_r_solver.remove_column_from_inf_heap(j);
}
void pivot(int entering, int leaving) {
m_mpq_lar_core_solver.pivot(entering, leaving);
}
template <typename ChangeReport>
void change_basic_columns_dependend_on_a_given_nb_column_report(unsigned j,
const numeric_pair<mpq> & delta,
const numeric_pair<mpq>& delta,
const ChangeReport& after) {
for (const auto & c : A_r().m_columns[j]) {
unsigned bj = m_mpq_lar_core_solver.m_r_basis[c.var()];
if (tableau_with_costs()) {
m_basic_columns_with_changed_cost.insert(bj);
}
m_mpq_lar_core_solver.m_r_solver.add_delta_to_x_and_track_feasibility(bj, - A_r().get_val(c) * delta);
after(bj);
TRACE("change_x_del",
tout << "changed basis column " << bj << ", it is " <<
( m_mpq_lar_core_solver.m_r_solver.column_is_feasible(bj)? "feas":"inf") << std::endl;);
}
}
for (const auto& c : A_r().m_columns[j]) {
unsigned bj = m_mpq_lar_core_solver.m_r_basis[c.var()];
if (tableau_with_costs())
m_basic_columns_with_changed_cost.insert(bj);
m_mpq_lar_core_solver.m_r_solver.add_delta_to_x_and_track_feasibility(bj, -A_r().get_val(c) * delta);
after(bj);
TRACE("change_x_del",
tout << "changed basis column " << bj << ", it is " << (m_mpq_lar_core_solver.m_r_solver.column_is_feasible(bj) ? "feas" : "inf") << std::endl;);
}
}
template <typename ChangeReport>
void set_value_for_nbasic_column_report(unsigned j,
const impq & new_val,
const impq& new_val,
const ChangeReport& after) {
lp_assert(!is_base(j));
auto & x = m_mpq_lar_core_solver.m_r_x[j];
auto& x = m_mpq_lar_core_solver.m_r_x[j];
auto delta = new_val - x;
x = new_val;
after(j);
change_basic_columns_dependend_on_a_given_nb_column_report(j, delta, after);
}
template <typename Blocker, typename ChangeReport>
bool try_to_patch(lpvar j, const mpq& val,
const Blocker& is_blocked,
@ -445,8 +447,8 @@ public:
impq delta = get_column_value(j) - ival;
for (auto c : A_r().column(j)) {
unsigned row_index = c.var();
const mpq & a = c.coeff();
unsigned rj = m_mpq_lar_core_solver.m_r_basis[row_index];
const mpq& a = c.coeff();
unsigned rj = m_mpq_lar_core_solver.m_r_basis[row_index];
impq rj_new_val = a * delta + get_column_value(rj);
// if (column_is_int(rj) && !rj_new_val.is_int())
// return false;
@ -463,65 +465,62 @@ public:
inline bool column_has_lower_bound(unsigned j) const {
return m_mpq_lar_core_solver.m_r_solver.column_has_lower_bound(j);
}
}
inline
constraint_index get_column_upper_bound_witness(unsigned j) const {
inline constraint_index get_column_upper_bound_witness(unsigned j) const {
if (tv::is_term(j)) {
j = m_var_register.external_to_local(j);
}
return m_columns_to_ul_pairs()[j].upper_bound_witness();
}
inline
const impq& get_upper_bound(column_index j) const {
inline const impq& get_upper_bound(column_index j) const {
return m_mpq_lar_core_solver.m_r_solver.m_upper_bounds[j];
}
inline
const impq& get_lower_bound(column_index j) const {
inline const impq& get_lower_bound(column_index j) const {
return m_mpq_lar_core_solver.m_r_solver.m_lower_bounds[j];
}
bool has_lower_bound(var_index var, constraint_index& ci, mpq& value, bool& is_strict) const;
bool has_lower_bound(var_index var, constraint_index& ci, mpq& value, bool& is_strict) const;
bool has_upper_bound(var_index var, constraint_index& ci, mpq& value, bool& is_strict) const;
bool has_value(var_index var, mpq& value) const;
bool fetch_normalized_term_column(const lar_term& t, std::pair<mpq, lpvar>& ) const;
bool fetch_normalized_term_column(const lar_term& t, std::pair<mpq, lpvar>&) const;
unsigned map_term_index_to_column_index(unsigned j) const;
bool column_is_fixed(unsigned j) const;
bool column_is_free(unsigned j) const;
unsigned column_to_reported_index(unsigned j) const;
lp_settings & settings();
lp_settings const & settings() const;
lp_settings& settings();
lp_settings const& settings() const;
statistics& stats();
void updt_params(params_ref const& p);
column_type get_column_type(unsigned j) const { return m_mpq_lar_core_solver.m_column_types()[j]; }
const impq & get_lower_bound(unsigned j) const { return m_mpq_lar_core_solver.m_r_lower_bounds()[j]; }
const impq & get_upper_bound(unsigned j) const { return m_mpq_lar_core_solver.m_r_upper_bounds()[j]; }
const impq& get_lower_bound(unsigned j) const { return m_mpq_lar_core_solver.m_r_lower_bounds()[j]; }
const impq& get_upper_bound(unsigned j) const { return m_mpq_lar_core_solver.m_r_upper_bounds()[j]; }
std::ostream& print_terms(std::ostream& out) const;
std::ostream& print_term(lar_term const& term, std::ostream & out) const;
static std::ostream& print_term_as_indices(lar_term const& term, std::ostream & out);
std::ostream& print_constraint_indices_only(const lar_base_constraint * c, std::ostream & out) const;
std::ostream& print_implied_bound(const implied_bound& be, std::ostream & out) const;
std::ostream& print_term(lar_term const& term, std::ostream& out) const;
static std::ostream& print_term_as_indices(lar_term const& term, std::ostream& out);
std::ostream& print_constraint_indices_only(const lar_base_constraint* c, std::ostream& out) const;
std::ostream& print_implied_bound(const implied_bound& be, std::ostream& out) const;
std::ostream& print_values(std::ostream& out) const;
std::ostream& display(std::ostream& out) const;
bool init_model() const;
mpq get_value(column_index const& j) const;
mpq get_tv_value(tv const& t) const;
const impq & get_tv_ivalue(tv const& t) const;
void get_model(std::unordered_map<var_index, mpq> & variable_values) const;
const impq& get_tv_ivalue(tv const& t) const;
void get_model(std::unordered_map<var_index, mpq>& variable_values) const;
void get_rid_of_inf_eps();
void get_model_do_not_care_about_diff_vars(std::unordered_map<var_index, mpq> & variable_values) const;
void get_model_do_not_care_about_diff_vars(std::unordered_map<var_index, mpq>& variable_values) const;
std::string get_variable_name(var_index vi) const override;
void set_variable_name(var_index vi, std::string);
inline unsigned number_of_vars() const { return m_var_register.size(); }
inline bool is_base(unsigned j) const { return m_mpq_lar_core_solver.m_r_heading[j] >= 0; }
inline const impq & column_lower_bound(unsigned j) const {
inline const impq& column_lower_bound(unsigned j) const {
return m_mpq_lar_core_solver.lower_bound(j);
}
inline const impq & column_upper_bound(unsigned j) const {
inline const impq& column_upper_bound(unsigned j) const {
return m_mpq_lar_core_solver.upper_bound(j);
}
@ -534,9 +533,9 @@ public:
}
std::pair<constraint_index, constraint_index> add_equality(lpvar j, lpvar k);
inline void get_bound_constraint_witnesses_for_column(unsigned j, constraint_index & lc, constraint_index & uc) const {
const ul_pair & ul = m_columns_to_ul_pairs[j];
inline void get_bound_constraint_witnesses_for_column(unsigned j, constraint_index& lc, constraint_index& uc) const {
const ul_pair& ul = m_columns_to_ul_pairs[j];
lc = ul.lower_bound_witness();
uc = ul.upper_bound_witness();
}
@ -553,19 +552,19 @@ public:
inline tv column2tv(column_index const& c) const {
return tv::raw(column_to_reported_index(c));
}
inline std::ostream& print_column_info(unsigned j, std::ostream& out) const {
m_mpq_lar_core_solver.m_r_solver.print_column_info(j, out);
if (tv::is_term(j)) {
print_term_as_indices(get_term(j), out) << "\n";
} else if (column_corresponds_to_term(j)) {
} else if (column_corresponds_to_term(j)) {
const lar_term& t = get_term(m_var_register.local_to_external(j));
print_term_as_indices(t, out) << "\n";
}
return out;
}
void subst_known_terms(lar_term*);
inline std::ostream& print_column_bound_info(unsigned j, std::ostream& out) const {
@ -576,67 +575,68 @@ public:
inline bool has_inf_int() const {
for (unsigned j = 0; j < column_count(); j++) {
if (column_is_int(j) && ! column_value_is_int(j))
if (column_is_int(j) && !column_value_is_int(j))
return true;
}
return false;
}
inline const vector<lar_term*> & terms() const { return m_terms; }
inline const vector<lar_term*>& terms() const { return m_terms; }
inline lar_term const& term(unsigned i) const { return *m_terms[i]; }
inline void set_int_solver(int_solver * int_slv) { m_int_solver = int_slv; }
inline int_solver * get_int_solver() { return m_int_solver; }
inline const int_solver * get_int_solver() const { return m_int_solver; }
inline const lar_term & get_term(tv const& t) const { lp_assert(t.is_term()); return *m_terms[t.id()]; }
lp_status find_feasible_solution();
inline void set_int_solver(int_solver* int_slv) { m_int_solver = int_slv; }
inline int_solver* get_int_solver() { return m_int_solver; }
inline const int_solver* get_int_solver() const { return m_int_solver; }
inline const lar_term& get_term(tv const& t) const {
lp_assert(t.is_term());
return *m_terms[t.id()];
}
lp_status find_feasible_solution();
void move_non_basic_columns_to_bounds(bool);
bool move_non_basic_column_to_bounds(unsigned j, bool);
inline bool r_basis_has_inf_int() const {
for (unsigned j : r_basis()) {
if (column_is_int(j) && ! column_value_is_int(j))
if (column_is_int(j) && !column_value_is_int(j))
return true;
}
return false;
}
void round_to_integer_solution();
inline const row_strip<mpq> & get_row(unsigned i) const { return A_r().m_rows[i]; }
inline const row_strip<mpq> & basic2row(unsigned i) const { return A_r().m_rows[row_of_basic_column(i)]; }
inline const column_strip & get_column(unsigned i) const { return A_r().m_columns[i]; }
inline const row_strip<mpq>& get_row(unsigned i) const { return A_r().m_rows[i]; }
inline const row_strip<mpq>& basic2row(unsigned i) const { return A_r().m_rows[row_of_basic_column(i)]; }
inline const column_strip& get_column(unsigned i) const { return A_r().m_columns[i]; }
bool row_is_correct(unsigned i) const;
bool ax_is_correct() const;
bool get_equality_and_right_side_for_term_on_current_x(tv const& t, mpq &rs, constraint_index& ci, bool &upper_bound) const;
bool get_equality_and_right_side_for_term_on_current_x(tv const& t, mpq& rs, constraint_index& ci, bool& upper_bound) const;
bool var_is_int(var_index v) const;
inline const vector<int> & r_heading() const { return m_mpq_lar_core_solver.m_r_heading; }
inline const vector<unsigned> & r_basis() const { return m_mpq_lar_core_solver.r_basis(); }
inline const vector<unsigned> & r_nbasis() const { return m_mpq_lar_core_solver.r_nbasis(); }
inline bool column_is_real(unsigned j) const { return !column_is_int(j); }
inline const vector<int>& r_heading() const { return m_mpq_lar_core_solver.m_r_heading; }
inline const vector<unsigned>& r_basis() const { return m_mpq_lar_core_solver.r_basis(); }
inline const vector<unsigned>& r_nbasis() const { return m_mpq_lar_core_solver.r_nbasis(); }
inline bool column_is_real(unsigned j) const { return !column_is_int(j); }
lp_status get_status() const;
bool has_changed_columns() const { return !m_columns_with_changed_bounds.empty(); }
bool has_changed_columns() const { return !m_columns_with_changed_bounds.empty(); }
void set_status(lp_status s);
lp_status solve();
void fill_explanation_from_crossed_bounds_column(explanation & evidence) const;
void fill_explanation_from_crossed_bounds_column(explanation& evidence) const;
bool term_is_used_as_row(unsigned term) const;
bool tighten_term_bounds_by_delta(tv const& t, const impq&);
lar_solver();
void set_track_pivoted_rows(bool v);
bool get_track_pivoted_rows() const;
bool get_track_pivoted_rows() const;
~lar_solver() override;
const vector<impq>& r_x() const { return m_mpq_lar_core_solver.m_r_x; }
bool column_is_int(unsigned j) const;
inline bool column_value_is_int(unsigned j) const { return m_mpq_lar_core_solver.m_r_x[j].is_int(); }
inline static_matrix<mpq, impq> & A_r() { return m_mpq_lar_core_solver.m_r_A; }
inline const static_matrix<mpq, impq> & A_r() const { return m_mpq_lar_core_solver.m_r_A; }
inline static_matrix<mpq, impq>& A_r() { return m_mpq_lar_core_solver.m_r_A; }
inline const static_matrix<mpq, impq>& A_r() const { return m_mpq_lar_core_solver.m_r_A; }
// columns
bool column_is_int(column_index const& j) const { return column_is_int((unsigned)j); }
// const impq& get_ivalue(column_index const& j) const { return get_column_value(j); }
// const impq& get_ivalue(column_index const& j) const { return get_column_value(j); }
const impq& get_column_value(column_index const& j) const { return m_mpq_lar_core_solver.m_r_x[j]; }
inline
var_index external_to_local(unsigned j) const {
inline var_index external_to_local(unsigned j) const {
var_index local_j;
if (m_var_register.external_is_used(j, local_j) ||
m_term_register.external_is_used(j, local_j)) {
return local_j;
}
else {
} else {
return -1;
}
}
@ -647,6 +647,5 @@ public:
}
friend int_solver;
friend int_branch;
};
}
} // namespace lp

View file

@ -4,39 +4,40 @@
Nikolaj Bjorner (nbjorner)
Lev Nachmanson (levnach)
*/
//clang-format off
#pragma once
#include "math/lp/lp_settings.h"
#include <utility>
#include "math/lp/lp_settings.h"
#include "util/uint_set.h"
namespace lp {
template <typename T>
class lp_bound_propagator {
class edge; // forward definition
class edge; // forward definition
// vertex represents a column
// The set of vertices is organised in a tree.
// The set of vertices is organized in a tree.
// The edges of the tree are rows,
// Vertices with m_neg set to false grow with the same rate as the root.
// Vertices with m_neq set to true diminish with the same rate as the roow grows.
// When two vertices with the same m_neg have the same value of columns
// then we have an equality betweet the columns.
class vertex {
unsigned m_column;
vector<edge> m_edges;
edge m_edge_from_parent;
unsigned m_level; // the distance in hops to the root;
// it is handy to find the common ancestor
public:
// then we have an equality between the columns.
class vertex {
unsigned m_column;
vector<edge> m_edges;
edge m_edge_from_parent;
unsigned m_level; // the distance in hops to the root;
// it is handy to find the common ancestor
public:
vertex() {}
vertex(unsigned column) :
m_column(column),
m_level(0)
{}
vertex(unsigned column) : m_column(column),
m_level(0) {}
unsigned column() const { return m_column; }
const vertex* parent() const { return m_edge_from_parent.source(); }
vertex* parent() { return m_edge_from_parent.source(); }
unsigned level() const { return m_level; }
void set_edge_from_parent(edge &e) { m_edge_from_parent = e; }
void set_edge_from_parent(edge& e) { m_edge_from_parent = e; }
const edge& edge_from_parent() const { return m_edge_from_parent; }
void add_child(int row, vertex* child) {
SASSERT(*this != *child);
SASSERT(child->parent() == nullptr);
@ -45,7 +46,7 @@ class lp_bound_propagator {
child->set_edge_from_parent(e);
child->m_level = m_level + 1;
}
const vector<edge> & edges() const { return m_edges; }
const vector<edge>& edges() const { return m_edges; }
bool operator==(const vertex& o) const {
return m_column == o.m_column;
}
@ -58,7 +59,8 @@ class lp_bound_propagator {
vertex* m_source;
vertex* m_target;
int m_row;
public:
public:
edge(vertex* source, vertex* target, int row) : m_source(source), m_target(target), m_row(row) {}
edge() : m_source(nullptr), m_target(nullptr), m_row(-1) {}
const vertex* source() const { return m_source; }
@ -69,57 +71,58 @@ class lp_bound_propagator {
edge reverse() const { return edge(m_target, m_source, m_row); }
};
static int other(int x, int y, int z) { SASSERT(x == z || y == z); return x == z ? y : x; }
std::ostream& print_vert(std::ostream & out, const vertex* v) const {
static int other(int x, int y, int z) {
SASSERT(x == z || y == z);
return x == z ? y : x;
}
std::ostream& print_vert(std::ostream& out, const vertex* v) const {
out << "(c = " << v->column() << ", parent = {";
if (v->parent())
out << "(" << v->parent()->column() << ")";
else
out << "null";
out << "} , lvl = " << v->level();
if (m_pol.contains(v->column()))
out << (pol(v) == -1? " -":" +");
out << "null";
out << "} , lvl = " << v->level();
if (m_pol.contains(v->column()))
out << (pol(v) == -1 ? " -" : " +");
else
out << " not in m_pol";
out << ')';
return out;
}
hashtable<unsigned, u_hash, u_eq> m_visited_rows;
hashtable<unsigned, u_hash, u_eq> m_visited_columns;
u_map<vertex*> m_vertices;
vertex* m_root = nullptr;
hashtable<unsigned, u_hash, u_eq> m_visited_rows;
hashtable<unsigned, u_hash, u_eq> m_visited_columns;
u_map<vertex*> m_vertices;
vertex* m_root = nullptr;
// At some point we can find a row with a single vertex non fixed vertex
// then we can fix the whole tree,
// by adjusting the vertices offsets, so they become absolute.
// If the tree is fixed then in addition to checking with the m_vals_to_verts
// we are going to check with the m_fixed_var_tables.
const vertex* m_fixed_vertex = nullptr;
explanation m_fixed_vertex_explanation;
const vertex* m_fixed_vertex = nullptr;
explanation m_fixed_vertex_explanation;
// a pair (o, j) belongs to m_vals_to_verts iff x[j] = x[m_root->column()] + o
map<mpq, const vertex*, obj_hash<mpq>, default_eq<mpq>> m_vals_to_verts;
map<mpq, const vertex*, obj_hash<mpq>, default_eq<mpq>> m_vals_to_verts;
// a pair (o, j) belongs to m_vals_to_verts_neg iff -x[j] = x[m_root->column()] + o
map<mpq, const vertex*, obj_hash<mpq>, default_eq<mpq>> m_vals_to_verts_neg;
map<mpq, const vertex*, obj_hash<mpq>, default_eq<mpq>> m_vals_to_verts_neg;
// x[m_root->column()] - m_pol[j].pol()*x[j] == const;
// to bind polarity and the vertex in the table
u_map<int> m_pol;
u_map<int> m_pol;
// if m_pos.contains(j) then x[j] = x[m_root->column()] + o
uint_set m_pos;
uint_set m_pos;
// these maps map a column index to the corresponding index in ibounds
std::unordered_map<unsigned, unsigned> m_improved_lower_bounds;
std::unordered_map<unsigned, unsigned> m_improved_upper_bounds;
T& m_imp;
vector<implied_bound> m_ibounds;
std::unordered_map<unsigned, unsigned> m_improved_lower_bounds;
std::unordered_map<unsigned, unsigned> m_improved_upper_bounds;
T& m_imp;
vector<implied_bound> m_ibounds;
map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>> m_val2fixed_row;
map<mpq, unsigned, obj_hash<mpq>, default_eq<mpq>> m_val2fixed_row;
bool is_fixed_row(unsigned r, unsigned & x) {
bool is_fixed_row(unsigned r, unsigned& x) {
x = UINT_MAX;
const auto & row = lp().get_row(r);
const auto& row = lp().get_row(r);
for (unsigned k = 0; k < row.size(); k++) {
const auto& c = row[k];
if (column_is_fixed(c.var()))
@ -130,7 +133,7 @@ class lp_bound_propagator {
}
return x != UINT_MAX;
}
void try_add_equation_with_internal_fixed_tables(unsigned r1) {
SASSERT(m_fixed_vertex);
unsigned v1, v2;
@ -154,8 +157,8 @@ class lp_bound_propagator {
TRACE("eq", print_row(tout, r1); print_row(tout, r2); tout << v1 << " == " << v2 << " = " << val(v1) << "\n");
add_eq_on_columns(ex, v1, v2, true);
}
void try_add_equation_with_lp_fixed_tables(unsigned row_index, const vertex *v) {
void try_add_equation_with_lp_fixed_tables(unsigned row_index, const vertex* v) {
SASSERT(m_fixed_vertex);
unsigned v_j = v->column();
unsigned j = null_lpvar;
@ -163,23 +166,24 @@ class lp_bound_propagator {
try_add_equation_with_internal_fixed_tables(row_index);
return;
}
TRACE("cheap_eq",
tout << "v_j = "; lp().print_column_info(v_j, tout) << std::endl;
tout << "v_j = ";
lp().print_column_info(v_j, tout) << std::endl;
tout << "v = "; print_vert(tout, v) << std::endl;
tout << "found j " << j << std::endl; lp().print_column_info(j, tout)<< std::endl;
tout << "found j " << j << std::endl; lp().print_column_info(j, tout) << std::endl;
tout << "found j = " << j << std::endl;);
vector<edge> path = connect_in_tree(v, m_fixed_vertex);
explanation ex = get_explanation_from_path(path);
ex.add_expl(m_fixed_vertex_explanation);
explain_fixed_column(j, ex);
add_eq_on_columns(ex, j, v_j, true);
add_eq_on_columns(ex, j, v_j, true);
}
void try_add_equation_with_val_table(const vertex *v) {
void try_add_equation_with_val_table(const vertex* v) {
SASSERT(m_fixed_vertex);
unsigned v_j = v->column();
const vertex *u = nullptr;
const vertex* u = nullptr;
if (!m_vals_to_verts.find(val(v_j), u)) {
m_vals_to_verts.insert(val(v_j), v);
return;
@ -187,7 +191,7 @@ class lp_bound_propagator {
unsigned j = u->column();
if (j == v_j || is_int(j) != is_int(v_j))
return;
TRACE("cheap_eq", tout << "found j=" << j << " for v=";
print_vert(tout, v) << "\n in m_vals_to_verts\n";);
vector<edge> path = connect_in_tree(u, v);
@ -198,7 +202,7 @@ class lp_bound_propagator {
static bool not_set(unsigned j) { return j == UINT_MAX; }
static bool is_set(unsigned j) { return j != UINT_MAX; }
void create_root(unsigned row_index) {
SASSERT(!m_root && !m_fixed_vertex);
unsigned x, y;
@ -210,31 +214,30 @@ class lp_bound_propagator {
}
TRACE("cheap_eq", print_row(tout, row_index););
m_root = alloc_v(x);
set_polarity(m_root, 1); // keep m_root in the positive table
set_polarity(m_root, 1); // keep m_root in the positive table
if (not_set(y)) {
set_fixed_vertex(m_root);
explain_fixed_in_row(row_index, m_fixed_vertex_explanation);
}
else {
vertex *v = add_child_with_check(row_index, y, m_root, polarity);
} else {
vertex* v = add_child_with_check(row_index, y, m_root, polarity);
if (v)
explore_under(v);
}
explore_under(m_root);
}
void explore_under(vertex * v) {
void explore_under(vertex* v) {
check_for_eq_and_add_to_val_tables(v);
go_over_vertex_column(v);
}
// In case of only one non fixed column, and the function returns true,
// this column would be represened by x.
bool is_tree_offset_row(unsigned row_index, unsigned & x, unsigned & y, int & polarity) const {
x = y = UINT_MAX;
bool is_tree_offset_row(unsigned row_index, unsigned& x, unsigned& y, int& polarity) const {
x = y = UINT_MAX;
const row_cell<mpq>* x_cell = nullptr;
const row_cell<mpq>* y_cell = nullptr;
const auto & row = lp().get_row(row_index);
const auto& row = lp().get_row(row_index);
for (unsigned k = 0; k < row.size(); k++) {
const auto& c = row[k];
if (column_is_fixed(c.var()))
@ -242,25 +245,21 @@ class lp_bound_propagator {
if (not_set(x)) {
if (c.coeff().is_one() || c.coeff().is_minus_one()) {
x = c.var();
x_cell = & c;
}
else
x_cell = &c;
} else
return false;
}
else if (not_set(y)) {
} else if (not_set(y)) {
if (c.coeff().is_one() || c.coeff().is_minus_one()) {
y = c.var();
y_cell = & c;
}
else
y_cell = &c;
} else
return false;
}
else
} else
return false;
}
if (is_set(x)) {
if (is_set(y))
polarity = x_cell->coeff().is_pos() == y_cell->coeff().is_pos()? -1 : 1;
polarity = x_cell->coeff().is_pos() == y_cell->coeff().is_pos() ? -1 : 1;
else
polarity = 1;
return true;
@ -268,18 +267,18 @@ class lp_bound_propagator {
return false;
}
void go_over_vertex_column(vertex * v) {
void go_over_vertex_column(vertex* v) {
lpvar j = v->column();
if (!check_insert(m_visited_columns, j))
return;
for (const auto & c : lp().get_column(j)) {
for (const auto& c : lp().get_column(j)) {
unsigned row_index = c.var();
if (!check_insert(m_visited_rows, row_index))
continue;
vertex* u = get_child_from_row(row_index, v);
if (u)
explore_under(u);
if (u)
explore_under(u);
}
}
@ -295,21 +294,18 @@ class lp_bound_propagator {
m_pol.reset();
m_vertices.reset();
}
struct reset_cheap_eq {
lp_bound_propagator& p;
reset_cheap_eq(lp_bound_propagator& p):p(p) {}
reset_cheap_eq(lp_bound_propagator& p) : p(p) {}
~reset_cheap_eq() { p.reset_cheap_eq_eh(); }
};
public:
lp_bound_propagator(T& imp):
m_imp(imp) {}
public:
lp_bound_propagator(T& imp) : m_imp(imp) {}
const vector<implied_bound>& ibounds() const { return m_ibounds; }
void init() {
m_improved_upper_bounds.clear();
m_improved_lower_bounds.clear();
@ -318,24 +314,24 @@ public:
const lar_solver& lp() const { return m_imp.lp(); }
lar_solver& lp() { return m_imp.lp(); }
column_type get_column_type(unsigned j) const {
return m_imp.lp().get_column_type(j);
}
const impq & get_lower_bound(unsigned j) const {
const impq& get_lower_bound(unsigned j) const {
return m_imp.lp().get_lower_bound(j);
}
const mpq & get_lower_bound_rational(unsigned j) const {
const mpq& get_lower_bound_rational(unsigned j) const {
return m_imp.lp().get_lower_bound(j).x;
}
const impq & get_upper_bound(unsigned j) const {
const impq& get_upper_bound(unsigned j) const {
return m_imp.lp().get_upper_bound(j);
}
const mpq & get_upper_bound_rational(unsigned j) const {
const mpq& get_upper_bound_rational(unsigned j) const {
return m_imp.lp().get_upper_bound(j).x;
}
@ -343,40 +339,37 @@ public:
bool column_is_fixed(lpvar j) const {
return lp().column_is_fixed(j) && get_lower_bound(j).y.is_zero();
}
void try_add_bound(mpq const& v, unsigned j, bool is_low, bool coeff_before_j_is_pos, unsigned row_or_term_index, bool strict) {
j = m_imp.lp().column_to_reported_index(j);
lconstraint_kind kind = is_low? GE : LE;
void try_add_bound(mpq const& v, unsigned j, bool is_low, bool coeff_before_j_is_pos, unsigned row_or_term_index, bool strict) {
j = m_imp.lp().column_to_reported_index(j);
lconstraint_kind kind = is_low ? GE : LE;
if (strict)
kind = static_cast<lconstraint_kind>(kind / 2);
if (!m_imp.bound_is_interesting(j, kind, v))
return;
unsigned k; // index to ibounds
unsigned k; // index to ibounds
if (is_low) {
if (try_get_value(m_improved_lower_bounds, j, k)) {
auto & found_bound = m_ibounds[k];
auto& found_bound = m_ibounds[k];
if (v > found_bound.m_bound || (v == found_bound.m_bound && !found_bound.m_strict && strict)) {
found_bound = implied_bound(v, j, is_low, coeff_before_j_is_pos, row_or_term_index, strict);
TRACE("try_add_bound", m_imp.lp().print_implied_bound(found_bound, tout););
}
}
else {
} else {
m_improved_lower_bounds[j] = m_ibounds.size();
m_ibounds.push_back(implied_bound(v, j, is_low, coeff_before_j_is_pos, row_or_term_index, strict));
TRACE("try_add_bound", m_imp.lp().print_implied_bound(m_ibounds.back(), tout););
}
}
else { // the upper bound case
} else { // the upper bound case
if (try_get_value(m_improved_upper_bounds, j, k)) {
auto & found_bound = m_ibounds[k];
auto& found_bound = m_ibounds[k];
if (v < found_bound.m_bound || (v == found_bound.m_bound && !found_bound.m_strict && strict)) {
found_bound = implied_bound(v, j, is_low, coeff_before_j_is_pos, row_or_term_index, strict);
TRACE("try_add_bound", m_imp.lp().print_implied_bound(found_bound, tout););
}
}
else {
} else {
m_improved_upper_bounds[j] = m_ibounds.size();
m_ibounds.push_back(implied_bound(v, j, is_low, coeff_before_j_is_pos, row_or_term_index, strict));
TRACE("try_add_bound", m_imp.lp().print_implied_bound(m_ibounds.back(), tout););
@ -388,18 +381,18 @@ public:
m_imp.consume(a, ci);
}
const mpq& val(unsigned j) const {
return lp().get_column_value(j).x;
const mpq& val(unsigned j) const {
return lp().get_column_value(j).x;
}
const mpq& val(const vertex* v) const {
return val(v->column());
}
bool tree_contains_r(vertex* root, vertex *v) const {
bool tree_contains_r(vertex* root, vertex* v) const {
if (*root == *v)
return true;
for (auto e : root->edges())
for (auto e : root->edges())
if (tree_contains_r(e.target(), v))
return true;
return false;
@ -415,7 +408,7 @@ public:
m_pol.insert(j, p);
}
void check_and_set_polarity(vertex* v, int polarity, unsigned row_index, vertex*v_parent) {
void check_and_set_polarity(vertex* v, int polarity, unsigned row_index, vertex* v_parent) {
int prev_pol;
if (!m_pol.find(v->column(), prev_pol)) {
set_polarity(v, polarity);
@ -429,106 +422,107 @@ public:
m_fixed_vertex_explanation = get_explanation_from_path(path);
explain_fixed_in_row(row_index, m_fixed_vertex_explanation);
set_fixed_vertex(v);
TRACE("cheap_eq",
tout << "polarity switch: " << polarity << "\nv = "; print_vert(tout , v) << "\nu = "; tout << "fixed vertex explanation\n";
for (auto p : m_fixed_vertex_explanation)
lp().constraints().display(tout, [this](lpvar j) { return lp().get_variable_name(j);}, p.ci()););
TRACE("cheap_eq",
tout << "polarity switch: " << polarity << "\nv = ";
print_vert(tout, v) << "\nu = "; tout << "fixed vertex explanation\n";
for (auto p
: m_fixed_vertex_explanation)
lp()
.constraints()
.display(
tout, [this](lpvar j) { return lp().get_variable_name(j); }, p.ci()););
}
bool tree_contains(vertex *v) const {
bool tree_contains(vertex* v) const {
if (!m_root)
return false;
return tree_contains_r(m_root, v);
}
vertex * alloc_v(unsigned column) {
vertex * v = alloc(vertex, column);
vertex* alloc_v(unsigned column) {
vertex* v = alloc(vertex, column);
m_vertices.insert(column, v);
SASSERT(!tree_contains(v));
return v;
}
unsigned column(unsigned row, unsigned index) {
return lp().get_row(row)[index].var();
}
bool fixed_phase() const { return m_fixed_vertex; }
// Returns the vertex to start exploration from, or nullptr.
// It is assumed that parent->column() is present in the row
vertex* get_child_from_row(unsigned row_index, vertex* parent) {
TRACE("cheap_eq_det", print_row(tout, row_index););
unsigned x, y; int row_polarity;
unsigned x, y;
int row_polarity;
if (!is_tree_offset_row(row_index, x, y, row_polarity)) {
TRACE("cheap_eq_det", tout << "not an offset row\n"; );
TRACE("cheap_eq_det", tout << "not an offset row\n";);
return nullptr;
}
if (not_set(y)) { // there is only one fixed variable in the row
if (not_set(y)) { // there is only one fixed variable in the row
if (!fixed_phase()) {
set_fixed_vertex(parent);
explain_fixed_in_row(row_index, m_fixed_vertex_explanation);
}
return nullptr;
}
SASSERT(is_set(x) && is_set(y));
unsigned col = other(x, y, parent->column());
return add_child_with_check(row_index, col, parent, row_polarity);
}
vertex * add_child_with_check(unsigned row_index, unsigned col, vertex* parent, int row_polarity) {
vertex* add_child_with_check(unsigned row_index, unsigned col, vertex* parent, int row_polarity) {
vertex* vy;
if (m_vertices.find(col, vy)) {
SASSERT(vy != nullptr);
if (!fixed_phase()) {
check_and_set_polarity(vy, pol(parent) * row_polarity, row_index, parent);
check_and_set_polarity(vy, pol(parent) * row_polarity, row_index, parent);
}
return nullptr; // it is not a new vertex
}
return nullptr; // it is not a new vertex
}
vy = alloc_v(col);
parent->add_child(row_index, vy);
if (!fixed_phase())
check_and_set_polarity(vy, row_polarity * pol(parent), row_index, parent);
return vy;
return vy;
}
bool is_equal(lpvar j, lpvar k) const {
bool is_equal(lpvar j, lpvar k) const {
return m_imp.is_equal(col_to_imp(j), col_to_imp(k));
}
void check_for_eq_and_add_to_val_table(vertex* v, map<mpq, const vertex*, obj_hash<mpq>, default_eq<mpq>>& table) {
void check_for_eq_and_add_to_val_table(vertex* v, map<mpq, const vertex*, obj_hash<mpq>, default_eq<mpq>>& table) {
TRACE("cheap_eq", tout << "v = "; print_vert(tout, v) << "\n";);
const vertex *k; // the other vertex
const vertex* k; // the other vertex
if (table.find(val(v), k)) {
TRACE("cheap_eq", tout << "found k " ; print_vert(tout, k) << "\n";);
TRACE("cheap_eq", tout << "found k "; print_vert(tout, k) << "\n";);
if (k->column() != v->column() &&
is_int(k->column()) == is_int(v->column()) &&
!is_equal(k->column(), v->column())) {
report_eq(k, v);
}
else {
} else {
TRACE("cheap_eq", tout << "no report\n";);
}
}
else {
} else {
TRACE("cheap_eq", tout << "registered: " << val(v) << " -> { "; print_vert(tout, v) << "} \n";);
table.insert(val(v), v);
}
}
void check_for_eq_and_add_to_val_tables(vertex* v) {
TRACE("cheap_eq_det", print_vert(tout, v) << "\n";);
if (!fixed_phase()) {
if (pol(v->column()) == -1)
check_for_eq_and_add_to_val_table(v, m_vals_to_verts_neg);
else
else
check_for_eq_and_add_to_val_table(v, m_vals_to_verts);
}
}
void clear_for_eq() {
m_visited_rows.reset();
m_visited_columns.reset();
@ -542,41 +536,40 @@ public:
std::ostream& print_path(const vector<edge>& path, std::ostream& out) const {
out << "path = \n";
for (const edge& k : path)
for (const edge& k : path)
print_edge(k, out) << "\n";
return out;
}
// we have v_i and v_j, indices of vertices at the same offsets
void report_eq(const vertex* v_i, const vertex* v_j) {
SASSERT(v_i != v_j);
SASSERT(lp().get_column_value(v_i->column()) == lp().get_column_value(v_j->column()));
TRACE("cheap_eq", tout << v_i->column() << " = " << v_j->column() << "\nu = ";
print_vert(tout, v_i) << "\nv = "; print_vert(tout, v_j) <<"\n");
print_vert(tout, v_i) << "\nv = "; print_vert(tout, v_j) << "\n");
vector<edge> path = connect_in_tree(v_i, v_j);
lp::explanation exp = get_explanation_from_path(path);
add_eq_on_columns(exp, v_i->column(), v_j->column(), false);
}
std::ostream& print_expl(std::ostream & out, const explanation& exp) const {
for (auto p : exp)
lp().constraints().display(out, [this](lpvar j) { return lp().get_variable_name(j);}, p.ci());
std::ostream& print_expl(std::ostream& out, const explanation& exp) const {
for (auto p : exp)
lp().constraints().display(
out, [this](lpvar j) { return lp().get_variable_name(j); }, p.ci());
return out;
}
bool add_eq_on_columns(const explanation& exp, lpvar j, lpvar k, bool is_fixed) {
SASSERT(j != k);
unsigned je = lp().column_to_reported_index(j);
unsigned ke = lp().column_to_reported_index(k);
TRACE("cheap_eq",
tout << "reporting eq " << j << ", " << k << "\n";
TRACE("cheap_eq",
tout << "reporting eq " << j << ", " << k << "\n";
tout << "reported idx " << je << ", " << ke << "\n";
print_expl(tout, exp);
tout << "theory_vars v" << lp().local_to_external(je) << " == v" << lp().local_to_external(ke) << "\n";
);
tout << "theory_vars v" << lp().local_to_external(je) << " == v" << lp().local_to_external(ke) << "\n";);
bool added = m_imp.add_eq(je, ke, exp, is_fixed);
if (added) {
if (is_fixed)
@ -600,75 +593,75 @@ public:
bool is_int(lpvar j) const {
return lp().column_is_int(j);
}
explanation get_explanation_from_path(vector<edge>& path) const {
explanation ex;
for (edge &e : path)
for (edge& e : path)
explain_fixed_in_row(e.row(), ex);
return ex;
}
void explain_fixed_in_row(unsigned row, explanation& ex) const {
TRACE("cheap_eq", tout << lp().get_row(row) << std::endl);
for (const auto & c : lp().get_row(row))
if (lp().is_fixed(c.var()))
for (const auto& c : lp().get_row(row))
if (lp().is_fixed(c.var()))
explain_fixed_column(c.var(), ex);
}
void explain_fixed_column(unsigned j, explanation & ex) const {
void explain_fixed_column(unsigned j, explanation& ex) const {
SASSERT(column_is_fixed(j));
constraint_index lc, uc;
constraint_index lc, uc;
lp().get_bound_constraint_witnesses_for_column(j, lc, uc);
ex.push_back(lc);
ex.push_back(uc);
ex.push_back(uc);
}
vector<edge> connect_in_tree(const vertex* u, const vertex* v) const {
vector<edge> path;
TRACE("cheap_eq_details", tout << "u = " ; print_vert(tout, u); tout << "\nv = ";print_vert(tout, v) << "\n";);
TRACE("cheap_eq_details", tout << "u = "; print_vert(tout, u); tout << "\nv = "; print_vert(tout, v) << "\n";);
vector<edge> v_branch;
// equalize the levels
while (u->level() > v->level()) {
path.push_back(u->edge_from_parent().reverse());
u = u->parent();
}
while (u->level() < v->level()) {
v_branch.push_back(v->edge_from_parent());
v = v->parent();
}
SASSERT(u->level() == v->level());
TRACE("cheap_eq_details", tout << "u = " ; print_vert(tout, u); tout << "\nv = "; print_vert(tout, v) << "\n";);
TRACE("cheap_eq_details", tout << "u = "; print_vert(tout, u); tout << "\nv = "; print_vert(tout, v) << "\n";);
while (u != v) {
path.push_back(u->edge_from_parent().reverse());
v_branch.push_back(v->edge_from_parent());
u = u->parent();
v = v->parent();
}
for (unsigned i = v_branch.size(); i--; ) {
path.push_back(v_branch[i]);
for (unsigned i = v_branch.size(); i--;) {
path.push_back(v_branch[i]);
}
TRACE("cheap_eq", print_path(path, tout););
return path;
}
bool tree_is_correct() const {
std::unordered_set<int> vs;
return tree_is_correct(m_root, vs);
}
bool tree_is_correct(vertex* v, std::unordered_set<int>& visited_verts) const {
if (fixed_phase())
return true;
if (visited_verts.find(v->column()) != visited_verts.end())
if (visited_verts.find(v->column()) != visited_verts.end())
return false;
visited_verts.insert(v->column());
for (auto e : v->edges())
for (auto e : v->edges())
if (!tree_is_correct(e.target(), visited_verts))
return false;
return true;
}
std::ostream& print_tree(std::ostream & out, vertex* v) const {
std::ostream& print_tree(std::ostream& out, vertex* v) const {
print_vert(out, v);
out << "\nchildren :\n";
for (auto c : v->edges()) {
@ -683,75 +676,74 @@ public:
try_add_equation_with_lp_fixed_tables(row_index, v);
try_add_equation_with_val_table(v);
}
void handle_fixed_phase(unsigned row_index) {
if (!fixed_phase())
return;
const vertex* v = m_root;
try_add_equation_with_fixed_tables(row_index, v);
for (auto e: v->edges())
for (auto e : v->edges())
try_add_equation_with_fixed_tables(row_index, e.target());
}
void cheap_eq_tree(unsigned row_index) {
reset_cheap_eq _reset(*this);
TRACE("cheap_eq_det", tout << "row_index = " << row_index << "\n";);
if (!check_insert(m_visited_rows, row_index))
if (!check_insert(m_visited_rows, row_index))
return;
create_root(row_index);
if (!m_root)
return;
TRACE("cheap_eq", tout << "tree = "; print_tree(tout, m_root) << "\n";);
TRACE("cheap_eq", tout << "tree = "; print_tree(tout, m_root) << "\n";);
SASSERT(tree_is_correct());
handle_fixed_phase(row_index);
TRACE("cheap_eq",
tout << "done for row_index " << row_index << "\n";
tout << "tree size = " << verts_size(););
}
std::ostream& print_row(std::ostream & out, unsigned row_index) const {
unsigned x, y; int polarity;
std::ostream& print_row(std::ostream& out, unsigned row_index) const {
unsigned x, y;
int polarity;
if (true || !is_tree_offset_row(row_index, x, y, polarity))
return lp().get_int_solver()->display_row_info(out, row_index);
bool first = true;
for (const auto &c: lp().A_r().m_rows[row_index]) {
for (const auto& c : lp().A_r().m_rows[row_index]) {
if (lp().column_is_fixed(c.var()))
continue;
if (c.coeff().is_one()) {
if (!first)
out << "+";
}
else if (c.coeff().is_minus_one())
out << "-";
if (!first)
out << "+";
} else if (c.coeff().is_minus_one())
out << "-";
out << lp().get_variable_name(c.var()) << " ";
first = false;
first = false;
}
out << "\n";
return out;
}
void set_fixed_vertex(vertex *v) {
void set_fixed_vertex(vertex* v) {
TRACE("cheap_eq", if (v) print_vert(tout, v); else tout << "set m_fixed_vertex to nullptr"; tout << "\n";);
SASSERT(!m_fixed_vertex || v == nullptr);
m_fixed_vertex = v;
}
unsigned verts_size() const {
return subtree_size(m_root);
}
unsigned subtree_size(vertex* v) const {
unsigned r = 1; // 1 for v
unsigned r = 1; // 1 for v
for (auto e : v->edges())
r += subtree_size(e.target());
return r;
}
void delete_tree(vertex * v) {
void delete_tree(vertex* v) {
for (auto p : v->edges())
delete_tree(p.target());
dealloc(v);
@ -763,7 +755,6 @@ public:
return false;
table.insert(j);
return true;
}
}
};
}
} // namespace lp

File diff suppressed because it is too large Load diff

View file

@ -59,6 +59,7 @@ template <typename T, typename X> void lp_primal_core_solver<T, X>::advance_on_e
}
unsigned j_nz = this->m_m() + 1; // this number is greater than the max column size
std::list<unsigned>::iterator entering_iter = m_non_basis_list.end();
unsigned n = 0;
for (auto non_basis_iter = m_non_basis_list.begin(); number_of_benefitial_columns_to_go_over && non_basis_iter != m_non_basis_list.end(); ++non_basis_iter) {
unsigned j = *non_basis_iter;
if (!column_is_benefitial_for_entering_basis(j))
@ -71,8 +72,9 @@ template <typename T, typename X> void lp_primal_core_solver<T, X>::advance_on_e
entering_iter = non_basis_iter;
if (number_of_benefitial_columns_to_go_over)
number_of_benefitial_columns_to_go_over--;
n = 1;
}
else if (t == j_nz && this->m_settings.random_next() % 2 == 0) {
else if (t == j_nz && this->m_settings.random_next(++n) == 0) {
entering_iter = non_basis_iter;
}
}// while (number_of_benefitial_columns_to_go_over && initial_offset_in_non_basis != offset_in_nb);
@ -166,7 +168,8 @@ template <typename T, typename X>void lp_primal_core_solver<T, X>::advance_on_en
}
this->update_basis_and_x_tableau(entering, leaving, t);
this->iters_with_no_cost_growing() = 0;
} else {
}
else {
this->pivot_column_tableau(entering, this->m_basis_heading[leaving]);
this->change_basis(entering, leaving);
}

View file

@ -20,8 +20,9 @@ Revision History:
#pragma once
#include <sstream>
// clang-format off
#include <limits.h>
#include "util/debug.h"
namespace nla {
class core;
}

View file

@ -0,0 +1,3 @@
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0

File diff suppressed because it is too large Load diff