Skip to content

Commit 65bdd58

Browse files
committed
remove struct entry
1 parent a9098a5 commit 65bdd58

File tree

1 file changed

+50
-64
lines changed

1 file changed

+50
-64
lines changed

src/math/lp/dioph_eq.cpp

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
Data structures are:
1717
-- term_o: inherits lar_term and differs from it by having a constant, while
1818
lar_term is just a sum of monomials
19-
-- entry : has a dependency lar_term, keeping the history of the entry
20-
updates, the rational constant of the corresponding term_o, and the entry
21-
status that is in {F,S, FRESH}. The entry status is used for efficiency
22-
reasons. It allows quickly check if an entry belongs to F, S, or neither.
23-
dioph_eq::imp main fields are
2419
-- lra: pointer to lar_solver.
2520
-- lia: point to int_solver.
26-
-- m_entries: it keeps all "entry" objects.
21+
-- m_sum_of_fixed: it keeps the contribution of the fixed variables to the row
2722
-- m_e_matrix: i-th row of this matrix keeps the term corresponding to
28-
m_entries[i]. The actual term corresponding to m_entry[i] is the sum of the
29-
matrix i-th row and the constant m_entry[].m_c.
23+
-- m_l_matrix: the m_l_matrix[i] produces m_e_matrix[i] by using the terms definitions of lar_solver
24+
-- m_k2s: when the variable k is substituted in the row s of m_e_matrix, the pair (k,s) is added to m_k2s.
25+
m_k2s is a one to one mapping.
26+
-- m_fresh_k2xt_terms: when a fresh definitions is created for a variable k, then the triple
27+
(k,xt,t) is added to m_fresh_k2xt_terms, where xt is the fresh variable, and xt t it the term defining the substitution: something like k - xt + 5z + 6y = 0.
28+
The set of pairs (k, xt) is a one to one mapping
29+
m_fresh_definitions[i]: is the list of all xt that were defined for row m_e_matrix[i]
30+
3031
The mapping between the columns of lar_solver and m_e_matrix is controlled by m_var_register.
3132
local_to_lar_solver(lar_solver_to_local(j)) == j. If local_to_lar_solver(j) == -1
3233
then j is a fresh variable, that is such that got introduced when normalizing a term like 3x-6y + 5z +11 = 0,
@@ -314,14 +315,8 @@ namespace lp {
314315
return out;
315316
}
316317

