VIS

src/sat/satImplication.c

Go to the documentation of this file.
00001 
00019 #include "satInt.h"
00020 #include "baig.h"
00021 
00022 static char rcsid[] UNUSED = "$Id: satImplication.c,v 1.10 2009/04/11 18:26:37 fabio Exp $";
00023 
00024 /*---------------------------------------------------------------------------*/
00025 /* Constant declarations                                                     */
00026 /*---------------------------------------------------------------------------*/
00027 
00030 /*---------------------------------------------------------------------------*/
00031 /* Static function prototypes                                                */
00032 /*---------------------------------------------------------------------------*/
00033 
00034 
00037 typedef int (*IMPLY_FN)
00038     (satManager_t *cm, satLevel_t *d, long v, long left, long right);
00039 
00040 
00041 IMPLY_FN satImplicationFN[64]= {
00042   sat_ImplyStop,                 /* 0    */
00043   sat_ImplyConflict,             /* 1  */
00044   sat_ImplyLeftForward,          /* 2  */
00045   sat_ImplyLeftForward,          /* 3  */
00046   sat_ImplyStop,                 /* 4  */
00047   sat_ImplyConflict,             /* 5  */
00048   sat_ImplyLeftForward,          /* 6  */
00049   sat_ImplyLeftForward,          /* 7  */
00050   sat_ImplySplit,                /* 8  */
00051   sat_ImplyConflict,             /* 9  */
00052   sat_ImplyLeftForward,          /* 10  */
00053   sat_ImplyLeftForward,          /* 11  */
00054   sat_ImplySplit,                /* 12  */
00055   sat_ImplyConflict,             /* 13  */
00056   sat_ImplyLeftForward,          /* 14  */
00057   sat_ImplyLeftForward,          /* 15  */
00058   sat_ImplyStop,                 /* 16  */
00059   sat_ImplyConflict,             /* 17  */
00060   sat_ImplyRightForward,         /* 18  */
00061   sat_ImplyRightForward,         /* 19  */
00062   sat_ImplyConflict,             /* 20  */
00063   sat_ImplyStop,                 /* 21  */
00064   sat_ImplyForwardOne,           /* 22  */
00065   sat_ImplyForwardOne,           /* 23  */
00066   sat_ImplyPropRight,            /* 24  */
00067   sat_ImplyPropRightOne,         /* 25  */
00068   sat_ImplyStop,                 /* 26  */
00069   sat_ImplyStop,                 /* 27  */
00070   sat_ImplyPropRight,            /* 28  */
00071   sat_ImplyPropRightOne,         /* 29  */
00072   sat_ImplyStop,                 /* 30  */
00073   sat_ImplyStop,                 /* 31  */
00074   sat_ImplySplit,                /* 32  */
00075   sat_ImplyConflict,             /* 33  */
00076   sat_ImplyRightForward,         /* 34  */
00077   sat_ImplyRightForward,         /* 35  */
00078   sat_ImplyPropLeft,             /* 36  */
00079   sat_ImplyPropLeftOne,          /* 37  */
00080   sat_ImplyStop,                 /* 38  */
00081   sat_ImplyStop,                 /* 39  */
00082   sat_ImplySplit,                /* 40  */
00083   sat_ImplyPropLeftRight,        /* 41  */
00084   sat_ImplyStop,                 /* 42  */
00085   sat_ImplyStop,                 /* 43  */
00086   sat_ImplySplit,                /* 44  */
00087   sat_ImplyPropLeftRight,        /* 45  */
00088   sat_ImplyStop,                 /* 46  */
00089   sat_ImplyStop,                 /* 47  */
00090   sat_ImplySplit,                /* 48  */
00091   sat_ImplyConflict,             /* 49  */
00092   sat_ImplyRightForward,         /* 50  */
00093   sat_ImplyRightForward,         /* 51  */
00094   sat_ImplyPropLeft,             /* 52  */
00095   sat_ImplyPropLeftOne,          /* 53  */
00096   sat_ImplyStop,                 /* 54  */
00097   sat_ImplyStop,                 /* 55  */
00098   sat_ImplySplit,                /* 56  */
00099   sat_ImplyPropLeftRight,        /* 57  */
00100   sat_ImplyStop,                 /* 58  */
00101   sat_ImplyStop,                 /* 59  */
00102   sat_ImplySplit,                /* 60  */
00103   sat_ImplyPropLeftRight,        /* 61  */
00104   sat_ImplyStop,                 /* 62  */
00105   sat_ImplyStop,                 /* 63  */
00106 };
00107 
00108 
00109 
00110 /*---------------------------------------------------------------------------*/
00111 /* Definition of exported functions                                          */
00112 /*---------------------------------------------------------------------------*/
00113 
00114 
00133 int
00134 sat_ImplyStop(
00135         satManager_t *cm,
00136         satLevel_t *d,
00137         long v,
00138         long left,
00139         long right)
00140 {
00141   return(0);
00142 }
00143 
00162 int
00163 sat_ImplySplit(
00164         satManager_t *cm,
00165         satLevel_t *d,
00166         long v,
00167         long left,
00168         long right)
00169 {
00170   return(0);
00171 }
00172 
00190 int
00191 sat_ImplyConflict(
00192         satManager_t *cm,
00193         satLevel_t *d,
00194         long v,
00195         long left,
00196         long right)
00197 {
00198   if(left != 2)  {
00199     d->conflict = SATnormalNode(v);
00200   }
00201   return(0);
00202 }
00203 
00204 
00222 int
00223 sat_ImplyLeftForward(
00224         satManager_t *cm,
00225         satLevel_t *d,
00226         long v,
00227         long left,
00228         long right)
00229 {
00230 
00231   v = SATnormalNode(v);
00232   left = SATnormalNode(left);
00233 
00234   SATvalue(v) = 0;
00235   SATmakeImplied(v, d);
00236   SATante(v) = left;
00237 
00238   sat_Enqueue(cm->queue, v);
00239   SATflags(v) |= InQueueMask;
00240 
00241   SATflags(v) |= (SATflags(left) & ObjMask);
00242 
00243   cm->each->nAigImplications++;
00244   return(0);
00245 }
00246 
00264 int
00265 sat_ImplyRightForward(
00266         satManager_t *cm,
00267         satLevel_t *d,
00268         long v,
00269         long left,
00270         long right)
00271 {
00272   v = SATnormalNode(v);
00273   right = SATnormalNode(right);
00274 
00275   SATvalue(v) = 0;
00276   SATmakeImplied(v, d);
00277   SATante(v) = right;
00278 
00279   sat_Enqueue(cm->queue, v);
00280   SATflags(v) |= InQueueMask;
00281 
00282   SATflags(v) |= (SATflags(right) & ObjMask);
00283   cm->each->nAigImplications++;
00284 
00285   return(0);
00286 }
00287 
00288 
00306 int
00307 sat_ImplyForwardOne(
00308         satManager_t *cm,
00309         satLevel_t *d,
00310         long v,
00311         long left,
00312         long right)
00313 {
00314   v = SATnormalNode(v);
00315   left = SATnormalNode(left);
00316   right = SATnormalNode(right);
00317 
00318   SATvalue(v) = 1;
00319   SATmakeImplied(v, d);
00320   SATante(v) = right;
00321   SATante2(v) = left;
00322 
00323   sat_Enqueue(cm->queue, v);
00324   SATflags(v) |= InQueueMask;
00325 
00326   SATflags(v) |= (SATflags(right) & ObjMask);
00327   SATflags(v) |= (SATflags(left) & ObjMask);
00328   cm->each->nAigImplications++;
00329 
00330   return(0);
00331 }
00332 
00350 int
00351 sat_ImplyPropRight(
00352         satManager_t *cm,
00353         satLevel_t *d,
00354         long v,
00355         long left,
00356         long right)
00357 {
00358   int isInverted;
00359 
00360   isInverted = SATisInverted(right);
00361   v = SATnormalNode(v);
00362   left = SATnormalNode(left);
00363   right = SATnormalNode(right);
00364 
00365   SATmakeImplied(right, d);
00366   SATvalue(right) = isInverted;
00367 
00368   SATante(right) = left;
00369   SATante2(right) = v;
00370 
00371   sat_Enqueue(cm->queue, right);
00372   SATflags(right) |= InQueueMask;
00373 
00374   SATflags(right) |= (SATflags(left) & ObjMask);
00375   SATflags(right) |= (SATflags(v) & ObjMask);
00376   cm->each->nAigImplications++;
00377   return(0);
00378 }
00379 
00397 int
00398 sat_ImplyPropRightOne(
00399         satManager_t *cm,
00400         satLevel_t *d,
00401         long v,
00402         long left,
00403         long right)
00404 {
00405   int isInverted;
00406 
00407   isInverted = SATisInverted(right);
00408   v = SATnormalNode(v);
00409   left = SATnormalNode(left);
00410   right = SATnormalNode(right);
00411 
00412   SATmakeImplied(right, d);
00413 
00414   SATante(right) = v;
00415 
00416   SATvalue(right) = !isInverted;
00417 
00418   sat_Enqueue(cm->queue, right);
00419   SATflags(right) |= InQueueMask;
00420 
00421   SATflags(right) |= (SATflags(v) & ObjMask);
00422   cm->each->nAigImplications++;
00423   return(0);
00424 }
00425 
00426 
00444 int
00445 sat_ImplyPropLeft(
00446         satManager_t *cm,
00447         satLevel_t *d,
00448         long v,
00449         long left,
00450         long right)
00451 {
00452   int isInverted;
00453 
00454   isInverted = SATisInverted(left);
00455   v = SATnormalNode(v);
00456   left = SATnormalNode(left);
00457   right = SATnormalNode(right);
00458 
00459   SATmakeImplied(left, d);
00460 
00461   SATante(left) = v;
00462   SATante2(left) = right;
00463 
00464   SATvalue(left) = isInverted;
00465 
00466   sat_Enqueue(cm->queue, left);
00467   SATflags(left) |= InQueueMask;
00468 
00469   SATflags(left) |= (SATflags(v) & ObjMask);
00470   SATflags(left) |= (SATflags(right) & ObjMask);
00471   cm->each->nAigImplications++;
00472   return(0);
00473 }
00474 
00475 
00476 
00494 int
00495 sat_ImplyPropLeftOne(
00496         satManager_t *cm,
00497         satLevel_t *d,
00498         long v,
00499         long left,
00500         long right)
00501 {
00502   int isInverted;
00503 
00504   isInverted = SATisInverted(left);
00505   v = SATnormalNode(v);
00506   left = SATnormalNode(left);
00507   right = SATnormalNode(right);
00508 
00509   SATmakeImplied(left, d);
00510 
00511   SATante(left) = v;
00512 
00513   SATvalue(left) = !isInverted;
00514 
00515   sat_Enqueue(cm->queue, left);
00516   SATflags(left) |= InQueueMask;
00517 
00518   SATflags(left) |= (SATflags(v) & ObjMask);
00519   cm->each->nAigImplications++;
00520   return(0);
00521 }
00522 
00523 
00524 
00542 int
00543 sat_ImplyPropLeftRight(
00544         satManager_t *cm,
00545         satLevel_t *d,
00546         long v,
00547         long left,
00548         long right)
00549 {
00550   int isLeftInverted;
00551   int isRightInverted;
00552 
00553   if(left == right)     return 1;
00554 
00555   isLeftInverted = SATisInverted(left);
00556   isRightInverted = SATisInverted(right);
00557 
00558   v = SATnormalNode(v);
00559   left = SATnormalNode(left);
00560   right = SATnormalNode(right);
00561 
00562   SATmakeImplied(left, d);
00563   SATmakeImplied(right, d);
00564 
00565   SATante(left) = v;
00566   SATante(right) = v;
00567 
00568   SATvalue(left) = !isLeftInverted;
00569   SATvalue(right) = !isRightInverted;
00570 
00571   sat_Enqueue(cm->queue, left);
00572   SATflags(left) |= InQueueMask;
00573 
00574   sat_Enqueue(cm->queue, right);
00575   SATflags(right) |= InQueueMask;
00576 
00577   SATflags(left) |= (SATflags(v) & ObjMask);
00578   SATflags(right) |= (SATflags(v) & ObjMask);
00579   cm->each->nAigImplications += 2;
00580   return(0);
00581 }
00582 
00583 
00595 void
00596 sat_ImplyCNF(
00597         satManager_t *cm,
00598         satLevel_t *d,
00599         long v,
00600         satArray_t *wl)
00601 {
00602 int size, i, j;
00603 long *space;
00604 long lit, *plit, *tplit;
00605 int dir;
00606 long nv, tv, *oplit, *wlit;
00607 int isInverted, value;
00608 int tsize, inverted;
00609 long cur, clit;
00610 satStatistics_t *stats;
00611 satOption_t *option;
00612 satQueue_t *Q;
00613 satVariable_t *var;
00614 
00615   size = wl->num;
00616   space = wl->space;
00617   Q = cm->queue;
00618   stats = cm->each;
00619   option = cm->option;
00620   nv = 0;
00621   wlit = 0;
00622 
00623   for(i=0; i<size; i++) {
00624     plit = (long*)space[i];
00625 
00626     if(plit == 0 || *plit == 0) {
00627       size--;
00628       space[i] = space[size];
00629       i--;
00630       continue;
00631     }
00632 
00633     lit = *plit;
00634     dir = SATgetDir(lit);
00635     oplit = plit;
00636 
00637     while(1) {
00638       oplit += dir;
00639       if(*oplit <=0) {
00640         if(dir == 1) nv =- (*oplit);
00641         if(dir == SATgetDir(lit)) {
00642           oplit = plit;
00643           dir = -dir;
00644           continue;
00645         }
00646 
00647         tv = SATgetNode(*wlit);
00648         isInverted = SATisInverted(tv);
00649         tv = SATnormalNode(tv);
00650         value = SATvalue(tv) ^ isInverted;
00651         if(value == 0) {  
00652           d->conflict = nv;
00653           wl->num = size;
00654           return;
00655         }
00656         else if(value > 1) { 
00657           SATvalue(tv) = !isInverted;
00658           SATmakeImplied(tv, d);
00659           SATante(tv) = nv;
00660 
00661           if((SATflags(tv) & InQueueMask) == 0) {
00662             sat_Enqueue(Q, tv);
00663             SATflags(tv) |= InQueueMask;
00664           }
00665 
00666           stats->nCNFImplications++;
00667 
00671           if(option->incTraceObjective == 1) {
00672             tsize = SATnumLits(nv);
00673             for(j=0, tplit=(long*)SATfirstLit(nv); j<tsize; j++, tplit++) {
00674               cur = SATgetNode(*tplit);
00675               cur = SATnormalNode(cur);
00676               if(SATflags(cur) & ObjMask) {
00677                 SATflags(tv) |= ObjMask;
00678                 break;
00679               }
00680             }
00681           }
00682         }
00683 
00684         break;
00685       }
00686 
00687       clit = *oplit;
00688 
00689       tv = SATgetNode(clit);
00690       inverted = SATisInverted(tv);
00691       tv = SATnormalNode(tv);
00692       value = SATvalue(tv) ^ inverted;
00693 
00694       if(SATisWL(clit)) { /* found other watched literal */
00695         wlit = oplit;
00696         if(value == 1)  {
00697           break;
00698         }
00699         continue;
00700       }
00701 
00702       if(value == 0)
00703         continue;
00704 
00705       SATunsetWL(plit);
00706 
00707       size--;
00708       space[i] = space[size];
00709       i--;
00710 
00712       var = SATgetVariable(tv);
00713       SATsetWL(oplit, dir);
00714 
00715       inverted = !inverted;
00716       if(var->wl[inverted]) {
00717         sat_ArrayInsert(var->wl[inverted], (long)oplit);
00718       }
00719       else {
00720         var->wl[inverted] = sat_ArrayAlloc(4);
00721         sat_ArrayInsert(var->wl[inverted], (long)oplit);
00722       }
00723 
00724       break;
00725     }
00726   }
00727   wl->num = size;
00728 }
00729 
00730 
00745 int
00746 sat_ImplyNode(
00747         satManager_t *cm,
00748         satLevel_t *d,
00749         long v)
00750 {
00751 long *fan, cur;
00752 int value;
00753 int i, size;
00754 long left, right;
00755 satVariable_t *var;
00756 satArray_t *wlArray;
00757 
00758   value = SATvalue(v) ^ 1;
00759   var = SATgetVariable(v);
00760   wlArray = var->wl[value];
00761 
00763   if(wlArray && wlArray->size) {
00764     sat_ImplyCNF(cm, d, v, wlArray);
00765   }
00766 
00767   if(d->conflict)
00768     return(0);
00769 
00771   fan = (long *)SATfanout(v);
00772   size = SATnFanout(v);
00773   for(i=0; i<size; i++, fan++) {
00774     cur = *fan;
00775     cur = cur >> 1;
00776     cur = SATnormalNode(cur);
00777     left = SATleftChild(cur);
00778     right = SATrightChild(cur);
00779     value = SATgetValue(left, right, cur);
00780 
00781     (satImplicationFN[value])(cm, d, cur, left, right);
00782 
00783     if(d->conflict)
00784       return(0);
00785   }
00786 
00787   left = SATleftChild(v);
00788   right = SATrightChild(v);
00789   value = SATgetValue(left, right, v);
00790 
00791   (satImplicationFN[value])(cm, d, v, left, right);
00792 
00793   if(d->conflict)
00794     return(0);
00795 
00796 
00797   return(1);
00798 }
00799 
00800 
00814 void
00815 sat_ImplicationMain(
00816         satManager_t *cm,
00817         satLevel_t *d)
00818 {
00819 satQueue_t *Q, *BDDQ;
00820 long v;
00821 
00822   Q = cm->queue;
00823   BDDQ = cm->BDDQueue;
00824 
00825   while(1) {
00826     v = sat_Dequeue(Q);
00827     while(v && d->conflict == 0) {
00828       sat_ImplyNode(cm, d, v);
00829       SATflags(v) &= ResetInQueueMask;
00830 
00831       v = sat_Dequeue(Q);
00832     }
00833 
00834     if(d->conflict)
00835       break;
00836 
00837     if(cm->option->BDDImplication) {
00838       v = sat_Dequeue(Q);
00839       while(v && d->conflict == 0) {
00840         sat_ImplyBDD(cm, d, v);
00841         SATflags(v) &= ResetInQueueMask;
00842 
00843         v = sat_Dequeue(Q);
00844       }
00845     }
00846 
00847     if(Q->size == 0 && BDDQ->size == 0)
00848       break;
00849   }
00850 
00851   sat_CleanImplicationQueue(cm);
00852   cm->implicatedSoFar += d->implied->num;
00853 }
00854 
00855 
00856 
00868 void
00869 sat_CleanImplicationQueue(
00870         satManager_t *cm)
00871 {
00872 satQueue_t *Q;
00873  int i;
00874  bAigEdge_t v;
00875 
00876   Q = cm->queue;
00877 
00878   if(Q->size <=0)       return;
00879 
00880   //Bing: important, can't be zero, since some var's value,which are now in
00881   //implication queue will be erased. This may lead to not being able to
00882   //identify conflict
00883   if(cm->option->AbRf || cm->option->IP || cm->option->arosat){
00884     if(Q->front <= Q->rear) {
00885       for(i=Q->front; i<=Q->rear; i++) {
00886         v = Q->array[i];
00887         SATflags(v) &= ResetInQueueMask;
00888       }
00889     }
00890     else {
00891       for(i=Q->front; i<Q->max; i++) {
00892         v = Q->array[i];
00893         SATflags(v) &= ResetInQueueMask;
00894       }
00895       for(i=0; i<Q->rear; i++) {
00896         v = Q->array[i];
00897         SATflags(v) &= ResetInQueueMask;
00898       }
00899     }
00900   }
00901 #if 0
00902   if(Q->front <= Q->rear) {
00903     for(i=Q->front; i<=Q->rear; i++) {
00904       v = Q->array[i];
00905       SATflags(v) &= ResetInQueueMask;
00906       SATante(v) = 0;
00907       SATante2(v) = 0;
00908       SATvalue(v) = 2;
00909     }
00910   }
00911   else {
00912     for(i=Q->front; i<Q->max; i++) {
00913       v = Q->array[i];
00914       SATflags(v) &= ResetInQueueMask;
00915       SATante(v) = 0;
00916       SATante2(v) = 0;
00917       SATvalue(v) = 2;
00918     }
00919     for(i=0; i<Q->rear; i++) {
00920       v = Q->array[i];
00921       SATflags(v) &= ResetInQueueMask;
00922       SATante(v) = 0;
00923       SATante2(v) = 0;
00924       SATvalue(v) = 2;
00925     }
00926   }
00927 #endif
00928 
00929   Q->size = 0;
00930   Q->front = 1;
00931   Q->rear = 0;
00932 }
00933 
00934 
00946 void
00947 sat_ImplyArray(
00948         satManager_t *cm,
00949         satLevel_t *d,
00950         satArray_t *arr)
00951 {
00952 int i, size;
00953 long v;
00954 int inverted, value;
00955 satQueue_t *Q;
00956 
00957   if(arr == 0)  return;
00958   if(cm->status)return;
00959 
00960   size = arr->num;
00961   Q = cm->queue;
00962   for(i=0; i<size; i++) {
00963     v = arr->space[i];
00964     inverted = SATisInverted(v);
00965     v = SATnormalNode(v);
00966 
00967     value = !inverted;
00968     if(SATvalue(v) < 2) {
00969       if(SATvalue(v) == value)  continue;
00970       else {
00971         cm->status = SAT_UNSAT;
00972         d->conflict = v;
00973         //Bing:
00974         return;
00975       }
00976     }
00977 
00978     SATvalue(v) = value;
00979     SATmakeImplied(v, d);
00980     //Bing:
00981     cm->each->nCNFImplications++;
00982     if(cm->option->coreGeneration && cm->option->IP){
00983       st_insert(cm->anteTable,(char *)SATnormalNode(v),(char *)SATnormalNode(v));
00984     }
00985 
00986     if((SATflags(v) & InQueueMask) == 0) {
00987       sat_Enqueue(Q, v);
00988       SATflags(v) |= InQueueMask;
00989     }
00990   }
00991 }
00992 
01005 void
01006 sat_ImplyBDD(
01007         satManager_t *cm,
01008         satLevel_t *d,
01009         long v)
01010 {
01012 }
01013 
01026 void
01027 sat_BuildLevelZeroHyperImplicationGraph(
01028         satManager_t *cm)
01029 {
01031 }