1 /****************************************************************************
2 Copyright (c) 2006 - 2014, Armin Biere, Johannes Kepler University.
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to
6 deal in the Software without restriction, including without limitation the
7 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 ****************************************************************************/
33 /* By default code for 'all different constraints' is disabled, since 'NADC'
38 /* By default we enable failed literals, since 'NFL' is undefined.
43 /* By default we 'detach satisfied (large) clauses', e.g. NDSC undefined.
48 /* Do not use luby restart schedule instead of inner/outer.
53 /* Enabling this define, will use gnuplot to visualize how the scores evolve.
58 // #define WRITEGIF /* ... to generate a video */
63 #include <unistd.h> /* for 'usleep' */
67 #define MINRESTART 100 /* minimum restart interval */
68 #define MAXRESTART 1000000 /* maximum restart interval */
69 #define RDECIDE 1000 /* interval of random decisions */
70 #define FRESTART 110 /* restart increase factor in percent */
71 #define FREDUCE 110 /* reduce increase factor in percent */
72 #define FREDADJ 121 /* reduce increase adjustment factor */
73 #define MAXCILS 10 /* maximal number of unrecycled internals */
74 #define FFLIPPED 10000 /* flipped reduce factor */
75 #define FFLIPPEDPREC 10000000/* flipped reduce factor precision */
78 #define NO_BINARY_CLAUSES /* store binary clauses more compactly */
81 /* For debugging purposes you may want to define 'LOGGING', which actually
82 * can be enforced by using the '--log' option for the configure script.
85 #define LOG(code) do { code; } while (0)
87 #define LOG(code) do { } while (0)
89 #define NOLOG(code) do { } while (0) /* log exception */
90 #define ONLYLOG(code) do { code; } while (0) /* force logging */
92 #define FALSE ((Val)-1)
93 #define UNDEF ((Val)0)
96 #define COMPACT_TRACECHECK_TRACE_FMT 0
97 #define EXTENDED_TRACECHECK_TRACE_FMT 1
98 #define RUP_TRACE_FMT 2
100 #define NEWN(p,n) do { (p) = new (ps, sizeof (*(p)) * (n)); } while (0)
101 #define CLRN(p,n) do { memset ((p), 0, sizeof (*(p)) * (n)); } while (0)
102 #define CLR(p) CLRN(p,1)
103 #define DELETEN(p,n) \
104 do { delete (ps, p, sizeof (*(p)) * (n)); (p) = 0; } while (0)
106 #define RESIZEN(p,old_num,new_num) \
108 size_t old_size = sizeof (*(p)) * (old_num); \
109 size_t new_size = sizeof (*(p)) * (new_num); \
110 (p) = resize (ps, (p), old_size, new_size) ; \
113 #define ENLARGE(start,head,end) \
115 unsigned old_num = (unsigned)((end) - (start)); \
116 size_t new_num = old_num ? (2 * old_num) : 1; \
117 unsigned count = (head) - (start); \
118 assert ((start) <= (end)); \
119 RESIZEN((start),old_num,new_num); \
120 (head) = (start) + count; \
121 (end) = (start) + new_num; \
124 #define NOTLIT(l) (ps->lits + (1 ^ ((l) - ps->lits)))
126 #define LIT2IDX(l) ((unsigned)((l) - ps->lits) / 2)
127 #define LIT2IMPLS(l) (ps->impls + (unsigned)((l) - ps->lits))
128 #define LIT2INT(l) ((int)(LIT2SGN(l) * LIT2IDX(l)))
129 #define LIT2SGN(l) (((unsigned)((l) - ps->lits) & 1) ? -1 : 1)
130 #define LIT2VAR(l) (ps->vars + LIT2IDX(l))
131 #define LIT2HTPS(l) (ps->htps + (unsigned)((l) - ps->lits))
132 #define LIT2JWH(l) (ps->jwh + ((l) - ps->lits))
135 #define LIT2DHTPS(l) (ps->dhtps + (unsigned)((l) - ps->lits))
138 #ifdef NO_BINARY_CLAUSES
139 typedef unsigned long Wrd;
140 #define ISLITREASON(C) (1&(Wrd)C)
141 #define LIT2REASON(L) \
142 (assert (L->val==TRUE), ((Cls*)(1 + (2*(L - ps->lits)))))
143 #define REASON2LIT(C) ((Lit*)(ps->lits + ((Wrd)C)/2))
146 #define ENDOFCLS(c) ((void*)((c)->lits + (c)->size))
148 #define SOC ((ps->oclauses == ps->ohead) ? ps->lclauses : ps->oclauses)
149 #define EOC (ps->lhead)
150 #define NXC(p) (((p) + 1 == ps->ohead) ? ps->lclauses : (p) + 1)
152 #define OIDX2IDX(idx) (2 * ((idx) + 1))
153 #define LIDX2IDX(idx) (2 * (idx) + 1)
155 #define ISLIDX(idx) ((idx)&1)
157 #define IDX2OIDX(idx) (assert(!ISLIDX(idx)), (idx)/2 - 1)
158 #define IDX2LIDX(idx) (assert(ISLIDX(idx)), (idx)/2)
160 #define EXPORTIDX(idx) \
161 ((ISLIDX(idx) ? (IDX2LIDX (idx) + (ps->ohead - ps->oclauses)) : IDX2OIDX(idx)) + 1)
164 (assert(i), (ISLIDX(i) ? ps->lclauses : ps->oclauses)[(i)/2 - !ISLIDX(i)])
166 #define IDX2ZHN(i) (assert(i), (ISLIDX(i) ? ps->zhains[(i)/2] : 0))
168 #define CLS2TRD(c) (((Trd*)(c)) - 1)
169 #define CLS2IDX(c) ((((Trd*)(c)) - 1)->idx)
172 ((Act*)((assert((c)->learned)),assert((c)->size>2),ENDOFCLS(c)))
174 #define VAR2LIT(v) (ps->lits + 2 * ((v) - ps->vars))
175 #define VAR2RNK(v) (ps->rnks + ((v) - ps->vars))
177 #define RNK2LIT(r) (ps->lits + 2 * ((r) - ps->rnks))
178 #define RNK2VAR(r) (ps->vars + ((r) - ps->rnks))
180 #define BLK_FILL_BYTES 8
181 #define SIZE_OF_BLK (sizeof (Blk) - BLK_FILL_BYTES)
183 #define PTR2BLK(void_ptr) \
184 ((void_ptr) ? (Blk*)(((char*)(void_ptr)) - SIZE_OF_BLK) : 0)
186 #define AVERAGE(a,b) ((b) ? (((double)a) / (double)(b)) : 0.0)
187 #define PERCENT(a,b) (100.0 * AVERAGE(a,b))
191 fputs ("*** picosat: " msg "\n", stderr); \
195 #define ABORTIF(cond,msg) \
197 if (!(cond)) break; \
201 #define ZEROFLT (0x00000000u)
202 #define EPSFLT (0x00000001u)
203 #define INFFLT (0xffffffffu)
205 #define FLTCARRY (1u << 25)
206 #define FLTMSB (1u << 24)
207 #define FLTMAXMANTISSA (FLTMSB - 1)
209 #define FLTMANTISSA(d) ((d) & FLTMAXMANTISSA)
210 #define FLTEXPONENT(d) ((int)((d) >> 24) - 128)
212 #define FLTMINEXPONENT (-128)
213 #define FLTMAXEXPONENT (127)
215 #define CMPSWAPFLT(a,b) \
226 #define UNPACKFLT(u,m,e) \
228 (m) = FLTMANTISSA(u); \
229 (e) = FLTEXPONENT(u); \
233 #define INSERTION_SORT_LIMIT 10
235 #define SORTING_SWAP(T,p,q) \
242 #define SORTING_CMP_SWAP(T,cmp,p,q) \
244 if ((cmp) (ps, *(p), *(q)) > 0) \
245 SORTING_SWAP (T, p, q); \
248 #define QUICKSORT_PARTITION(T,cmp,a,l,r) \
252 i = (l) - 1; /* result in 'i' */ \
257 while ((cmp) (ps, (a)[++i], pivot) < 0) \
259 while ((cmp) (ps, pivot, (a)[--j]) < 0) \
264 SORTING_SWAP (T, (a) + i, (a) + j); \
266 SORTING_SWAP (T, (a) + i, (a) + (r)); \
269 #define QUICKSORT(T,cmp,a,n) \
271 int l = 0, r = (n) - 1, m, ll, rr, i; \
272 assert (ps->ihead == ps->indices); \
273 if (r - l <= INSERTION_SORT_LIMIT) \
278 SORTING_SWAP (T, (a) + m, (a) + r - 1); \
279 SORTING_CMP_SWAP (T, cmp, (a) + l, (a) + r - 1); \
280 SORTING_CMP_SWAP (T, cmp, (a) + l, (a) + r); \
281 SORTING_CMP_SWAP (T, cmp, (a) + r - 1, (a) + r); \
282 QUICKSORT_PARTITION (T, cmp, (a), l + 1, r - 1); \
295 if (r - l > INSERTION_SORT_LIMIT) \
297 assert (rr - ll > INSERTION_SORT_LIMIT); \
298 if (ps->ihead == ps->eoi) \
299 ENLARGE (ps->indices, ps->ihead, ps->eoi); \
301 if (ps->ihead == ps->eoi) \
302 ENLARGE (ps->indices, ps->ihead, ps->eoi); \
305 else if (rr - ll > INSERTION_SORT_LIMIT) \
310 else if (ps->ihead > ps->indices) \
320 #define INSERTION_SORT(T,cmp,a,n) \
323 int l = 0, r = (n) - 1, i, j; \
324 for (i = r; i > l; i--) \
325 SORTING_CMP_SWAP (T, cmp, (a) + i - 1, (a) + i); \
326 for (i = l + 2; i <= r; i++) \
330 while ((cmp) (ps, pivot, (a)[j - 1]) < 0) \
332 (a)[j] = (a)[j - 1]; \
340 #define CHECK_SORTED(cmp,a,n) do { } while(0)
342 #define CHECK_SORTED(cmp,a,n) \
345 for (i = 0; i < (n) - 1; i++) \
346 assert ((cmp) (ps, (a)[i], (a)[i + 1]) <= 0); \
350 #define SORT(T,cmp,a,n) \
354 QUICKSORT (T, cmp, aa, nn); \
355 INSERTION_SORT (T, cmp, aa, nn); \
356 assert (ps->ihead == ps->indices); \
357 CHECK_SORTED (cmp, aa, nn); \
360 #define WRDSZ (sizeof (long) * 8)
362 typedef unsigned Flt; /* 32 bit deterministic soft float */
363 typedef Flt Act; /* clause and variable activity */
364 typedef struct Blk Blk; /* allocated memory block */
365 typedef struct Cls Cls; /* clause */
366 typedef struct Lit Lit; /* literal */
367 typedef struct Rnk Rnk; /* variable to score mapping */
368 typedef signed char Val; /* TRUE, UNDEF, FALSE */
369 typedef struct Var Var; /* variable */
371 typedef struct Trd Trd; /* trace data for clauses */
372 typedef struct Zhn Zhn; /* compressed chain (=zain) data */
373 typedef unsigned char Znt; /* compressed antecedent data */
376 #ifdef NO_BINARY_CLAUSES
377 typedef struct Ltk Ltk;
382 unsigned count : WRDSZ == 32 ? 27 : 32;
383 unsigned ldsize : WRDSZ == 32 ? 5 : 32;
394 unsigned mark : 1; /*bit 1*/
395 unsigned resolved : 1; /*bit 2*/
396 unsigned phase : 1; /*bit 3*/
397 unsigned assigned : 1; /*bit 4*/
398 unsigned used : 1; /*bit 5*/
399 unsigned failed : 1; /*bit 6*/
400 unsigned internal : 1; /*bit 7*/
401 unsigned usedefphase : 1; /*bit 8*/
402 unsigned defphase : 1; /*bit 9*/
403 unsigned msspos : 1; /*bit 10*/
404 unsigned mssneg : 1; /*bit 11*/
405 unsigned humuspos : 1; /*bit 12*/
406 unsigned humusneg : 1; /*bit 13*/
407 unsigned partial : 1; /*bit 14*/
409 unsigned core : 1; /*bit 15*/
423 unsigned pos : 30; /* 0 iff not on heap */
424 unsigned moreimportant : 1;
425 unsigned lessimportant : 1;
432 unsigned collect:1; /* bit 1 */
433 unsigned learned:1; /* bit 2 */
434 unsigned locked:1; /* bit 3 */
435 unsigned used:1; /* bit 4 */
437 unsigned connected:1; /* bit 5 */
440 unsigned collected:1; /* bit 6 */
441 unsigned core:1; /* bit 7 */
444 #define LDMAXGLUE 25 /* 32 - 7 */
445 #define MAXGLUE ((1<<LDMAXGLUE)-1)
447 unsigned glue:LDMAXGLUE;
474 size_t size; /* this is what we really use */
475 void *as_two_ptrs[2]; /* 2 * sizeof (void*) alignment of data */
479 char data[BLK_FILL_BYTES];
502 enum Phase defaultphase;
503 int last_sat_call_result;
521 #ifdef NO_BINARY_CLAUSES
524 int implvalid, cimplvalid;
528 Lit **trail, **thead, **eot, **ttail, ** ttail2;
532 unsigned adecidelevel;
533 Lit **als, **alshead, **alstail, **eoals;
534 Lit **CLS, **clshead, **eocls;
535 int *rils, *rilshead, *eorils;
536 int *cils, *cilshead, *eocils;
537 int *fals, *falshead, *eofals;
539 int *mssass, szmssass;
540 int *mcsass, nmcsass, szmcsass;
542 Lit *failed_assumption;
543 int extracted_all_failed_assumptions;
544 Rnk **heap, **hhead, **eoh;
545 Cls **oclauses, **ohead, **eoo; /* original clauses */
546 Cls **lclauses, **lhead, ** EOL; /* learned clauses */
547 int * soclauses, * sohead, * eoso; /* saved original clauses */
552 Zhn **zhains, **zhead, **eoz;
561 Lit **added, **ahead, **eoa;
562 Var **marked, **mhead, **eom;
563 Var **dfs, **dhead, **eod;
564 Cls **resolved, **rhead, **eor;
565 unsigned char *levels, *levelshead, *eolevels;
566 unsigned *dused, *dusedhead, *eodused;
567 unsigned char *buffer, *bhead, *eob;
568 Act vinc, lscore, ilvinc, ifvinc;
572 Act cinc, lcinc, ilcinc, fcinc;
574 size_t current_bytes;
577 double seconds, flseconds;
580 int measurealltimeinlib;
595 unsigned lreduceadjustcnt;
596 unsigned lreduceadjustinc;
597 unsigned lastreduceconflicts;
598 unsigned llocked; /* locked large learned clauses */
605 unsigned lubymaxdelta;
608 unsigned long long lsimplify;
609 unsigned long long propagations;
610 unsigned long long lpropagations;
611 unsigned fixed; /* top level assignments */
614 unsigned ifailedlits;
615 unsigned efailedlits;
619 unsigned long long flprops;
620 unsigned long long floopsed, fltried, flskipped;
622 unsigned long long fllimit;
630 unsigned noclauses; /* current number large original clauses */
631 unsigned nlclauses; /* current number large learned clauses */
632 unsigned olits; /* current literals in large original clauses */
633 unsigned llits; /* current literals in large learned clauses */
634 unsigned oadded; /* added original clauses */
635 unsigned ladded; /* added learned clauses */
636 unsigned loadded; /* added original large clauses */
637 unsigned lladded; /* added learned large clauses */
638 unsigned addedclauses; /* oadded + ladded */
639 unsigned vused; /* used variables */
640 unsigned llitsadded; /* added learned literals */
641 unsigned long long visits;
643 unsigned loused; /* used large original clauses */
644 unsigned llused; /* used large learned clauses */
645 unsigned long long bvisits;
646 unsigned long long tvisits;
647 unsigned long long lvisits;
648 unsigned long long othertrue;
649 unsigned long long othertrue2;
650 unsigned long long othertruel;
651 unsigned long long othertrue2u;
652 unsigned long long othertruelu;
653 unsigned long long ltraversals;
654 unsigned long long traversals;
656 unsigned long long antecedents;
660 unsigned assumptions;
665 unsigned long long derefs;
667 unsigned minimizedllits;
668 unsigned nonminimizedllits;
670 Lit *** ados, *** hados, *** eados;
675 unsigned adoconflicts;
676 unsigned adoconflictlimit;
680 unsigned long long flips;
682 unsigned long long FORCED;
683 unsigned long long assignments;
684 unsigned inclreduces;
685 unsigned staticphasedecisions;
686 unsigned skippedrestarts;
688 int * indices, * ihead, *eoi;
691 unsigned long long saved_flips;
692 unsigned saved_max_var;
693 unsigned min_flipped;
697 picosat_realloc eresize;
698 picosat_free edelete;
708 packflt (unsigned m, int e)
712 assert (FLTMINEXPONENT <= e);
713 assert (e <= FLTMAXEXPONENT);
714 res = m | ((e + 128) << 24);
719 base2flt (unsigned m, int e)
728 if (e <= FLTMINEXPONENT)
739 while (m >= FLTCARRY)
741 if (e >= FLTMAXEXPONENT)
750 return packflt (m, e);
754 addflt (Flt a, Flt b)
756 unsigned ma, mb, delta;
763 UNPACKFLT (a, ma, ea);
764 UNPACKFLT (b, mb, eb);
775 if (ea == FLTMAXEXPONENT)
782 assert (ma < FLTCARRY);
783 ma &= FLTMAXMANTISSA;
785 return packflt (ma, ea);
789 mulflt (Flt a, Flt b)
792 unsigned long long accu;
799 UNPACKFLT (a, ma, ea);
800 UNPACKFLT (b, mb, eb);
804 if (ea > FLTMAXEXPONENT)
807 if (ea < FLTMINEXPONENT)
814 if (accu >= FLTCARRY)
816 if (ea == FLTMAXEXPONENT)
822 if (accu >= FLTCARRY)
826 assert (accu < FLTCARRY);
827 assert (accu & FLTMSB);
832 return packflt (ma, ea);
836 ascii2flt (const char *str)
838 Flt ten = base2flt (10, 0);
839 Flt onetenth = base2flt (26843546, -28);
840 Flt res = ZEROFLT, tmp, base;
849 return INFFLT; /* better abort ? */
851 res = base2flt (ch - '0', 0);
859 return INFFLT; /* better abort? */
861 res = mulflt (res, ten);
862 tmp = base2flt (ch - '0', 0);
863 res = addflt (res, tmp);
871 return INFFLT; /* better abort ? */
874 tmp = mulflt (base2flt (ch - '0', 0), base);
875 res = addflt (res, tmp);
880 return INFFLT; /* better abort? */
882 base = mulflt (base, onetenth);
883 tmp = mulflt (base2flt (ch - '0', 0), base);
884 res = addflt (res, tmp);
891 #if defined(VISCORES)
905 for (i = e; i < 0; i++)
910 for (i = 0; i < e; i++)
922 return FLTEXPONENT (a) + 24;
926 cmpflt (Flt a, Flt b)
938 new (PS * ps, size_t size)
946 bytes = size + SIZE_OF_BLK;
949 b = ps->enew (ps->emgr, bytes);
953 ABORTIF (!b, "out of memory in 'new'");
955 b->header.size = size;
957 ps->current_bytes += size;
958 if (ps->current_bytes > ps->max_bytes)
959 ps->max_bytes = ps->current_bytes;
964 delete (PS * ps, void *void_ptr, size_t size)
976 b = PTR2BLK (void_ptr);
978 assert (size <= ps->current_bytes);
979 ps->current_bytes -= size;
981 assert (b->header.size == size);
983 bytes = size + SIZE_OF_BLK;
985 ps->edelete (ps->emgr, b, bytes);
991 resize (PS * ps, void *void_ptr, size_t old_size, size_t new_size)
993 size_t old_bytes, new_bytes;
996 b = PTR2BLK (void_ptr);
998 assert (old_size <= ps->current_bytes);
999 ps->current_bytes -= old_size;
1001 if ((old_bytes = old_size))
1003 assert (old_size && b && b->header.size == old_size);
1004 old_bytes += SIZE_OF_BLK;
1009 if ((new_bytes = new_size))
1010 new_bytes += SIZE_OF_BLK;
1013 b = ps->eresize (ps->emgr, b, old_bytes, new_bytes);
1015 b = realloc (b, new_bytes);
1023 ABORTIF (!b, "out of memory in 'resize'");
1025 b->header.size = new_size;
1028 ps->current_bytes += new_size;
1029 if (ps->current_bytes > ps->max_bytes)
1030 ps->max_bytes = ps->current_bytes;
1036 int2unsigned (int l)
1038 return (l < 0) ? 1 + 2 * -l : 2 * l;
1042 int2lit (PS * ps, int l)
1044 return ps->lits + int2unsigned (l);
1048 end_of_lits (Cls * c)
1050 return c->lits + c->size;
1053 #if !defined(NDEBUG) || defined(LOGGING)
1056 dumplits (PS * ps, Lit ** l, Lit ** end)
1065 else if (l + 1 == end)
1067 fprintf (ps->out, "%d ", LIT2INT (l[0]));
1071 assert (l + 2 <= end);
1072 first = (abs (LIT2INT (l[0])) > abs (LIT2INT (l[1])));
1073 fprintf (ps->out, "%d ", LIT2INT (l[first]));
1074 fprintf (ps->out, "%d ", LIT2INT (l[!first]));
1075 for (p = l + 2; p < end; p++)
1076 fprintf (ps->out, "%d ", LIT2INT (*p));
1079 fputc ('0', ps->out);
1083 dumpcls (PS * ps, Cls * c)
1089 end = end_of_lits (c);
1090 dumplits (ps, c->lits, end);
1093 fprintf (ps->out, " clause(%u)", CLS2IDX (c));
1097 fputs ("DECISION", ps->out);
1101 dumpclsnl (PS * ps, Cls * c)
1104 fputc ('\n', ps->out);
1112 for (p = SOC; p != EOC; p = NXC (p))
1131 delete_prefix (PS * ps)
1136 delete (ps, ps->prefix, strlen (ps->prefix) + 1);
1141 new_prefix (PS * ps, const char * str)
1145 ps->prefix = new (ps, strlen (str) + 1);
1146 strcpy (ps->prefix, str);
1151 picosat_malloc pnew, picosat_realloc presize, picosat_free pdelete)
1156 int count = 3 - !pnew - !presize - !pdelete;
1158 ABORTIF (count && !pnew, "API usage: missing 'picosat_set_new'");
1159 ABORTIF (count && !presize, "API usage: missing 'picosat_set_resize'");
1160 ABORTIF (count && !pdelete, "API usage: missing 'picosat_set_delete'");
1163 ps = pnew ? pnew (pmgr, sizeof *ps) : malloc (sizeof *ps);
1164 ABORTIF (!ps, "failed to allocate memory for PicoSAT manager");
1165 memset (ps, 0, sizeof *ps);
1169 ps->eresize = presize;
1170 ps->edelete = pdelete;
1174 ps->defaultphase = JWLPHASE;
1178 ps->lastrheader = -2;
1180 ps->adoconflictlimit = UINT_MAX;
1182 ps->min_flipped = UINT_MAX;
1184 NEWN (ps->lits, 2 * ps->size_vars);
1185 NEWN (ps->jwh, 2 * ps->size_vars);
1186 NEWN (ps->htps, 2 * ps->size_vars);
1188 NEWN (ps->dhtps, 2 * ps->size_vars);
1190 NEWN (ps->impls, 2 * ps->size_vars);
1191 NEWN (ps->vars, ps->size_vars);
1192 NEWN (ps->rnks, ps->size_vars);
1194 /* because '0' pos denotes not on heap
1196 ENLARGE (ps->heap, ps->hhead, ps->eoh);
1197 ps->hhead = ps->heap + 1;
1199 ps->vinc = base2flt (1, 0); /* initial var activity */
1200 ps->ifvinc = ascii2flt ("1.05"); /* var score rescore factor */
1202 ps->fvinc = ascii2flt ("0.9523809"); /* 1/f = 1/1.05 */
1203 ps->nvinc = ascii2flt ("0.0476191"); /* 1 - 1/f = 1 - 1/1.05 */
1205 ps->lscore = base2flt (1, 90); /* var activity rescore limit */
1206 ps->ilvinc = base2flt (1, -90); /* inverse of 'lscore' */
1208 ps->cinc = base2flt (1, 0); /* initial clause activity */
1209 ps->fcinc = ascii2flt ("1.001"); /* cls activity rescore factor */
1210 ps->lcinc = base2flt (1, 90); /* cls activity rescore limit */
1211 ps->ilcinc = base2flt (1, -90); /* inverse of 'ilcinc' */
1213 ps->lreduceadjustcnt = ps->lreduceadjustinc = 100;
1214 ps->lpropagations = ~0ull;
1217 new_prefix (ps, "c ");
1221 #ifdef NO_BINARY_CLAUSES
1222 memset (&ps->impl, 0, sizeof (ps->impl));
1225 memset (&ps->cimpl, 0, sizeof (ps->impl));
1230 ps->fviscores = popen (
1231 "/usr/bin/gnuplot -background black"
1232 " -xrm 'gnuplot*textColor:white'"
1233 " -xrm 'gnuplot*borderColor:white'"
1234 " -xrm 'gnuplot*axisColor:white'"
1236 fprintf (ps->fviscores, "unset key\n");
1237 // fprintf (ps->fviscores, "set log y\n");
1238 fflush (ps->fviscores);
1239 system ("rm -rf /tmp/picosat-viscores");
1240 system ("mkdir /tmp/picosat-viscores");
1241 system ("mkdir /tmp/picosat-viscores/data");
1243 system ("mkdir /tmp/picosat-viscores/gif");
1244 fprintf (ps->fviscores,
1245 "set terminal gif giant animate opt size 1024,768 x000000 xffffff"
1248 fprintf (ps->fviscores,
1249 "set output \"/tmp/picosat-viscores/gif/animated.gif\"\n");
1252 ps->defaultphase = JWLPHASE;
1254 ps->last_sat_call_result = 0;
1260 bytes_clause (PS * ps, unsigned size, unsigned learned)
1265 res += size * sizeof (Lit *);
1266 res -= 2 * sizeof (Lit *);
1268 if (learned && size > 2)
1269 res += sizeof (Act); /* add activity */
1273 res += sizeof (Trd); /* add trace data */
1282 new_clause (PS * ps, unsigned size, unsigned learned)
1291 bytes = bytes_clause (ps, size, learned);
1292 tmp = new (ps, bytes);
1300 trd->idx = LIDX2IDX (ps->lhead - ps->lclauses);
1302 trd->idx = OIDX2IDX (ps->ohead - ps->oclauses);
1311 res->learned = learned;
1324 if (learned && size > 2)
1326 Act * p = CLS2ACT (res);
1334 delete_clause (PS * ps, Cls * c)
1341 bytes = bytes_clause (ps, c->size, c->learned);
1347 delete (ps, trd, bytes);
1351 delete (ps, c, bytes);
1355 delete_clauses (PS * ps)
1358 for (p = SOC; p != EOC; p = NXC (p))
1360 delete_clause (ps, *p);
1362 DELETEN (ps->oclauses, ps->eoo - ps->oclauses);
1363 DELETEN (ps->lclauses, ps->EOL - ps->lclauses);
1365 ps->ohead = ps->eoo = ps->lhead = ps->EOL = 0;
1371 delete_zhain (PS * ps, Zhn * zhain)
1378 for (p = znt; *p; p++)
1381 delete (ps, zhain, sizeof (Zhn) + (p - znt) + 1);
1385 delete_zhains (PS * ps)
1388 for (p = ps->zhains; p < ps->zhead; p++)
1390 delete_zhain (ps, z);
1392 DELETEN (ps->zhains, ps->eoz - ps->zhains);
1393 ps->eoz = ps->zhead = 0;
1398 #ifdef NO_BINARY_CLAUSES
1400 lrelease (PS * ps, Ltk * stk)
1403 DELETEN (stk->start, (1 << (stk->ldsize)));
1404 memset (stk, 0, sizeof (*stk));
1414 for (p = a; *p; p++)
1420 resetadoconflict (PS * ps)
1422 assert (ps->adoconflict);
1423 delete_clause (ps->adoconflict);
1424 ps->adoconflict = 0;
1428 reset_ados (PS * ps)
1432 for (p = ps->ados; p < ps->hados; p++)
1433 DELETEN (*p, llength (*p) + 1);
1435 DELETEN (ps->ados, ps->eados - ps->ados);
1436 ps->hados = ps->eados = 0;
1438 DELETEN (ps->adotab, ps->szadotab);
1439 ps->szadotab = ps->nadotab = 0;
1441 if (ps->adoconflict)
1442 resetadoconflict (ps);
1444 ps->adoconflicts = 0;
1445 ps->adoconflictlimit = UINT_MAX;
1446 ps->adodisabled = 0;
1455 ps->state == RESET, "API usage: reset without initialization");
1457 delete_clauses (ps);
1461 #ifdef NO_BINARY_CLAUSES
1464 for (i = 2; i <= 2 * ps->max_var + 1; i++)
1465 lrelease (ps, ps->impls + i);
1472 DELETEN (ps->saved, ps->saved_size);
1474 DELETEN (ps->htps, 2 * ps->size_vars);
1476 DELETEN (ps->dhtps, 2 * ps->size_vars);
1478 DELETEN (ps->impls, 2 * ps->size_vars);
1479 DELETEN (ps->lits, 2 * ps->size_vars);
1480 DELETEN (ps->jwh, 2 * ps->size_vars);
1481 DELETEN (ps->vars, ps->size_vars);
1482 DELETEN (ps->rnks, ps->size_vars);
1483 DELETEN (ps->trail, ps->eot - ps->trail);
1484 DELETEN (ps->heap, ps->eoh - ps->heap);
1485 DELETEN (ps->als, ps->eoals - ps->als);
1486 DELETEN (ps->CLS, ps->eocls - ps->CLS);
1487 DELETEN (ps->rils, ps->eorils - ps->rils);
1488 DELETEN (ps->cils, ps->eocils - ps->cils);
1489 DELETEN (ps->fals, ps->eofals - ps->fals);
1490 DELETEN (ps->mass, ps->szmass);
1491 DELETEN (ps->mssass, ps->szmssass);
1492 DELETEN (ps->mcsass, ps->szmcsass);
1493 DELETEN (ps->humus, ps->szhumus);
1494 DELETEN (ps->added, ps->eoa - ps->added);
1495 DELETEN (ps->marked, ps->eom - ps->marked);
1496 DELETEN (ps->dfs, ps->eod - ps->dfs);
1497 DELETEN (ps->resolved, ps->eor - ps->resolved);
1498 DELETEN (ps->levels, ps->eolevels - ps->levels);
1499 DELETEN (ps->dused, ps->eodused - ps->dused);
1500 DELETEN (ps->buffer, ps->eob - ps->buffer);
1501 DELETEN (ps->indices, ps->eoi - ps->indices);
1502 DELETEN (ps->soclauses, ps->eoso - ps->soclauses);
1504 delete (ps, ps->rline[0], ps->szrline);
1505 delete (ps, ps->rline[1], ps->szrline);
1506 assert (getenv ("LEAK") || !ps->current_bytes); /* found leak if failing */
1508 pclose (ps->fviscores);
1511 ps->edelete (ps->emgr, ps, sizeof *ps);
1517 tpush (PS * ps, Lit * lit)
1519 assert (ps->lits < lit && lit <= ps->lits + 2* ps->max_var + 1);
1520 if (ps->thead == ps->eot)
1522 unsigned ttail2count = ps->ttail2 - ps->trail;
1523 unsigned ttailcount = ps->ttail - ps->trail;
1525 unsigned ttailadocount = ps->ttailado - ps->trail;
1527 ENLARGE (ps->trail, ps->thead, ps->eot);
1528 ps->ttail = ps->trail + ttailcount;
1529 ps->ttail2 = ps->trail + ttail2count;
1531 ps->ttailado = ps->trail + ttailadocount;
1539 assign_reason (PS * ps, Var * v, Cls * reason)
1541 #if defined(NO_BINARY_CLAUSES) && !defined(NDEBUG)
1542 assert (reason != &ps->impl);
1550 assign_phase (PS * ps, Lit * lit)
1552 unsigned new_phase, idx;
1553 Var * v = LIT2VAR (lit);
1556 /* In 'simplifying' mode we only need to keep 'min_flipped' up to date if
1557 * we force assignments on the top level. The other assignments will be
1558 * undone and thus we can keep the old saved value of the phase.
1560 if (!ps->LEVEL || !ps->simplifying)
1563 new_phase = (LIT2SGN (lit) > 0);
1567 ps->sdflips -= ps->sdflips/FFLIPPED;
1569 if (new_phase != v->phase)
1571 assert (FFLIPPEDPREC >= FFLIPPED);
1572 ps->sdflips += FFLIPPEDPREC / FFLIPPED;
1575 idx = LIT2IDX (lit);
1576 if (idx < ps->min_flipped)
1577 ps->min_flipped = idx;
1579 NOLOG (fprintf (ps->out,
1581 ps->prefix, LIT2INT (lit)));
1585 v->phase = new_phase;
1590 NOTLIT (lit)->val = FALSE;
1594 assign (PS * ps, Lit * lit, Cls * reason)
1596 Var * v = LIT2VAR (lit);
1597 assert (lit->val == UNDEF);
1601 v->level = ps->LEVEL;
1602 assign_phase (ps, lit);
1603 assign_reason (ps, v, reason);
1608 cmp_added (PS * ps, Lit * k, Lit * l)
1610 Val a = k->val, b = l->val;
1614 if (a == UNDEF && b != UNDEF)
1617 if (a != UNDEF && b == UNDEF)
1625 assert (b != UNDEF);
1626 res = v->level - u->level;
1628 return res; /* larger level first */
1631 res = cmpflt (VAR2RNK (u)->score, VAR2RNK (v)->score);
1633 return res; /* smaller activity first */
1635 return u - v; /* smaller index first */
1639 sorttwolits (Lit ** v)
1641 Lit * a = v[0], * b = v[1];
1653 sortlits (PS * ps, Lit ** v, unsigned size)
1656 sorttwolits (v); /* same order with and with out 'NO_BINARY_CLAUSES' */
1658 SORT (Lit *, cmp_added, v, size);
1661 #ifdef NO_BINARY_CLAUSES
1663 setimpl (PS * ps, Lit * a, Lit * b)
1665 assert (!ps->implvalid);
1666 assert (ps->impl.size == 2);
1668 ps->impl.lits[0] = a;
1669 ps->impl.lits[1] = b;
1671 sorttwolits (ps->impl.lits);
1684 setcimpl (PS * ps, Lit * a, Lit * b)
1686 assert (!ps->cimplvalid);
1687 assert (ps->cimpl.size == 2);
1689 ps->cimpl.lits[0] = a;
1690 ps->cimpl.lits[1] = b;
1692 sorttwolits (ps->cimpl.lits);
1699 resetcimpl (PS * ps)
1701 assert (ps->cimplvalid);
1708 cmp_ptr (PS * ps, void *l, void *k)
1711 return ((char*)l) - (char*)k; /* arbitrarily already reverse */
1715 cmp_rnk (Rnk * r, Rnk * s)
1717 if (!r->moreimportant && s->moreimportant)
1720 if (r->moreimportant && !s->moreimportant)
1723 if (!r->lessimportant && s->lessimportant)
1726 if (r->lessimportant && !s->lessimportant)
1729 if (r->score < s->score)
1732 if (r->score > s->score)
1735 return -cmp_ptr (0, r, s);
1739 hup (PS * ps, Rnk * v)
1745 assert (!ps->simplifying);
1751 assert (vpos < ps->hhead - ps->heap);
1752 assert (ps->heap[vpos] == v);
1760 if (cmp_rnk (u, v) > 0)
1773 static Cls *add_simplified_clause (PS *, int);
1776 add_antecedent (PS * ps, Cls * c)
1780 #ifdef NO_BINARY_CLAUSES
1781 if (ISLITREASON (c))
1786 #elif defined(STATS) && defined(TRACE)
1789 if (ps->rhead == ps->eor)
1790 ENLARGE (ps->resolved, ps->rhead, ps->eor);
1792 assert (ps->rhead < ps->eor);
1798 #ifdef NO_BINARY_CLAUSES
1799 #error "can not combine TRACE and NO_BINARY_CLAUSES"
1805 add_lit (PS * ps, Lit * lit)
1809 if (ps->ahead == ps->eoa)
1810 ENLARGE (ps->added, ps->ahead, ps->eoa);
1816 push_var_as_marked (PS * ps, Var * v)
1818 if (ps->mhead == ps->eom)
1819 ENLARGE (ps->marked, ps->mhead, ps->eom);
1825 mark_var (PS * ps, Var * v)
1829 push_var_as_marked (ps, v);
1832 #ifdef NO_BINARY_CLAUSES
1835 impl2reason (PS * ps, Lit * lit)
1839 other = ps->impl.lits[0];
1841 other = ps->impl.lits[1];
1842 assert (other->val == FALSE);
1843 res = LIT2REASON (NOTLIT (other));
1850 /* Whenever we have a top level derived unit we really should derive a unit
1851 * clause otherwise the resolutions in 'add_simplified_clause' become
1855 resolve_top_level_unit (PS * ps, Lit * lit, Cls * reason)
1857 unsigned count_resolved;
1858 Lit **p, **eol, *other;
1861 assert (ps->rhead == ps->resolved);
1862 assert (ps->ahead == ps->added);
1865 add_antecedent (ps, reason);
1869 eol = end_of_lits (reason);
1870 for (p = reason->lits; p < eol; p++)
1873 u = LIT2VAR (other);
1877 add_antecedent (ps, u->reason);
1881 /* Some of the literals could be assumptions. If at least one
1882 * variable is not an assumption, we should resolve.
1884 if (count_resolved >= 2)
1886 #ifdef NO_BINARY_CLAUSES
1887 if (reason == &ps->impl)
1890 reason = add_simplified_clause (ps, 1);
1891 #ifdef NO_BINARY_CLAUSES
1892 if (reason->size == 2)
1894 assert (reason == &ps->impl);
1895 reason = impl2reason (ps, lit);
1898 assign_reason (ps, v, reason);
1902 ps->ahead = ps->added;
1903 ps->rhead = ps->resolved;
1910 fixvar (PS * ps, Var * v)
1914 assert (VAR2LIT (v) != UNDEF);
1923 if (ps->simplifying)
1934 use_var (PS * ps, Var * v)
1944 assign_forced (PS * ps, Lit * lit, Cls * reason)
1949 assert (lit->val == UNDEF);
1954 assign (ps, lit, reason);
1956 #ifdef NO_BINARY_CLAUSES
1957 assert (reason != &ps->impl);
1958 if (ISLITREASON (reason))
1960 reason = setimpl (ps, lit, NOTLIT (REASON2LIT (reason)));
1964 LOG ( fprintf (ps->out,
1965 "%sassign %d at level %d by ",
1966 ps->prefix, LIT2INT (lit), ps->LEVEL);
1967 dumpclsnl (ps, reason));
1973 if (!ps->LEVEL && reason->size > 1)
1975 reason = resolve_top_level_unit (ps, lit, reason);
1979 #ifdef NO_BINARY_CLAUSES
1980 if (ISLITREASON (reason) || reason == &ps->impl)
1987 assert (!reason->locked);
1989 if (reason->learned && reason->size > 2)
1993 #ifdef NO_BINARY_CLAUSES
1994 if (reason == &ps->impl)
2002 #ifdef NO_BINARY_CLAUSES
2005 lpush (PS * ps, Lit * lit, Cls * c)
2007 int pos = (c->lits[0] == lit);
2008 Ltk * s = LIT2IMPLS (lit);
2009 unsigned oldsize, newsize;
2011 assert (c->size == 2);
2016 assert (!s->ldsize);
2021 oldsize = (1 << (s->ldsize));
2022 assert (s->count <= oldsize);
2023 if (s->count == oldsize)
2025 newsize = 2 * oldsize;
2026 RESIZEN (s->start, oldsize, newsize);
2031 s->start[s->count++] = c->lits[pos];
2037 connect_head_tail (PS * ps, Lit * lit, Cls * c)
2040 assert (c->size >= 1);
2043 #ifdef NO_BINARY_CLAUSES
2047 s = LIT2IMPLS (lit);
2053 if (c->lits[0] != lit)
2055 assert (c->size >= 2);
2056 assert (c->lits[1] == lit);
2067 zpush (PS * ps, Zhn * zhain)
2071 if (ps->zhead == ps->eoz)
2072 ENLARGE (ps->zhains, ps->zhead, ps->eoz);
2074 *ps->zhead++ = zhain;
2078 cmp_resolved (PS * ps, Cls * c, Cls * d)
2085 return CLS2IDX (c) - CLS2IDX (d);
2089 bpushc (PS * ps, unsigned char ch)
2091 if (ps->bhead == ps->eob)
2092 ENLARGE (ps->buffer, ps->bhead, ps->eob);
2098 bpushu (PS * ps, unsigned u)
2102 bpushc (ps, u | 0x80);
2110 bpushd (PS * ps, unsigned prev, unsigned this)
2113 assert (prev < this);
2114 delta = this - prev;
2121 unsigned prev, this, count, rcount;
2126 assert (ps->bhead == ps->buffer);
2127 assert (ps->rhead > ps->resolved);
2129 rcount = ps->rhead - ps->resolved;
2130 SORT (Cls *, cmp_resolved, ps->resolved, rcount);
2133 for (p = ps->resolved; p < ps->rhead; p++)
2136 this = CLS2TRD (c)->idx;
2137 bpushd (ps, prev, this);
2142 count = ps->bhead - ps->buffer;
2144 res = new (ps, sizeof (Zhn) + count);
2147 memcpy (res->znt, ps->buffer, count);
2149 ps->bhead = ps->buffer;
2151 ps->znts += count - 1;
2159 add_resolved (PS * ps, int learned)
2161 #if defined(STATS) || defined(TRACE)
2164 for (p = ps->resolved; p < ps->rhead; p++)
2185 if (learned && ps->trace)
2190 ps->rhead = ps->resolved;
2194 incjwh (PS * ps, Cls * c)
2196 Lit **p, *lit, ** eol;
2202 eol = end_of_lits (c);
2204 for (p = c->lits; p < eol; p++)
2209 if (val && ps->LEVEL > 0)
2223 inc = base2flt (1, -size);
2225 for (p = c->lits; p < eol; p++)
2229 sum = addflt (*f, inc);
2235 write_rup_header (PS * ps, FILE * file)
2240 sprintf (line, "%%RUPD32 %u %u", ps->rupvariables, ps->rupclauses);
2243 for (i = 255 - strlen (line); i >= 0; i--)
2251 add_simplified_clause (PS * ps, int learned)
2253 unsigned num_true, num_undef, num_false, size, count_resolved;
2254 Lit **p, **q, *lit, ** end;
2255 unsigned litlevel, glue;
2260 #if !defined(NDEBUG) && defined(TRACE)
2268 size = ps->ahead - ps->added;
2270 add_resolved (ps, learned);
2275 ps->llitsadded += size;
2295 assert (ps->addedclauses == ps->ladded + ps->oadded);
2297 #ifdef NO_BINARY_CLAUSES
2299 res = setimpl (ps, ps->added[0], ps->added[1]);
2303 sortlits (ps, ps->added, size);
2307 if (ps->lhead == ps->EOL)
2309 ENLARGE (ps->lclauses, ps->lhead, ps->EOL);
2311 /* A very difficult to find bug, which only occurs if the
2312 * learned clauses stack is immediately allocated before the
2313 * original clauses stack without padding. In this case, we
2314 * have 'SOC == EOC', which terminates all loops using the
2315 * idiom 'for (p = SOC; p != EOC; p = NXC(p))' immediately.
2316 * Unfortunately this occurred in 'fix_clause_lits' after
2317 * using a recent version of the memory allocator of 'Google'
2318 * perftools in the context of one large benchmark for
2319 * our SMT solver 'Boolector'.
2321 if (ps->EOL == ps->oclauses)
2322 ENLARGE (ps->lclauses, ps->lhead, ps->EOL);
2325 #if !defined(NDEBUG) && defined(TRACE)
2326 idx = LIDX2IDX (ps->lhead - ps->lclauses);
2331 if (ps->ohead == ps->eoo)
2333 ENLARGE (ps->oclauses, ps->ohead, ps->eoo);
2334 if (ps->EOL == ps->oclauses)
2335 ENLARGE (ps->oclauses, ps->ohead, ps->eoo); /* ditto */
2338 #if !defined(NDEBUG) && defined(TRACE)
2339 idx = OIDX2IDX (ps->ohead - ps->oclauses);
2343 assert (ps->EOL != ps->oclauses); /* ditto */
2345 res = new_clause (ps, size, learned);
2351 assert (ps->dusedhead == ps->dused);
2353 for (p = ps->added; p < ps->ahead; p++)
2358 litlevel = LIT2VAR (lit)->level;
2359 assert (litlevel <= ps->LEVEL);
2360 while (ps->levels + litlevel >= ps->levelshead)
2362 if (ps->levelshead >= ps->eolevels)
2363 ENLARGE (ps->levels, ps->levelshead, ps->eolevels);
2364 assert (ps->levelshead < ps->eolevels);
2365 *ps->levelshead++ = 0;
2367 if (!ps->levels[litlevel])
2369 if (ps->dusedhead >= ps->eodused)
2370 ENLARGE (ps->dused, ps->dusedhead, ps->eodused);
2371 assert (ps->dusedhead < ps->eodused);
2372 *ps->dusedhead++ = litlevel;
2373 ps->levels[litlevel] = 1;
2381 while (ps->dusedhead > ps->dused)
2383 litlevel = *--ps->dusedhead;
2384 assert (ps->levels + litlevel < ps->levelshead);
2385 assert (ps->levels[litlevel]);
2386 ps->levels[litlevel] = 0;
2390 assert (glue <= MAXGLUE);
2393 #if !defined(NDEBUG) && defined(TRACE)
2395 assert (CLS2IDX (res) == idx);
2402 #if !defined(NDEBUG) && defined(TRACE)
2403 if (ps->trace && learned)
2404 assert (ps->zhead - ps->zhains == ps->lhead - ps->lclauses);
2406 assert (ps->lhead != ps->oclauses); /* ditto */
2409 if (learned && ps->rup)
2411 if (!ps->rupstarted)
2413 write_rup_header (ps, ps->rup);
2418 num_true = num_undef = num_false = 0;
2421 for (p = ps->added; p < ps->ahead; p++)
2426 if (learned && ps->rup)
2427 fprintf (ps->rup, "%d ", LIT2INT (lit));
2431 num_true += (val == TRUE);
2432 num_undef += (val == UNDEF);
2433 num_false += (val == FALSE);
2435 assert (num_false + num_true + num_undef == size);
2437 if (learned && ps->rup)
2438 fputs ("0\n", ps->rup);
2440 ps->ahead = ps->added; /* reset */
2442 if (!reentered) // TODO merge
2445 assert (size <= 2 || !reentered); // TODO remove
2446 connect_head_tail (ps, res->lits[0], res);
2448 connect_head_tail (ps, res->lits[1], res);
2457 #ifdef NO_BINARY_CLAUSES
2464 LOG ( fprintf (ps->out, "%s%s ", ps->prefix, learned ? "learned" : "original");
2465 dumpclsnl (ps, res));
2467 /* Shrink clause by resolving it against top level assignments.
2469 if (!ps->LEVEL && num_false > 0)
2471 assert (ps->ahead == ps->added);
2472 assert (ps->rhead == ps->resolved);
2475 add_antecedent (ps, res);
2477 end = end_of_lits (res);
2478 for (p = res->lits; p < end; p++)
2484 if (lit->val == FALSE)
2486 add_antecedent (ps, v->reason);
2493 assert (count_resolved >= 2);
2496 #ifdef NO_BINARY_CLAUSES
2497 if (res == &ps->impl)
2501 goto REENTER; /* and return simplified clause */
2504 if (!num_true && num_undef == 1) /* unit clause */
2507 for (p = res->lits; p < res->lits + size; p++)
2509 if ((*p)->val == UNDEF)
2518 #ifdef NO_BINARY_CLAUSES
2521 Lit * other = res->lits[0];
2523 other = res->lits[1];
2525 assert (other->val == FALSE);
2526 reason = LIT2REASON (NOTLIT (other));
2529 assign_forced (ps, lit, reason);
2533 if (num_false == size && !ps->conflict)
2535 #ifdef NO_BINARY_CLAUSES
2536 if (res == &ps->impl)
2537 ps->conflict = setcimpl (ps, res->lits[0], res->lits[1]);
2543 if (!learned && !num_true && num_undef)
2546 #ifdef NO_BINARY_CLAUSES
2547 if (res == &ps->impl)
2554 trivial_clause (PS * ps)
2556 Lit **p, **q, *prev;
2559 SORT (Lit *, cmp_ptr, ps->added, ps->ahead - ps->added);
2563 for (p = q; p < ps->ahead; p++)
2569 if (prev == this) /* skip repeated literals */
2572 /* Top level satisfied ?
2574 if (this->val == TRUE && !v->level)
2577 if (prev == NOTLIT (this))/* found pair of dual literals */
2583 ps->ahead = q; /* shrink */
2589 simplify_and_add_original_clause (PS * ps)
2591 #ifdef NO_BINARY_CLAUSES
2594 if (trivial_clause (ps))
2596 ps->ahead = ps->added;
2598 if (ps->ohead == ps->eoo)
2599 ENLARGE (ps->oclauses, ps->ohead, ps->eoo);
2608 if (ps->CLS != ps->clshead)
2609 add_lit (ps, NOTLIT (ps->clshead[-1]));
2611 #ifdef NO_BINARY_CLAUSES
2614 add_simplified_clause (ps, 0);
2615 #ifdef NO_BINARY_CLAUSES
2616 if (c == &ps->impl) assert (!ps->implvalid);
2626 unsigned len = ps->ahead - ps->added;
2627 Lit ** ado, ** p, ** q, *lit;
2631 assert (!ps->trace);
2634 ABORTIF (ps->ados < ps->hados && llength (ps->ados[0]) != len,
2635 "internal: non matching all different constraint object lengths");
2637 if (ps->hados == ps->eados)
2638 ENLARGE (ps->ados, ps->hados, ps->eados);
2640 NEWN (ado, len + 1);
2646 while (p < ps->ahead)
2651 "internal: variable in multiple all different objects");
2653 if (!u && !lit->val)
2658 assert (q == ado + len);
2661 /* TODO simply do a conflict test as in propado */
2665 "adding fully instantiated all different object not implemented yet");
2668 assert (u->inado == ado);
2672 ps->ahead = ps->added;
2678 hdown (PS * ps, Rnk * r)
2680 unsigned end, rpos, cpos, opos;
2683 assert (r->pos > 0);
2684 assert (ps->heap[r->pos] == r);
2686 end = ps->hhead - ps->heap;
2696 child = ps->heap[cpos];
2698 if (cmp_rnk (r, child) < 0)
2702 other = ps->heap[opos];
2704 if (cmp_rnk (child, other) < 0)
2711 else if (opos < end)
2713 child = ps->heap[opos];
2715 if (cmp_rnk (r, child) >= 0)
2723 ps->heap[rpos] = child;
2735 assert (ps->hhead > ps->heap + 1);
2745 assert (ps->hhead > ps->heap + 1);
2750 end = --ps->hhead - ps->heap;
2754 last = ps->heap[end];
2756 ps->heap[last->pos = 1] = last;
2763 hpush (PS * ps, Rnk * r)
2767 if (ps->hhead == ps->eoh)
2768 ENLARGE (ps->heap, ps->hhead, ps->eoh);
2770 r->pos = ps->hhead++ - ps->heap;
2771 ps->heap[r->pos] = r;
2776 fix_trail_lits (PS * ps, long delta)
2779 for (p = ps->trail; p < ps->thead; p++)
2783 #ifdef NO_BINARY_CLAUSES
2785 fix_impl_lits (PS * ps, long delta)
2790 for (s = ps->impls + 2; s <= ps->impls + 2 * ps->max_var + 1; s++)
2791 for (p = s->start; p < s->start + s->count; p++)
2797 fix_clause_lits (PS * ps, long delta)
2800 Lit **q, *lit, **eol;
2802 for (p = SOC; p != EOC; p = NXC (p))
2809 eol = end_of_lits (clause);
2812 assert (q - clause->lits <= (int) clause->size);
2821 fix_added_lits (PS * ps, long delta)
2824 for (p = ps->added; p < ps->ahead; p++)
2829 fix_assumed_lits (PS * ps, long delta)
2832 for (p = ps->als; p < ps->alshead; p++)
2837 fix_cls_lits (PS * ps, long delta)
2840 for (p = ps->CLS; p < ps->clshead; p++)
2845 fix_heap_rnks (PS * ps, long delta)
2849 for (p = ps->heap + 1; p < ps->hhead; p++)
2856 fix_ado (long delta, Lit ** ado)
2859 for (p = ado; *p; p++)
2864 fix_ados (PS * ps, long delta)
2868 for (p = ps->ados; p < ps->hados; p++)
2869 fix_ado (delta, *p);
2875 enlarge (PS * ps, unsigned new_size_vars)
2877 long rnks_delta, lits_delta;
2878 Lit *old_lits = ps->lits;
2879 Rnk *old_rnks = ps->rnks;
2881 RESIZEN (ps->lits, 2 * ps->size_vars, 2 * new_size_vars);
2882 RESIZEN (ps->jwh, 2 * ps->size_vars, 2 * new_size_vars);
2883 RESIZEN (ps->htps, 2 * ps->size_vars, 2 * new_size_vars);
2885 RESIZEN (ps->dhtps, 2 * ps->size_vars, 2 * new_size_vars);
2887 RESIZEN (ps->impls, 2 * ps->size_vars, 2 * new_size_vars);
2888 RESIZEN (ps->vars, ps->size_vars, new_size_vars);
2889 RESIZEN (ps->rnks, ps->size_vars, new_size_vars);
2891 if ((lits_delta = ps->lits - old_lits))
2893 fix_trail_lits (ps, lits_delta);
2894 fix_clause_lits (ps, lits_delta);
2895 fix_added_lits (ps, lits_delta);
2896 fix_assumed_lits (ps, lits_delta);
2897 fix_cls_lits (ps, lits_delta);
2898 #ifdef NO_BINARY_CLAUSES
2899 fix_impl_lits (ps, lits_delta);
2902 fix_ados (ps, lits_delta);
2906 if ((rnks_delta = ps->rnks - old_rnks))
2908 fix_heap_rnks (ps, rnks_delta);
2911 assert (ps->mhead == ps->marked);
2913 ps->size_vars = new_size_vars;
2917 unassign (PS * ps, Lit * lit)
2923 assert (lit->val == TRUE);
2925 LOG ( fprintf (ps->out, "%sunassign %d\n", ps->prefix, LIT2INT (lit)));
2930 #ifdef NO_BINARY_CLAUSES
2931 assert (reason != &ps->impl);
2932 if (ISLITREASON (reason))
2940 assert (reason->locked);
2942 if (reason->learned && reason->size > 2)
2944 assert (ps->llocked > 0);
2950 NOTLIT (lit)->val = UNDEF;
2958 Cls * p, * next, ** q;
2960 q = LIT2DHTPS (lit);
2966 Lit * other = p->lits[0];
2975 assert (p->lits[1] == lit);
2980 *q = *LIT2HTPS (other);
2981 *LIT2HTPS (other) = p;
2990 assert (ps->nadotab);
2991 assert (*v->adotabpos == v->ado);
3002 var2reason (PS * ps, Var * var)
3004 Cls * res = var->reason;
3005 #ifdef NO_BINARY_CLAUSES
3006 Lit * this, * other;
3007 if (ISLITREASON (res))
3009 this = VAR2LIT (var);
3010 if (this->val == FALSE)
3011 this = NOTLIT (this);
3013 other = REASON2LIT (res);
3014 assert (other->val == TRUE);
3015 assert (this->val == TRUE);
3016 res = setimpl (ps, NOTLIT (other), this);
3025 mark_clause_to_be_collected (Cls * c)
3027 assert (!c->collect);
3032 undo (PS * ps, unsigned new_level)
3037 while (ps->thead > ps->trail)
3041 if (v->level == new_level)
3043 ps->thead++; /* fix pre decrement */
3050 ps->LEVEL = new_level;
3051 ps->ttail = ps->thead;
3052 ps->ttail2 = ps->thead;
3054 ps->ttailado = ps->thead;
3057 #ifdef NO_BINARY_CLAUSES
3058 if (ps->conflict == &ps->cimpl)
3062 if (ps->conflict && ps->conflict == ps->adoconflict)
3063 resetadoconflict (ps);
3065 ps->conflict = ps->mtcls;
3066 if (ps->LEVEL < ps->adecidelevel)
3068 assert (ps->als < ps->alshead);
3069 ps->adecidelevel = 0;
3070 ps->alstail = ps->als;
3072 LOG ( fprintf (ps->out, "%sback to level %u\n", ps->prefix, ps->LEVEL));
3078 clause_satisfied (Cls * c)
3080 Lit **p, **eol, *lit;
3082 eol = end_of_lits (c);
3083 for (p = c->lits; p < eol; p++)
3086 if (lit->val == TRUE)
3094 original_clauses_satisfied (PS * ps)
3098 for (p = ps->oclauses; p < ps->ohead; p++)
3108 assert (clause_satisfied (c));
3113 assumptions_satisfied (PS * ps)
3117 for (p = ps->als; p < ps->alshead; p++)
3120 assert (lit->val == TRUE);
3129 double now = picosat_time_stamp ();
3130 double delta = now - ps->entered;
3131 delta = (delta < 0) ? 0 : delta;
3132 ps->seconds += delta;
3139 return ps->current_bytes / (double) (1 << 20);
3145 return ps->decisions ? ps->levelsum / ps->decisions : 0.0;
3151 assert (ps->lastrheader <= ps->reports);
3153 if (ps->lastrheader == ps->reports)
3156 ps->lastrheader = ps->reports;
3158 fprintf (ps->out, "%s\n", ps->prefix);
3159 fprintf (ps->out, "%s %s\n", ps->prefix, ps->rline[0]);
3160 fprintf (ps->out, "%s %s\n", ps->prefix, ps->rline[1]);
3161 fprintf (ps->out, "%s\n", ps->prefix);
3165 dynamic_flips_per_assignment_per_mille (PS * ps)
3167 assert (FFLIPPEDPREC >= 1000);
3168 return ps->sdflips / (FFLIPPEDPREC / 1000);
3174 high_agility (PS * ps)
3176 return dynamic_flips_per_assignment_per_mille (ps) >= 200;
3180 very_high_agility (PS * ps)
3182 return dynamic_flips_per_assignment_per_mille (ps) >= 250;
3188 medium_agility (PS * ps)
3190 return dynamic_flips_per_assignment_per_mille (ps) >= 230;
3201 if (ps->reports < 0)
3203 /* strip trailing white space
3205 for (x = 0; x <= 1; x++)
3207 p = ps->rline[x] + strlen (ps->rline[x]);
3208 while (p-- > ps->rline[x])
3220 fputc ('\n', ps->out);
3226 relemhead (PS * ps, const char * name, int fp, double val)
3228 int x, y, len, size;
3232 if (ps->reports < 0)
3235 y = (ps->RCOUNT / 2) * 12 + x * 6;
3237 if (ps->RCOUNT == 1)
3238 sprintf (ps->rline[1], "%6s", "");
3240 len = strlen (name);
3241 while (ps->szrline <= len + y + 1)
3243 size = ps->szrline ? 2 * ps->szrline : 128;
3244 ps->rline[0] = resize (ps, ps->rline[0], ps->szrline, size);
3245 ps->rline[1] = resize (ps, ps->rline[1], ps->szrline, size);
3249 fmt = (len <= 6) ? "%6s%10s" : "%-10s%4s";
3250 sprintf (ps->rline[x] + y, fmt, name, "");
3256 if (val > -100 && (tmp = val * 10.0 - 0.5) > -1000.0)
3258 fprintf (ps->out, "-%4.1f ", -tmp / 10.0);
3262 tmp = -val / 10.0 + 0.5;
3270 fprintf (ps->out, "-%2ue%u ", tmp, e);
3275 if (fp && val < 1000 && (tmp = val * 10.0 + 0.5) < 10000)
3277 fprintf (ps->out, "%5.1f ", tmp / 10.0);
3279 else if (!fp && (tmp = val) < 100000)
3281 fprintf (ps->out, "%5u ", tmp);
3285 tmp = val / 10.0 + 0.5;
3294 fprintf (ps->out, "%3ue%u ", tmp, e);
3302 relem (PS * ps, const char *name, int fp, double val)
3305 relemhead (ps, name, fp, val);
3311 reduce_limit_on_lclauses (PS * ps)
3313 unsigned res = ps->lreduce;
3319 report (PS * ps, int replevel, char type)
3323 if (ps->verbosity < replevel)
3331 for (rounds = (ps->reports < 0) ? 2 : 1; rounds; rounds--)
3333 if (ps->reports >= 0)
3334 fprintf (ps->out, "%s%c ", ps->prefix, type);
3336 relem (ps, "seconds", 1, ps->seconds);
3337 relem (ps, "level", 1, avglevel (ps));
3338 assert (ps->fixed <= ps->max_var);
3339 relem (ps, "variables", 0, ps->max_var - ps->fixed);
3340 relem (ps, "used", 1, PERCENT (ps->vused, ps->max_var));
3341 relem (ps, "original", 0, ps->noclauses);
3342 relem (ps, "conflicts", 0, ps->conflicts);
3343 // relem (ps, "decisions", 0, ps->decisions);
3344 // relem (ps, "conf/dec", 1, PERCENT(ps->conflicts,ps->decisions));
3345 // relem (ps, "limit", 0, reduce_limit_on_lclauses (ps));
3346 relem (ps, "learned", 0, ps->nlclauses);
3347 // relem (ps, "limit", 1, PERCENT (ps->nlclauses, reduce_limit_on_lclauses (ps)));
3348 relem (ps, "limit", 0, ps->lreduce);
3350 relem (ps, "learning", 1, PERCENT (ps->llused, ps->lladded));
3352 relem (ps, "agility", 1, dynamic_flips_per_assignment_per_mille (ps) / 10.0);
3353 // relem (ps, "original", 0, ps->noclauses);
3354 relem (ps, "MB", 1, mb (ps));
3355 // relem (ps, "lladded", 0, ps->lladded);
3356 // relem (ps, "llused", 0, ps->llused);
3358 relem (ps, 0, 0, 0);
3363 /* Adapt this to the number of rows in your terminal.
3367 if (ps->reports % (ROWS - 3) == (ROWS - 4))
3374 bcp_queue_is_empty (PS * ps)
3376 if (ps->ttail != ps->thead)
3379 if (ps->ttail2 != ps->thead)
3383 if (ps->ttailado != ps->thead)
3393 assert (!ps->mtcls);
3394 assert (!ps->failed_assumption);
3395 if (ps->alstail < ps->alshead)
3397 assert (!ps->conflict);
3398 assert (bcp_queue_is_empty (ps));
3399 return ps->thead == ps->trail + ps->max_var; /* all assigned */
3405 Rnk *p, *eor = ps->rnks + ps->max_var;
3406 for (p = ps->rnks + 1; p <= eor; p++)
3407 if (p->score != INFFLT)
3408 p->score = mulflt (p->score, ps->ilvinc);
3409 ps->vinc = mulflt (ps->vinc, ps->ilvinc);;
3411 ps->nvinc = mulflt (ps->nvinc, ps->lscore);;
3416 inc_score (PS * ps, Var * v)
3422 if (ps->simplifying)
3435 assert (score != INFFLT);
3437 score = addflt (score, ps->vinc);
3438 assert (score < INFFLT);
3443 if (score > ps->lscore)
3448 inc_activity (PS * ps, Cls * c)
3459 *p = addflt (*p, ps->cinc);
3463 hashlevel (unsigned l)
3465 return 1u << (l & 31);
3469 push (PS * ps, Var * v)
3471 if (ps->dhead == ps->eod)
3472 ENLARGE (ps->dfs, ps->dhead, ps->eod);
3480 assert (ps->dfs < ps->dhead);
3481 return *--ps->dhead;
3487 unsigned open, minlevel, siglevels, l, old, i, orig;
3488 Lit *this, *other, **p, **q, **eol;
3489 Var *v, *u, **m, *start, *uip;
3492 assert (ps->conflict);
3494 assert (ps->ahead == ps->added);
3495 assert (ps->mhead == ps->marked);
3496 assert (ps->rhead == ps->resolved);
3498 /* First, search for First UIP variable and mark all resolved variables.
3499 * At the same time determine the minimum decision level involved.
3500 * Increase activities of resolved variables.
3504 minlevel = ps->LEVEL;
3512 add_antecedent (ps, c);
3513 inc_activity (ps, c);
3514 eol = end_of_lits (c);
3515 for (p = c->lits; p < eol; p++)
3519 if (other->val == TRUE)
3522 assert (other->val == FALSE);
3524 u = LIT2VAR (other);
3532 if (u->level == ps->LEVEL)
3538 push_var_as_marked (ps, u);
3542 /* The statistics counter 'nonminimizedllits' sums up the
3543 * number of literals that would be added if only the
3544 * 'first UIP' scheme for learned clauses would be used
3545 * and no clause minimization.
3547 ps->nonminimizedllits++;
3549 if (u->level < minlevel)
3550 minlevel = u->level;
3552 siglevels |= hashlevel (u->level);
3567 goto DONE_FIRST_UIP;
3571 uip = LIT2VAR (this);
3577 c = var2reason (ps, uip);
3578 #ifdef NO_BINARY_CLAUSES
3583 if ((!open && ps->LEVEL) || !c)
3594 this = VAR2LIT (uip);
3595 this += (this->val == TRUE);
3596 ps->nonminimizedllits++;
3597 ps->minimizedllits++;
3605 assert (!ps->LEVEL);
3607 /* Second, try to mark more intermediate variables, with the goal to
3608 * minimize the conflict clause. This is a DFS from already marked
3609 * variables backward through the implication graph. It tries to reach
3610 * other marked variables. If the search reaches an unmarked decision
3611 * variable or a variable assigned below the minimum level of variables in
3612 * the first uip learned clause or a level on which no variable has been
3613 * marked, then the variable from which the DFS is started is not
3614 * redundant. Otherwise the start variable is redundant and will
3615 * eventually be removed from the learned clause in step 4. We initially
3616 * implemented BFS, but then profiling revelead that this step is a bottle
3617 * neck for certain incremental applications. After switching to DFS this
3618 * hot spot went away.
3620 orig = ps->mhead - ps->marked;
3621 for (i = 0; i < orig; i++)
3623 start = ps->marked[i];
3625 assert (start->mark);
3626 assert (start != uip);
3627 assert (start->level < ps->LEVEL);
3632 old = ps->mhead - ps->marked;
3633 assert (ps->dhead == ps->dfs);
3636 while (ps->dhead > ps->dfs)
3641 c = var2reason (ps, u);
3642 #ifdef NO_BINARY_CLAUSES
3648 (l < minlevel || ((hashlevel (l) & ~siglevels)))))
3650 while (ps->mhead > ps->marked + old) /* reset all marked */
3651 (*--ps->mhead)->mark = 0;
3653 ps->dhead = ps->dfs; /* and DFS stack */
3657 eol = end_of_lits (c);
3658 for (p = c->lits; p < eol; p++)
3670 for (m = ps->marked; m < ps->mhead; m++)
3675 assert (!v->resolved);
3679 c = var2reason (ps, v);
3683 #ifdef NO_BINARY_CLAUSES
3687 eol = end_of_lits (c);
3688 for (p = c->lits; p < eol; p++)
3692 u = LIT2VAR (other);
3696 if (!u->mark) /* 'MARKTEST' */
3703 add_antecedent (ps, c);
3707 for (m = ps->marked; m < ps->mhead; m++)
3721 if (this->val == TRUE)
3722 this++; /* actually NOTLIT */
3725 ps->minimizedllits++;
3728 assert (ps->ahead <= ps->eoa);
3729 assert (ps->rhead <= ps->eor);
3731 ps->mhead = ps->marked;
3737 Lit ** eol, ** p, * lit;
3742 double start = picosat_time_stamp ();
3744 assert (ps->failed_assumption);
3745 assert (ps->failed_assumption->val == FALSE);
3747 v = LIT2VAR (ps->failed_assumption);
3748 reason = var2reason (ps, v);
3749 if (!reason) return;
3750 #ifdef NO_BINARY_CLAUSES
3751 if (reason == &ps->impl)
3755 eol = end_of_lits (reason);
3756 for (p = reason->lits; p != eol; p++)
3760 if (u == v) continue;
3761 if (u->reason) break;
3763 if (p == eol) return;
3765 assert (ps->ahead == ps->added);
3766 assert (ps->mhead == ps->marked);
3767 assert (ps->rhead == ps->resolved);
3771 add_lit (ps, NOTLIT (ps->failed_assumption));
3775 v = ps->marked[next++];
3779 reason = var2reason (ps, v);
3780 #ifdef NO_BINARY_CLAUSES
3781 if (reason == &ps->impl)
3784 add_antecedent (ps, reason);
3785 eol = end_of_lits (reason);
3786 for (p = reason->lits; p != eol; p++)
3790 if (u == v) continue;
3791 if (u->mark) continue;
3798 if (lit->val == TRUE) lit = NOTLIT (lit);
3802 while (ps->marked + next < ps->mhead);
3804 c = add_simplified_clause (ps, 1);
3805 v = LIT2VAR (ps->failed_assumption);
3807 #ifdef NO_BINARY_CLAUSES
3808 if (!ISLITREASON (reason))
3811 assert (reason->locked);
3813 if (reason->learned && reason->size > 2)
3815 assert (ps->llocked > 0);
3820 #ifdef NO_BINARY_CLAUSES
3823 c = impl2reason (ps, NOTLIT (ps->failed_assumption));
3828 assert (c->learned);
3829 assert (!c->locked);
3834 assert (ps->llocked > 0);
3840 while (ps->mhead > ps->marked)
3841 (*--ps->mhead)->mark = 0;
3844 fprintf (ps->out, "%sfanalyze took %.1f seconds\n",
3845 ps->prefix, picosat_time_stamp () - start);
3848 /* Propagate assignment of 'this' to 'FALSE' by visiting all binary clauses in
3849 * which 'this' occurs.
3852 prop2 (PS * ps, Lit * this)
3854 #ifdef NO_BINARY_CLAUSES
3864 assert (this->val == FALSE);
3866 #ifdef NO_BINARY_CLAUSES
3867 lstk = LIT2IMPLS (this);
3868 start = lstk->start;
3869 l = start + lstk->count;
3872 /* The counter 'visits' is the number of clauses that are
3873 * visited during propagations of assignments.
3887 if (LIT2VAR (other)->level < ps->LEVEL)
3895 assign_forced (ps, other, LIT2REASON (NOTLIT(this)));
3899 if (ps->conflict == &ps->cimpl)
3901 ps->conflict = setcimpl (ps, this, other);
3904 /* Traverse all binary clauses with 'this'. Head/Tail pointers for binary
3905 * clauses do not have to be modified here.
3907 p = LIT2IMPLS (this);
3908 for (c = *p; c; c = next)
3914 assert (!c->collect);
3916 assert (!c->collected);
3918 assert (c->size == 2);
3936 if (LIT2VAR (other)->level < ps->LEVEL)
3945 assign_forced (ps, other, c); /* unit clause */
3947 #endif /* !defined(NO_BINARY_CLAUSES) */
3952 should_disconnect_head_tail (PS * ps, Lit * lit)
3957 assert (lit->val == TRUE);
3960 litlevel = v->level;
3966 if (ps->simplifying)
3970 return litlevel < ps->LEVEL;
3975 propl (PS * ps, Lit * this)
3977 Lit **l, *other, *prev, *new_lit, **eol;
3978 Cls *next, **htp_ptr, **new_htp_ptr;
3984 htp_ptr = LIT2HTPS (this);
3985 assert (this->val == FALSE);
3987 /* Traverse all non binary clauses with 'this'. Head/Tail pointers are
3990 for (c = *htp_ptr; c; c = next)
3996 ps->traversals++; /* other is dereferenced at least */
4007 assert (!c->collected);
4009 assert (c->size > 0);
4014 assert (c->size != 1);
4018 c->next[1] = c->next[0];
4021 else if (c->size == 1) /* With assumptions we need to
4022 * traverse unit clauses as well.
4025 assert (!ps->conflict);
4031 assert (other == this && c->size > 1);
4035 assert (other == c->lits[1]);
4036 assert (this == c->lits[0]);
4037 assert (next == c->next[0]);
4038 assert (!c->collect);
4040 if (other->val == TRUE)
4047 if (should_disconnect_head_tail (ps, other))
4049 new_htp_ptr = LIT2DHTPS (other);
4050 c->next[0] = *new_htp_ptr;
4064 eol = c->lits + c->size;
4080 if (new_lit->val != FALSE) break;
4085 while (l > c->lits + 2)
4091 assert (c->lits[0] == this);
4093 assert (other == c->lits[1]);
4094 if (other->val == FALSE) /* found conflict */
4096 assert (!ps->conflict);
4101 assign_forced (ps, other, c); /* unit clause */
4106 assert (new_lit->val == TRUE || new_lit->val == UNDEF);
4107 c->lits[0] = new_lit;
4109 new_htp_ptr = LIT2HTPS (new_lit);
4110 c->next[0] = *new_htp_ptr;
4119 static unsigned primes[] = { 996293, 330643, 753947, 500873 };
4121 #define PRIMES ((sizeof primes)/sizeof *primes)
4124 hash_ado (Lit ** ado, unsigned salt)
4126 unsigned i, res, tmp;
4129 assert (salt < PRIMES);
4134 for (p = ado; (lit = *p); p++)
4144 assert (i < PRIMES);
4152 return res & (ps->szadotab - 1);
4156 cmp_ado (Lit ** a, Lit ** b)
4158 Lit ** p, ** q, * l, * k;
4161 for (p = a, q = b; (l = *p); p++, q++)
4165 if ((res = (l->val - k->val)))
4175 find_ado (Lit ** ado)
4177 Lit *** res, ** other;
4178 unsigned pos, delta;
4180 pos = hash_ado (ado, 0);
4181 assert (pos < ps->szadotab);
4182 res = ps->adotab + pos;
4185 if (!other || !cmp_ado (other, ado))
4188 delta = hash_ado (ado, 1);
4193 assert (delta < ps->szadotab);
4198 if (pos >= ps->szadotab)
4199 pos -= ps->szadotab;
4201 assert (pos < ps->szadotab);
4202 res = ps->adotab + pos;
4204 if (!other || !cmp_ado (other, ado))
4210 enlarge_adotab (PS * ps)
4212 /* TODO make this generic */
4214 ABORTIF (ps->szadotab,
4215 "internal: all different objects table needs larger initial size");
4216 assert (!ps->nadotab);
4217 ps->szadotab = 10000;
4218 NEWN (ps->adotab, ps->szadotab);
4219 CLRN (ps->adotab, ps->szadotab);
4225 Lit ** p, ** q, *** adotabpos, **ado, * lit;
4228 if (ps->level && ps->adodisabled)
4231 assert (!ps->conflict);
4232 assert (!ps->adoconflict);
4233 assert (VAR2LIT (v)->val != UNDEF);
4234 assert (!v->adotabpos);
4241 for (p = v->ado; (lit = *p); p++)
4242 if (lit->val == UNDEF)
4252 if (4 * ps->nadotab >= 3 * ps->szadotab) /* at least 75% filled */
4253 enlarge_adotab (ps);
4255 adotabpos = find_ado (v->ado);
4261 v->adotabpos = adotabpos;
4262 *adotabpos = v->ado;
4266 assert (ado != v->ado);
4268 ps->adoconflict = new_clause (2 * llength (ado), 1);
4269 q = ps->adoconflict->lits;
4271 for (p = ado; (lit = *p); p++)
4272 *q++ = lit->val == FALSE ? lit : NOTLIT (lit);
4274 for (p = v->ado; (lit = *p); p++)
4275 *q++ = lit->val == FALSE ? lit : NOTLIT (lit);
4277 assert (q == ENDOFCLS (ps->adoconflict));
4278 ps->conflict = ps->adoconflict;
4289 assert (!ps->conflict);
4296 if (ps->ttail2 < ps->thead) /* prioritize implications */
4299 prop2 (ps, NOTLIT (*ps->ttail2++));
4301 else if (ps->ttail < ps->thead) /* unit clauses or clauses with length > 2 */
4303 if (ps->conflict) break;
4304 propl (ps, NOTLIT (*ps->ttail++));
4305 if (ps->conflict) break;
4308 else if (ps->ttailado < ps->thead)
4310 if (ps->conflict) break;
4311 propado (ps, LIT2VAR (*ps->ttailado++));
4312 if (ps->conflict) break;
4316 break; /* all assignments propagated, so break */
4319 ps->propagations += props;
4325 unsigned res, vlevel;
4330 for (p = ps->added; p < ps->ahead; p++)
4334 assert (vlevel <= ps->LEVEL);
4335 if (vlevel < ps->LEVEL && vlevel > res)
4347 Rnk *p, *eor = ps->rnks + ps->max_var;
4348 char name[100], cmd[200];
4353 for (p = ps->rnks + 1; p <= ps->eor; p++)
4358 s = mulflt (s, ps->nvinc);
4359 assert (flt2double (s) <= 1.0);
4362 sprintf (name, "/tmp/picosat-viscores/data/%08u", ps->conflicts);
4363 sprintf (cmd, "sort -n|nl>%s", name);
4365 data = popen (cmd, "w");
4366 for (p = ps->rnks + 1; p <= ps->eor; p++)
4371 s = mulflt (s, ps->nvinc);
4372 fprintf (data, "%lf %d\n", 100.0 * flt2double (s), (int)(p - ps->rnks));
4377 for (i = 0; i < 8; i++)
4379 sprintf (cmd, "awk '$3%%8==%d' %s>%s.%d", i, name, name, i);
4383 fprintf (ps->fviscores, "set title \"%u\"\n", ps->conflicts);
4384 fprintf (ps->fviscores, "plot [0:%u] 0, 100 * (1 - 1/1.1), 100", ps->max_var);
4386 for (i = 0; i < 8; i++)
4387 fprintf (ps->fviscores,
4388 ", \"%s.%d\" using 1:2:3 with labels tc lt %d",
4391 fputc ('\n', ps->fviscores);
4392 fflush (ps->fviscores);
4394 usleep (50000); /* refresh rate of 20 Hz */
4406 int l = log2flt (ps->cinc);
4408 factor = base2flt (1, -l);
4410 for (p = ps->lclauses; p != ps->lhead; p++)
4421 assert (c->learned);
4427 *a = mulflt (*a, factor);
4430 ps->cinc = mulflt (ps->cinc, factor);
4437 ps->nvinc = mulflt (ps->nvinc, ps->fvinc);
4439 ps->vinc = mulflt (ps->vinc, ps->ifvinc);
4443 inc_max_var (PS * ps)
4449 assert (ps->max_var < ps->size_vars);
4451 if (ps->max_var + 1 == ps->size_vars)
4452 enlarge (ps, ps->size_vars + 2*(ps->size_vars + 3) / 4); /* +25% */
4454 ps->max_var++; /* new index of variable */
4455 assert (ps->max_var); /* no unsigned overflow */
4457 assert (ps->max_var < ps->size_vars);
4459 lit = ps->lits + 2 * ps->max_var;
4460 lit[0].val = lit[1].val = UNDEF;
4462 memset (ps->htps + 2 * ps->max_var, 0, 2 * sizeof *ps->htps);
4464 memset (ps->dhtps + 2 * ps->max_var, 0, 2 * sizeof *ps->dhtps);
4466 memset (ps->impls + 2 * ps->max_var, 0, 2 * sizeof *ps->impls);
4467 memset (ps->jwh + 2 * ps->max_var, 0, 2 * sizeof *ps->jwh);
4469 v = ps->vars + ps->max_var; /* initialize variable components */
4472 r = ps->rnks + ps->max_var; /* initialize rank */
4479 force (PS * ps, Cls * c)
4481 Lit ** p, ** eol, * lit, * forced;
4487 eol = end_of_lits (c);
4488 for (p = c->lits; p < eol; p++)
4491 if (lit->val == UNDEF)
4495 #ifdef NO_BINARY_CLAUSES
4497 reason = LIT2REASON (NOTLIT (p[p == c->lits ? 1 : -1]));
4501 assert (lit->val == FALSE);
4504 #ifdef NO_BINARY_CLAUSES
4511 assign_forced (ps, forced, reason);
4515 inc_lreduce (PS * ps)
4520 ps->lreduce *= FREDUCE;
4522 report (ps, 1, '+');
4532 LOG ( fprintf (ps->out, "%sconflict ", ps->prefix); dumpclsnl (ps, ps->conflict));
4535 new_level = drive (ps);
4536 // TODO: why not? assert (new_level != 1 || (ps->ahead - ps->added) == 2);
4537 c = add_simplified_clause (ps, 1);
4538 undo (ps, new_level);
4545 !--ps->lreduceadjustcnt)
4547 /* With FREDUCE==110 and FREDADJ=121 we stir 'lreduce' to be
4548 * proportional to 'sqrt(conflicts)'. In earlier version we actually
4549 * used 'FREDADJ=150', which results in 'lreduce' to approximate
4550 * 'conflicts^(log(1.1)/log(1.5))' which is close to the fourth root
4551 * of 'conflicts', since log(1.1)/log(1.5)=0.235 (as observed by
4552 * Donald Knuth). The square root is the same we get by a Glucose
4553 * style increase, which simply adds a constant at every reduction.
4554 * This would be way simpler to implement but for now we keep the more
4555 * complicated code using the adjust increments and counters.
4557 ps->lreduceadjustinc *= FREDADJ; ps->lreduceadjustinc /= 100; ps->lreduceadjustcnt
4558 = ps->lreduceadjustinc;
4562 if (ps->verbosity >= 4 && !(ps->conflicts % 1000))
4563 report (ps, 4, 'C');
4569 ps->cinc = mulflt (ps->cinc, ps->fcinc);
4570 if (ps->lcinc < ps->cinc)
4585 disconnect_clause (PS * ps, Cls * c)
4587 assert (c->connected);
4593 assert (ps->nlclauses > 0);
4596 assert (ps->llits >= c->size);
4597 ps->llits -= c->size;
4601 assert (ps->noclauses > 0);
4604 assert (ps->olits >= c->size);
4605 ps->olits -= c->size;
4615 clause_is_toplevel_satisfied (PS * ps, Cls * c)
4617 Lit *lit, **p, **eol = end_of_lits (c);
4620 for (p = c->lits; p < eol; p++)
4623 if (lit->val == TRUE)
4635 collect_clause (PS * ps, Cls * c)
4637 assert (c->collect);
4641 assert (!c->collected);
4644 disconnect_clause (ps, c);
4647 if (ps->trace && (!c->learned || c->used))
4650 delete_clause (ps, c);
4656 collect_clauses (PS * ps)
4658 Cls *c, **p, **q, * next;
4663 res = ps->current_bytes;
4665 eol = ps->lits + 2 * ps->max_var + 1;
4666 for (lit = ps->lits + 2; lit <= eol; lit++)
4668 for (i = 0; i <= 1; i++)
4672 #ifdef NO_BINARY_CLAUSES
4673 Ltk * lstk = LIT2IMPLS (lit);
4676 if (lit->val != TRUE || LIT2VAR (lit)->level)
4677 for (s = r; s < lstk->start + lstk->count; s++)
4680 Var *v = LIT2VAR (other);
4685 lstk->count = r - lstk->start;
4688 p = LIT2IMPLS (lit);
4694 for (c = *p; c; c = next)
4697 if (c->lits[0] != lit)
4710 for (lit = ps->lits + 2; lit <= eol; lit++)
4712 p = LIT2DHTPS (lit);
4715 Lit * other = c->lits[0];
4722 assert (c->lits[1] == lit);
4734 for (p = SOC; p != EOC; p = NXC (p))
4744 if (collect_clause (ps, c))
4753 for (p = q; p < ps->ohead; p++)
4759 for (p = q; p < ps->lhead; p++)
4765 assert (ps->current_bytes <= res);
4766 res -= ps->current_bytes;
4767 ps->recycled += res;
4769 LOG ( fprintf (ps->out, "%scollected %ld bytes\n", ps->prefix, (long)res));
4775 need_to_reduce (PS * ps)
4777 return ps->nlclauses >= reduce_limit_on_lclauses (ps);
4783 inc_drestart (PS * ps)
4785 ps->drestart *= FRESTART;
4786 ps->drestart /= 100;
4788 if (ps->drestart >= MAXRESTART)
4789 ps->drestart = MAXRESTART;
4793 inc_ddrestart (PS * ps)
4795 ps->ddrestart *= FRESTART;
4796 ps->ddrestart /= 100;
4798 if (ps->ddrestart >= MAXRESTART)
4799 ps->ddrestart = MAXRESTART;
4808 for (k = 1; k < 32; k++)
4809 if (i == (1 << k) - 1)
4810 return 1 << (k - 1);
4813 if ((1 << (k - 1)) <= i && i < (1 << k) - 1)
4814 return luby (i - (1 << (k-1)) + 1);
4821 inc_lrestart (PS * ps, int skip)
4825 delta = 100 * luby (++ps->lubycnt);
4826 ps->lrestart = ps->conflicts + delta;
4828 if (ps->waslubymaxdelta)
4829 report (ps, 1, skip ? 'N' : 'R');
4831 report (ps, 2, skip ? 'n' : 'r');
4833 if (delta > ps->lubymaxdelta)
4835 ps->lubymaxdelta = delta;
4836 ps->waslubymaxdelta = 1;
4839 ps->waslubymaxdelta = 0;
4844 init_restart (PS * ps)
4847 /* TODO: why is it better in incremental usage to have smaller initial
4848 * outer restart interval?
4850 ps->ddrestart = ps->calls > 1 ? MINRESTART : 1000;
4851 ps->drestart = MINRESTART;
4852 ps->lrestart = ps->conflicts + ps->drestart;
4855 ps->lubymaxdelta = 0;
4856 ps->waslubymaxdelta = 0;
4857 inc_lrestart (ps, 0);
4870 outer = (ps->drestart >= ps->ddrestart);
4873 skip = very_high_agility (ps);
4875 skip = high_agility (ps);
4877 skip = medium_agility (ps);
4882 ps->skippedrestarts++;
4885 assert (ps->conflicts >= ps->lrestart);
4890 assert (ps->LEVEL > 1);
4891 LOG ( fprintf (ps->out, "%srestart %u\n", ps->prefix, ps->restarts));
4898 kind = skip ? 'N' : 'R';
4900 ps->drestart = MINRESTART;
4911 assert (ps->drestart <= MAXRESTART);
4912 ps->lrestart = ps->conflicts + ps->drestart;
4913 assert (ps->lrestart > ps->conflicts);
4915 report (outer ? 1 : 2, kind);
4917 inc_lrestart (ps, skip);
4922 assign_decision (PS * ps, Lit * lit)
4924 assert (!ps->conflict);
4928 LOG ( fprintf (ps->out, "%snew level %u\n", ps->prefix, ps->LEVEL));
4929 LOG ( fprintf (ps->out,
4930 "%sassign %d at level %d <= DECISION\n",
4931 ps->prefix, LIT2INT (lit), ps->LEVEL));
4933 assign (ps, lit, 0);
4939 lit_has_binary_clauses (PS * ps, Lit * lit)
4941 #ifdef NO_BINARY_CLAUSES
4942 Ltk* lstk = LIT2IMPLS (lit);
4943 return lstk->count != 0;
4945 return *LIT2IMPLS (lit) != 0;
4953 unsigned long long propagaions_before_bcp = ps->propagations;
4957 ps->flprops += ps->propagations - propagaions_before_bcp;
4962 cmp_inverse_rnk (PS * ps, Rnk * a, Rnk * b)
4965 return -cmp_rnk (a, b);
4969 rnk2jwh (PS * ps, Rnk * r)
4971 Flt res, sum, pjwh, njwh;
4977 pjwh = *LIT2JWH (plit);
4978 njwh = *LIT2JWH (nlit);
4980 res = mulflt (pjwh, njwh);
4982 sum = addflt (pjwh, njwh);
4983 sum = mulflt (sum, base2flt (1, -10));
4984 res = addflt (res, sum);
4990 cmp_inverse_jwh_rnk (PS * ps, Rnk * r, Rnk * s)
4992 Flt a = rnk2jwh (ps, r);
4993 Flt b = rnk2jwh (ps, s);
4994 int res = cmpflt (a, b);
4999 return cmp_inverse_rnk (ps, r, s);
5005 unsigned i, j, old_trail_count, common, saved_count;
5006 unsigned new_saved_size, oldladded = ps->ladded;
5007 unsigned long long limit, delta;
5008 Lit * lit, * other, * pivot;
5009 Rnk * r, ** p, ** q;
5010 int new_trail_count;
5016 if (ps->heap + 1 >= ps->hhead)
5019 if (ps->propagations < ps->fllimit)
5023 started = ps->seconds;
5029 delta = ps->propagations/10;
5030 if (delta >= 100*1000*1000) delta = 100*1000*1000;
5031 else if (delta <= 100*1000) delta = 100*1000;
5033 limit = ps->propagations + delta;
5034 ps->fllimit = ps->propagations;
5036 assert (!ps->LEVEL);
5037 assert (ps->simplifying);
5039 if (ps->flcalls <= 1)
5040 SORT (Rnk *, cmp_inverse_jwh_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
5042 SORT (Rnk *, cmp_inverse_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
5044 i = 1; /* NOTE: heap starts at position '1' */
5046 while (ps->propagations < limit)
5048 if (ps->heap + i == ps->hhead)
5050 if (ps->ladded == oldladded)
5057 oldladded = ps->ladded;
5060 assert (ps->heap + i < ps->hhead);
5068 if (!lit_has_binary_clauses (ps, NOTLIT (lit)))
5079 LOG ( fprintf (ps->out, "%strying %d as failed literal\n",
5080 ps->prefix, LIT2INT (lit)));
5082 assign_decision (ps, lit);
5083 old_trail_count = ps->thead - ps->trail;
5088 EXPLICITLY_FAILED_LITERAL:
5089 LOG ( fprintf (ps->out, "%sfound explicitly failed literal %d\n",
5090 ps->prefix, LIT2INT (lit)));
5102 assert (!ps->LEVEL);
5109 if (ps->propagations >= limit)
5117 if (!lit_has_binary_clauses (ps, NOTLIT (lit)))
5129 LOG ( fprintf (ps->out, "%strying %d as failed literals\n",
5130 ps->prefix, LIT2INT (lit)));
5132 new_trail_count = ps->thead - ps->trail;
5133 saved_count = new_trail_count - old_trail_count;
5135 if (saved_count > ps->saved_size)
5137 new_saved_size = ps->saved_size ? 2 * ps->saved_size : 1;
5138 while (saved_count > new_saved_size)
5139 new_saved_size *= 2;
5141 RESIZEN (ps->saved, ps->saved_size, new_saved_size);
5142 ps->saved_size = new_saved_size;
5145 for (j = 0; j < saved_count; j++)
5146 ps->saved[j] = ps->trail[old_trail_count + j];
5150 assign_decision (ps, lit);
5154 goto EXPLICITLY_FAILED_LITERAL;
5156 pivot = (ps->thead - ps->trail <= new_trail_count) ? lit : NOTLIT (lit);
5159 for (j = 0; j < saved_count; j++)
5160 if ((other = ps->saved[j])->val == TRUE)
5161 ps->saved[common++] = other;
5167 "%sfound %d literals implied by %d and %d\n",
5169 LIT2INT (NOTLIT (lit)), LIT2INT (lit)));
5171 #if 1 // set to zero to disable 'lifting'
5174 /* TODO: For some Velev benchmarks, extracting the common implicit
5175 * failed literals took quite some time. This needs to be fixed by
5176 * a dedicated analyzer. Up to then we bound the number of
5177 * propagations in this loop as well.
5179 && ps->propagations < limit + delta
5182 other = ps->saved[j];
5184 if (other->val == TRUE)
5187 assert (!other->val);
5189 LOG ( fprintf (ps->out,
5190 "%sforcing %d as forced implicitly failed literal\n",
5191 ps->prefix, LIT2INT (other)));
5193 assert (pivot != NOTLIT (other));
5194 assert (pivot != other);
5196 assign_decision (ps, NOTLIT (other));
5199 assert (ps->LEVEL == 1);
5204 assert (!ps->LEVEL);
5208 assign_decision (ps, pivot);
5215 assert (ps->LEVEL == 1);
5222 assert (!ps->LEVEL);
5226 assign_decision (ps, NOTLIT (pivot));
5232 assert (ps->LEVEL == 1);
5247 assert (!ps->LEVEL);
5250 assert (!ps->LEVEL);
5253 assert (!ps->LEVEL);
5265 ps->fllimit += 9 * (ps->propagations - ps->fllimit); /* 10% for failed literals */
5269 /* First flush top level assigned literals. Those are prohibited from
5270 * being pushed up the heap during 'faillits' since 'simplifying' is set.
5272 assert (ps->heap < ps->hhead);
5273 for (p = q = ps->heap + 1; p < ps->hhead; p++)
5283 /* Then resort with respect to EVSIDS score and fix positions.
5285 SORT (Rnk *, cmp_inverse_rnk, ps->heap + 1, ps->hhead - (ps->heap + 1));
5286 for (p = ps->heap + 1; p < ps->hhead; p++)
5287 (*p)->pos = p - ps->heap;
5290 ps->flseconds += ps->seconds - started;
5296 simplify (PS * ps, int forced)
5298 Lit * lit, * notlit, ** t;
5299 unsigned collect, delta;
5301 size_t bytes_collected;
5311 assert (!ps->mtcls);
5312 assert (!satisfied (ps));
5313 assert (forced || ps->lsimplify <= ps->propagations);
5314 assert (forced || ps->fsimplify <= ps->fixed);
5319 ps->simplifying = 1;
5321 ps->simplifying = 0;
5327 if (ps->cils != ps->cilshead)
5329 assert (ps->ttail == ps->thead);
5330 assert (ps->ttail2 == ps->thead);
5331 ps->ttail = ps->trail;
5332 for (t = ps->trail; t < ps->thead; t++)
5338 assert (LIT2INT (lit) < 0);
5339 assert (lit->val == TRUE);
5345 ps->ttail2 = ps->thead = ps->ttail;
5347 for (q = ps->cils; q != ps->cilshead; q++)
5350 assert (0 < ilit && ilit <= (int) ps->max_var);
5351 v = ps->vars + ilit;
5352 assert (v->internal);
5355 lit = int2lit (ps, -ilit);
5356 assert (lit->val == UNDEF);
5358 notlit = NOTLIT (lit);
5359 assert (notlit->val == UNDEF);
5360 notlit->val = FALSE;
5365 for (p = SOC; p != EOC; p = NXC (p))
5379 assert (!c->collect);
5380 if (clause_is_toplevel_satisfied (ps, c))
5382 mark_clause_to_be_collected (c);
5387 LOG ( fprintf (ps->out, "%scollecting %d clauses\n", ps->prefix, collect));
5391 collect_clauses (ps);
5393 ps->srecycled += bytes_collected;
5396 if (ps->cils != ps->cilshead)
5398 for (q = ps->cils; q != ps->cilshead; q++)
5401 assert (0 < ilit && ilit <= (int) ps->max_var);
5402 assert (ps->vars[ilit].internal);
5403 if (ps->rilshead == ps->eorils)
5404 ENLARGE (ps->rils, ps->rilshead, ps->eorils);
5405 *ps->rilshead++ = ilit;
5406 lit = int2lit (ps, -ilit);
5407 assert (lit->val == TRUE);
5409 notlit = NOTLIT (lit);
5410 assert (notlit->val == FALSE);
5411 notlit->val = UNDEF;
5413 ps->cilshead = ps->cils;
5416 delta = 10 * (ps->olits + ps->llits) + 100000;
5417 if (delta > 2000000)
5419 ps->lsimplify = ps->propagations + delta;
5420 ps->fsimplify = ps->fixed;
5423 report (ps, 1, 's');
5429 assert (!ps->LEVEL);
5430 assert (bcp_queue_is_empty (ps));
5431 assert (ps->isimplify < ps->fixed);
5434 report (ps, 2, 'i');
5436 ps->drestart = MINRESTART;
5437 ps->lrestart = ps->conflicts + ps->drestart;
5441 ps->isimplify = ps->fixed;
5445 cmp_glue_activity_size (PS * ps, Cls * c, Cls * d)
5451 assert (c->learned);
5452 assert (d->learned);
5454 if (c->glue < d->glue) // smaller glue preferred
5457 if (c->glue > d->glue)
5465 if (a < b) // then higher activity
5471 if (c->size < d->size) // then smaller size
5474 if (c->size > d->size)
5481 reduce (PS * ps, unsigned percentage)
5483 unsigned redcount, lcollect, collect, target;
5485 size_t bytes_collected;
5489 assert (ps->rhead == ps->resolved);
5491 ps->lastreduceconflicts = ps->conflicts;
5493 assert (percentage <= 100);
5494 LOG ( fprintf (ps->out,
5495 "%sreducing %u%% learned clauses\n",
5496 ps->prefix, percentage));
5498 while (ps->nlclauses - ps->llocked > (unsigned)(ps->eor - ps->resolved))
5499 ENLARGE (ps->resolved, ps->rhead, ps->eor);
5504 for (p = ((ps->fsimplify < ps->fixed) ? SOC : ps->lclauses); p != EOC; p = NXC (p))
5518 assert (!c->collect);
5519 if (ps->fsimplify < ps->fixed && clause_is_toplevel_satisfied (ps, c))
5521 mark_clause_to_be_collected (c);
5524 if (c->learned && c->size > 2)
5536 assert (ps->rhead < ps->eor);
5539 assert (ps->rhead <= ps->eor);
5541 ps->fsimplify = ps->fixed;
5543 redcount = ps->rhead - ps->resolved;
5544 SORT (Cls *, cmp_glue_activity_size, ps->resolved, redcount);
5546 assert (ps->nlclauses >= lcollect);
5547 target = ps->nlclauses - lcollect + 1;
5549 target = (percentage * target + 99) / 100;
5551 if (target >= redcount)
5554 ps->rhead = ps->resolved + target;
5555 while (ps->rhead > ps->resolved)
5558 mark_clause_to_be_collected (c);
5561 if (c->learned && c->size > 2) /* just for consistency */
5571 collect_clauses (ps);
5573 ps->rrecycled += bytes_collected;
5575 report (ps, 2, '-');
5579 inc_lreduce (ps); /* avoid dead lock */
5581 assert (ps->rhead == ps->resolved);
5585 init_reduce (PS * ps)
5587 // lreduce = loadded / 2;
5590 if (ps->lreduce < 100)
5595 "%s\n%sinitial reduction limit %u clauses\n%s\n",
5596 ps->prefix, ps->prefix, ps->lreduce, ps->prefix);
5602 unsigned res = ps->srng;
5603 ps->srng *= 1664525u;
5604 ps->srng += 1013904223u;
5605 NOLOG ( fprintf (ps->out, "%srng () = %u\n", ps->prefix, res));
5610 rrng (PS * ps, unsigned low, unsigned high)
5612 unsigned long long tmp;
5613 unsigned res, elements;
5614 assert (low <= high);
5615 elements = high - low + 1;
5621 NOLOG ( fprintf (ps->out, "%srrng (ps, %u, %u) = %u\n", ps->prefix, low, high, res));
5622 assert (low <= res);
5623 assert (res <= high);
5628 decide_phase (PS * ps, Lit * lit)
5630 Lit * not_lit = NOTLIT (lit);
5631 Var *v = LIT2VAR (lit);
5633 assert (LIT2SGN (lit) > 0);
5638 /* assign to TRUE */
5642 /* assign to FALSE */
5646 else if (!v->assigned)
5649 ps->staticphasedecisions++;
5651 if (ps->defaultphase == POSPHASE)
5653 /* assign to TRUE */
5655 else if (ps->defaultphase == NEGPHASE)
5657 /* assign to FALSE */
5660 else if (ps->defaultphase == RNDPHASE)
5662 /* randomly assign default phase */
5663 if (rrng (ps, 1, 2) != 2)
5666 else if (*LIT2JWH(lit) <= *LIT2JWH (not_lit))
5668 /* assign to FALSE (Jeroslow-Wang says there are more short
5669 * clauses with negative occurence of this variable, so satisfy
5670 * those, to minimize BCP)
5676 /* assign to TRUE (... but strictly more positive occurrences) */
5681 /* repeat last phase: phase saving heuristic */
5685 /* assign to TRUE (last phase was TRUE as well) */
5689 /* assign to FALSE (last phase was FALSE as well) */
5698 gcd (unsigned a, unsigned b)
5726 unsigned idx, delta, spread;
5730 if (rrng (ps, 1, spread) != 2)
5733 assert (1 <= ps->max_var);
5734 idx = rrng (ps, 1, ps->max_var);
5735 res = int2lit (ps, idx);
5737 if (res->val != UNDEF)
5739 delta = rrng (ps, 1, ps->max_var);
5740 while (gcd (delta, ps->max_var) != 1)
5743 assert (1 <= delta);
5744 assert (delta <= ps->max_var);
5748 if (idx > ps->max_var)
5750 res = int2lit (ps, idx);
5751 } while (res->val != UNDEF);
5757 res = decide_phase (ps, res);
5758 LOG ( fprintf (ps->out, "%srdecide %d\n", ps->prefix, LIT2INT (res)));
5773 if (res->val == UNDEF) break;
5775 NOLOG ( fprintf (ps->out,
5776 "%shpop %u %u %u\n",
5777 ps->prefix, r - ps->rnks,
5778 FLTMANTISSA(r->score),
5779 FLTEXPONENT(r->score)));
5785 res = decide_phase (ps, res);
5787 LOG ( fprintf (ps->out, "%ssdecide %d\n", ps->prefix, LIT2INT (res)));
5798 assert (ps->als < ps->alshead);
5799 assert (!ps->failed_assumption);
5801 while (ps->alstail < ps->alshead)
5803 lit = *ps->alstail++;
5805 if (lit->val == FALSE)
5807 ps->failed_assumption = lit;
5812 LOG ( fprintf (ps->out, "%sfirst failed assumption %d\n",
5813 ps->prefix, LIT2INT (ps->failed_assumption)));
5818 if (lit->val == TRUE)
5821 if (v->level > ps->adecidelevel)
5822 ps->adecidelevel = v->level;
5829 LOG ( fprintf (ps->out, "%sadecide %d\n", ps->prefix, LIT2INT (lit)));
5830 ps->adecidelevel = ps->LEVEL + 1;
5843 assert (!satisfied (ps));
5844 assert (!ps->conflict);
5846 if (ps->alstail < ps->alshead && (lit = adecide (ps)))
5848 else if (ps->failed_assumption)
5850 else if (satisfied (ps))
5852 else if (!(lit = rdecide (ps)))
5856 assign_decision (ps, lit);
5858 ps->levelsum += ps->LEVEL;
5863 sat (PS * ps, int l)
5865 int count = 0, backtracked;
5874 return PICOSAT_UNSATISFIABLE;
5879 if (ps->lsimplify <= ps->propagations)
5883 return PICOSAT_UNSATISFIABLE;
5893 ps->isimplify = ps->fixed;
5907 return PICOSAT_UNSATISFIABLE;
5916 original_clauses_satisfied (ps);
5917 assumptions_satisfied (ps);
5919 return PICOSAT_SATISFIABLE;
5925 if (!ps->LEVEL && ps->isimplify < ps->fixed)
5929 if (l >= 0 && count >= l) /* decision limit reached ? */
5930 return PICOSAT_UNKNOWN;
5932 if (ps->propagations >= ps->lpropagations)/* propagation limit reached ? */
5933 return PICOSAT_UNKNOWN;
5936 if (!ps->adodisabled && ps->adoconflicts >= ps->adoconflictlimit)
5938 assert (bcp_queue_is_empty (ps));
5939 return PICOSAT_UNKNOWN;
5943 if (ps->fsimplify < ps->fixed && ps->lsimplify <= ps->propagations)
5946 if (!bcp_queue_is_empty (ps))
5950 return PICOSAT_UNSATISFIABLE;
5953 return PICOSAT_SATISFIABLE;
5955 assert (!ps->LEVEL);
5959 if (need_to_reduce (ps))
5962 if (ps->conflicts >= ps->lrestart && ps->LEVEL > 2)
5966 if (ps->failed_assumption)
5967 return PICOSAT_UNSATISFIABLE;
5978 for (v = ps->vars + 1; v <= ps->vars + ps->max_var; v++)
5981 memset (ps->jwh, 0, 2 * (ps->max_var + 1) * sizeof *ps->jwh);
5983 for (p = ps->oclauses; p < ps->ohead; p++)
6002 unsigned idx, prev, this, delta, i, lcore, vcore;
6003 unsigned *stack, *shead, *eos;
6004 Lit **q, **eol, *lit;
6012 assert (ps->mtcls || ps->failed_assumption);
6016 lcore = ps->ocore = vcore = 0;
6018 stack = shead = eos = 0;
6019 ENLARGE (stack, shead, eos);
6023 idx = CLS2IDX (ps->mtcls);
6028 assert (ps->failed_assumption);
6029 v = LIT2VAR (ps->failed_assumption);
6032 idx = CLS2IDX (reason);
6036 while (shead > stack)
6039 zhain = IDX2ZHN (idx);
6059 for (p = zhain->znt; (byte = *p); p++, i += 7)
6061 delta |= (byte & 0x7f) << i;
6065 this = prev + delta;
6066 assert (prev < this); /* no overflow */
6069 ENLARGE (stack, shead, eos);
6082 assert (!c->learned);
6090 eol = end_of_lits (c);
6091 for (q = c->lits; q < eol; q++)
6101 if (!ps->failed_assumption) continue;
6102 if (lit != ps->failed_assumption) continue;
6105 if (!reason) continue;
6106 if (reason->core) continue;
6108 idx = CLS2IDX (reason);
6110 ENLARGE (stack, shead, eos);
6116 DELETEN (stack, eos - stack);
6120 "%s%u core variables out of %u (%.1f%%)\n"
6121 "%s%u core original clauses out of %u (%.1f%%)\n"
6122 "%s%u core learned clauses out of %u (%.1f%%)\n",
6123 ps->prefix, vcore, ps->max_var, PERCENT (vcore, ps->max_var),
6124 ps->prefix, ps->ocore, ps->oadded, PERCENT (ps->ocore, ps->oadded),
6125 ps->prefix, lcore, ps->ladded, PERCENT (lcore, ps->ladded));
6131 trace_lits (PS * ps, Cls * c, FILE * file)
6133 Lit **p, **eol = end_of_lits (c);
6138 for (p = c->lits; p < eol; p++)
6139 fprintf (file, "%d ", LIT2INT (*p));
6145 write_idx (PS * ps, unsigned idx, FILE * file)
6147 fprintf (file, "%ld", EXPORTIDX (idx));
6151 trace_clause (PS * ps, unsigned idx, Cls * c, FILE * file, int fmt)
6155 assert (fmt == RUP_TRACE_FMT || !c->learned);
6156 assert (CLS2IDX (c) == idx);
6158 if (fmt != RUP_TRACE_FMT)
6160 write_idx (ps, idx, file);
6164 trace_lits (ps, c, file);
6166 if (fmt != RUP_TRACE_FMT)
6173 trace_zhain (PS * ps, unsigned idx, Zhn * zhain, FILE * file, int fmt)
6175 unsigned prev, this, delta, i;
6180 assert (zhain->core);
6182 write_idx (ps, idx, file);
6185 if (fmt == EXTENDED_TRACECHECK_TRACE_FMT)
6189 trace_lits (ps, c, file);
6193 assert (fmt == COMPACT_TRACECHECK_TRACE_FMT);
6201 for (p = zhain->znt; (byte = *p); p++, i += 7)
6203 delta |= (byte & 0x7f) << i;
6207 this = prev + delta;
6210 write_idx (ps, this, file);
6217 fputs (" 0\n", file);
6221 write_core (PS * ps, FILE * file)
6226 fprintf (file, "p cnf %u %u\n", ps->max_var, core (ps));
6228 for (p = SOC; p != EOC; p = NXC (p))
6232 if (!c || c->learned || !c->core)
6235 eol = end_of_lits (c);
6236 for (q = c->lits; q < eol; q++)
6237 fprintf (file, "%d ", LIT2INT (*q));
6239 fputs ("0\n", file);
6246 write_trace (PS * ps, FILE * file, int fmt)
6255 if (fmt == RUP_TRACE_FMT)
6257 ps->rupvariables = picosat_variables (ps),
6258 ps->rupclauses = picosat_added_original_clauses (ps);
6259 write_rup_header (ps, file);
6262 for (p = SOC; p != EOC; p = NXC (p))
6266 if (ps->oclauses <= p && p < ps->eoo)
6268 i = OIDX2IDX (p - ps->oclauses);
6269 assert (!c || CLS2IDX (c) == i);
6273 assert (ps->lclauses <= p && p < ps->EOL);
6274 i = LIDX2IDX (p - ps->lclauses);
6277 zhain = IDX2ZHN (i);
6283 if (fmt == RUP_TRACE_FMT)
6284 trace_clause (ps,i, c, file, fmt);
6286 trace_zhain (ps, i, zhain, file, fmt);
6291 if (fmt != RUP_TRACE_FMT && c)
6294 trace_clause (ps, i, c, file, fmt);
6306 write_core_wrapper (PS * ps, FILE * file, int fmt)
6310 write_core (ps, file);
6318 import_lit (PS * ps, int lit, int nointernal)
6323 ABORTIF (lit == INT_MIN, "API usage: INT_MIN literal");
6324 ABORTIF (abs (lit) > (int) ps->max_var && ps->CLS != ps->clshead,
6325 "API usage: new variable index after 'picosat_push'");
6327 if (abs (lit) <= (int) ps->max_var)
6329 res = int2lit (ps, lit);
6331 if (nointernal && v->internal)
6332 ABORT ("API usage: trying to import invalid literal");
6333 else if (!nointernal && !v->internal)
6334 ABORT ("API usage: trying to import invalid context");
6338 while (abs (lit) > (int) ps->max_var)
6340 res = int2lit (ps, lit);
6348 reset_core (PS * ps)
6354 for (i = 1; i <= ps->max_var; i++)
6355 ps->vars[i].core = 0;
6357 for (p = SOC; p != EOC; p = NXC (p))
6361 for (q = ps->zhains; q != ps->zhead; q++)
6370 reset_assumptions (PS * ps)
6374 ps->failed_assumption = 0;
6376 if (ps->extracted_all_failed_assumptions)
6378 for (p = ps->als; p < ps->alshead; p++)
6379 LIT2VAR (*p)->failed = 0;
6381 ps->extracted_all_failed_assumptions = 0;
6384 ps->alstail = ps->alshead = ps->als;
6385 ps->adecidelevel = 0;
6389 check_ready (PS * ps)
6391 ABORTIF (!ps || ps->state == RESET, "API usage: uninitialized");
6395 check_sat_state (PS * ps)
6397 ABORTIF (ps->state != SAT, "API usage: expected to be in SAT state");
6401 check_unsat_state (PS * ps)
6403 ABORTIF (ps->state != UNSAT, "API usage: expected to be in UNSAT state");
6407 check_sat_or_unsat_or_unknown_state (PS * ps)
6409 ABORTIF (ps->state != SAT && ps->state != UNSAT && ps->state != UNKNOWN,
6410 "API usage: expected to be in SAT, UNSAT, or UNKNOWN state");
6414 reset_partial (PS * ps)
6419 for (idx = 1; idx <= ps->max_var; idx++)
6420 ps->vars[idx].partial = 0;
6425 reset_incremental_usage (PS * ps)
6427 unsigned num_non_false;
6430 check_sat_or_unsat_or_unknown_state (ps);
6432 LOG ( fprintf (ps->out, "%sRESET incremental usage\n", ps->prefix));
6437 reset_assumptions (ps);
6442 for (q = ps->conflict->lits; q < end_of_lits (ps->conflict); q++)
6445 if (lit->val != FALSE)
6449 // assert (num_non_false >= 2); // TODO: why this assertion?
6450 #ifdef NO_BINARY_CLAUSES
6451 if (ps->conflict == &ps->cimpl)
6455 if (ps->conflict == ps->adoconflict)
6456 resetadoconflict (ps);
6467 ps->saved_flips = ps->flips;
6468 ps->min_flipped = UINT_MAX;
6469 ps->saved_max_var = ps->max_var;
6481 ps->entered = picosat_time_stamp ();
6487 assert (ps->nentered);
6495 check_trace_support_and_execute (PS * ps,
6497 void (*f)(PS*,FILE*,int), int fmt)
6500 check_unsat_state (ps);
6502 ABORTIF (!ps->trace, "API usage: tracing disabled");
6510 ABORT ("compiled without trace support");
6515 extract_all_failed_assumptions (PS * ps)
6522 assert (!ps->extracted_all_failed_assumptions);
6524 assert (ps->failed_assumption);
6525 assert (ps->mhead == ps->marked);
6527 if (ps->marked == ps->eom)
6528 ENLARGE (ps->marked, ps->mhead, ps->eom);
6530 v = LIT2VAR (ps->failed_assumption);
6534 while (pos < ps->mhead - ps->marked)
6536 v = ps->marked[pos++];
6538 c = var2reason (ps, v);
6541 eol = end_of_lits (c);
6542 for (p = c->lits; p < eol; p++)
6548 #ifdef NO_BINARY_CLAUSES
6554 for (p = ps->als; p < ps->alshead; p++)
6557 if (!u->mark) continue;
6559 LOG ( fprintf (ps->out,
6560 "%sfailed assumption %d\n",
6561 ps->prefix, LIT2INT (*p)));
6564 while (ps->mhead > ps->marked)
6565 (*--ps->mhead)->mark = 0;
6567 ps->extracted_all_failed_assumptions = 1;
6571 picosat_copyright (void)
6573 return "Copyright (c) 2006 - 2014 Armin Biere JKU Linz";
6579 return init (0, 0, 0, 0);
6583 picosat_minit (void * pmgr,
6584 picosat_malloc pnew,
6585 picosat_realloc presize,
6588 ABORTIF (!pnew, "API usage: zero 'picosat_malloc' argument");
6589 ABORTIF (!presize, "API usage: zero 'picosat_realloc' argument");
6590 ABORTIF (!pfree, "API usage: zero 'picosat_free' argument");
6591 return init (pmgr, pnew, presize, pfree);
6596 picosat_adjust (PS * ps, int new_max_var)
6598 unsigned new_size_vars;
6600 ABORTIF (abs (new_max_var) > (int) ps->max_var && ps->CLS != ps->clshead,
6601 "API usage: adjusting variable index after 'picosat_push'");
6604 new_max_var = abs (new_max_var);
6605 new_size_vars = new_max_var + 1;
6607 if (ps->size_vars < new_size_vars)
6608 enlarge (ps, new_size_vars);
6610 while (ps->max_var < (unsigned) new_max_var)
6617 picosat_inc_max_var (PS * ps)
6619 if (ps->measurealltimeinlib)
6626 if (ps->measurealltimeinlib)
6633 picosat_context (PS * ps)
6635 return ps->clshead == ps->CLS ? 0 : LIT2INT (ps->clshead[-1]);
6639 picosat_push (PS * ps)
6645 if (ps->measurealltimeinlib)
6650 if (ps->state != READY)
6651 reset_incremental_usage (ps);
6653 if (ps->rils != ps->rilshead)
6655 res = *--ps->rilshead;
6656 assert (ps->vars[res].internal);
6663 assert (!v->internal);
6666 LOG ( fprintf (ps->out, "%snew internal variable index %d\n", ps->prefix, res));
6669 lit = int2lit (ps, res);
6671 if (ps->clshead == ps->eocls)
6672 ENLARGE (ps->CLS, ps->clshead, ps->eocls);
6673 *ps->clshead++ = lit;
6677 LOG ( fprintf (ps->out, "%snew context %d at depth %ld after push\n",
6678 ps->prefix, res, (long)(ps->clshead - ps->CLS)));
6680 if (ps->measurealltimeinlib)
6687 picosat_pop (PS * ps)
6691 ABORTIF (ps->CLS == ps->clshead, "API usage: too many 'picosat_pop'");
6692 ABORTIF (ps->added != ps->ahead, "API usage: incomplete clause");
6694 if (ps->measurealltimeinlib)
6699 if (ps->state != READY)
6700 reset_incremental_usage (ps);
6702 assert (ps->CLS < ps->clshead);
6703 lit = *--ps->clshead;
6704 LOG ( fprintf (ps->out, "%sclosing context %d at depth %ld after pop\n",
6705 ps->prefix, LIT2INT (lit), (long)(ps->clshead - ps->CLS) + 1));
6707 if (ps->cilshead == ps->eocils)
6708 ENLARGE (ps->cils, ps->cilshead, ps->eocils);
6709 *ps->cilshead++ = LIT2INT (lit);
6711 if (ps->cilshead - ps->cils > MAXCILS) {
6712 LOG ( fprintf (ps->out,
6713 "%srecycling %ld interals with forced simplification\n",
6714 ps->prefix, (long)(ps->cilshead - ps->cils)));
6718 res = picosat_context (ps);
6720 LOG ( fprintf (ps->out, "%snew context %d at depth %ld after pop\n",
6721 ps->prefix, res, (long)(ps->clshead - ps->CLS)));
6723 LOG ( fprintf (ps->out, "%souter most context reached after pop\n", ps->prefix));
6725 if (ps->measurealltimeinlib)
6732 picosat_set_verbosity (PS * ps, int new_verbosity_level)
6735 ps->verbosity = new_verbosity_level;
6739 picosat_set_plain (PS * ps, int new_plain_value)
6742 ps->plain = new_plain_value;
6746 picosat_enable_trace_generation (PS * ps)
6751 ABORTIF (ps->addedclauses,
6752 "API usage: trace generation enabled after adding clauses");
6753 res = ps->trace = 1;
6759 picosat_set_incremental_rup_file (PS * ps, FILE * rup_file, int m, int n)
6762 assert (!ps->rupstarted);
6764 ps->rupvariables = m;
6769 picosat_set_output (PS * ps, FILE * output_file)
6772 ps->out = output_file;
6776 picosat_measure_all_calls (PS * ps)
6779 ps->measurealltimeinlib = 1;
6783 picosat_set_prefix (PS * ps, const char * str)
6786 new_prefix (ps, str);
6790 picosat_set_seed (PS * ps, unsigned s)
6797 picosat_reset (PS * ps)
6804 picosat_add (PS * ps, int int_lit)
6806 int res = ps->oadded;
6809 if (ps->measurealltimeinlib)
6814 ABORTIF (ps->rup && ps->rupstarted && ps->oadded >= (unsigned)ps->rupclauses,
6815 "API usage: adding too many clauses after RUP header written");
6817 ABORTIF (ps->addingtoado,
6818 "API usage: 'picosat_add' and 'picosat_add_ado_lit' mixed");
6820 if (ps->state != READY)
6821 reset_incremental_usage (ps);
6825 if (ps->sohead == ps->eoso)
6826 ENLARGE (ps->soclauses, ps->sohead, ps->eoso);
6828 *ps->sohead++ = int_lit;
6833 lit = import_lit (ps, int_lit, 1);
6837 simplify_and_add_original_clause (ps);
6839 if (ps->measurealltimeinlib)
6846 picosat_add_arg (PS * ps, ...)
6851 while ((lit = va_arg (ap, int)))
6852 (void) picosat_add (ps, lit);
6854 return picosat_add (ps, 0);
6858 picosat_add_lits (PS * ps, int * lits)
6862 for (p = lits; (lit = *p); p++)
6863 (void) picosat_add (ps, lit);
6864 return picosat_add (ps, 0);
6868 picosat_add_ado_lit (PS * ps, int external_lit)
6873 if (ps->measurealltimeinlib)
6878 if (ps->state != READY)
6879 reset_incremental_usage (ps);
6881 ABORTIF (!ps->addingtoado && ps->ahead > ps->added,
6882 "API usage: 'picosat_add' and 'picosat_add_ado_lit' mixed");
6886 ps->addingtoado = 1;
6887 internal_lit = import_lit (external_lit, 1);
6888 add_lit (internal_lit);
6892 ps->addingtoado = 0;
6895 if (ps->measurealltimeinlib)
6899 (void) external_lit;
6900 ABORT ("compiled without all different constraint support");
6905 assume (PS * ps, Lit * lit)
6907 if (ps->alshead == ps->eoals)
6909 assert (ps->alstail == ps->als);
6910 ENLARGE (ps->als, ps->alshead, ps->eoals);
6911 ps->alstail = ps->als;
6914 *ps->alshead++ = lit;
6915 LOG ( fprintf (ps->out, "%sassumption %d\n", ps->prefix, LIT2INT (lit)));
6919 assume_contexts (PS * ps)
6922 if (ps->als != ps->alshead)
6924 for (p = ps->CLS; p != ps->clshead; p++)
6928 static const char * enumstr (int i) {
6930 if (last == 1) return "st";
6931 if (last == 2) return "nd";
6932 if (last == 3) return "rd";
6937 tderef (PS * ps, int int_lit)
6942 assert (abs (int_lit) <= (int) ps->max_var);
6944 lit = int2lit (ps, int_lit);
6950 if (lit->val == TRUE)
6953 if (lit->val == FALSE)
6960 pderef (PS * ps, int int_lit)
6965 assert (abs (int_lit) <= (int) ps->max_var);
6967 v = ps->vars + abs (int_lit);
6971 lit = int2lit (ps, int_lit);
6973 if (lit->val == TRUE)
6976 if (lit->val == FALSE)
6983 minautarky (PS * ps)
6985 unsigned * occs, maxoccs, tmpoccs, npartial;
6986 int * p, * c, lit, best, val;
6991 assert (!ps->partial);
6995 NEWN (occs, 2*ps->max_var + 1);
6996 CLRN (occs, 2*ps->max_var + 1);
6997 occs += ps->max_var;
6998 for (p = ps->soclauses; p < ps->sohead; p++)
7000 assert (occs[0] == ps->oadded);
7002 for (c = ps->soclauses; c < ps->sohead; c = p + 1)
7009 for (p = c; (lit = *p); p++)
7011 val = tderef (ps, lit);
7020 maxoccs = occs[lit];
7023 val = pderef (ps, lit);
7028 val = int2lit (ps, lit)->val;
7032 tmpoccs = occs[lit];
7033 if (best && tmpoccs <= maxoccs)
7041 LOG ( fprintf (ps->out, "%sautark %d with %d occs%s\n",
7042 ps->prefix, best, maxoccs, tl ? " (top)" : ""));
7043 ps->vars[abs (best)].partial = 1;
7046 for (p = c; (lit = *p); p++)
7048 assert (occs[lit] > 0);
7052 occs -= ps->max_var;
7053 DELETEN (occs, 2*ps->max_var + 1);
7058 "%sautarky of size %u out of %u satisfying all clauses (%.1f%%)\n",
7059 ps->prefix, npartial, ps->max_var, PERCENT (npartial, ps->max_var));
7063 picosat_assume (PS * ps, int int_lit)
7067 if (ps->measurealltimeinlib)
7072 if (ps->state != READY)
7073 reset_incremental_usage (ps);
7075 assume_contexts (ps);
7076 lit = import_lit (ps, int_lit, 1);
7079 if (ps->measurealltimeinlib)
7084 picosat_sat (PS * ps, int l)
7092 LOG ( fprintf (ps->out, "%sSTART call %u\n", ps->prefix, ps->calls));
7094 if (ps->added < ps->ahead)
7097 if (ps->addingtoado)
7098 ABORT ("API usage: incomplete all different constraint");
7101 ABORT ("API usage: incomplete clause");
7104 if (ps->state != READY)
7105 reset_incremental_usage (ps);
7107 assume_contexts (ps);
7111 assert (ps->state == READY);
7115 case PICOSAT_UNSATISFIABLE:
7119 case PICOSAT_SATISFIABLE:
7125 ps->state = UNKNOWN;
7136 LOG ( fprintf (ps->out, "%sEND call %u result %d\n", ps->prefix, ps->calls, res));
7138 ps->last_sat_call_result = res;
7144 picosat_res (PS * ps)
7146 return ps->last_sat_call_result;
7150 picosat_deref (PS * ps, int int_lit)
7155 check_sat_state (ps);
7156 ABORTIF (!int_lit, "API usage: can not deref zero literal");
7157 ABORTIF (ps->mtcls, "API usage: deref after empty clause generated");
7163 if (abs (int_lit) > (int) ps->max_var)
7166 lit = int2lit (ps, int_lit);
7168 if (lit->val == TRUE)
7171 if (lit->val == FALSE)
7178 picosat_deref_toplevel (PS * ps, int int_lit)
7181 ABORTIF (!int_lit, "API usage: can not deref zero literal");
7186 if (abs (int_lit) > (int) ps->max_var)
7189 return tderef (ps, int_lit);
7193 picosat_inconsistent (PS * ps)
7196 return ps->mtcls != 0;
7200 picosat_corelit (PS * ps, int int_lit)
7203 check_unsat_state (ps);
7204 ABORTIF (!int_lit, "API usage: zero literal can not be in core");
7206 assert (ps->mtcls || ps->failed_assumption);
7211 ABORTIF (!ps->trace, "tracing disabled");
7212 if (ps->measurealltimeinlib)
7215 if (abs (int_lit) <= (int) ps->max_var)
7216 res = ps->vars[abs (int_lit)].core;
7217 assert (!res || ps->failed_assumption || ps->vars[abs (int_lit)].used);
7218 if (ps->measurealltimeinlib)
7223 ABORT ("compiled without trace support");
7229 picosat_coreclause (PS * ps, int ocls)
7232 check_unsat_state (ps);
7234 ABORTIF (ocls < 0, "API usage: negative original clause index");
7235 ABORTIF (ocls >= (int)ps->oadded, "API usage: original clause index exceeded");
7237 assert (ps->mtcls || ps->failed_assumption);
7244 ABORTIF (!ps->trace, "tracing disabled");
7245 if (ps->measurealltimeinlib)
7248 clsptr = ps->oclauses + ocls;
7249 assert (clsptr < ps->ohead);
7253 if (ps->measurealltimeinlib)
7259 ABORT ("compiled without trace support");
7265 picosat_failed_assumption (PS * ps, int int_lit)
7269 ABORTIF (!int_lit, "API usage: zero literal as assumption");
7271 check_unsat_state (ps);
7274 assert (ps->failed_assumption);
7275 if (abs (int_lit) > (int) ps->max_var)
7277 if (!ps->extracted_all_failed_assumptions)
7278 extract_all_failed_assumptions (ps);
7279 lit = import_lit (ps, int_lit, 1);
7285 picosat_failed_context (PS * ps, int int_lit)
7289 ABORTIF (!int_lit, "API usage: zero literal as context");
7290 ABORTIF (abs (int_lit) > (int) ps->max_var, "API usage: invalid context");
7292 check_unsat_state (ps);
7293 assert (ps->failed_assumption);
7294 if (!ps->extracted_all_failed_assumptions)
7295 extract_all_failed_assumptions (ps);
7296 lit = import_lit (ps, int_lit, 0);
7302 picosat_failed_assumptions (PS * ps)
7308 ps->falshead = ps->fals;
7310 check_unsat_state (ps);
7313 assert (ps->failed_assumption);
7314 if (!ps->extracted_all_failed_assumptions)
7315 extract_all_failed_assumptions (ps);
7317 for (p = ps->als; p < ps->alshead; p++)
7323 ilit = LIT2INT (lit);
7324 if (ps->falshead == ps->eofals)
7325 ENLARGE (ps->fals, ps->falshead, ps->eofals);
7326 *ps->falshead++ = ilit;
7329 if (ps->falshead == ps->eofals)
7330 ENLARGE (ps->fals, ps->falshead, ps->eofals);
7331 *ps->falshead++ = 0;
7336 picosat_mus_assumptions (PS * ps, void * s, void (*cb)(void*,const int*), int fix)
7338 int i, j, ilit, len, norig = ps->alshead - ps->als, nwork, * work, res;
7339 signed char * redundant;
7348 check_unsat_state (ps);
7352 assert (ps->failed_assumption);
7353 if (!ps->extracted_all_failed_assumptions)
7354 extract_all_failed_assumptions (ps);
7356 for (p = ps->als; p < ps->alshead; p++)
7357 if (LIT2VAR (*p)->failed)
7362 DELETEN (ps->mass, ps->szmass);
7363 ps->szmass = len + 1;
7364 NEWN (ps->mass, ps->szmass);
7367 for (p = ps->als; p < ps->alshead; p++)
7373 ilit = LIT2INT (lit);
7375 ps->mass[i++] = ilit;
7381 "%sinitial set of failed assumptions of size %d out of %d (%.0f%%)\n",
7382 ps->prefix, len, norig, PERCENT (len, norig));
7388 for (i = 0; i < len; i++)
7389 work[i] = ps->mass[i];
7391 NEWN (redundant, nwork);
7392 CLRN (redundant, nwork);
7394 for (i = 0; i < nwork; i++)
7399 if (ps->verbosity > 1)
7401 "%strying to drop %d%s assumption %d\n",
7402 ps->prefix, i, enumstr (i), work[i]);
7403 for (j = 0; j < nwork; j++)
7405 if (i == j) continue;
7406 if (j < i && fix) continue;
7407 if (redundant[j]) continue;
7408 picosat_assume (ps, work[j]);
7411 res = picosat_sat (ps, -1);
7414 if (ps->verbosity > 1)
7416 "%sfailed to drop %d%s assumption %d\n",
7417 ps->prefix, i, enumstr (i), work[i]);
7421 picosat_add (ps, work[i]);
7422 picosat_add (ps, 0);
7428 if (ps->verbosity > 1)
7430 "%ssuceeded to drop %d%s assumption %d\n",
7431 ps->prefix, i, enumstr (i), work[i]);
7433 for (j = 0; j < nwork; j++)
7435 failed = picosat_failed_assumption (ps, work[j]);
7438 assert ((j < i && fix) || redundant[j] == !failed);
7445 if (ps->verbosity > 1)
7447 "%salso suceeded to drop %d%s assumption %d\n",
7448 ps->prefix, j, enumstr (j), work[j]);
7456 for (j = 0; j < nwork; j++)
7458 ps->mass[len++] = work[j];
7460 assert (len < oldlen);
7464 picosat_add (ps, -work[i]);
7465 picosat_add (ps, 0);
7469 for (j = 0; j <= i; j++)
7470 assert (redundant[j] >= 0);
7472 for (j = i + 1; j < nwork; j++)
7474 if (redundant[j] >= 0)
7479 picosat_add (ps, -work[j]);
7480 picosat_add (ps, 0);
7488 "%sreduced set of failed assumptions of size %d out of %d (%.0f%%)\n",
7489 ps->prefix, len, norig, PERCENT (len, norig));
7495 DELETEN (work, nwork);
7496 DELETEN (redundant, nwork);
7500 fprintf (ps->out, "%sreinitializing unsat state\n", ps->prefix);
7504 for (i = 0; i < len; i++)
7505 picosat_assume (ps, ps->mass[i]);
7510 picosat_sat (ps, -1);
7515 assert (!ps->extracted_all_failed_assumptions);
7516 extract_all_failed_assumptions (ps);
7523 mss (PS * ps, int * a, int size)
7527 assert (!ps->mtcls);
7530 DELETEN (ps->mssass, ps->szmssass);
7535 ps->szmssass = size + 1;
7536 NEWN (ps->mssass, ps->szmssass);
7538 LOG ( fprintf (ps->out, "%ssearch MSS over %d assumptions\n", ps->prefix, size));
7541 for (i = k; i < size; i++)
7543 for (j = 0; j < k; j++)
7544 picosat_assume (ps, ps->mssass[j]);
7546 LOG ( fprintf (ps->out,
7547 "%strying to add assumption %d to MSS : %d\n",
7548 ps->prefix, i, a[i]));
7550 picosat_assume (ps, a[i]);
7552 res = picosat_sat (ps, -1);
7555 LOG ( fprintf (ps->out,
7556 "%sadding assumption %d to MSS : %d\n", ps->prefix, i, a[i]));
7558 ps->mssass[k++] = a[i];
7560 for (j = i + 1; j < size; j++)
7562 if (picosat_deref (ps, a[j]) <= 0)
7565 LOG ( fprintf (ps->out,
7566 "%salso adding assumption %d to MSS : %d\n",
7567 ps->prefix, j, a[j]));
7569 ps->mssass[k++] = a[j];
7583 LOG ( fprintf (ps->out,
7584 "%signoring assumption %d in MSS : %d\n", ps->prefix, i, a[i]));
7588 LOG ( fprintf (ps->out, "%sfound MSS of size %d\n", ps->prefix, k));
7594 reassume (PS * ps, const int * a, int size)
7597 LOG ( fprintf (ps->out, "%sreassuming all assumptions\n", ps->prefix));
7598 for (i = 0; i < size; i++)
7599 picosat_assume (ps, a[i]);
7603 picosat_maximal_satisfiable_subset_of_assumptions (PS * ps)
7609 "API usage: CNF inconsistent (use 'picosat_inconsistent')");
7613 size = ps->alshead - ps->als;
7616 for (i = 0; i < size; i++)
7617 a[i] = LIT2INT (ps->als[i]);
7619 res = mss (ps, a, size);
7620 reassume (ps, a, size);
7630 check_mss_flags_clean (PS * ps)
7634 for (i = 1; i <= ps->max_var; i++)
7636 assert (!ps->vars[i].msspos);
7637 assert (!ps->vars[i].mssneg);
7645 push_mcsass (PS * ps, int lit)
7647 if (ps->nmcsass == ps->szmcsass)
7649 ps->szmcsass = ps->szmcsass ? 2*ps->szmcsass : 1;
7650 RESIZEN (ps->mcsass, ps->nmcsass, ps->szmcsass);
7653 ps->mcsass[ps->nmcsass++] = lit;
7657 next_mss (PS * ps, int mcs)
7659 int i, *a, size, mssize, mcsize, lit, inmss;
7660 const int * res, * p;
7663 if (ps->mtcls) return 0;
7665 check_mss_flags_clean (ps);
7667 if (mcs && ps->mcsass)
7669 DELETEN (ps->mcsass, ps->szmcsass);
7670 ps->nmcsass = ps->szmcsass = 0;
7674 size = ps->alshead - ps->als;
7677 for (i = 0; i < size; i++)
7678 a[i] = LIT2INT (ps->als[i]);
7680 (void) picosat_sat (ps, -1);
7682 //TODO short cut for 'picosat_res () == 10'?
7686 assert (picosat_res (ps) == 20);
7691 res = mss (ps, a, size);
7699 for (p = res; (lit = *p); p++)
7701 v = ps->vars + abs (lit);
7704 assert (!v->msspos);
7709 assert (!v->mssneg);
7717 for (i = 0; i < size; i++)
7720 v = ps->vars + abs (lit);
7721 if (lit > 0 && v->msspos)
7723 else if (lit < 0 && v->mssneg)
7728 if (mssize < mcsize)
7731 picosat_add (ps, -lit);
7736 picosat_add (ps, lit);
7740 push_mcsass (ps, lit);
7742 picosat_add (ps, 0);
7744 push_mcsass (ps, 0);
7746 for (i = 0; i < size; i++)
7749 v = ps->vars + abs (lit);
7756 reassume (ps, a, size);
7763 picosat_next_maximal_satisfiable_subset_of_assumptions (PS * ps)
7767 res = next_mss (ps, 0);
7773 picosat_next_minimal_correcting_subset_of_assumptions (PS * ps)
7775 const int * res, * tmp;
7777 tmp = next_mss (ps, 1);
7778 res = tmp ? ps->mcsass : 0;
7784 picosat_humus (PS * ps,
7785 void (*callback)(void*state,int nmcs,int nhumus),
7788 int lit, nmcs, j, nhumus;
7789 const int * mcs, * p;
7794 for (i = 1; i <= ps->max_var; i++)
7797 assert (!v->humuspos);
7798 assert (!v->humusneg);
7802 while ((mcs = picosat_next_minimal_correcting_subset_of_assumptions (ps)))
7804 for (p = mcs; (lit = *p); p++)
7806 v = ps->vars + abs (lit);
7825 LOG ( fprintf (ps->out,
7826 "%smcs %d of size %d humus %d\n",
7827 ps->prefix, nmcs, (int)(p - mcs), nhumus));
7829 callback (state, nmcs, nhumus);
7831 assert (!ps->szhumus);
7833 for (i = 1; i <= ps->max_var; i++)
7841 assert (nhumus + 1 == ps->szhumus);
7842 NEWN (ps->humus, ps->szhumus);
7844 for (i = 1; i <= ps->max_var; i++)
7849 assert (j < nhumus);
7850 ps->humus[j++] = (int) i;
7854 assert (j < nhumus);
7855 assert (i < INT_MAX);
7856 ps->humus[j++] = - (int) i;
7859 assert (j == nhumus);
7860 assert (j < ps->szhumus);
7867 picosat_usedlit (PS * ps, int int_lit)
7871 check_sat_or_unsat_or_unknown_state (ps);
7872 ABORTIF (!int_lit, "API usage: zero literal can not be used");
7873 int_lit = abs (int_lit);
7874 res = (int_lit <= (int) ps->max_var) ? ps->vars[int_lit].used : 0;
7879 picosat_write_clausal_core (PS * ps, FILE * file)
7881 check_trace_support_and_execute (ps, file, write_core_wrapper, 0);
7885 picosat_write_compact_trace (PS * ps, FILE * file)
7887 check_trace_support_and_execute (ps, file, write_trace,
7888 COMPACT_TRACECHECK_TRACE_FMT);
7892 picosat_write_extended_trace (PS * ps, FILE * file)
7894 check_trace_support_and_execute (ps, file, write_trace,
7895 EXTENDED_TRACECHECK_TRACE_FMT);
7899 picosat_write_rup_trace (PS * ps, FILE * file)
7901 check_trace_support_and_execute (ps, file, write_trace, RUP_TRACE_FMT);
7905 picosat_max_bytes_allocated (PS * ps)
7908 return ps->max_bytes;
7912 picosat_set_propagation_limit (PS * ps, unsigned long long l)
7914 ps->lpropagations = l;
7918 picosat_propagations (PS * ps)
7920 return ps->propagations;
7924 picosat_visits (PS * ps)
7930 picosat_decisions (PS * ps)
7932 return ps->decisions;
7936 picosat_variables (PS * ps)
7939 return (int) ps->max_var;
7943 picosat_added_original_clauses (PS * ps)
7946 return (int) ps->oadded;
7950 picosat_stats (PS * ps)
7955 assert (ps->sdecisions + ps->rdecisions + ps->assumptions == ps->decisions);
7958 fprintf (ps->out, "%s%u calls\n", ps->prefix, ps->calls);
7961 fprintf (ps->out, "%s%u contexts", ps->prefix, ps->contexts);
7963 fprintf (ps->out, " %u internal variables", ps->internals);
7965 fprintf (ps->out, "\n");
7967 fprintf (ps->out, "%s%u iterations\n", ps->prefix, ps->iterations);
7968 fprintf (ps->out, "%s%u restarts", ps->prefix, ps->restarts);
7970 fprintf (ps->out, " (%u skipped)", ps->skippedrestarts);
7972 fputc ('\n', ps->out);
7974 fprintf (ps->out, "%s%u failed literals", ps->prefix, ps->failedlits);
7977 ", %u calls, %u rounds, %llu propagations",
7978 ps->flcalls, ps->flrounds, ps->flprops);
7980 fputc ('\n', ps->out);
7983 "%sfl: %u = %.1f%% implicit, %llu oopsed, %llu tried, %llu skipped\n",
7985 ps->ifailedlits, PERCENT (ps->ifailedlits, ps->failedlits),
7986 ps->floopsed, ps->fltried, ps->flskipped);
7989 fprintf (ps->out, "%s%u conflicts", ps->prefix, ps->conflicts);
7991 fprintf (ps->out, " (%u uips = %.1f%%)\n", ps->uips, PERCENT(ps->uips,ps->conflicts));
7993 fputc ('\n', ps->out);
7996 fprintf (ps->out, "%s%u adc conflicts\n", ps->prefix, ps->adoconflicts);
7999 fprintf (ps->out, "%s%llu dereferenced literals\n", ps->prefix, ps->derefs);
8001 fprintf (ps->out, "%s%u decisions", ps->prefix, ps->decisions);
8003 fprintf (ps->out, " (%u random = %.2f%%",
8004 ps->rdecisions, PERCENT (ps->rdecisions, ps->decisions));
8005 fprintf (ps->out, ", %u assumptions", ps->assumptions);
8006 fputc (')', ps->out);
8008 fputc ('\n', ps->out);
8011 "%s%u static phase decisions (%.1f%% of all variables)\n",
8013 ps->staticphasedecisions, PERCENT (ps->staticphasedecisions, ps->max_var));
8015 fprintf (ps->out, "%s%u fixed variables\n", ps->prefix, ps->fixed);
8016 assert (ps->nonminimizedllits >= ps->minimizedllits);
8017 redlits = ps->nonminimizedllits - ps->minimizedllits;
8018 fprintf (ps->out, "%s%u learned literals\n", ps->prefix, ps->llitsadded);
8019 fprintf (ps->out, "%s%.1f%% deleted literals\n",
8020 ps->prefix, PERCENT (redlits, ps->nonminimizedllits));
8025 "%s%llu antecedents (%.1f antecedents per clause",
8026 ps->prefix, ps->antecedents, AVERAGE (ps->antecedents, ps->conflicts));
8028 fprintf (ps->out, ", %.1f bytes/antecedent)", AVERAGE (ps->znts, ps->antecedents));
8029 fputs (")\n", ps->out);
8032 fprintf (ps->out, "%s%llu propagations (%.1f propagations per decision)\n",
8033 ps->prefix, ps->propagations, AVERAGE (ps->propagations, ps->decisions));
8034 fprintf (ps->out, "%s%llu visits (%.1f per propagation)\n",
8035 ps->prefix, ps->visits, AVERAGE (ps->visits, ps->propagations));
8037 "%s%llu binary clauses visited (%.1f%% %.1f per propagation)\n",
8038 ps->prefix, ps->bvisits,
8039 PERCENT (ps->bvisits, ps->visits),
8040 AVERAGE (ps->bvisits, ps->propagations));
8042 "%s%llu ternary clauses visited (%.1f%% %.1f per propagation)\n",
8043 ps->prefix, ps->tvisits,
8044 PERCENT (ps->tvisits, ps->visits),
8045 AVERAGE (ps->tvisits, ps->propagations));
8047 "%s%llu large clauses visited (%.1f%% %.1f per propagation)\n",
8048 ps->prefix, ps->lvisits,
8049 PERCENT (ps->lvisits, ps->visits),
8050 AVERAGE (ps->lvisits, ps->propagations));
8051 fprintf (ps->out, "%s%llu other true (%.1f%% of visited clauses)\n",
8052 ps->prefix, ps->othertrue, PERCENT (ps->othertrue, ps->visits));
8054 "%s%llu other true in binary clauses (%.1f%%)"
8055 ", %llu upper (%.1f%%)\n",
8056 ps->prefix, ps->othertrue2, PERCENT (ps->othertrue2, ps->othertrue),
8057 ps->othertrue2u, PERCENT (ps->othertrue2u, ps->othertrue2));
8059 "%s%llu other true in large clauses (%.1f%%)"
8060 ", %llu upper (%.1f%%)\n",
8061 ps->prefix, ps->othertruel, PERCENT (ps->othertruel, ps->othertrue),
8062 ps->othertruelu, PERCENT (ps->othertruelu, ps->othertruel));
8063 fprintf (ps->out, "%s%llu ternary and large traversals (%.1f per visit)\n",
8064 ps->prefix, ps->traversals, AVERAGE (ps->traversals, ps->visits));
8065 fprintf (ps->out, "%s%llu large traversals (%.1f per large visit)\n",
8066 ps->prefix, ps->ltraversals, AVERAGE (ps->ltraversals, ps->lvisits));
8067 fprintf (ps->out, "%s%llu assignments\n", ps->prefix, ps->assignments);
8069 fprintf (ps->out, "%s%llu propagations\n", ps->prefix, picosat_propagations (ps));
8070 fprintf (ps->out, "%s%llu visits\n", ps->prefix, picosat_visits (ps));
8072 fprintf (ps->out, "%s%.1f%% variables used\n", ps->prefix, PERCENT (ps->vused, ps->max_var));
8075 fprintf (ps->out, "%s%.1f seconds in library\n", ps->prefix, ps->seconds);
8076 fprintf (ps->out, "%s%.1f megaprops/second\n",
8077 ps->prefix, AVERAGE (ps->propagations / 1e6f, ps->seconds));
8078 fprintf (ps->out, "%s%.1f megavisits/second\n",
8079 ps->prefix, AVERAGE (ps->visits / 1e6f, ps->seconds));
8080 fprintf (ps->out, "%sprobing %.1f seconds %.0f%%\n",
8081 ps->prefix, ps->flseconds, PERCENT (ps->flseconds, ps->seconds));
8084 "%srecycled %.1f MB in %u reductions\n",
8085 ps->prefix, ps->rrecycled / (double) (1 << 20), ps->reductions);
8087 "%srecycled %.1f MB in %u simplifications\n",
8088 ps->prefix, ps->srecycled / (double) (1 << 20), ps->simps);
8090 fprintf (ps->out, "%s%u simplifications\n", ps->prefix, ps->simps);
8091 fprintf (ps->out, "%s%u reductions\n", ps->prefix, ps->reductions);
8092 fprintf (ps->out, "%s%.1f MB recycled\n", ps->prefix, ps->recycled / (double) (1 << 20));
8094 fprintf (ps->out, "%s%.1f MB maximally allocated\n",
8095 ps->prefix, picosat_max_bytes_allocated (ps) / (double) (1 << 20));
8099 #include <sys/time.h>
8100 #include <sys/resource.h>
8101 #include <sys/unistd.h>
8105 picosat_time_stamp (void)
8111 if (!getrusage (RUSAGE_SELF, &u))
8113 res += u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec;
8114 res += u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec;
8121 picosat_seconds (PS * ps)
8128 picosat_print (PS * ps, FILE * file)
8130 #ifdef NO_BINARY_CLAUSES
8131 Lit * lit, *other, * last;
8138 if (ps->measurealltimeinlib)
8144 n += ps->alshead - ps->als;
8146 for (p = SOC; p != EOC; p = NXC (p))
8160 #ifdef NO_BINARY_CLAUSES
8161 last = int2lit (ps, -ps->max_var);
8162 for (lit = int2lit (ps, 1); lit <= last; lit++)
8164 stack = LIT2IMPLS (lit);
8165 eol = stack->start + stack->count;
8166 for (q = stack->start; q < eol; q++)
8172 fprintf (file, "p cnf %d %u\n", ps->max_var, n);
8174 for (p = SOC; p != EOC; p = NXC (p))
8185 eol = end_of_lits (c);
8186 for (q = c->lits; q < eol; q++)
8187 fprintf (file, "%d ", LIT2INT (*q));
8189 fputs ("0\n", file);
8192 #ifdef NO_BINARY_CLAUSES
8193 last = int2lit (ps, -ps->max_var);
8194 for (lit = int2lit (ps, 1); lit <= last; lit++)
8196 stack = LIT2IMPLS (lit);
8197 eol = stack->start + stack->count;
8198 for (q = stack->start; q < eol; q++)
8199 if ((other = *q) >= lit)
8200 fprintf (file, "%d %d 0\n", LIT2INT (lit), LIT2INT (other));
8206 for (r = ps->als; r < ps->alshead; r++)
8207 fprintf (file, "%d 0\n", LIT2INT (*r));
8212 if (ps->measurealltimeinlib)
8217 picosat_enter (PS * ps)
8223 picosat_leave (PS * ps)
8229 picosat_message (PS * ps, int vlevel, const char * fmt, ...)
8233 if (vlevel > ps->verbosity)
8236 fputs (ps->prefix, ps->out);
8238 vfprintf (ps->out, fmt, ap);
8240 fputc ('\n', ps->out);
8244 picosat_changed (PS * ps)
8249 check_sat_state (ps);
8251 res = (ps->min_flipped <= ps->saved_max_var);
8252 assert (!res || ps->saved_flips != ps->flips);
8258 picosat_reset_phases (PS * ps)
8264 picosat_reset_scores (PS * ps)
8267 ps->hhead = ps->heap + 1;
8268 for (r = ps->rnks + 1; r <= ps->rnks + ps->max_var; r++)
8276 picosat_remove_learned (PS * ps, unsigned percentage)
8279 reset_incremental_usage (ps);
8280 reduce (ps, percentage);
8285 picosat_set_global_default_phase (PS * ps, int phase)
8288 ABORTIF (phase < 0, "API usage: 'picosat_set_global_default_phase' "
8289 "with negative argument");
8290 ABORTIF (phase > 3, "API usage: 'picosat_set_global_default_phase' "
8291 "with argument > 3");
8292 ps->defaultphase = phase;
8296 picosat_set_default_phase_lit (PS * ps, int int_lit, int phase)
8304 lit = import_lit (ps, int_lit, 1);
8309 newphase = (int_lit < 0) == (phase < 0);
8310 v->defphase = v->phase = newphase;
8311 v->usedefphase = v->assigned = 1;
8315 v->usedefphase = v->assigned = 0;
8320 picosat_set_more_important_lit (PS * ps, int int_lit)
8328 lit = import_lit (ps, int_lit, 1);
8332 ABORTIF (r->lessimportant, "can not mark variable more and less important");
8334 if (r->moreimportant)
8337 r->moreimportant = 1;
8344 picosat_set_less_important_lit (PS * ps, int int_lit)
8352 lit = import_lit (ps, int_lit, 1);
8356 ABORTIF (r->moreimportant, "can not mark variable more and less important");
8358 if (r->lessimportant)
8361 r->lessimportant = 1;
8370 picosat_ado_conflicts (PS * ps)
8373 return ps->adoconflicts;
8377 picosat_disable_ado (PS * ps)
8380 assert (!ps->adodisabled);
8381 ps->adodisabled = 1;
8385 picosat_enable_ado (PS * ps)
8388 assert (ps->adodisabled);
8389 ps->adodisabled = 0;
8393 picosat_set_ado_conflict_limit (unsigned newadoconflictlimit)
8396 ps->adoconflictlimit = newadoconflictlimit;
8402 picosat_simplify (PS * ps)
8405 reset_incremental_usage (ps);
8411 picosat_haveados (void)
8421 picosat_save_original_clauses (PS * ps)
8423 if (ps->saveorig) return;
8424 ABORTIF (ps->oadded, "API usage: 'picosat_save_original_clauses' too late");
8429 picosat_deref_partial (PS * ps, int int_lit)
8432 check_sat_state (ps);
8433 ABORTIF (!int_lit, "API usage: can not partial deref zero literal");
8434 ABORTIF (ps->mtcls, "API usage: deref partial after empty clause generated");
8435 ABORTIF (!ps->saveorig, "API usage: 'picosat_save_original_clauses' missing");
8444 return pderef (ps, int_lit);