ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
Glucose.cpp
Go to the documentation of this file.
1/***************************************************************************************[Solver.cc]
2 Glucose -- Copyright (c) 2013, Gilles Audemard, Laurent Simon
3 CRIL - Univ. Artois, France
4 LRI - Univ. Paris Sud, France
5
6Glucose sources are based on MiniSat (see below MiniSat copyrights). Permissions and copyrights of
7Glucose are exactly the same as Minisat on which it is based on. (see below).
8
9---------------
10
11Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
12Copyright (c) 2007-2010, Niklas Sorensson
13
14Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
15associated documentation files (the "Software"), to deal in the Software without restriction,
16including without limitation the rights to use, copy, modify, merge, publish, distribute,
17sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19
20The above copyright notice and this permission notice shall be included in all copies or
21substantial portions of the Software.
22
23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
24NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
27OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28**************************************************************************************************/
29
30#include <math.h>
31
32#include "sat/glucose/Sort.h"
33#include "sat/glucose/Solver.h"
35#include "sat/glucose/System.h"
36
38
39using namespace Gluco;
40
41//=================================================================================================
42// Options:
43
44static const char* _cat = "CORE";
45static const char* _cr = "CORE -- RESTART";
46static const char* _cred = "CORE -- REDUCE";
47static const char* _cm = "CORE -- MINIMIZE";
48static const char* _certified = "CORE -- CERTIFIED UNSAT";
49
50
51
52
53static BoolOption opt_incremental (_cat,"incremental", "Use incremental SAT solving", false);
54static DoubleOption opt_K (_cr, "K", "The constant used to force restart", 0.8, DoubleRange(0, false, 1, false));
55static DoubleOption opt_R (_cr, "R", "The constant used to block restart", 1.4, DoubleRange(1, false, 5, false));
56static IntOption opt_size_lbd_queue (_cr, "szLBDQueue", "The size of moving average for LBD (restarts)", 50, IntRange(10, INT32_MAX));
57static IntOption opt_size_trail_queue (_cr, "szTrailQueue", "The size of moving average for trail (block restarts)", 5000, IntRange(10, INT32_MAX));
58
59static IntOption opt_first_reduce_db (_cred, "firstReduceDB", "The number of conflicts before the first reduce DB", 2000, IntRange(0, INT32_MAX));
60static IntOption opt_inc_reduce_db (_cred, "incReduceDB", "Increment for reduce DB", 300, IntRange(0, INT32_MAX));
61static IntOption opt_spec_inc_reduce_db (_cred, "specialIncReduceDB", "Special increment for reduce DB", 1000, IntRange(0, INT32_MAX));
62static IntOption opt_lb_lbd_frozen_clause (_cred, "minLBDFrozenClause", "Protect clauses if their LBD decrease and is lower than (for one turn)", 30, IntRange(0, INT32_MAX));
63
64static IntOption opt_lb_size_minimzing_clause (_cm, "minSizeMinimizingClause", "The min size required to minimize clause", 30, IntRange(3, INT32_MAX));
65static IntOption opt_lb_lbd_minimzing_clause (_cm, "minLBDMinimizingClause", "The min LBD required to minimize clause", 6, IntRange(3, INT32_MAX));
66
67
68static DoubleOption opt_var_decay (_cat, "var-decay", "The variable activity decay factor", 0.8, DoubleRange(0, false, 1, false));
69static DoubleOption opt_clause_decay (_cat, "cla-decay", "The clause activity decay factor", 0.999, DoubleRange(0, false, 1, false));
70static DoubleOption opt_random_var_freq (_cat, "rnd-freq", "The frequency with which the decision heuristic tries to choose a random variable", 0, DoubleRange(0, true, 1, true));
71static DoubleOption opt_random_seed (_cat, "rnd-seed", "Used by the random variable selection", 91648253, DoubleRange(0, false, HUGE_VAL, false));
72static IntOption opt_ccmin_mode (_cat, "ccmin-mode", "Controls conflict clause minimization (0=none, 1=basic, 2=deep)", 2, IntRange(0, 2));
73static IntOption opt_phase_saving (_cat, "phase-saving", "Controls the level of phase saving (0=none, 1=limited, 2=full)", 2, IntRange(0, 2));
74static BoolOption opt_rnd_init_act (_cat, "rnd-init", "Randomize the initial activity", false);
75/*
76static IntOption opt_restart_first (_cat, "rfirst", "The base restart interval", 100, IntRange(1, INT32_MAX));
77static DoubleOption opt_restart_inc (_cat, "rinc", "Restart interval increase factor", 2, DoubleRange(1, false, HUGE_VAL, false));
78*/
79static DoubleOption opt_garbage_frac (_cat, "gc-frac", "The fraction of wasted memory allowed before a garbage collection is triggered", 0.20, DoubleRange(0, false, HUGE_VAL, false));
80
81
82BoolOption opt_certified_ (_certified, "certified", "Certified UNSAT using DRUP format", false );
83StringOption opt_certified_file_ (_certified, "certified-output", "Certified UNSAT output file", "NULL");
84
85
86//=================================================================================================
87// Constructor/Destructor:
88
89
91
92 // Parameters (user settable):
93 //
94 SolverType(0)
95 , pCnfFunc(NULL)
96 , nCallConfl(1000)
98 , pstop(NULL)
99 , nRuntimeLimit(0)
100
101 , verbosity (0)
102 , verbEveryConflicts(10000)
103 , showModel (0)
104 , K (opt_K)
105 , R (opt_R)
106 , sizeLBDQueue (opt_size_lbd_queue)
107 , sizeTrailQueue (opt_size_trail_queue)
108 , firstReduceDB (opt_first_reduce_db)
109 , incReduceDB (opt_inc_reduce_db)
110 , specialIncReduceDB (opt_spec_inc_reduce_db)
111 , lbLBDFrozenClause (opt_lb_lbd_frozen_clause)
112 , lbSizeMinimizingClause (opt_lb_size_minimzing_clause)
113 , lbLBDMinimizingClause (opt_lb_lbd_minimzing_clause)
114 , var_decay (opt_var_decay)
115 , clause_decay (opt_clause_decay)
116 , random_var_freq (opt_random_var_freq)
117 , random_seed (opt_random_seed)
118 , ccmin_mode (opt_ccmin_mode)
119 , phase_saving (opt_phase_saving)
120 , rnd_pol (false)
121 , rnd_init_act (opt_rnd_init_act)
122 , garbage_frac (opt_garbage_frac)
123 , certifiedOutput (NULL)
125 // Statistics: (formerly in 'SolverStats')
126 //
130 , curRestart(1)
131
132 , ok (true)
133 , cla_inc (1)
134 , var_inc (1)
137 , qhead (0)
138 , simpDB_assigns (-1)
139 , simpDB_props (0)
143
144 // Resource constraints:
145 //
146 , conflict_budget (-1)
147 , propagation_budget (-1)
149 , incremental(opt_incremental)
151{
152 MYFLAG=0;
153 // Initialize only first time. Useful for incremental solving, useless otherwise
154 lbdQueue.initSize(sizeLBDQueue);
155 trailQueue.initSize(sizeTrailQueue);
156 sumLBD = 0;
160
161
162 if(certifiedUNSAT) {
163 if(!strcmp(opt_certified_file_,"NULL")) {
164 certifiedOutput = fopen("/dev/stdout", "wb");
165 } else {
166 certifiedOutput = fopen(opt_certified_file_, "wb");
167 }
168 // fprintf(certifiedOutput,"o proof DRUP\n");
169 }
170}
171
172
174{
175}
176
177
178/****************************************************************
179 Set the incremental mode
180****************************************************************/
181
182// This function set the incremental mode to true.
183// You can add special code for this mode here.
184
186 incremental = true;
187}
188
189// Number of variables without selectors
193
194
195//=================================================================================================
196// Minor methods:
197
198
199// Creates a new SAT variable in the solver. If 'decision' is cleared, variable will not be
200// used as a decision variable (NOTE! This has effects on the meaning of a SATISFIABLE result).
201//
202Var Solver::newVar(bool sign, bool dvar)
203{
204 int v = nVars();
205 watches .init(mkLit(v, false));
206 watches .init(mkLit(v, true ));
207 watchesBin .init(mkLit(v, false));
208 watchesBin .init(mkLit(v, true ));
209 assigns .push(l_Undef);
210 vardata .push(mkVarData(CRef_Undef, 0));
211 //activity .push(0);
212 activity .push(rnd_init_act ? drand(random_seed) * 0.00001 : 0);
213 seen .push(0);
214 permDiff .push(0);
215 polarity .push(sign);
216 decision .push();
217 trail .capacity(v+1);
218 setDecisionVar(v, dvar);
219 return v;
220}
221
222
223
225{
226 assert(decisionLevel() == 0);
227 if (!ok) return false;
228
229 if ( 0 ) {
230 for ( int i = 0; i < ps.size(); i++ )
231 printf( "%s%d ", (toInt(ps[i]) & 1) ? "-":"", toInt(ps[i]) >> 1 );
232 printf( "\n" );
233 }
234
235 // Check if clause is satisfied and remove false/duplicate literals:
236 sort(ps);
237
238 vec<Lit> oc;
239 oc.clear();
240
241 Lit p; int i, j, flag = 0;
242 if(certifiedUNSAT) {
243 for (i = j = 0, p = lit_Undef; i < ps.size(); i++) {
244 oc.push(ps[i]);
245 if (value(ps[i]) == l_True || ps[i] == ~p || value(ps[i]) == l_False)
246 flag = 1;
247 }
248 }
249
250 for (i = j = 0, p = lit_Undef; i < ps.size(); i++)
251 if (value(ps[i]) == l_True || ps[i] == ~p)
252 return true;
253 else if (value(ps[i]) != l_False && ps[i] != p)
254 ps[j++] = p = ps[i];
255 ps.shrink(i - j);
256
257 if ( 0 ) {
258 for ( int i = 0; i < ps.size(); i++ )
259 printf( "%s%d ", (toInt(ps[i]) & 1) ? "-":"", toInt(ps[i]) >> 1 );
260 printf( "\n" );
261 }
262
263 if (flag && (certifiedUNSAT)) {
264 for (i = j = 0, p = lit_Undef; i < ps.size(); i++)
265 fprintf(certifiedOutput, "%i ", (var(ps[i]) + 1) * (-2 * sign(ps[i]) + 1));
266 fprintf(certifiedOutput, "0\n");
267
268 fprintf(certifiedOutput, "d ");
269 for (i = j = 0, p = lit_Undef; i < oc.size(); i++)
270 fprintf(certifiedOutput, "%i ", (var(oc[i]) + 1) * (-2 * sign(oc[i]) + 1));
271 fprintf(certifiedOutput, "0\n");
272 }
273
274 if (ps.size() == 0)
275 return ok = false;
276 else if (ps.size() == 1){
277 uncheckedEnqueue(ps[0]);
278 return ok = (propagate() == CRef_Undef);
279 }else{
280 CRef cr = ca.alloc(ps, false);
281 clauses.push(cr);
282 attachClause(cr);
283 }
284
285 return true;
286}
287
288
290 const Clause& c = ca[cr];
291
292 assert(c.size() > 1);
293 if(c.size()==2) {
294 watchesBin[~c[0]].push(Watcher(cr, c[1]));
295 watchesBin[~c[1]].push(Watcher(cr, c[0]));
296 } else {
297 watches[~c[0]].push(Watcher(cr, c[1]));
298 watches[~c[1]].push(Watcher(cr, c[0]));
299 }
300 if (c.learnt()) learnts_literals += c.size();
301 else clauses_literals += c.size(); }
302
303
304
305
306void Solver::detachClause(CRef cr, bool strict) {
307 const Clause& c = ca[cr];
308
309 assert(c.size() > 1);
310 if(c.size()==2) {
311 if (strict){
312 remove(watchesBin[~c[0]], Watcher(cr, c[1]));
313 remove(watchesBin[~c[1]], Watcher(cr, c[0]));
314 }else{
315 // Lazy detaching: (NOTE! Must clean all watcher lists before garbage collecting this clause)
316 watchesBin.smudge(~c[0]);
317 watchesBin.smudge(~c[1]);
318 }
319 } else {
320 if (strict){
321 remove(watches[~c[0]], Watcher(cr, c[1]));
322 remove(watches[~c[1]], Watcher(cr, c[0]));
323 }else{
324 // Lazy detaching: (NOTE! Must clean all watcher lists before garbage collecting this clause)
325 watches.smudge(~c[0]);
326 watches.smudge(~c[1]);
327 }
328 }
329 if (c.learnt()) learnts_literals -= c.size();
330 else clauses_literals -= c.size(); }
331
332
334
335 Clause& c = ca[cr];
336
337 if (certifiedUNSAT) {
338 fprintf(certifiedOutput, "d ");
339 for (int i = 0; i < c.size(); i++)
340 fprintf(certifiedOutput, "%i ", (var(c[i]) + 1) * (-2 * sign(c[i]) + 1));
341 fprintf(certifiedOutput, "0\n");
342 }
343
344 detachClause(cr);
345 // Don't leave pointers to free'd memory!
346 if (locked(c)) vardata[var(c[0])].reason = CRef_Undef;
347 c.mark(1);
348 ca.free_(cr);
349}
350
351
352bool Solver::satisfied(const Clause& c) const {
353 if(incremental) // Check clauses with many selectors is too time consuming
354 return (value(c[0]) == l_True) || (value(c[1]) == l_True);
355
356 // Default mode.
357 for (int i = 0; i < c.size(); i++)
358 if (value(c[i]) == l_True)
359 return true;
360 return false;
361}
362
363/************************************************************
364 * Compute LBD functions
365 *************************************************************/
366
367inline unsigned int Solver::computeLBD(const vec<Lit> & lits,int end) {
368 int nblevels = 0;
369 MYFLAG++;
370
371 if(incremental) { // ----------------- INCREMENTAL MODE
372 if(end==-1) end = lits.size();
373 unsigned int nbDone = 0;
374 for(int i=0;i<lits.size();i++) {
375 if(nbDone>=end) break;
376 if(isSelector(var(lits[i]))) continue;
377 nbDone++;
378 int l = level(var(lits[i]));
379 if (permDiff[l] != MYFLAG) {
380 permDiff[l] = MYFLAG;
381 nblevels++;
382 }
383 }
384 } else { // -------- DEFAULT MODE. NOT A LOT OF DIFFERENCES... BUT EASIER TO READ
385 for(int i=0;i<lits.size();i++) {
386 int l = level(var(lits[i]));
387 if (permDiff[l] != MYFLAG) {
388 permDiff[l] = MYFLAG;
389 nblevels++;
390 }
391 }
392 }
393
394 return nblevels;
395}
396
397inline unsigned int Solver::computeLBD(const Clause &c) {
398 int nblevels = 0;
399 MYFLAG++;
400
401 if(incremental) { // ----------------- INCREMENTAL MODE
402 int nbDone = 0;
403 for(int i=0;i<c.size();i++) {
404 if(nbDone>=c.sizeWithoutSelectors()) break;
405 if(isSelector(var(c[i]))) continue;
406 nbDone++;
407 int l = level(var(c[i]));
408 if (permDiff[l] != MYFLAG) {
409 permDiff[l] = MYFLAG;
410 nblevels++;
411 }
412 }
413 } else { // -------- DEFAULT MODE. NOT A LOT OF DIFFERENCES... BUT EASIER TO READ
414 for(int i=0;i<c.size();i++) {
415 int l = level(var(c[i]));
416 if (permDiff[l] != MYFLAG) {
417 permDiff[l] = MYFLAG;
418 nblevels++;
419 }
420 }
421 }
422 return nblevels;
423}
424
425
426/******************************************************************
427 * Minimisation with binary reolution
428 ******************************************************************/
430
431 // Find the LBD measure
432 unsigned int lbd = computeLBD(out_learnt);
433 Lit p = ~out_learnt[0];
434
435 if(lbd<=lbLBDMinimizingClause){
436 MYFLAG++;
437
438 for(int i = 1;i<out_learnt.size();i++) {
439 permDiff[var(out_learnt[i])] = MYFLAG;
440 }
441
442 vec<Watcher>& wbin = watchesBin[p];
443 int nb = 0;
444 for(int k = 0;k<wbin.size();k++) {
445 Lit imp = wbin[k].blocker;
446 if(permDiff[var(imp)]==MYFLAG && value(imp)==l_True) {
447 nb++;
448 permDiff[var(imp)]= MYFLAG-1;
449 }
450 }
451 int l = out_learnt.size()-1;
452 if(nb>0) {
454 for(int i = 1;i<out_learnt.size()-nb;i++) {
455 if(permDiff[var(out_learnt[i])]!=MYFLAG) {
456 Lit p = out_learnt[l];
457 out_learnt[l] = out_learnt[i];
458 out_learnt[i] = p;
459 l--;i--;
460 }
461 }
462
463 out_learnt.shrink(nb);
464
465 }
466 }
467}
468
469// Revert to the state at given level (keeping all assignment at 'level' but not beyond).
470//
472 if (decisionLevel() > level){
473 for (int c = trail.size()-1; c >= trail_lim[level]; c--){
474 Var x = var(trail[c]);
475 assigns [x] = l_Undef;
476 if (phase_saving > 1 || ((phase_saving == 1) && c > trail_lim.last()))
477 polarity[x] = sign(trail[c]);
478 insertVarOrder(x); }
480 trail.shrink(trail.size() - trail_lim[level]);
481 trail_lim.shrink(trail_lim.size() - level);
482 }
483}
484
485
486//=================================================================================================
487// Major methods:
488
489
491{
492 Var next = var_Undef;
493
494 // Random decision:
495 if (drand(random_seed) < random_var_freq && !order_heap.empty()){
496 next = order_heap[irand(random_seed,order_heap.size())];
497 if (value(next) == l_Undef && decision[next])
498 rnd_decisions++; }
499
500 // Activity based decision:
501 while (next == var_Undef || value(next) != l_Undef || !decision[next])
502 if (order_heap.empty()){
503 next = var_Undef;
504 break;
505 }else
506 next = order_heap.removeMin();
507
508 return next == var_Undef ? lit_Undef : mkLit(next, rnd_pol ? drand(random_seed) < 0.5 : (polarity[next] != 0));
509}
510
511
512/*_________________________________________________________________________________________________
513|
514| analyze : (confl : Clause*) (out_learnt : vec<Lit>&) (out_btlevel : int&) -> [void]
515|
516| Description:
517| Analyze conflict and produce a reason clause.
518|
519| Pre-conditions:
520| * 'out_learnt' is assumed to be cleared.
521| * Current decision level must be greater than root level.
522|
523| Post-conditions:
524| * 'out_learnt[0]' is the asserting literal at level 'out_btlevel'.
525| * If out_learnt.size() > 1 then 'out_learnt[1]' has the greatest decision level of the
526| rest of literals. There may be others from the same level though.
527|
528|________________________________________________________________________________________________@*/
529void Solver::analyze(CRef confl, vec<Lit>& out_learnt,vec<Lit>&selectors, int& out_btlevel,unsigned int &lbd,unsigned int &szWithoutSelectors)
530{
531 int pathC = 0;
532 Lit p = lit_Undef;
533
534 // Generate conflict clause:
535 //
536 out_learnt.push(); // (leave room for the asserting literal)
537 int index = trail.size() - 1;
538
539 do{
540 assert(confl != CRef_Undef); // (otherwise should be UIP)
541 Clause& c = ca[confl];
542
543 // Special case for binary clauses
544 // The first one has to be SAT
545 if( p != lit_Undef && c.size()==2 && value(c[0])==l_False) {
546
547 assert(value(c[1])==l_True);
548 Lit tmp = c[0];
549 c[0] = c[1], c[1] = tmp;
550 }
551
552 if (c.learnt())
554
555#ifdef DYNAMICNBLEVEL
556 // DYNAMIC NBLEVEL trick (see competition'09 companion paper)
557 if(c.learnt() && c.lbd()>2) {
558 unsigned int nblevels = computeLBD(c);
559 if(nblevels+1<c.lbd() ) { // improve the LBD
560 if(c.lbd()<=lbLBDFrozenClause) {
561 c.setCanBeDel(false);
562 }
563 // seems to be interesting : keep it for the next round
564 c.setLBD(nblevels); // Update it
565 }
566 }
567#endif
568
569
570 for (int j = (p == lit_Undef) ? 0 : 1; j < c.size(); j++){
571 Lit q = c[j];
572
573 if (!seen[var(q)] && level(var(q)) > 0){
574 if(!isSelector(var(q)))
576 seen[var(q)] = 1;
577 if (level(var(q)) >= decisionLevel()) {
578 pathC++;
579#ifdef UPDATEVARACTIVITY
580 // UPDATEVARACTIVITY trick (see competition'09 companion paper)
581 if(!isSelector(var(q)) && (reason(var(q))!= CRef_Undef) && ca[reason(var(q))].learnt())
582 lastDecisionLevel.push(q);
583#endif
584
585 } else {
586 if(isSelector(var(q))) {
587 assert(value(q) == l_False);
588 selectors.push(q);
589 } else
590 out_learnt.push(q);
591 }
592 }
593 }
594
595 // Select next clause to look at:
596 while (!seen[var(trail[index--])]);
597 p = trail[index+1];
598 confl = reason(var(p));
599 seen[var(p)] = 0;
600 pathC--;
601
602 }while (pathC > 0);
603 out_learnt[0] = ~p;
604
605 // Simplify conflict clause:
606 //
607 int i, j;
608
609 for(i = 0;i<selectors.size();i++)
610 out_learnt.push(selectors[i]);
611
612 out_learnt.copyTo_(analyze_toclear);
613 if (ccmin_mode == 2){
614 uint32_t abstract_level = 0;
615 for (i = 1; i < out_learnt.size(); i++)
616 abstract_level |= abstractLevel(var(out_learnt[i])); // (maintain an abstraction of levels involved in conflict)
617
618 for (i = j = 1; i < out_learnt.size(); i++)
619 if (reason(var(out_learnt[i])) == CRef_Undef || !litRedundant(out_learnt[i], abstract_level))
620 out_learnt[j++] = out_learnt[i];
621
622 }else if (ccmin_mode == 1){
623 for (i = j = 1; i < out_learnt.size(); i++){
624 Var x = var(out_learnt[i]);
625
626 if (reason(x) == CRef_Undef)
627 out_learnt[j++] = out_learnt[i];
628 else{
629 Clause& c = ca[reason(var(out_learnt[i]))];
630 // Thanks to Siert Wieringa for this bug fix!
631 for (int k = ((c.size()==2) ? 0:1); k < c.size(); k++)
632 if (!seen[var(c[k])] && level(var(c[k])) > 0){
633 out_learnt[j++] = out_learnt[i];
634 break; }
635 }
636 }
637 }else
638 i = j = out_learnt.size();
639
640 max_literals += out_learnt.size();
641 out_learnt.shrink(i - j);
642 tot_literals += out_learnt.size();
643
644
645 /* ***************************************
646 Minimisation with binary clauses of the asserting clause
647 First of all : we look for small clauses
648 Then, we reduce clauses with small LBD.
649 Otherwise, this can be useless
650 */
651 if(!incremental && out_learnt.size()<=lbSizeMinimizingClause) {
653 }
654 // Find correct backtrack level:
655 //
656 if (out_learnt.size() == 1)
657 out_btlevel = 0;
658 else{
659 int max_i = 1;
660 // Find the first literal assigned at the next-highest level:
661 for (int i = 2; i < out_learnt.size(); i++)
662 if (level(var(out_learnt[i])) > level(var(out_learnt[max_i])))
663 max_i = i;
664 // Swap-in this literal at index 1:
665 Lit p = out_learnt[max_i];
666 out_learnt[max_i] = out_learnt[1];
667 out_learnt[1] = p;
668 out_btlevel = level(var(p));
669 }
670
671
672 // Compute the size of the clause without selectors (incremental mode)
673 if(incremental) {
674 szWithoutSelectors = 0;
675 for(int i=0;i<out_learnt.size();i++) {
676 if(!isSelector(var((out_learnt[i])))) szWithoutSelectors++;
677 else if(i>0) break;
678 }
679 } else
680 szWithoutSelectors = out_learnt.size();
681
682 // Compute LBD
683 lbd = computeLBD(out_learnt,out_learnt.size()-selectors.size());
684
685
686#ifdef UPDATEVARACTIVITY
687 // UPDATEVARACTIVITY trick (see competition'09 companion paper)
688 if(lastDecisionLevel.size()>0) {
689 for(int i = 0;i<lastDecisionLevel.size();i++) {
690 if(ca[reason(var(lastDecisionLevel[i]))].lbd()<lbd)
691 varBumpActivity(var(lastDecisionLevel[i]));
692 }
693 lastDecisionLevel.clear();
694 }
695#endif
696
697
698
699 for (j = 0; j < analyze_toclear.size(); j++) seen[var(analyze_toclear[j])] = 0; // ('seen[]' is now cleared)
700 for(j = 0 ; j<selectors.size() ; j++) seen[var(selectors[j])] = 0;
701}
702
703
704// Check if 'p' can be removed. 'abstract_levels' is used to abort early if the algorithm is
705// visiting literals at levels that cannot be removed later.
706bool Solver::litRedundant(Lit p, uint32_t abstract_levels)
707{
708 analyze_stack.clear(); analyze_stack.push(p);
709 int top = analyze_toclear.size();
710 while (analyze_stack.size() > 0){
712 Clause& c = ca[reason(var(analyze_stack.last()))]; analyze_stack.pop();
713 if(c.size()==2 && value(c[0])==l_False) {
714 assert(value(c[1])==l_True);
715 Lit tmp = c[0];
716 c[0] = c[1], c[1] = tmp;
717 }
718
719 for (int i = 1; i < c.size(); i++){
720 Lit p = c[i];
721 if (!seen[var(p)] && level(var(p)) > 0){
722 if (reason(var(p)) != CRef_Undef && (abstractLevel(var(p)) & abstract_levels) != 0){
723 seen[var(p)] = 1;
724 analyze_stack.push(p);
725 analyze_toclear.push(p);
726 }else{
727 for (int j = top; j < analyze_toclear.size(); j++)
728 seen[var(analyze_toclear[j])] = 0;
729 analyze_toclear.shrink(analyze_toclear.size() - top);
730 return false;
731 }
732 }
733 }
734 }
735
736 return true;
737}
738
739
740/*_________________________________________________________________________________________________
741|
742| analyzeFinal : (p : Lit) -> [void]
743|
744| Description:
745| Specialized analysis procedure to express the final conflict in terms of assumptions.
746| Calculates the (possibly empty) set of assumptions that led to the assignment of 'p', and
747| stores the result in 'out_conflict'.
748|________________________________________________________________________________________________@*/
749void Solver::analyzeFinal(Lit p, vec<Lit>& out_conflict)
750{
751 out_conflict.clear();
752 out_conflict.push(p);
753
754 if (decisionLevel() == 0)
755 return;
756
757 seen[var(p)] = 1;
758
759 for (int i = trail.size()-1; i >= trail_lim[0]; i--){
760 Var x = var(trail[i]);
761 if (seen[x]){
762 if (reason(x) == CRef_Undef){
763 assert(level(x) > 0);
764 out_conflict.push(~trail[i]);
765 }else{
766 Clause& c = ca[reason(x)];
767 // for (int j = 1; j < c.size(); j++) Minisat (glucose 2.0) loop
768 // Bug in case of assumptions due to special data structures for Binary.
769 // Many thanks to Sam Bayless (sbayless@cs.ubc.ca) for discover this bug.
770 for (int j = ((c.size()==2) ? 0:1); j < c.size(); j++)
771 if (level(var(c[j])) > 0)
772 seen[var(c[j])] = 1;
773 }
774
775 seen[x] = 0;
776 }
777 }
778
779 seen[var(p)] = 0;
780}
781
782
784{
785 assert(value(p) == l_Undef);
786 assigns[var(p)] = lbool(!sign(p));
787 vardata[var(p)] = mkVarData(from, decisionLevel());
788 trail.push_(p);
789}
790
791
792/*_________________________________________________________________________________________________
793|
794| propagate : [void] -> [Clause*]
795|
796| Description:
797| Propagates all enqueued facts. If a conflict arises, the conflicting clause is returned,
798| otherwise CRef_Undef.
799|
800| Post-conditions:
801| * the propagation queue is empty, even if there was a conflict.
802|________________________________________________________________________________________________@*/
804{
805 CRef confl = CRef_Undef;
806 int num_props = 0;
807 watches.cleanAll();
808 watchesBin.cleanAll();
809 while (qhead < trail.size()){
810 Lit p = trail[qhead++]; // 'p' is enqueued fact to propagate.
811 vec<Watcher>& ws = watches[p];
812 Watcher *i, *j, *end;
813 num_props++;
814
815 // First, Propagate binary clauses
816 vec<Watcher>& wbin = watchesBin[p];
817 for(int k = 0;k<wbin.size();k++) {
818 Lit imp = wbin[k].blocker;
819 if(value(imp) == l_False) {
820 return wbin[k].cref;
821 }
822 if(value(imp) == l_Undef) {
823 uncheckedEnqueue(imp,wbin[k].cref);
824 }
825 }
826
827 for (i = j = (Watcher*)ws, end = i + ws.size(); i != end;){
828 // Try to avoid inspecting the clause:
829 Lit blocker = i->blocker;
830 if (value(blocker) == l_True){
831 *j++ = *i++; continue; }
832
833 // Make sure the false literal is data[1]:
834 CRef cr = i->cref;
835 Clause& c = ca[cr];
836 Lit false_lit = ~p;
837 if (c[0] == false_lit)
838 c[0] = c[1], c[1] = false_lit;
839 assert(c[1] == false_lit);
840 i++;
841
842 // If 0th watch is true, then clause is already satisfied.
843 Lit first = c[0];
844 Watcher w = Watcher(cr, first);
845 if (first != blocker && value(first) == l_True){
846 *j++ = w; continue; }
847
848 // Look for new watch:
849 if(incremental) { // ----------------- INCREMENTAL MODE
850 int choosenPos = -1;
851 for (int k = 2; k < c.size(); k++) {
852
853 if (value(c[k]) != l_False){
854 if(decisionLevel()>assumptions.size()) {
855 choosenPos = k;
856 break;
857 } else {
858 choosenPos = k;
859
860 if(value(c[k])==l_True || !isSelector(var(c[k]))) {
861 break;
862 }
863 }
864
865 }
866 }
867 if(choosenPos!=-1) {
868 c[1] = c[choosenPos]; c[choosenPos] = false_lit;
869 watches[~c[1]].push(w);
870 goto NextClause; }
871 } else { // ----------------- DEFAULT MODE (NOT INCREMENTAL)
872 for (int k = 2; k < c.size(); k++) {
873
874 if (value(c[k]) != l_False){
875 c[1] = c[k]; c[k] = false_lit;
876 watches[~c[1]].push(w);
877 goto NextClause; }
878 }
879 }
880
881 // Did not find watch -- clause is unit under assignment:
882 *j++ = w;
883 if (value(first) == l_False){
884 confl = cr;
885 qhead = trail.size();
886 // Copy the remaining watches:
887 while (i < end)
888 *j++ = *i++;
889 }else {
890 uncheckedEnqueue(first, cr);
891
892
893 }
894 NextClause:;
895 }
896 ws.shrink(i - j);
897 }
898 propagations += num_props;
899 simpDB_props -= num_props;
900
901 return confl;
902}
903
904
905/*_________________________________________________________________________________________________
906|
907| reduceDB : () -> [void]
908|
909| Description:
910| Remove half of the learnt clauses, minus the clauses locked by the current assignment. Locked
911| clauses are clauses that are reason to some assignment. Binary clauses are never removed.
912|________________________________________________________________________________________________@*/
913struct reduceDB_lt {
914 ClauseAllocator& ca;
916 bool operator () (CRef x, CRef y) {
917
918 // Main criteria... Like in MiniSat we keep all binary clauses
919 if(ca[x].size()> 2 && ca[y].size()==2) return 1;
920
921 if(ca[y].size()> 2 && ca[x].size()==2) return 0;
922 if(ca[x].size()==2 && ca[y].size()==2) return 0;
923
924 // Second one based on literal block distance
925 if(ca[x].lbd()> ca[y].lbd()) return 1;
926 if(ca[x].lbd()< ca[y].lbd()) return 0;
927
928 // Finally we can use old activity or size, we choose the last one
929 return ca[x].activity() < ca[y].activity();
930 //return x->size() < y->size();
931 //return ca[x].size() > 2 && (ca[y].size() == 2 || ca[x].activity() < ca[y].activity()); }
932 }
933};
934
936{
937 int i, j;
938 nbReduceDB++;
940
941 // We have a lot of "good" clauses, it is difficult to compare them. Keep more !
943 // Useless :-)
944 if(ca[learnts.last()].lbd()<=5) nbclausesbeforereduce +=specialIncReduceDB;
945
946 // Don't delete binary or locked clauses. From the rest, delete clauses from the first half
947 // Keep clauses which seem to be usefull (their lbd was reduce during this sequence)
948
949 int limit = learnts.size() / 2;
950 for (i = j = 0; i < learnts.size(); i++){
951 Clause& c = ca[learnts[i]];
952 if (c.lbd()>2 && c.size() > 2 && c.canBeDel() && !locked(c) && (i < limit)) {
955 }
956 else {
957 if(!c.canBeDel()) limit++; //we keep c, so we can delete an other clause
958 c.setCanBeDel(true); // At the next step, c can be delete
959 learnts[j++] = learnts[i];
960 }
961 }
962 learnts.shrink(i - j);
963 checkGarbage();
964}
965
966
968{
969 int i, j;
970 for (i = j = 0; i < cs.size(); i++){
971 Clause& c = ca[cs[i]];
972 if (satisfied(c))
973 removeClause(cs[i]);
974 else
975 cs[j++] = cs[i];
976 }
977 cs.shrink(i - j);
978}
979
980
982{
983 vec<Var> vs;
984 for (Var v = 0; v < nVars(); v++)
985 if (decision[v] && value(v) == l_Undef)
986 vs.push(v);
987 order_heap.build(vs);
988}
989
990
991/*_________________________________________________________________________________________________
992|
993| simplify : [void] -> [bool]
994|
995| Description:
996| Simplify the clause database according to the current top-level assigment. Currently, the only
997| thing done here is the removal of satisfied clauses, but more things can be put here.
998|________________________________________________________________________________________________@*/
1000{
1001 assert(decisionLevel() == 0);
1002
1003 if (!ok || propagate() != CRef_Undef)
1004 return ok = false;
1005
1006 if (nAssigns() == simpDB_assigns || (simpDB_props > 0))
1007 return true;
1008
1009 // Remove satisfied clauses:
1011 if (remove_satisfied) // Can be turned off.
1013
1014 checkGarbage();
1015
1017
1019 simpDB_props = clauses_literals + learnts_literals; // (shouldn't depend on stats really, but it will do for now)
1020
1021 return true;
1022}
1023
1024
1025/*_________________________________________________________________________________________________
1026|
1027| search : (nof_conflicts : int) (params : const SearchParams&) -> [lbool]
1028|
1029| Description:
1030| Search for a model the specified number of conflicts.
1031| NOTE! Use negative value for 'nof_conflicts' indicate infinity.
1032|
1033| Output:
1034| 'l_True' if a partial assigment that is consistent with respect to the clauseset is found. If
1035| all variables are decision variables, this means that the clause set is satisfiable. 'l_False'
1036| if the clause set is unsatisfiable. 'l_Undef' if the bound on number of conflicts is reached.
1037|________________________________________________________________________________________________@*/
1038lbool Solver::search(int nof_conflicts)
1039{
1040 assert(ok);
1041 int backtrack_level;
1042 int conflictC = 0;
1043 vec<Lit> learnt_clause,selectors;
1044 unsigned int nblevels,szWoutSelectors;
1045 bool blocked=false;
1046 starts++;
1047 for (;;){
1048 CRef confl = propagate();
1049 if (confl != CRef_Undef){
1050 // CONFLICT
1051 conflicts++; conflictC++;conflictsRestarts++;
1052 if(conflicts%5000==0 && var_decay<0.95)
1053 var_decay += 0.01;
1054
1055 if (verbosity >= 1 && conflicts%verbEveryConflicts==0){
1056 printf("c | %8d %7d %5d | %7d %8d %8d | %5d %8d %6d %8d | %6.3f %% |\n",
1057 (int)starts,(int)nbstopsrestarts, (int)(conflicts/starts),
1058 (int)dec_vars - (trail_lim.size() == 0 ? trail.size() : trail_lim[0]), nClauses(), (int)clauses_literals,
1059 (int)nbReduceDB, nLearnts(), (int)nbDL2,(int)nbRemovedClauses, progressEstimate()*100);
1060 }
1061 if (decisionLevel() == 0) {
1062 return l_False;
1063
1064 }
1065
1066 trailQueue.push(trail.size());
1067 // BLOCK RESTART (CP 2012 paper)
1068 if( conflictsRestarts>LOWER_BOUND_FOR_BLOCKING_RESTART && lbdQueue.isvalid() && trail.size()>R*trailQueue.getavg()) {
1069 lbdQueue.fastclear();
1071 if(!blocked) {lastblockatrestart=starts;nbstopsrestartssame++;blocked=true;}
1072 }
1073
1074 learnt_clause.clear();
1075 selectors.clear();
1076 analyze(confl, learnt_clause, selectors,backtrack_level,nblevels,szWoutSelectors);
1077
1078 lbdQueue.push(nblevels);
1079 sumLBD += nblevels;
1080
1081 cancelUntil(backtrack_level);
1082
1083 if (certifiedUNSAT) {
1084 for (int i = 0; i < learnt_clause.size(); i++)
1085 fprintf(certifiedOutput, "%i " , (var(learnt_clause[i]) + 1) *
1086 (-2 * sign(learnt_clause[i]) + 1) );
1087 fprintf(certifiedOutput, "0\n");
1088 }
1089
1090 if (learnt_clause.size() == 1){
1091 uncheckedEnqueue(learnt_clause[0]);nbUn++;
1092 }else{
1093 CRef cr = ca.alloc(learnt_clause, true);
1094 ca[cr].setLBD(nblevels);
1095 ca[cr].setSizeWithoutSelectors(szWoutSelectors);
1096 if(nblevels<=2) nbDL2++; // stats
1097 if(ca[cr].size()==2) nbBin++; // stats
1098 learnts.push(cr);
1099 attachClause(cr);
1100
1101 claBumpActivity(ca[cr]);
1102 uncheckedEnqueue(learnt_clause[0], cr);
1103 }
1106
1107
1108 }else{
1109
1110 // Our dynamic restart, see the SAT09 competition compagnion paper
1111 if ( (conflictsRestarts && lbdQueue.isvalid() && lbdQueue.getavg()*K > sumLBD/conflictsRestarts) || (pstop && *pstop) ) {
1112 lbdQueue.fastclear();
1114 int bt = 0;
1115 if(incremental) { // DO NOT BACKTRACK UNTIL 0.. USELESS
1116 bt = (decisionLevel()<assumptions.size()) ? decisionLevel() : assumptions.size();
1117 }
1118 cancelUntil(bt);
1119 return l_Undef;
1120 }
1121
1122 // Simplify the set of problem clauses:
1123 if (decisionLevel() == 0 && !simplify()) {
1124 return l_False;
1125 }
1126 // Perform clause database reduction !
1128 {
1129
1130 assert(learnts.size()>0);
1132 reduceDB();
1134 }
1135
1136 Lit next = lit_Undef;
1137 while (decisionLevel() < assumptions.size()){
1138 // Perform user provided assumption:
1140 if (value(p) == l_True){
1141 // Dummy decision level:
1143 }else if (value(p) == l_False){
1145 return l_False;
1146 }else{
1147 next = p;
1148 break;
1149 }
1150 }
1151
1152 if (next == lit_Undef){
1153 // New variable decision:
1154 decisions++;
1155 next = pickBranchLit();
1156
1157 if (next == lit_Undef){
1158 //printf("c last restart ## conflicts : %d %d \n",conflictC,decisionLevel());
1159 // Model found:
1160 return l_True;
1161 }
1162 }
1163
1164 // Increase decision level and enqueue 'next'
1166 uncheckedEnqueue(next);
1167 }
1168 }
1169}
1170
1171
1173{
1174 double progress = 0;
1175 double F = 1.0 / nVars();
1176
1177 for (int i = 0; i <= decisionLevel(); i++){
1178 int beg = i == 0 ? 0 : trail_lim[i - 1];
1179 int end = i == decisionLevel() ? trail.size() : trail_lim[i];
1180 progress += pow(F, i) * (end - beg);
1181 }
1182
1183 return progress / nVars();
1184}
1185
1187
1188 printf("c---------- Glucose Stats -------------------------\n");
1189 printf("c restarts : %ld\n", starts);
1190 printf("c nb ReduceDB : %ld\n", nbReduceDB);
1191 printf("c nb removed Clauses : %ld\n", nbRemovedClauses);
1192 printf("c nb learnts DL2 : %ld\n", nbDL2);
1193 printf("c nb learnts size 2 : %ld\n", nbBin);
1194 printf("c nb learnts size 1 : %ld\n", nbUn);
1195
1196 printf("c conflicts : %ld\n", conflicts);
1197 printf("c decisions : %ld\n", decisions);
1198 printf("c propagations : %ld\n", propagations);
1199
1200 printf("c SAT Calls : %d in %g seconds\n", nbSatCalls, totalTime4Sat);
1201 printf("c UNSAT Calls : %d in %g seconds\n", nbUnsatCalls, totalTime4Unsat);
1202 printf("c--------------------------------------------------\n");
1203
1204
1205}
1206
1207
1208// NOTE: assumptions passed in member-variable 'assumptions'.
1210{
1211
1213 printf("Can not use incremental and certified unsat in the same time\n");
1214 exit(-1);
1215 }
1216 model.clear();
1217 conflict.clear();
1218 if (!ok) return l_False;
1219 double curTime = cpuTime();
1220
1221
1222 solves++;
1223
1224
1225
1226 lbool status = l_Undef;
1227 if(!incremental && verbosity>=1) {
1228 printf("c ========================================[ MAGIC CONSTANTS ]==============================================\n");
1229 printf("c | Constants are supposed to work well together :-) |\n");
1230 printf("c | however, if you find better choices, please let us known... |\n");
1231 printf("c |-------------------------------------------------------------------------------------------------------|\n");
1232 printf("c | | | |\n");
1233 printf("c | - Restarts: | - Reduce Clause DB: | - Minimize Asserting: |\n");
1234 printf("c | * LBD Queue : %6d | * First : %6d | * size < %3d |\n",lbdQueue.maxSize(),nbclausesbeforereduce,lbSizeMinimizingClause);
1235 printf("c | * Trail Queue : %6d | * Inc : %6d | * lbd < %3d |\n",trailQueue.maxSize(),incReduceDB,lbLBDMinimizingClause);
1236 printf("c | * K : %6.2f | * Special : %6d | |\n",K,specialIncReduceDB);
1237 printf("c | * R : %6.2f | * Protected : (lbd)< %2d | |\n",R,lbLBDFrozenClause);
1238 printf("c | | | |\n");
1239printf("c ==================================[ Search Statistics (every %6d conflicts) ]=========================\n",verbEveryConflicts);
1240 printf("c | |\n");
1241
1242 printf("c | RESTARTS | ORIGINAL | LEARNT | Progress |\n");
1243 printf("c | NB Blocked Avg Cfc | Vars Clauses Literals | Red Learnts LBD2 Removed | |\n");
1244 printf("c =========================================================================================================\n");
1245 }
1246
1247 // Search:
1248 int curr_restarts = 0;
1249 while (status == l_Undef){
1250 status = search(0); // the parameter is useless in glucose, kept to allow modifications
1251 if (!withinBudget() || terminate_search_early || (pstop && *pstop)) break;
1252 if (nRuntimeLimit && Abc_Clock() > nRuntimeLimit) break;
1253 curr_restarts++;
1254 }
1255
1256 if (!incremental && verbosity >= 1)
1257 printf("c =========================================================================================================\n");
1258
1259
1260 if (certifiedUNSAT){ // Want certified output
1261 if (status == l_False)
1262 fprintf(certifiedOutput, "0\n");
1263 fclose(certifiedOutput);
1264 }
1265
1266
1267 if (status == l_True){
1268 // Extend & copy model:
1269 model.growTo(nVars());
1270 for (int i = 0; i < nVars(); i++) model[i] = value(i);
1271 }else if (status == l_False && conflict.size() == 0)
1272 ok = false;
1273
1274 cancelUntil(0);
1275
1276 double finalTime = cpuTime();
1277 if(status==l_True) {
1278 nbSatCalls++;
1279 totalTime4Sat +=(finalTime-curTime);
1280 }
1281 if(status==l_False) {
1282 nbUnsatCalls++;
1283 totalTime4Unsat +=(finalTime-curTime);
1284 }
1285
1286 // ABC callback
1287 if (pCnfFunc && !terminate_search_early) {// hack to avoid calling callback twise if the solver was terminated early
1288 int * pCex = NULL;
1289 int message = (status == l_True ? 1 : status == l_False ? 0 : -1);
1290 if (status == l_True) {
1291 pCex = new int[nVars()];
1292 for (int i = 0; i < nVars(); i++)
1293 pCex[i] = (model[i] == l_True);
1294 }
1295
1296 int callback_result = pCnfFunc(pCnfMan, message, pCex);
1297 assert(callback_result == 0);
1298 }
1299 else if (pCnfFunc)
1300 terminate_search_early = false; // for next run
1301
1302 return status;
1303}
1304
1305//=================================================================================================
1306// Writing CNF to DIMACS:
1307//
1308// FIXME: this needs to be rewritten completely.
1309
1310static Var mapVar(Var x, vec<Var>& map, Var& max)
1311{
1312 if (map.size() <= x || map[x] == -1){
1313 map.growTo(x+1, -1);
1314 map[x] = max++;
1315 }
1316 return map[x];
1317}
1318
1319
1320void Solver::toDimacs(FILE* f, Clause& c, vec<Var>& map, Var& max)
1321{
1322 if (satisfied(c)) return;
1323
1324 for (int i = 0; i < c.size(); i++)
1325 if (value(c[i]) != l_False)
1326 fprintf(f, "%s%d ", sign(c[i]) ? "-" : "", mapVar(var(c[i]), map, max)+1);
1327 fprintf(f, "0\n");
1328}
1329
1330
1331void Solver::toDimacs(const char *file, const vec<Lit>& assumps)
1332{
1333 FILE* f = fopen(file, "wb");
1334 if (f == NULL)
1335 fprintf(stderr, "could not open file %s\n", file), exit(1);
1336 toDimacs(f, assumps);
1337 fclose(f);
1338}
1339
1340
1341void Solver::toDimacs(FILE* f, const vec<Lit>& assumps)
1342{
1343 // Handle case when solver is in contradictory state:
1344 if (!ok){
1345 fprintf(f, "p cnf 1 2\n1 0\n-1 0\n");
1346 return; }
1347
1348 vec<Var> map; Var max = 0;
1349
1350 // Cannot use removeClauses here because it is not safe
1351 // to deallocate them at this point. Could be improved.
1352 int i, cnt = 0;
1353 for (i = 0; i < clauses.size(); i++)
1354 if (!satisfied(ca[clauses[i]]))
1355 cnt++;
1356
1357 for (i = 0; i < clauses.size(); i++)
1358 if (!satisfied(ca[clauses[i]])){
1359 Clause& c = ca[clauses[i]];
1360 for (int j = 0; j < c.size(); j++)
1361 if (value(c[j]) != l_False)
1362 mapVar(var(c[j]), map, max);
1363 }
1364
1365 // Assumptions are added as unit clauses:
1366 cnt += assumptions.size();
1367
1368 fprintf(f, "p cnf %d %d\n", max, cnt);
1369
1370 for (i = 0; i < assumptions.size(); i++){
1372 fprintf(f, "%s%d 0\n", sign(assumptions[i]) ? "-" : "", mapVar(var(assumptions[i]), map, max)+1);
1373 }
1374
1375 for (i = 0; i < clauses.size(); i++)
1376 toDimacs(f, ca[clauses[i]], map, max);
1377
1378 if (verbosity > 0)
1379 printf("Wrote %d clauses with %d variables.\n", cnt, max);
1380}
1381
1382
1383//=================================================================================================
1384// Garbage Collection methods:
1385
1387{
1388 int v, s, i, j;
1389 // All watchers:
1390 //
1391 // for (int i = 0; i < watches.size(); i++)
1392 watches.cleanAll();
1393 watchesBin.cleanAll();
1394 for (v = 0; v < nVars(); v++)
1395 for (s = 0; s < 2; s++){
1396 Lit p = mkLit(v, s != 0);
1397 // printf(" >>> RELOCING: %s%d\n", sign(p)?"-":"", var(p)+1);
1398 vec<Watcher>& ws = watches[p];
1399 for (j = 0; j < ws.size(); j++)
1400 ca.reloc(ws[j].cref, to);
1401 vec<Watcher>& ws2 = watchesBin[p];
1402 for (j = 0; j < ws2.size(); j++)
1403 ca.reloc(ws2[j].cref, to);
1404 }
1405
1406 // All reasons:
1407 //
1408 for (i = 0; i < trail.size(); i++){
1409 Var v = var(trail[i]);
1410
1411 if (reason(v) != CRef_Undef && (ca[reason(v)].reloced() || locked(ca[reason(v)])))
1412 ca.reloc(vardata[v].reason, to);
1413 }
1414
1415 // All learnt:
1416 //
1417 for (i = 0; i < learnts.size(); i++)
1418 ca.reloc(learnts[i], to);
1419
1420 // All original:
1421 //
1422 for (i = 0; i < clauses.size(); i++)
1423 ca.reloc(clauses[i], to);
1424}
1425
1426
1428{
1429 // Initialize the next region to a size corresponding to the estimated utilization degree. This
1430 // is not precise but should avoid some unnecessary reallocations for the new region:
1431 ClauseAllocator to(ca.size() - ca.wasted());
1432
1433 relocAll(to);
1434 if (verbosity >= 2)
1435 printf("| Garbage collection: %12d bytes => %12d bytes |\n",
1437 to.moveTo(ca);
1438}
1439
1441{
1442 // Reset everything
1443 ok = true;
1444 K = (double)opt_K;
1445 R = (double)opt_R;
1446 firstReduceDB = opt_first_reduce_db;
1447 var_decay = (double)opt_var_decay;
1448 //max_var_decay = opt_max_var_decay;
1450 curRestart = 1;
1451 cla_inc = var_inc = 1;
1452 watches.clear(false); // We don't free the memory, new calls should be of the same size order.
1453 watchesBin.clear(false);
1454 //unaryWatches.clear(false);
1455 qhead = 0;
1456 simpDB_assigns = -1;
1457 simpDB_props = 0;
1458 order_heap.clear(false);
1460 //lastLearntClause = CRef_Undef;
1461 conflict_budget = -1;
1462 propagation_budget = -1;
1464 totalTime4Sat = 0.;
1465 totalTime4Unsat = 0.;
1467 MYFLAG = 0;
1468 lbdQueue.clear(false);
1469 lbdQueue.initSize(sizeLBDQueue);
1470 trailQueue.clear(false);
1471 trailQueue.initSize(sizeTrailQueue);
1472 sumLBD = 0;
1474 //stats.clear();
1475 //stats.growTo(coreStatsSize, 0);
1476 clauses.clear(false);
1477 learnts.clear(false);
1478 //permanentLearnts.clear(false);
1479 //unaryWatchedClauses.clear(false);
1480 model.clear(false);
1481 conflict.clear(false);
1482 activity.clear(false);
1483 assigns.clear(false);
1484 polarity.clear(false);
1485 //forceUNSAT.clear(false);
1486 decision.clear(false);
1487 trail.clear(false);
1488 nbpos.clear(false);
1489 trail_lim.clear(false);
1490 vardata.clear(false);
1491 assumptions.clear(false);
1492 permDiff.clear(false);
1493 lastDecisionLevel.clear(false);
1494 ca.clear();
1495 seen.clear(false);
1496 analyze_stack.clear(false);
1497 analyze_toclear.clear(false);
1498 add_tmp.clear(false);
1499 assumptionPositions.clear(false);
1500 initialPositions.clear(false);
1501}
1502
unsigned int uint32_t
Definition Fxch.h:32
StringOption opt_certified_file_(_certified, "certified-output", "Certified UNSAT output file", "NULL")
BoolOption opt_certified_(_certified, "certified", "Certified UNSAT using DRUP format", false)
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define l_True
Definition bmcBmcS.c:35
#define l_Undef
Definition bmcBmcS.c:34
#define l_False
Definition bmcBmcS.c:36
#define var_Undef
Definition SolverTypes.h:45
#define INT32_MAX
Definition pstdint.h:415
ABC_NAMESPACE_IMPL_START typedef signed char value
void moveTo(ClauseAllocator &to)
unsigned size
unsigned int sizeWithoutSelectors() const
unsigned learnt
void setLBD(int i)
void setCanBeDel(bool b)
unsigned mark
uint32_t size() const
Definition Alloc.h:58
int64_t rnd_decisions
Definition Solver.h:194
int64_t starts
Definition Solver.h:194
double progress_estimate
Definition Solver.h:252
FILE * certifiedOutput
Definition Solver.h:188
bool locked(const Clause &c) const
Definition Solver.h:407
int64_t nbReducedClauses
Definition Solver.h:194
int specialIncReduceDB
Definition Solver.h:170
Lit pickBranchLit()
Definition Glucose.cpp:490
double K
Definition Solver.h:162
int decisionLevel() const
Definition Solver.h:417
double var_decay
Definition Solver.h:177
static double drand(double &seed)
Definition Solver.h:353
bool addClause_(vec< Lit > &ps)
Definition Glucose.cpp:224
int64_t max_literals
Definition Solver.h:195
double sizeTrailQueue
Definition Solver.h:165
int nbUnsatCalls
Definition Solver.h:295
CRef propagate()
Definition Glucose.cpp:803
lbool search(int nof_conflicts)
Definition Glucose.cpp:1038
void toDimacs(FILE *f, const vec< Lit > &assumps)
Definition Glucose.cpp:1341
int64_t lastblockatrestart
Definition Solver.h:194
bool rnd_pol
Definition Solver.h:183
vec< int > assumptionPositions
Definition Solver.h:296
OccLists< Lit, vec< Watcher >, WatcherDeleted > watches
Definition Solver.h:234
double var_inc
Definition Solver.h:232
double clause_decay
Definition Solver.h:178
vec< Lit > assumptions
Definition Solver.h:250
int nAssigns() const
Definition Solver.h:423
int SolverType
Definition Solver.h:50
vec< Lit > add_tmp
Definition Solver.h:276
vec< int > trail_lim
Definition Solver.h:245
double garbage_frac
Definition Solver.h:185
int64_t nbReduceDB
Definition Solver.h:194
void cancelUntil(int level)
Definition Glucose.cpp:471
bool isSelector(Var v)
Definition Solver.h:347
vec< VarData > vardata
Definition Solver.h:246
vec< lbool > assigns
Definition Solver.h:240
unsigned int computeLBD(const vec< Lit > &lits, int end=-1)
Definition Glucose.cpp:367
int ccmin_mode
Definition Solver.h:181
bool litRedundant(Lit p, uint32_t abstract_levels)
Definition Glucose.cpp:706
vec< unsigned int > permDiff
Definition Solver.h:254
float sumLBD
Definition Solver.h:266
double totalTime4Unsat
Definition Solver.h:294
lbool value(Var x) const
Definition Solver.h:419
int(* pCnfFunc)(void *p, int, int *)
Definition Solver.h:59
int nCallConfl
Definition Solver.h:60
int64_t conflictsRestarts
Definition Solver.h:194
static int irand(double &seed, int size)
Definition Solver.h:360
int64_t nbDL2
Definition Solver.h:194
int incReduceDB
Definition Solver.h:169
double R
Definition Solver.h:163
virtual void garbageCollect()
Definition Glucose.cpp:1427
double progressEstimate() const
Definition Glucose.cpp:1172
void relocAll(ClauseAllocator &to)
Definition Glucose.cpp:1386
int64_t solves
Definition Solver.h:194
vec< char > polarity
Definition Solver.h:241
bool certifiedUNSAT
Definition Solver.h:189
void removeClause(CRef cr)
Definition Glucose.cpp:333
vec< Lit > trail
Definition Solver.h:243
int simpDB_assigns
Definition Solver.h:248
int64_t propagations
Definition Solver.h:194
double sizeLBDQueue
Definition Solver.h:164
virtual void reset()
Definition Glucose.cpp:1440
vec< CRef > clauses
Definition Solver.h:237
vec< double > activity
Definition Solver.h:231
void reduceDB()
Definition Glucose.cpp:935
void detachClause(CRef cr, bool strict=false)
Definition Glucose.cpp:306
void checkGarbage()
Definition Solver.h:395
void rebuildOrderHeap()
Definition Glucose.cpp:981
int64_t nbstopsrestarts
Definition Solver.h:194
double random_var_freq
Definition Solver.h:179
void minimisationWithBinaryResolution(vec< Lit > &out_learnt)
Definition Glucose.cpp:429
int nbSatCalls
Definition Solver.h:295
vec< Lit > conflict
Definition Solver.h:153
vec< CRef > learnts
Definition Solver.h:238
void newDecisionLevel()
Definition Solver.h:415
void attachClause(CRef cr)
Definition Glucose.cpp:289
vec< char > decision
Definition Solver.h:242
int64_t learnts_literals
Definition Solver.h:195
int verbEveryConflicts
Definition Solver.h:159
void removeSatisfied(vec< CRef > &cs)
Definition Glucose.cpp:967
void analyzeFinal(Lit p, vec< Lit > &out_conflict)
Definition Glucose.cpp:749
ClauseAllocator ca
Definition Solver.h:261
void claDecayActivity()
Definition Solver.h:387
OccLists< Lit, vec< Watcher >, WatcherDeleted > watchesBin
Definition Solver.h:236
int64_t conflict_budget
Definition Solver.h:286
int nVars() const
Definition Solver.h:426
uint64_t nRuntimeLimit
Definition Solver.h:63
uint32_t abstractLevel(Var x) const
Definition Solver.h:418
vec< int > nbpos
Definition Solver.h:244
static VarData mkVarData(CRef cr, int l)
Definition Solver.h:202
bool remove_satisfied
Definition Solver.h:253
int nbVarsInitialFormula
Definition Solver.h:293
int * pstop
Definition Solver.h:62
bool asynch_interrupt
Definition Solver.h:288
int nbclausesbeforereduce
Definition Solver.h:263
int64_t simpDB_props
Definition Solver.h:249
vec< lbool > model
Definition Solver.h:152
bqueue< unsigned int > trailQueue
Definition Solver.h:265
int64_t clauses_literals
Definition Solver.h:195
void initNbInitialVars(int nb)
Definition Glucose.cpp:190
void insertVarOrder(Var x)
Definition Solver.h:371
bool withinBudget() const
Definition Solver.h:443
bool terminate_search_early
Definition Solver.h:61
int64_t decisions
Definition Solver.h:194
Heap< VarOrderLt > order_heap
Definition Solver.h:251
virtual ~Solver()
Definition Glucose.cpp:173
int firstReduceDB
Definition Solver.h:168
double totalTime4Sat
Definition Solver.h:294
int incremental
Definition Solver.h:292
void claBumpActivity(Clause &c)
Definition Solver.h:388
unsigned int lbLBDFrozenClause
Definition Solver.h:171
int64_t nbRemovedClauses
Definition Solver.h:194
int64_t tot_literals
Definition Solver.h:195
vec< int > initialPositions
Definition Solver.h:296
lbool solve_()
Definition Glucose.cpp:1209
int lbSizeMinimizingClause
Definition Solver.h:174
Var newVar(bool polarity=true, bool dvar=true)
Definition Glucose.cpp:202
int64_t dec_vars
Definition Solver.h:195
void varDecayActivity()
Definition Solver.h:374
void varBumpActivity(Var v, double inc)
Definition Solver.h:376
bool simplify()
Definition Glucose.cpp:999
int showModel
Definition Solver.h:160
void * pCnfMan
Definition Solver.h:58
bool rnd_init_act
Definition Solver.h:184
int phase_saving
Definition Solver.h:182
int verbosity
Definition Solver.h:158
int64_t nbBin
Definition Solver.h:194
vec< char > seen
Definition Solver.h:273
int64_t propagation_budget
Definition Solver.h:287
void setDecisionVar(Var v, bool b)
Definition Solver.h:430
void printIncrementalStats()
Definition Glucose.cpp:1186
int64_t conflicts
Definition Solver.h:194
bqueue< unsigned int > lbdQueue
Definition Solver.h:265
void setIncrementalMode()
Definition Glucose.cpp:185
CRef reason(Var x) const
Definition Solver.h:368
vec< Lit > analyze_toclear
Definition Solver.h:275
int level(Var x) const
Definition Solver.h:369
vec< Lit > analyze_stack
Definition Solver.h:274
void analyze(CRef confl, vec< Lit > &out_learnt, vec< Lit > &selectors, int &out_btlevel, unsigned int &nblevels, unsigned int &szWithoutSelectors)
Definition Glucose.cpp:529
double random_seed
Definition Solver.h:180
void uncheckedEnqueue(Lit p, CRef from=CRef_Undef)
Definition Glucose.cpp:783
unsigned int lbLBDMinimizingClause
Definition Solver.h:175
int64_t nbstopsrestartssame
Definition Solver.h:194
int64_t nbUn
Definition Solver.h:194
bool satisfied(const Clause &c) const
Definition Glucose.cpp:352
double cla_inc
Definition Solver.h:230
unsigned int MYFLAG
Definition Solver.h:277
long curRestart
Definition Solver.h:198
int nClauses() const
Definition Solver.h:424
int nLearnts() const
Definition Solver.h:425
int size(void) const
Definition Vec.h:65
void shrink(int nelems)
Definition Vec.h:66
void copyTo_(vec< T > &copy) const
Definition Vec.h:93
void clear(bool dealloc=false)
Definition Vec.h:124
void push(void)
Definition Vec.h:75
void map()
Cube * p
Definition exorList.c:222
#define LOWER_BOUND_FOR_BLOCKING_RESTART
Definition Constants.h:32
#define RATIOREMOVECLAUSES
Definition Constants.h:27
RegionAllocator< uint32_t >::Ref CRef
Definition Alg.h:28
void sort(T *array, int size, LessThan lt)
Definition Sort.h:58
int toInt(Var v)
Definition SolverTypes.h:74
RegionAllocator< uint32_t >::Ref CRef
const Lit lit_Undef
Definition SolverTypes.h:81
int var(Lit p)
Definition SolverTypes.h:71
bool sign(Lit p)
Definition SolverTypes.h:70
int Var
Definition SolverTypes.h:52
const CRef CRef_Undef
Lit mkLit(Var var, bool sign=false)
Definition SolverTypes.h:67
#define true
Definition place_base.h:28
#define false
Definition place_base.h:29
signed char lbool
Definition satVec.h:135
Definition file.h:23
reduceDB_lt(ClauseAllocator &ca_)
Definition Glucose.cpp:915
ClauseAllocator & ca
Definition Solver.cpp:522
#define assert(ex)
Definition util_old.h:213
int strcmp()
VOID_HACK exit()
#define HUGE_VAL
Definition util_old.h:295