Skip to content

Commit 083926c

Browse files
committed
debug dio
Signed-off-by: Lev Nachmanson <levnach@hotmail.com>
1 parent a0ece6d commit 083926c

File tree

3 files changed

+146
-60
lines changed

3 files changed

+146
-60
lines changed

src/math/lp/dioph_eq.cpp

+142-58
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace lp {
5656
ret.add_monomial(p.coeff(), p.j());
5757
}
5858
ret.c() = c();
59-
ret.j() = j();
59+
ret.set_j(j());
6060
return ret;
6161
}
6262
term_o(const lar_term& t) : lar_term(t), m_c(0) {
@@ -233,7 +233,12 @@ namespace lp {
233233
// {coeff*lar.get_term(k)})
234234

235235
std_vector<unsigned> m_k2s;
236-
std_vector<unsigned> m_fresh_definitions;
236+
struct fresh_definition {
237+
unsigned m_ei;
238+
unsigned m_origin;
239+
fresh_definition(unsigned i, unsigned o) : m_ei(i), m_origin(o) {}
240+
};
241+
std_vector<fresh_definition> m_fresh_definitions;
237242

238243
unsigned m_conflict_index = -1; // m_entries[m_conflict_index] gives the conflict
239244
unsigned m_max_number_of_iterations = 100;
@@ -281,9 +286,14 @@ namespace lp {
281286
undo_add_term(imp& s, const lar_term &t): m_s(s), m_t(t) {
282287
}
283288
void undo () {
289+
TRACE("dioph_eq", m_s.lra.print_term(m_t, tout););
284290
for (const auto & p: m_t) {
285291
auto it = m_s.m_columns_to_terms.find(p.var());
286292
it->second.erase(m_t.j());
293+
if (it->second.size() == 0) {
294+
m_s.m_columns_to_terms.erase(it);
295+
}
296+
287297
}
288298
}
289299
};
@@ -303,7 +313,7 @@ namespace lp {
303313
};
304314

305315
std::unordered_set<unsigned> m_changed_columns;
306-
// m_column_to_terms[j] is the set of k such lra.get_term(k) depends on j
316+
// m_column_to_terms[j] is the set of all k such lra.get_term(k) depends on j
307317
std::unordered_map<unsigned, std::unordered_set<unsigned>> m_columns_to_terms;
308318
friend undo_fixed_column;
309319
void add_changed_column(unsigned j) {
@@ -326,7 +336,6 @@ namespace lp {
326336
TRACE("dioph_eq", tout << "not all vars are integrall\n";);
327337
return;
328338
}
329-
TRACE("dioph_eq", tout << "inserted" << std::endl;);
330339
m_added_terms.push_back(t);
331340
auto undo = undo_add_term(*this, *t);
332341
lra.trail().push(undo);
@@ -364,6 +373,7 @@ namespace lp {
364373
}
365374

366375
void register_columns_to_term(const lar_term& t) {
376+
TRACE("dioph_eq", tout << "register term:"; lra.print_term(t, tout););
367377
for (const auto &p: t) {
368378
auto it = m_columns_to_terms.find(p.var());
369379
if (it != m_columns_to_terms.end()) {
@@ -373,12 +383,12 @@ namespace lp {
373383
std::unordered_set<unsigned> s;
374384
s.insert(t.j());
375385
m_columns_to_terms[p.var()] = s;
386+
TRACE("dioph_eq", tout << "insert " << p.var(););
376387
}
377388
}
378389
}
379390
// the term has form sum(a_i*x_i) - t.j() = 0,
380-
void fill_entry(const lar_term& t) {
381-
register_columns_to_term(t);
391+
void fill_entry(const lar_term& t) {
382392
TRACE("dioph_eq", print_lar_term_L(t, tout) << std::endl;);
383393
entry te = { mpq(0), entry_status::F};
384394
unsigned entry_index = (unsigned) m_entries.size();
@@ -462,45 +472,54 @@ namespace lp {
462472
std::unordered_set<unsigned> entries_to_recalculate;
463473
std::unordered_set<unsigned> changed_terms; // a term is signified by the term column, like j in lra.get_term(j)
464474
std_vector<unsigned> fresh_entries_to_remove;
465-
for (unsigned j = 0; j < m_fresh_definitions.size(); j++) {
466-
unsigned k = m_fresh_definitions[j];
467-
if (k == UINT_MAX) continue;
468-
for (const auto & p: m_e_matrix.m_rows[k]) {
469-
if (contains(m_changed_columns, p.var())) {
470-
fresh_entries_to_remove.push_back(k);
471-
continue;
475+
476+
for (unsigned j : m_changed_columns) {
477+
const auto it = m_columns_to_terms.find(j);
478+
if (it != m_columns_to_terms.end())
479+
for (unsigned k : it->second) {
480+
changed_terms.insert(k);
472481
}
473-
474-
}
475-
}
476-
TRACE("dioph_eq", tout << "found " << fresh_entries_to_remove.size() << " fresh entries to remove\n";
477-
tout << "m_changed_columns:\n";
478-
for (unsigned j : m_changed_columns) {
479-
tout << j << " ";
480-
}
481-
tout << std::endl;
482-
);
483-
if (fresh_entries_to_remove.size()) {
484-
NOT_IMPLEMENTED_YET();
485-
}
486-
for (unsigned j : m_changed_columns) {
487-
for (unsigned k : m_columns_to_terms[j]) {
488-
changed_terms.insert(k);
489-
}
482+
if (!m_var_register.external_is_used(j))
483+
continue;
490484
for (const auto& p : m_e_matrix.column(this->lar_solver_to_local(j))) {
491-
TRACE("dioph_eq", tout << "insert into entries_to_recalculate " << p.var() << " for changed_column j=" << j<< std::endl;
492-
tout << "p.coeff:" << p.coeff() << std::endl;
493-
);
494485
entries_to_recalculate.insert(p.var());
495486
}
496487
}
497488
for (unsigned j : changed_terms) {
498489
for (const auto & cs: m_l_matrix.column(j)) {
499-
TRACE("dioph_eq", tout << "insert into entries_to_recalculate " << cs.var() << " for changed_term j=" << j<< std::endl;);
500490
entries_to_recalculate.insert(cs.var());
501491
}
502492
}
503-
for(unsigned k : entries_to_recalculate) {
493+
494+
for (const lar_term* t : m_added_terms) {
495+
register_columns_to_term(*t);
496+
}
497+
498+
SASSERT(is_in_sync());
499+
TRACE("dioph_eq", tout << "entries_to_recalculate:"; for (unsigned j : entries_to_recalculate) {tout << " " << j;});
500+
for (unsigned j = 0; j < m_fresh_definitions.size(); j++) {
501+
const fresh_definition& fd = m_fresh_definitions[j];
502+
if (fd.m_ei == UINT_MAX) continue;
503+
if (contains(entries_to_recalculate, fd.m_origin)) {
504+
fresh_entries_to_remove.push_back(j);
505+
continue;
506+
}
507+
}
508+
509+
TRACE("dioph_eq", tout << "found " << fresh_entries_to_remove.size() << " fresh entries to remove\n";
510+
tout << "m_changed_columns:\n";
511+
std::vector<unsigned> v(m_changed_columns.begin(), m_changed_columns.end());
512+
std::sort(v.begin(), v.end());
513+
for (unsigned j : v) {
514+
tout << j << " ";
515+
}
516+
tout << std::endl;
517+
);
518+
if (fresh_entries_to_remove.size()) {
519+
NOT_IMPLEMENTED_YET();
520+
}
521+
522+
for(unsigned k : entries_to_recalculate) {
504523
recalculate_entry(k);
505524
}
506525
move_recalculated_to_F(entries_to_recalculate);
@@ -528,6 +547,16 @@ namespace lp {
528547
}
529548
}
530549

550+
bool entries_are_ok() {
551+
for (unsigned ei = 0; ei < m_entries.size(); ei++) {
552+
if (entry_invariant(ei) == false) {
553+
TRACE("dioph_eq", tout << "bad entry:"; print_entry(ei, tout););
554+
return false;
555+
}
556+
}
557+
return true;
558+
}
559+
531560
void init() {
532561
m_report_branch = false;
533562
m_conflict_index = -1;
@@ -541,18 +570,8 @@ namespace lp {
541570
fill_entry(*t);
542571
}
543572
m_added_terms.clear();
544-
/*
545-
for (unsigned j = 0; j < lra.column_count(); j++) {
546-
if (!lra.column_is_int(j) || !lra.column_has_term(j))
547-
continue;
548-
const lar_term& t = lra.get_term(j);
549-
if (!all_vars_are_int(t)) {
550-
TRACE("dioph_eq", tout << "not all vars are integrall\n";);
551-
continue;
552-
}
553-
fill_entry(t);
554-
}*/
555-
}
573+
SASSERT(entries_are_ok());
574+
}
556575

557576
template <typename K>
558577
mpq gcd_of_coeffs(const K& k) {
@@ -573,10 +592,6 @@ namespace lp {
573592
return lra.print_expl(out, ex);
574593
}
575594

576-
std::string var_str(unsigned j) {
577-
return std::string(is_fresh_var(j) ? "~" : "") + std::to_string(j);
578-
}
579-
580595
bool has_fresh_var(unsigned row_index) const {
581596
for (const auto& p : m_e_matrix.m_rows[row_index]) {
582597
if (is_fresh_var(p.var()))
@@ -671,6 +686,7 @@ namespace lp {
671686
if (m_indexed_work_vector[k].is_zero())
672687
return;
673688
const entry& e = entry_for_subs(k);
689+
SASSERT(e.m_entry_status == entry_status::S);
674690
TRACE("dioph_eq", tout << "k:" << k << ", in ";
675691
print_term_o(create_term_from_ind_c(), tout) << std::endl;
676692
tout << "subs with e:";
@@ -854,7 +870,10 @@ namespace lp {
854870
tout << "term_to_tighten + open_ml:";
855871
print_term_o(term_to_tighten + open_ml(m_tmp_l), tout)
856872
<< std::endl;
857-
);
873+
tout << "ls:"; print_term_o(fix_vars(term_to_tighten + open_ml(m_tmp_l)),tout) << std::endl;
874+
tout << "rs:"; print_term_o(term_to_lar_solver(remove_fresh_vars(create_term_from_ind_c())), tout ) << std::endl;
875+
876+
);
858877
SASSERT(
859878
fix_vars(term_to_tighten + open_ml(m_tmp_l)) ==
860879
term_to_lar_solver(remove_fresh_vars(create_term_from_ind_c())));
@@ -1283,6 +1302,69 @@ namespace lp {
12831302
return br;
12841303
}
12851304

1305+
bool columns_to_terms_is_correct() const {
1306+
std::unordered_map<unsigned, std::unordered_set<unsigned>> c2t;
1307+
for (unsigned k = 0; k < lra.terms().size(); k ++ ) {
1308+
const lar_term* t = lra.terms()[k];
1309+
if (!all_vars_are_int(*t)) continue;
1310+
for (const auto& p: *t) {
1311+
unsigned j = p.var();
1312+
auto it = c2t.find(j);
1313+
if (it == c2t.end()) {
1314+
std::unordered_set<unsigned> s;
1315+
s.insert(t->j());
1316+
c2t[j] = s;
1317+
} else {
1318+
it->second.insert(t->j());
1319+
}
1320+
1321+
}
1322+
}
1323+
for (const auto & p : c2t) {
1324+
unsigned j = p.first;
1325+
const auto it = m_columns_to_terms.find(j);
1326+
if (it == m_columns_to_terms.end()) {
1327+
TRACE("dioph_eq", tout << "column j" << j << " is not registered" << std::endl;
1328+
tout << "the column belongs to the the following terms:";
1329+
for (unsigned tj : p.second) {
1330+
tout << " " << tj;
1331+
}
1332+
tout << std::endl;
1333+
);
1334+
1335+
return false;
1336+
}
1337+
if (it->second != p.second) {
1338+
return false;
1339+
}
1340+
}
1341+
// reverse inclusion
1342+
for (const auto & p : m_columns_to_terms) {
1343+
unsigned j = p.first;
1344+
const auto it = c2t.find(j);
1345+
if (it == c2t.end()) {
1346+
TRACE("dioph_eq", tout << "should not be registered j " << j << std::endl;
1347+
lra.print_terms(tout););
1348+
return false;
1349+
}
1350+
if (it->second != p.second) {
1351+
return false;
1352+
}
1353+
}
1354+
return true;
1355+
}
1356+
bool is_in_sync() const {
1357+
unsigned n_local_columns = m_e_matrix.column_count();
1358+
for (unsigned j = 0; j < n_local_columns; j++) {
1359+
unsigned external_j = m_var_register.local_to_external(j);
1360+
if (external_j == UINT_MAX) continue;
1361+
if (external_j >= lra.column_count())
1362+
return false;
1363+
}
1364+
1365+
return columns_to_terms_is_correct();
1366+
}
1367+
12861368
public:
12871369
lia_move check() {
12881370
lra.stats().m_dio_calls++;
@@ -1443,9 +1525,7 @@ namespace lp {
14431525
}
14441526
while (!q.empty()) {
14451527
unsigned xt = pop_front(q);
1446-
term_o fresh_t = get_term_from_entry(m_fresh_definitions[xt]);
1447-
if (fresh_t.get_coeff(xt).is_minus_one() == false)
1448-
std::cout << "coeff:" << fresh_t.get_coeff(xt) << std::endl;
1528+
term_o fresh_t = get_term_from_entry(m_fresh_definitions[xt].m_ei);
14491529
SASSERT(fresh_t.get_coeff(xt).is_minus_one());
14501530
fresh_t.erase_var(xt);
14511531
if (!t.contains(xt))
@@ -1551,8 +1631,10 @@ namespace lp {
15511631

15521632
m_k2s.resize(k + 1, -1);
15531633
m_k2s[k] = fresh_row;
1554-
m_fresh_definitions.resize(xt + 1, -1);
1555-
m_fresh_definitions[xt] = fresh_row;
1634+
fresh_definition fd(-1, -1);
1635+
1636+
m_fresh_definitions.resize(xt + 1, fd);
1637+
m_fresh_definitions[xt] = fresh_definition(fresh_row, h);
15561638
TRACE("dioph_eq", tout << "changed entry:";
15571639
print_entry(h, tout) << std::endl;
15581640
tout << "added entry for fresh var:\n";
@@ -1576,8 +1658,10 @@ namespace lp {
15761658
if (need_print_dep) {
15771659
out << "\tm_l:{";
15781660
print_lar_term_L(l_term_from_row(ei), out) << "}, ";
1579-
print_ml(l_term_from_row(ei), out << "\n\texpl of fixed in m_l:\n") << "\n";
1661+
print_ml(l_term_from_row(ei), out) << std::endl;
1662+
out << "expl of fixed in m_l:{\n";
15801663
print_dep(out, explain_fixed_in_meta_term(l_term_from_row(ei)));
1664+
out << "}\n";
15811665
}
15821666
switch (e.m_entry_status) {
15831667
case entry_status::F:

src/math/lp/lar_solver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ namespace lp {
17141714
unsigned j = A_r().column_count();
17151715
SASSERT(ext_index == null_lpvar || external_to_local(ext_index) == j);
17161716
column ul(term);
1717-
term->j() = j; // point from the term to the column
1717+
term->set_j(j); // point from the term to the column
17181718
m_columns.push_back(ul);
17191719
m_trail.push(undo_add_column(*this));
17201720
add_basic_var_to_core_fields();

src/math/lp/lar_term.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class lar_term {
3232
public:
3333
// the column index related to the term
3434
lpvar j() const { return m_j; }
35-
lpvar& j() { return m_j; }
35+
void set_j(unsigned j) {
36+
m_j = j;
37+
}
3638
void add_monomial(const mpq& c, unsigned j) {
3739
if (c.is_zero())
3840
return;

0 commit comments

Comments
 (0)