3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-08-20 18:20:22 +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

@ -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