Skip to content

Commit 091121c

Browse files
committed
edits
1 parent 6c38c9c commit 091121c

File tree

1 file changed

+101
-39
lines changed

1 file changed

+101
-39
lines changed

src/unate_def.cpp

Lines changed: 101 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -138,53 +138,115 @@ void Unate::synthesis_unate_def(SimplifiedCNF& cnf) {
138138
}
139139
if (conf.verb >= 3) dump_cnf<Lit>(*s, "unate_def-start.cnf", sampl_set);
140140

141-
vector<Lit> assumps;
142-
vector<Lit> cl;
141+
vector<uint32_t> indic_to_var;
142+
indic_to_var.resize(s->nVars(), var_Undef);
143+
vector<char> still_open(cnf.nVars(), 0);
144+
vector<char> counted_test(cnf.nVars(), 0);
145+
for (const auto test: to_define) {
146+
const auto ind = var_to_indic.at(test);
147+
assert(ind != var_Undef);
148+
assert(ind < indic_to_var.size());
149+
indic_to_var[ind] = test;
150+
still_open[test] = 1;
151+
}
143152
s->set_bve(false);
144153

145154
uint32_t tested_num = 0;
146155
vector<Lit> unates;
147-
for(uint32_t test: to_define) {
148-
assert(sampl_set.count(test) == 0);
149-
verb_print(3, "[unate_def] testing var: " << test+1);
150-
tested_num++;
151-
if (tested_num % 300 == 299) {
152-
verb_print(1, "[unate_def] test no: " << setw(5) << tested_num
153-
<< " confl K: " << setw(4) << s->get_sum_conflicts()/1000
154-
<< " new units: " << setw(4) << new_units
155-
<< " T: " << setprecision(2) << fixed << (cpuTime() - my_time));
156-
}
157-
for(uint32_t i = 0; i < cnf.nVars(); i++) {
158-
if (i == test) continue;
159-
if (sampl_set.count(i)) continue;
160-
if (backward_defined.count(i)) continue;
161-
auto ind = var_to_indic.at(i);
156+
auto run_unate_batch = [&](const bool test_var_sign) -> bool {
157+
vector<Lit> assumps;
158+
assumps.reserve(to_define.size());
159+
for (const auto test: to_define) {
160+
if (!still_open[test]) continue;
161+
assert(sampl_set.count(test) == 0);
162+
if (!counted_test[test]) {
163+
counted_test[test] = 1;
164+
tested_num++;
165+
if (tested_num % 300 == 299) {
166+
verb_print(1, "[unate_def] test no: " << setw(5) << tested_num
167+
<< " confl K: " << setw(4) << s->get_sum_conflicts()/1000
168+
<< " new units: " << setw(4) << new_units
169+
<< " T: " << setprecision(2) << fixed << (cpuTime() - my_time));
170+
}
171+
}
172+
verb_print(3, "[unate_def] testing var: " << test+1 << " pass: " << (test_var_sign ? 1 : 0));
173+
const auto ind = var_to_indic.at(test);
162174
assert(ind != var_Undef);
163-
// Add indicator literals as hard clauses for this test.
164-
s->add_clause({Lit(ind, false)});
175+
assumps.push_back(Lit(ind, false));
165176
}
166-
for(int flip = 0; flip < 2; flip++) {
167-
assumps.clear();
168-
assumps.push_back(Lit(test, !flip));
169-
assumps.push_back(Lit(test+cnf.nVars(), flip));
170-
171-
verb_print(3, "[unate_def] assumps : " << assumps);
172-
s->set_no_confl_needed();
173-
s->set_max_confl(conf.backw_max_confl);
174-
const auto ret = s->solve(&assumps, true);
175-
if (ret == l_False) {
176-
Lit l = {Lit(test, flip)};
177-
unates.push_back(l);
178-
cnf.add_clause({l});
179-
verb_print(2, "[unate_def] good test. Setting: " << std::setw(3) << l
180-
<< " T: " << fixed << setprecision(2) << (cpuTime() - my_time));
181-
l = Lit(test+cnf.nVars(), flip);
182-
cl = {l};
183-
s->add_clause(cl);
184-
new_units++;
185-
break;
177+
if (assumps.empty()) return true;
178+
179+
vector<uint32_t> indep_vars;
180+
vector<uint32_t> non_indep_vars;
181+
uint32_t test_var = var_Undef;
182+
uint32_t test_indic = var_Undef;
183+
const Lit seed_indic = assumps.back();
184+
assumps.pop_back();
185+
test_indic = seed_indic.var();
186+
if (test_indic >= indic_to_var.size()) {
187+
verb_print(1, "[unate_def] seeded test indicator out of bounds: " << test_indic
188+
<< " indic_to_var size: " << indic_to_var.size());
189+
return false;
190+
}
191+
test_var = indic_to_var.at(test_indic);
192+
if (test_var == var_Undef || test_var >= cnf.nVars()) {
193+
verb_print(1, "[unate_def] seeded test variable invalid: " << test_var
194+
<< " for indicator: " << test_indic);
195+
return false;
196+
}
197+
assumps.push_back(Lit(test_var, test_var_sign));
198+
assumps.push_back(Lit(test_var + cnf.nVars(), !test_var_sign));
199+
FastBackwData b;
200+
b._assumptions = &assumps;
201+
b.indic_to_var = &indic_to_var;
202+
b.orig_num_vars = cnf.nVars();
203+
b.non_indep_vars = &non_indep_vars;
204+
b.indep_vars = &indep_vars;
205+
b.fast_backw_on = true;
206+
b.test_var = &test_var;
207+
b.test_indic = &test_indic;
208+
b.max_confl = conf.backw_max_confl;
209+
b.test_var_sign = test_var_sign;
210+
211+
s->set_no_confl_needed();
212+
const auto ret = s->find_fast_backw(b);
213+
if (ret == l_False) {
214+
verb_print(1, "[unate_def] find_fast_backw hit global UNSAT unexpectedly in pass " << (test_var_sign ? 1 : 0));
215+
return false;
216+
}
217+
assert(ret == l_True);
218+
for (const auto var: non_indep_vars) {
219+
if (var >= cnf.nVars()) {
220+
verb_print(1, "[unate_def] non_indep var out of bounds: " << var
221+
<< " nVars: " << cnf.nVars());
222+
return false;
186223
}
224+
if (!still_open[var]) continue;
225+
// fast_backw assumes y with sign=test_var_sign, y' with sign=!test_var_sign
226+
// This corresponds to original unate flip = !test_var_sign.
227+
const bool flip = !test_var_sign;
228+
Lit l = Lit(var, flip);
229+
unates.push_back(l);
230+
cnf.add_clause({l});
231+
verb_print(2, "[unate_def] good test. Setting: " << std::setw(3) << l
232+
<< " T: " << fixed << setprecision(2) << (cpuTime() - my_time));
233+
s->add_clause({Lit(var + cnf.nVars(), flip)});
234+
const auto ind = var_to_indic.at(var);
235+
if (ind == var_Undef) {
236+
verb_print(1, "[unate_def] missing indicator for var: " << var);
237+
return false;
238+
}
239+
s->add_clause({Lit(ind, false)});
240+
still_open[var] = 0;
241+
new_units++;
187242
}
243+
return true;
244+
};
245+
246+
bool batch_ok = run_unate_batch(false);
247+
if (batch_ok) batch_ok = run_unate_batch(true);
248+
if (!batch_ok) {
249+
verb_print(1, "[unate_def] batched unate run aborted early due to unexpected UNSAT");
188250
}
189251

190252
cnf.add_fixed_values(unates);

0 commit comments

Comments
 (0)