317-
// consider to move m_c to an m_e_matrix column
318-
struct entry {
319-
mpq m_c; // the constant of the term, the term is taken from the row of
320-
entry(const mpq & c) : m_c(c) {}
321-
};
322-
318+
std_vector<mpq> m_sum_of_fixed;
323319
var_register m_var_register;
324-
std_vector<entry> m_entries;
325320
// the terms are stored in m_A and m_c
326321
static_matrix<mpq, mpq> m_e_matrix; // the rows of the matrix are the terms,
327322
static_matrix<mpq, mpq> m_l_matrix; // the rows of the matrix are the l_terms providing the certificate to the entries modulo the constant part: look an entry_invariant that assures that the each two rows are in sync.
@@ -447,7 +442,7 @@ namespace lp {
447442
std::unordered_map<unsigned, std::unordered_set<unsigned>> m_columns_to_terms;
448443

449444

450-
unsigned m_conflict_index = -1; // m_entries[m_conflict_index] gives the conflict
445+
unsigned m_conflict_index = -1; // the row index of the conflict
451446
unsigned m_max_number_of_iterations = 100;
452447
unsigned m_number_of_iterations;
453448
struct branch {
@@ -549,12 +544,12 @@ namespace lp {
549544
};
550545

551546
void remove_last_entry() {
552-
unsigned ei = static_cast<unsigned>(m_entries.size() - 1);
547+
unsigned ei = static_cast<unsigned>(m_e_matrix.row_count() - 1);
553548

554549
if (m_k2s.has_val(ei)) {
555550
remove_from_S(ei);
556551
}
557-
m_entries.pop_back();
552+
m_sum_of_fixed.pop_back();
558553
}
559554

560555
void eliminate_last_term_column() {
@@ -633,7 +628,7 @@ namespace lp {
633628
remove_from_S(i);
634629
}
635630

636-
m_entries.pop_back();
631+
m_sum_of_fixed.pop_back();
637632
}
638633

639634

@@ -697,7 +692,7 @@ namespace lp {
697692
for (const auto& p : m_e_matrix.m_rows[i]) {
698693
t.add_monomial(p.coeff(), p.var());
699694
}
700-
t.c() = m_entries[i].m_c;
695+
t.c() = m_sum_of_fixed[i];
701696
return t;
702697
}
703698

@@ -729,10 +724,9 @@ namespace lp {
729724
// the term has form sum(a_i*x_i) - t.j() = 0,
730725
void fill_entry(const lar_term& t) {
731726
TRACE("dioph_eq", print_lar_term_L(t, tout) << std::endl;);
732-
entry te = { mpq(0)};
733-
unsigned entry_index = (unsigned) m_entries.size();
734-
m_entries.push_back(te);
735-
entry& e = m_entries.back();
727+
unsigned entry_index = (unsigned) m_e_matrix.row_count();
728+
m_sum_of_fixed.push_back(mpq(0));
729+
mpq & e = m_sum_of_fixed.back();
736730
SASSERT(m_l_matrix.row_count() == m_e_matrix.row_count());
737731
// fill m_l_matrix row
738732
m_l_matrix.add_row();
@@ -742,12 +736,12 @@ namespace lp {
742736
// fill E-entry
743737
m_e_matrix.add_row();
744738

745-
SASSERT(m_e_matrix.row_count() == m_entries.size());
739+
SASSERT(m_e_matrix.row_count() == m_e_matrix.row_count());
746740

747741
for (const auto& p : t.ext_coeffs()) {
748742
SASSERT(p.coeff().is_int());
749743
if (is_fixed(p.var()))
750-
e.m_c += p.coeff() * lia.lower_bound(p.var()).x;
744+
e += p.coeff() * lia.lower_bound(p.var()).x;
751745
else {
752746
unsigned lj = add_var(p.var());
753747
m_e_matrix.add_columns_up_to(lj);
@@ -759,7 +753,7 @@ namespace lp {
759753
SASSERT(entry_invariant(entry_index));
760754
}
761755
void subs_entry(unsigned ei) {
762-
if (ei >= m_entries.size()) return;
756+
if (ei >= m_e_matrix.row_count()) return;
763757
// q is the queue of variables that can be substituted in ei
764758
std::queue<unsigned> q;
765759
for (const auto& p: m_e_matrix.m_rows[ei]) {
@@ -839,7 +833,7 @@ namespace lp {
839833
void add_two_entries(const mpq& coeff, unsigned i0, unsigned i1 ) {
840834
m_e_matrix.add_rows(coeff, i0, i1);
841835
m_l_matrix.add_rows(coeff, i0, i1);
842-
m_entries[i1].m_c += coeff* m_entries[i0].m_c;
836+
m_sum_of_fixed[i1] += coeff* m_sum_of_fixed[i0];
843837
}
844838

845839
bool all_vars_are_int(const lar_term& term) const {
@@ -868,7 +862,7 @@ namespace lp {
868862

869863
void recalculate_entry(unsigned ei) {
870864
TRACE("dioph_eq", print_entry(ei, tout) << std::endl;);
871-
mpq &c = m_entries[ei].m_c;
865+
mpq &c = m_sum_of_fixed[ei];
872866
c = mpq(0);
873867
open_l_term_to_work_vector(ei, c);
874868
clear_e_row(ei);
@@ -1000,7 +994,7 @@ namespace lp {
1000994
SASSERT(i != k);
1001995
m_l_matrix.transpose_rows(i, k);
1002996
m_e_matrix.transpose_rows(i, k);
1003-
std::swap(m_entries[i], m_entries[k]);
997+
std::swap(m_sum_of_fixed[i], m_sum_of_fixed[k]);
1004998
m_k2s.transpose_val(i, k);
1005999

10061000
NOT_IMPLEMENTED_YET();
@@ -1024,7 +1018,7 @@ namespace lp {
10241018
}
10251019

10261020
bool entries_are_ok() {
1027-
for (unsigned ei = 0; ei < m_entries.size(); ei++) {
1021+
for (unsigned ei = 0; ei < m_e_matrix.row_count(); ei++) {
10281022
if (entry_invariant(ei) == false) {
10291023
TRACE("dioph_deb_eq", tout << "bad entry:"; print_entry(ei, tout););
10301024
return false;
@@ -1092,7 +1086,7 @@ namespace lp {
10921086
return g;
10931087
}
10941088

1095-
std::ostream& print_dep(std::ostream& out, u_dependency* dep) {
1089+
std::ostream& print_deps(std::ostream& out, u_dependency* dep) {
10961090
explanation ex(lra.flatten(dep));
10971091
return lra.print_expl(out, ex);
10981092
}
@@ -1105,7 +1099,7 @@ namespace lp {
11051099
return false;
11061100
}
11071101

1108-
void prepare_lia_branch_report(unsigned ei, const entry& e, const mpq& g,
1102+
void prepare_lia_branch_report(unsigned ei, const mpq& e, const mpq& g,
11091103
const mpq new_c) {
11101104
/* We have ep.m_e/g = 0, or sum((coff_i/g)*x_i) + new_c = 0,
11111105
or sum((coeff_i/g)*x_i) = -new_c, where new_c is not an integer
@@ -1129,20 +1123,20 @@ namespace lp {
11291123
// The function returns true if and only if there is no conflict. In the case of a conflict a branch
11301124
// can be returned as well.
11311125
bool normalize_e_by_gcd(unsigned ei) {
1132-
entry& e = m_entries[ei];
1126+
mpq & e = m_sum_of_fixed[ei];
11331127
TRACE("dioph_eq", print_entry(ei, tout) << std::endl;);
11341128
mpq g = gcd_of_coeffs(m_e_matrix.m_rows[ei]);
11351129
if (g.is_zero() || g.is_one()) {
1136-
SASSERT(g.is_one() || e.m_c.is_zero());
1130+
SASSERT(g.is_one() || e.is_zero());
11371131
return true;
11381132
}
11391133
TRACE("dioph_eq", tout << "g:" << g << std::endl;);
1140-
mpq c_g = e.m_c / g;
1134+
mpq c_g = e / g;
11411135
if (c_g.is_int()) {
11421136
for (auto& p : m_e_matrix.m_rows[ei]) {
11431137
p.coeff() /= g;
11441138
}
1145-
m_entries[ei].m_c = c_g;
1139+
m_sum_of_fixed[ei] = c_g;
11461140
// e.m_l *= (1 / g);
11471141
for (auto& p : m_l_matrix.m_rows[ei]) {
11481142
p.coeff() /= g;
@@ -1219,7 +1213,7 @@ namespace lp {
12191213
}
12201214

12211215
void subs_front_in_indexed_vector_by_S(unsigned k, std::queue<unsigned> &q) {
1222-
const entry& e = entry_for_subs(k);
1216+
const mpq& e = m_sum_of_fixed[m_k2s[k]];
12231217
TRACE("dioph_eq", tout << "k:" << k << ", in ";
12241218
print_term_o(create_term_from_ind_c(), tout) << std::endl;
12251219
tout << "subs with e:";
@@ -1248,7 +1242,7 @@ namespace lp {
12481242
can_substitute(j))
12491243
q.push(j);
12501244
}
1251-
m_c += coeff * e.m_c;
1245+
m_c += coeff * e;
12521246
add_l_row_to_term_with_index(coeff, sub_index(k));
12531247
TRACE("dioph_eq", tout << "after subs k:" << k << "\n";
12541248
print_term_o(create_term_from_ind_c(), tout) << std::endl;
@@ -1312,10 +1306,6 @@ namespace lp {
13121306
}
13131307
return ret;
13141308
}
1315-
const entry& entry_for_subs(unsigned k) const {
1316-
return m_entries[m_k2s[k]];
1317-
}
1318-
13191309

13201310
const unsigned sub_index(unsigned k) const {
13211311
return m_k2s[k];
@@ -1440,7 +1430,7 @@ namespace lp {
14401430
TRACE("dioph_eq", tout << "after process_q_with_S\nt:";
14411431
print_term_o(create_term_from_ind_c(), tout) << std::endl;
14421432
tout << "g:" << g << std::endl;
1443-
/*tout << "dep:"; print_dep(tout, m_term_with_index.m_data) << std::endl;*/);
1433+
/*tout << "dep:"; print_deps(tout, m_term_with_index.m_data) << std::endl;*/);
14441434

14451435
if (g.is_one())
14461436
return false;
@@ -1548,7 +1538,7 @@ namespace lp {
15481538
lra.mk_join(dep, lra.get_bound_constraint_witnesses_for_column(j));
15491539
TRACE("dioph_eq", tout << "jterm:";
15501540
print_lar_term_L(lra.get_term(j), tout) << "\ndep:";
1551-
print_dep(tout, dep) << std::endl;);
1541+
print_deps(tout, dep) << std::endl;);
15521542
lra.update_column_type_and_bound(j, kind, bound, dep);
15531543
lp_status st = lra.find_feasible_solution();
15541544
if ((int)st >= (int)lp::lp_status::FEASIBLE) {
@@ -1631,13 +1621,13 @@ namespace lp {
16311621
// do not change entry here
16321622
unsigned ei = m_k2s[j]; // entry index
16331623
mpq g = mpq(0); // gcd
1634-
mpq c = m_entries[ei].m_c;
1624+
mpq c = m_sum_of_fixed[ei];
16351625
for (const auto& p : m_e_matrix.m_rows[m_k2s[j]]) {
16361626
if (p.var() == j) {
16371627
const mpq & j_coeff = p.coeff();
16381628
SASSERT(j_coeff.is_one() || j_coeff.is_minus_one());
16391629
c += j_coeff * lra.get_lower_bound(local_to_lar_solver(j)).x;
1640-
TRACE("dio_br", tout << "the value of the vixed var is:" << lra.get_lower_bound(local_to_lar_solver(j)).x<<", m_entries[" << ei << "].m_c:" << m_entries[ei].m_c << ", new free coeff c:" << c << std::endl;);
1630+
TRACE("dio_br", tout << "the value of the vixed var is:" << lra.get_lower_bound(local_to_lar_solver(j)).x<<", m_sum_of_fixed[" << ei << "]:" << m_sum_of_fixed[ei] << ", new free coeff c:" << c << std::endl;);
16411631
continue;
16421632
}
16431633
if (g.is_zero()) {
@@ -2003,7 +1993,7 @@ namespace lp {
20031993
// a coefficient equal to j_sign which is +-1
20041994
void eliminate_var_in_f(unsigned ei, unsigned j, int j_sign) {
20051995
SASSERT(belongs_to_s(ei));
2006-
const auto & e = m_entries[ei];
1996+
const auto & e = m_sum_of_fixed[ei];
20071997
SASSERT(j_sign_is_correct(ei, j, j_sign));
20081998
TRACE("dioph_eq", tout << "eliminate var:" << j << " by using:";
20091999
print_entry(ei, tout) << std::endl;);
@@ -2038,9 +2028,9 @@ namespace lp {
20382028
unsigned i = c.var();
20392029
TRACE("dioph_eq", tout << "before pivot entry :";
20402030
print_entry(i, tout) << std::endl;);
2041-
m_entries[i].m_c -= j_sign * coeff * e.m_c;
2031+
m_sum_of_fixed[i] -= j_sign * coeff * e;
20422032
m_e_matrix.pivot_row_to_row_given_cell_with_sign(ei, c, j, j_sign);
2043-
//m_entries[i].m_l -= j_sign * coeff * e.m_l;
2033+
//m_sum_of_fixed[i].m_l -= j_sign * coeff * e.m_l;
20442034
m_l_matrix.add_rows( -j_sign*coeff, ei, i);
20452035
TRACE("dioph_eq", tout << "after pivoting c_row:";
20462036
print_entry(i, tout););
@@ -2264,26 +2254,22 @@ namespace lp {
22642254

22652255
std::ostream& print_entry(unsigned i, std::ostream& out,
22662256
bool print_dep = false) {
2267-
out << "m_entries[" << i << "]:";
2268-
return print_entry(i, m_entries[i], out, print_dep);
2269-
}
2270-
2271-
std::ostream& print_entry(unsigned ei, const entry& e, std::ostream& out,
2272-
bool need_print_dep = true) {
2257+
out << "m_sum_of_fixed[" << i << "]:";
22732258
out << "{\n";
2274-
print_term_o(get_term_from_entry(ei), out << "\tm_e:") << ",\n";
2259+
print_term_o(get_term_from_entry(i), out << "\tm_e:") << ",\n";
22752260
// out << "\tstatus:" << (int)e.m_entry_status;
2276-
if (need_print_dep) {
2261+
if (print_dep) {
2262+
auto l_term = l_term_from_row(i);
22772263
out << "\tm_l:{";
2278-
print_lar_term_L(l_term_from_row(ei), out) << "}, ";
2279-
print_ml(l_term_from_row(ei), out) << std::endl;
2264+
print_lar_term_L(l_term, out) << "}, ";
2265+
print_ml(l_term, out) << std::endl;
22802266
out << "expl of fixed in m_l:{\n";
2281-
print_dep(out, explain_fixed_in_meta_term(l_term_from_row(ei)));
2267+
print_deps(out, explain_fixed_in_meta_term(l_term));
22822268
out << "}\n";
22832269
}
2284-
if (belongs_to_f(ei)) { out << "in F\n"; }
2270+
if (belongs_to_f(i)) { out << "in F\n"; }
22852271
else {
2286-
unsigned j = m_k2s.get_key(ei);
2272+
unsigned j = m_k2s.get_key(i);
22872273
if (local_to_lar_solver(j) == UINT_MAX) {
22882274
out << "FRESH\n";
22892275
} else {
@@ -2319,7 +2305,7 @@ namespace lp {
23192305
for (unsigned ei=0; ei < m_e_matrix.row_count(); ei++) {
23202306
if (belongs_to_s(ei)) continue;
23212307
if (m_e_matrix.m_rows[ei].size() == 0) {
2322-
if (m_entries[ei].m_c.is_zero()) {
2308+
if (m_sum_of_fixed[ei].is_zero()) {
23232309
continue;
23242310
} else {
23252311
m_conflict_index = ei;

0 commit comments

Comments
 (0)