Skip to content

Commit 5158797

Browse files
committed
refine trail iterations with lar.m_terms
Signed-off-by: Lev Nachmanson <[email protected]>
1 parent 9a9ccf1 commit 5158797

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

src/math/lp/dioph_eq.cpp

+37-21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
the i-th entry.
4444
*/
4545
namespace lp {
46+
template <typename T, typename K> bool contains(const T& t, K j) {
47+
return t.find(j) != t.end();
48+
}
49+
4650
class dioph_eq::imp {
4751
// This class represents a term with an added constant number c, in form sum
4852
// {x_i*a_i} + c.
@@ -280,20 +284,32 @@ namespace lp {
280284

281285
struct undo_add_term : public trail {
282286
imp& m_s;
283-
lar_term m_t;
284-
undo_add_term(imp& s, const lar_term &t): m_s(s), m_t(t) {
287+
const lar_term* m_t;
288+
undo_add_term(imp& s, const lar_term *t): m_s(s), m_t(t) {
289+
TRACE("dioph_eq", m_s.print_lar_term_L(*t, tout); tout << "t->j()=" << t->j() << std::endl;);
285290
}
286291
void undo () {
287-
TRACE("dioph_eq", m_s.lra.print_term(m_t, tout););
288-
for (const auto & p: m_t) {
292+
TRACE("dioph_eq", m_s.lra.print_term(*m_t, tout); tout << ", m_t->j() =" << m_t->j() << std::endl;);
293+
if (!contains(m_s.m_active_terms, m_t)) {
294+
for (int i = m_s.m_added_terms.size() - 1; i >= 0; --i) {
295+
if (m_s.m_added_terms[i] == m_t) // the address is the same
296+
if (i != m_s.m_added_terms.size() -1) m_s.m_added_terms[i] = m_s.m_added_terms.back();
297+
m_s.m_added_terms.pop_back();
298+
break;
299+
}
300+
return;
301+
}
302+
NOT_IMPLEMENTED_YET();
303+
for (const auto & p: m_t->ext_coeffs()) {
289304
auto it = m_s.m_columns_to_terms.find(p.var());
290-
it->second.erase(m_t.j());
305+
SASSERT(it != m_s.m_columns_to_terms.end());
306+
it->second.erase(m_t->j());
291307
if (it->second.size() == 0) {
292308
m_s.m_columns_to_terms.erase(it);
293309
}
294310

295311
}
296-
}
312+
}
297313
};
298314

299315

@@ -319,23 +335,26 @@ namespace lp {
319335
m_changed_columns.insert(j);
320336
}
321337
std_vector<const lar_term*> m_added_terms;
322-
338+
std::unordered_set<const lar_term*> m_active_terms;
323339
std_vector<variable_branch_stats> m_branch_stats;
324340
std_vector<branch> m_branch_stack;
325341
std_vector<constraint_index> m_explanation_of_branches;
326342
void add_term_delegate(const lar_term* t) {
327343
unsigned j = t->j();
328344
TRACE("dioph_eq", tout << "term column t->j():" << j << std::endl; lra.print_term(*t, tout) << std::endl; );
329-
if (!lra.column_is_int(j) || !lra.column_has_term(j)) {
330-
TRACE("dioph_eq", tout << "ignored" << std::endl;);
345+
if (!lra.column_is_int(j)) {
346+
TRACE("dioph_eq", tout << "ignored a non-integral column" << std::endl;);
331347
return;
332348
}
349+
350+
CTRACE("dioph_eq", !lra.column_has_term(j), tout << "added term that is not associated with a column yet" << std::endl;);
351+
333352
if (!all_vars_are_int(*t)) {
334353
TRACE("dioph_eq", tout << "not all vars are integrall\n";);
335354
return;
336355
}
337356
m_added_terms.push_back(t);
338-
auto undo = undo_add_term(*this, *t);
357+
auto undo = undo_add_term(*this, t);
339358
lra.trail().push(undo);
340359
}
341360

@@ -371,7 +390,7 @@ namespace lp {
371390
}
372391

373392
void register_columns_to_term(const lar_term& t) {
374-
TRACE("dioph_eq", tout << "register term:"; lra.print_term(t, tout););
393+
TRACE("dioph_eq", tout << "register term:"; lra.print_term(t, tout); tout << ", t.j()=" << t.j() << std::endl;);
375394
for (const auto &p: t.ext_coeffs()) {
376395
auto it = m_columns_to_terms.find(p.var());
377396
if (it != m_columns_to_terms.end()) {
@@ -380,8 +399,7 @@ namespace lp {
380399
else {
381400
std::unordered_set<unsigned> s;
382401
s.insert(t.j());
383-
m_columns_to_terms[p.var()] = s;
384-
TRACE("dioph_eq", tout << "insert " << p.var(););
402+
m_columns_to_terms[p.var()] = s;
385403
}
386404
}
387405
}
@@ -450,9 +468,6 @@ namespace lp {
450468
SASSERT(entry_invariant(ei));
451469
}
452470

453-
template <typename T> bool contains(const T& t, unsigned j) {
454-
return t.find(j) != t.end();
455-
}
456471

457472
void process_changed_columns() {
458473
for (unsigned j : m_changed_columns) {
@@ -482,11 +497,6 @@ namespace lp {
482497
}
483498
}
484499

485-
for (const lar_term* t : m_added_terms) {
486-
register_columns_to_term(*t);
487-
}
488-
489-
SASSERT(is_in_sync());
490500
TRACE("dioph_eq", tout << "entries_to_recalculate:"; for (unsigned j : entries_to_recalculate) {tout << " " << j;});
491501
for (unsigned j = 0; j < m_fresh_definitions.size(); j++) {
492502
const fresh_definition& fd = m_fresh_definitions[j];
@@ -558,8 +568,13 @@ namespace lp {
558568
m_lra_level = 0;
559569
process_changed_columns();
560570
for (const lar_term* t: m_added_terms) {
571+
m_active_terms.insert(t);
561572
fill_entry(*t);
573+
register_columns_to_term(*t);
562574
}
575+
576+
SASSERT(is_in_sync());
577+
563578
m_added_terms.clear();
564579
SASSERT(entries_are_ok());
565580
}
@@ -1298,6 +1313,7 @@ namespace lp {
12981313
for (unsigned k = 0; k < lra.terms().size(); k ++ ) {
12991314
const lar_term* t = lra.terms()[k];
13001315
if (!all_vars_are_int(*t)) continue;
1316+
SASSERT(t->j() != UINT_MAX);
13011317
for (const auto& p: (*t).ext_coeffs()) {
13021318
unsigned j = p.var();
13031319
auto it = c2t.find(j);

src/math/lp/lar_term.h

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class lar_term {
104104
for (auto const& p : a) {
105105
add_monomial(p.coeff(), p.var());
106106
}
107+
m_j = a.j();
107108
}
108109

109110
lar_term(const vector<std::pair<mpq, unsigned>>& coeffs) {

0 commit comments

Comments
 (0)