VIS

src/puresat/puresatArosat.c

Go to the documentation of this file.
00001 
00049 #include "puresatInt.h"
00050 /*---------------------------------------------------------------------------*/
00051 /* Constant declarations                                                     */
00052 /*---------------------------------------------------------------------------*/
00053 
00054 /*---------------------------------------------------------------------------*/
00055 /* Type declarations                                                         */
00056 /*---------------------------------------------------------------------------*/
00057 
00058 
00059 /*---------------------------------------------------------------------------*/
00060 /* Structure declarations                                                    */
00061 /*---------------------------------------------------------------------------*/
00062 
00063 
00064 /*---------------------------------------------------------------------------*/
00065 /* Variable declarations                                                     */
00066 /*---------------------------------------------------------------------------*/
00067 
00068 /*---------------------------------------------------------------------------*/
00069 /* Macro declarations                                                        */
00070 /*---------------------------------------------------------------------------*/
00071 
00074 /*---------------------------------------------------------------------------*/
00075 /* Static function prototypes                                                */
00076 /*---------------------------------------------------------------------------*/
00077 
00078 
00091 static int
00092 ScoreCompare(
00093   const void * node1,
00094   const void * node2)
00095 {
00096 int v1;
00097 int v2;
00098 int s1, s2;
00099 
00100   v1 = *(int *)(node1);
00101   v2 = *(int *)(node2);
00102   s1 = *((int *)(node1) + 1);
00103   s2 = *((int *)(node2) + 1);
00104 
00105   if(s1 == s2) {
00106     return(v1 > v2);
00107   }
00108   return(s1 < s2);
00109 }
00110 
00113 /*---------------------------------------------------------------------------*/
00114 /* Definition of exported functions                                          */
00115 /*---------------------------------------------------------------------------*/
00116 
00117 
00118 /*---------------------------------------------------------------------------*/
00119 /* Definition of internal functions                                          */
00120 /*---------------------------------------------------------------------------*/
00121 
00122 
00123 typedef int (*ASIMPLY_FN)
00124     (satManager_t *cm, satLevel_t *d, long v, long left, long right);
00125 
00126 
00127 ASIMPLY_FN ASImplicationFN[64]= {
00128   AS_ImplyStop,                 /* 0    */
00129   AS_ImplyConflict,             /* 1  */
00130   AS_ImplyLeftForward,          /* 2  */
00131   AS_ImplyLeftForward,          /* 3  */
00132   AS_ImplyStop,                 /* 4  */
00133   AS_ImplyConflict,             /* 5  */
00134   AS_ImplyLeftForward,          /* 6  */
00135   AS_ImplyLeftForward,          /* 7  */
00136   AS_ImplySplit,                /* 8  */
00137   AS_ImplyConflict,             /* 9  */
00138   AS_ImplyLeftForward,          /* 10  */
00139   AS_ImplyLeftForward,          /* 11  */
00140   AS_ImplySplit,                /* 12  */
00141   AS_ImplyConflict,             /* 13  */
00142   AS_ImplyLeftForward,          /* 14  */
00143   AS_ImplyLeftForward,          /* 15  */
00144   AS_ImplyStop,                 /* 16  */
00145   AS_ImplyConflict,             /* 17  */
00146   AS_ImplyRightForward,         /* 18  */
00147   AS_ImplyRightForward,         /* 19  */
00148   AS_ImplyConflict,             /* 20  */
00149   AS_ImplyStop,                 /* 21  */
00150   AS_ImplyForwardOne,           /* 22  */
00151   AS_ImplyForwardOne,           /* 23  */
00152   AS_ImplyPropRight,            /* 24  */
00153   AS_ImplyPropRightOne,         /* 25  */
00154   AS_ImplyStop,                 /* 26  */
00155   AS_ImplyStop,                 /* 27  */
00156   AS_ImplyPropRight,            /* 28  */
00157   AS_ImplyPropRightOne,         /* 29  */
00158   AS_ImplyStop,                 /* 30  */
00159   AS_ImplyStop,                 /* 31  */
00160   AS_ImplySplit,                /* 32  */
00161   AS_ImplyConflict,             /* 33  */
00162   AS_ImplyRightForward,         /* 34  */
00163   AS_ImplyRightForward,         /* 35  */
00164   AS_ImplyPropLeft,             /* 36  */
00165   AS_ImplyPropLeftOne,          /* 37  */
00166   AS_ImplyStop,                 /* 38  */
00167   AS_ImplyStop,                 /* 39  */
00168   AS_ImplySplit,                /* 40  */
00169   AS_ImplyPropLeftRight,        /* 41  */
00170   AS_ImplyStop,                 /* 42  */
00171   AS_ImplyStop,                 /* 43  */
00172   AS_ImplySplit,                /* 44  */
00173   AS_ImplyPropLeftRight,        /* 45  */
00174   AS_ImplyStop,                 /* 46  */
00175   AS_ImplyStop,                 /* 47  */
00176   AS_ImplySplit,                /* 48  */
00177   AS_ImplyConflict,             /* 49  */
00178   AS_ImplyRightForward,         /* 50  */
00179   AS_ImplyRightForward,         /* 51  */
00180   AS_ImplyPropLeft,             /* 52  */
00181   AS_ImplyPropLeftOne,          /* 53  */
00182   AS_ImplyStop,                 /* 54  */
00183   AS_ImplyStop,                 /* 55  */
00184   AS_ImplySplit,                /* 56  */
00185   AS_ImplyPropLeftRight,        /* 57  */
00186   AS_ImplyStop,                 /* 58  */
00187   AS_ImplyStop,                 /* 59  */
00188   AS_ImplySplit,                /* 60  */
00189   AS_ImplyPropLeftRight,        /* 61  */
00190   AS_ImplyStop,                 /* 62  */
00191   AS_ImplyStop,                 /* 63  */
00192 };
00193 
00194 
00195 
00196 /*---------------------------------------------------------------------------*/
00197 /* Definition of exported functions                                          */
00198 /*---------------------------------------------------------------------------*/
00199 
00200 
00219 int
00220 AS_ImplyStop(
00221         satManager_t *cm,
00222         satLevel_t *d,
00223         long v,
00224         long left,
00225         long right)
00226 {
00227   return(0);
00228 }
00229 
00248 int
00249 AS_ImplySplit(
00250         satManager_t *cm,
00251         satLevel_t *d,
00252         long v,
00253         long left,
00254         long right)
00255 {
00256   return(0);
00257 }
00258 
00276 int
00277 AS_ImplyConflict(
00278         satManager_t *cm,
00279         satLevel_t *d,
00280         long v,
00281         long left,
00282         long right)
00283 {
00284   if(left != 2)  {
00285     d->conflict = SATnormalNode(v);
00286   }
00287   return(0);
00288 }
00289 
00290 
00308 int
00309 AS_ImplyLeftForward(
00310         satManager_t *cm,
00311         satLevel_t *d,
00312         long v,
00313         long left,
00314         long right)
00315 {
00316   if(sat_ASImp(cm,d,v,0))
00317     return(0);
00318 
00319   v = SATnormalNode(v);
00320   left = SATnormalNode(left);
00321 
00322   SATvalue(v) = 0;
00323   SATmakeImplied(v, d);
00324   SATante(v) = left;
00325 
00326   sat_Enqueue(cm->queue, v);
00327   SATflags(v) |= InQueueMask;
00328 
00329   SATflags(v) |= (SATflags(left) & ObjMask);
00330 
00331   cm->each->nAigImplications++;
00332 
00333   return(0);
00334 }
00335 
00353 int
00354 AS_ImplyRightForward(
00355         satManager_t *cm,
00356         satLevel_t *d,
00357         long v,
00358         long left,
00359         long right)
00360 {
00361 
00362   if(sat_ASImp(cm,d,v,0))
00363     return(0);
00364 
00365   v = SATnormalNode(v);
00366   right = SATnormalNode(right);
00367 
00368   SATvalue(v) = 0;
00369   SATmakeImplied(v, d);
00370   SATante(v) = right;
00371 
00372   sat_Enqueue(cm->queue, v);
00373   SATflags(v) |= InQueueMask;
00374 
00375   SATflags(v) |= (SATflags(right) & ObjMask);
00376   cm->each->nAigImplications++;
00377 
00378   return(0);
00379 }
00380 
00381 
00399 int
00400 AS_ImplyForwardOne(
00401         satManager_t *cm,
00402         satLevel_t *d,
00403         long v,
00404         long left,
00405         long right)
00406 {
00407 
00408   if(sat_ASImp(cm,d,v,1))
00409     return(0);
00410 
00411   v = SATnormalNode(v);
00412   left = SATnormalNode(left);
00413   right = SATnormalNode(right);
00414 
00415   SATvalue(v) = 1;
00416   SATmakeImplied(v, d);
00417   SATante(v) = right;
00418   SATante2(v) = left;
00419 
00420   sat_Enqueue(cm->queue, v);
00421   SATflags(v) |= InQueueMask;
00422 
00423   SATflags(v) |= (SATflags(right) & ObjMask);
00424   SATflags(v) |= (SATflags(left) & ObjMask);
00425   cm->each->nAigImplications++;
00426 
00427   return(0);
00428 }
00429 
00447 int
00448 AS_ImplyPropRight(
00449         satManager_t *cm,
00450         satLevel_t *d,
00451         long v,
00452         long left,
00453         long right)
00454 {
00455   int isInverted;
00456 
00457   isInverted = SATisInverted(right);
00458 
00459 
00460   if(sat_ASImp(cm,d,right, isInverted))
00461     return(0);
00462 
00463   v = SATnormalNode(v);
00464   left = SATnormalNode(left);
00465   right = SATnormalNode(right);
00466 
00467   SATmakeImplied(right, d);
00468   SATvalue(right) = isInverted;
00469 
00470   SATante(right) = left;
00471   SATante2(right) = v;
00472 
00473   sat_Enqueue(cm->queue, right);
00474   SATflags(right) |= InQueueMask;
00475 
00476   SATflags(right) |= (SATflags(left) & ObjMask);
00477   SATflags(right) |= (SATflags(v) & ObjMask);
00478   cm->each->nAigImplications++;
00479 
00480   return(0);
00481 }
00482 
00500 int
00501 AS_ImplyPropRightOne(
00502         satManager_t *cm,
00503         satLevel_t *d,
00504         long v,
00505         long left,
00506         long right)
00507 {
00508   int isInverted;
00509 
00510 
00511   if(sat_ASImp(cm,d,right,!SATisInverted(right)))
00512     return(0);
00513 
00514   isInverted = SATisInverted(right);
00515   v = SATnormalNode(v);
00516   left = SATnormalNode(left);
00517   right = SATnormalNode(right);
00518 
00519   SATmakeImplied(right, d);
00520 
00521   SATante(right) = v;
00522 
00523   SATvalue(right) = !isInverted;
00524 
00525   sat_Enqueue(cm->queue, right);
00526   SATflags(right) |= InQueueMask;
00527 
00528   SATflags(right) |= (SATflags(v) & ObjMask);
00529   cm->each->nAigImplications++;
00530 
00531   return(0);
00532 }
00533 
00534 
00552 int
00553 AS_ImplyPropLeft(
00554         satManager_t *cm,
00555         satLevel_t *d,
00556         long v,
00557         long left,
00558         long right)
00559 {
00560   int isInverted;
00561 
00562 
00563   if(sat_ASImp(cm,d,left,SATisInverted(left)))
00564     return(0);
00565 
00566   isInverted = SATisInverted(left);
00567   v = SATnormalNode(v);
00568   left = SATnormalNode(left);
00569   right = SATnormalNode(right);
00570 
00571   SATmakeImplied(left, d);
00572 
00573   SATante(left) = v;
00574   SATante2(left) = right;
00575 
00576   SATvalue(left) = isInverted;
00577 
00578   sat_Enqueue(cm->queue, left);
00579   SATflags(left) |= InQueueMask;
00580 
00581   SATflags(left) |= (SATflags(v) & ObjMask);
00582   SATflags(left) |= (SATflags(right) & ObjMask);
00583   cm->each->nAigImplications++;
00584 
00585   return(0);
00586 }
00587 
00588 
00589 
00607 int
00608 AS_ImplyPropLeftOne(
00609         satManager_t *cm,
00610         satLevel_t *d,
00611         long v,
00612         long left,
00613         long right)
00614 {
00615   int isInverted;
00616 
00617 
00618   if(sat_ASImp(cm,d,left,!SATisInverted(left)))
00619     return(0);
00620 
00621   isInverted = SATisInverted(left);
00622   v = SATnormalNode(v);
00623   left = SATnormalNode(left);
00624   right = SATnormalNode(right);
00625 
00626   SATmakeImplied(left, d);
00627 
00628   SATante(left) = v;
00629 
00630   SATvalue(left) = !isInverted;
00631 
00632   sat_Enqueue(cm->queue, left);
00633   SATflags(left) |= InQueueMask;
00634 
00635   SATflags(left) |= (SATflags(v) & ObjMask);
00636   cm->each->nAigImplications++;
00637 
00638   return(0);
00639 }
00640 
00641 
00642 
00660 int
00661 AS_ImplyPropLeftRight(
00662         satManager_t *cm,
00663         satLevel_t *d,
00664         long v,
00665         long left,
00666         long right)
00667 {
00668   int isLeftInverted;
00669   int isRightInverted;
00670 
00671   int lImp=1,rImp=1;
00672 
00673   if(left == right)     return 1;
00674 
00675 
00676   if(sat_ASImp(cm,d,left,!SATisInverted(left)))
00677     lImp = 0;
00678   if(sat_ASImp(cm,d,right,!SATisInverted(right)))
00679     rImp = 0;
00680 
00681 
00682   isLeftInverted = SATisInverted(left);
00683   isRightInverted = SATisInverted(right);
00684 
00685   v = SATnormalNode(v);
00686   left = SATnormalNode(left);
00687   right = SATnormalNode(right);
00688 
00689   if(lImp){
00690     SATmakeImplied(left, d);
00691     SATante(left) = v;
00692     SATvalue(left) = !isLeftInverted;
00693     sat_Enqueue(cm->queue, left);
00694     SATflags(left) |= InQueueMask;
00695     SATflags(left) |= (SATflags(v) & ObjMask);
00696     cm->each->nAigImplications ++;
00697   }
00698 
00699   if(rImp){
00700     SATmakeImplied(right, d);
00701     SATante(right) = v;
00702     SATvalue(right) = !isRightInverted;
00703     sat_Enqueue(cm->queue, right);
00704     SATflags(right) |= InQueueMask;
00705     SATflags(right) |= (SATflags(v) & ObjMask);
00706     cm->each->nAigImplications ++;
00707   }
00708 
00709   return(0);
00710 }
00711 
00724 void
00725 AS_Undo(
00726         satManager_t *cm,
00727         satLevel_t *d)
00728 {
00729 satArray_t *implied, *satisfied;
00730 int size, i;
00731 long *space, v;
00732  satVariable_t *var;
00733  int dfs;
00734 
00735   implied = d->implied;
00736   space = implied->space;
00737   size = implied->num;
00738   for(i=0; i<size; i++, space++) {
00739     v = *space;
00740 
00741     SATvalue(v) = 2;
00742     SATflags(v) &= ResetNewVisitedObjInQueueMask;
00743     SATante(v) = 0;
00744     SATante2(v) = 0;
00745     SATlevel(v) = -1;
00746 
00747 
00748     if(SATflags(SATnormalNode(v))&AssignedMask){
00749       SATflags(SATnormalNode(v))&=ResetAssignedMask;
00750       var=SATgetVariable(v);
00751       dfs = var->scores[0]/SCOREUNIT;
00752       cm->assignedArray[dfs]--;
00753 
00754 #if DBG
00755       assert(cm->assignedArray[dfs]>=0);
00756 #endif
00757     }
00758 
00759   }
00760 
00761   cm->implicatedSoFar -= size;
00762 
00763   if(d->satisfied) {
00764     satisfied = d->implied;
00765     space = satisfied->space;
00766     size = satisfied->num;
00767     for(i=0; i<size; i++, space++) {
00768       v = *space;
00769 
00770       SATflags(v) &= ResetBDDSatisfiedMask;
00771     }
00772     d->satisfied->num = 0;
00773   }
00774   if(d->level > 0) {
00775     if((cm->decisionHead[d->level-1]).currentVarPos < cm->currentVarPos)
00776       cm->currentVarPos = (cm->decisionHead[d->level-1]).currentVarPos;
00777   }
00778   else
00779     cm->currentVarPos = d->currentVarPos;
00780 
00781   d->implied->num = 0;
00782   d->currentVarPos = 0;
00783 
00784   d->conflict = 0;
00785 
00786 
00787 
00788 }
00789 
00790 
00803 void
00804 AS_ImplyCNF(
00805         satManager_t *cm,
00806         satLevel_t *d,
00807         long v,
00808         satArray_t *wl)
00809 {
00810 int size, i, j;
00811 long *space;
00812 long lit, *plit, *tplit;
00813 int dir;
00814 long nv, tv, *oplit, *wlit;
00815 int isInverted, value;
00816 int tsize, inverted;
00817 long cur, clit;
00818 satStatistics_t *stats;
00819 satOption_t *option;
00820 satQueue_t *Q;
00821 satVariable_t *var;
00822 
00823  long tmpv;
00824 
00825   size = wl->num;
00826   space = wl->space;
00827   Q = cm->queue;
00828   stats = cm->each;
00829   option = cm->option;
00830   nv = 0;
00831   wlit = 0;
00832 
00833   for(i=0; i<size; i++) {
00834     plit = (long*)space[i];
00835 
00836     if(plit == 0 || *plit == 0) {
00837       size--;
00838       space[i] = space[size];
00839       i--;
00840       continue;
00841     }
00842 
00843     lit = *plit;
00844     dir = SATgetDir(lit);
00845     oplit = plit;
00846 
00847     while(1) {
00848       oplit += dir;
00849       if(*oplit <=0) {
00850         if(dir == 1) nv =- (*oplit);
00851         if(dir == SATgetDir(lit)) {
00852           oplit = plit;
00853           dir = -dir;
00854           continue;
00855         }
00856 
00857         tv = SATgetNode(*wlit);
00858 
00859         tmpv = tv;
00860         isInverted = SATisInverted(tv);
00861         tv = SATnormalNode(tv);
00862         value = SATvalue(tv) ^ isInverted;
00863         if(value == 0) {  
00864           d->conflict = nv;
00865           wl->num = size;
00866           return;
00867         }
00868         else if(value > 1) { 
00870           if(sat_ASImp(cm,d,tmpv,!isInverted))
00871             break;
00872 
00873           SATvalue(tv) = !isInverted;
00874           SATmakeImplied(tv, d);
00875           SATante(tv) = nv;
00876 
00877           if((SATflags(tv) & InQueueMask) == 0) {
00878             sat_Enqueue(Q, tv);
00879             SATflags(tv) |= InQueueMask;
00880           }
00881 
00882           stats->nCNFImplications++;
00883 
00887           if(option->incTraceObjective == 1) {
00888             tsize = SATnumLits(nv);
00889             for(j=0, tplit=(long*)SATfirstLit(nv); j<tsize; j++, tplit++) {
00890               cur = SATgetNode(*tplit);
00891               cur = SATnormalNode(cur);
00892               if(SATflags(cur) & ObjMask) {
00893                 SATflags(tv) |= ObjMask;
00894                 break;
00895               }
00896             }
00897           }
00898         }
00899 
00900         break;
00901       }
00902 
00903       clit = *oplit;
00904 
00905       tv = SATgetNode(clit);
00906       inverted = SATisInverted(tv);
00907       tv = SATnormalNode(tv);
00908       value = SATvalue(tv) ^ inverted;
00909 
00910       if(SATisWL(clit)) { /* found other watched literal */
00911         wlit = oplit;
00912         if(value == 1)  {
00913           break;
00914         }
00915         continue;
00916       }
00917 
00918       if(value == 0)
00919         continue;
00920 
00921       SATunsetWL(plit);
00922 
00923       size--;
00924       space[i] = space[size];
00925       i--;
00926 
00928       var = SATgetVariable(tv);
00929       SATsetWL(oplit, dir);
00930 
00931       inverted = !inverted;
00932       if(var->wl[inverted]) {
00933         sat_ArrayInsert(var->wl[inverted], (long)oplit);
00934       }
00935       else {
00936         var->wl[inverted] = sat_ArrayAlloc(4);
00937         sat_ArrayInsert(var->wl[inverted], (long)oplit);
00938       }
00939 
00940       break;
00941     }
00942   }
00943   wl->num = size;
00944 }
00945 
00946 
00959 int
00960 AS_ImplyNode(
00961         satManager_t *cm,
00962         satLevel_t *d,
00963         long v)
00964 {
00965 long *fan, cur;
00966 int value;
00967 int i, size;
00968 long left, right;
00969 satVariable_t *var;
00970 satArray_t *wlArray;
00971 
00972   value = SATvalue(v) ^ 1;
00973   var = SATgetVariable(v);
00974   wlArray = var->wl[value];
00975 
00977   if(wlArray && wlArray->size) {
00978     AS_ImplyCNF(cm, d, v, wlArray);
00979   }
00980 
00981   if(d->conflict)
00982     return(0);
00983 
00985   fan = (long *)SATfanout(v);
00986   size = SATnFanout(v);
00987   for(i=0; i<size; i++, fan++) {
00988     cur = *fan;
00989     cur = cur >> 1;
00990     cur = SATnormalNode(cur);
00991     left = SATleftChild(cur);
00992     right = SATrightChild(cur);
00993     value = SATgetValue(left, right, cur);
00994 #if DBG
00995 
00996     assert(SATflags(SATnormalNode(left))&CoiMask);
00997     assert(SATflags(SATnormalNode(right))&CoiMask);
00998 #endif
00999     (ASImplicationFN[value])(cm, d, cur, left, right);
01000 
01001     if(d->conflict)
01002       return(0);
01003   }
01004 
01005   left = SATleftChild(v);
01006   right = SATrightChild(v);
01007   value = SATgetValue(left, right, v);
01008 #if DBG
01009 
01010   assert(left==bAig_NULL||SATflags(SATnormalNode(left))&CoiMask);
01011   assert(right==bAig_NULL||SATflags(SATnormalNode(right))&CoiMask);
01012 #endif
01013 
01014 
01015   (ASImplicationFN[value])(cm, d, v, left, right);
01016 
01017   if(d->conflict)
01018     return(0);
01019 
01020 
01021   return(1);
01022 }
01023 
01024 
01037 void
01038 AS_ImplicationMain(
01039         satManager_t *cm,
01040         satLevel_t *d)
01041 {
01042 satQueue_t *Q, *BDDQ;
01043 long v;
01044 
01045   Q = cm->queue;
01046   BDDQ = cm->BDDQueue;
01047 
01048   while(1) {
01049     v = sat_Dequeue(Q);
01050     while(v && d->conflict == 0) {
01051       AS_ImplyNode(cm, d, v);
01052       SATflags(v) &= ResetInQueueMask;
01053 
01054       v = sat_Dequeue(Q);
01055     }
01056 
01057     if(d->conflict)
01058       break;
01059 
01060     if(cm->option->BDDImplication) {
01061       v = sat_Dequeue(Q);
01062       while(v && d->conflict == 0) {
01063         sat_ImplyBDD(cm, d, v);
01064         SATflags(v) &= ResetInQueueMask;
01065 
01066         v = sat_Dequeue(Q);
01067       }
01068     }
01069 
01070     if(Q->size == 0 && BDDQ->size == 0)
01071       break;
01072   }
01073 
01074   sat_CleanImplicationQueue(cm);
01075   cm->implicatedSoFar += d->implied->num;
01076 }
01077 
01078 
01091 void
01092 AS_ImplyArray(
01093         satManager_t *cm,
01094         satLevel_t *d,
01095         satArray_t *arr)
01096 {
01097 int i, size;
01098 long v;
01099 int inverted, value;
01100 satQueue_t *Q;
01101  satVariable_t *var;
01102 
01103  int dfs;
01104 
01105   if(arr == 0)  return;
01106   if(cm->status)return;
01107 
01108   size = arr->num;
01109   Q = cm->queue;
01110   for(i=0; i<size; i++) {
01111     v = arr->space[i];
01112     inverted = SATisInverted(v);
01113     v = SATnormalNode(v);
01114 
01115     value = !inverted;
01116     if(SATvalue(v) < 2) {
01117       if(SATvalue(v) == value)  continue;
01118       else {
01119         cm->status = SAT_UNSAT;
01120         d->conflict = v;
01121 
01122         return;
01123       }
01124     }
01125 
01126 
01127     var = SATgetVariable(v);
01128     dfs = var->scores[0]/SCOREUNIT;
01129     if(dfs==cm->LatchLevel){
01130       if(!(SATflags(SATnormalNode(v))&AssignedMask)){
01131         SATflags(SATnormalNode(v))|=AssignedMask;
01132         cm->assignedArray[dfs]++;
01133 
01134 #if DBG
01135         assert(cm->assignedArray[dfs]<=cm->numArray[dfs]);
01136 #endif
01137         if(cm->assignedArray[dfs]==cm->numArray[dfs])
01138           sat_ASmergeLevel(cm);
01139       }
01140     }
01141 
01142 
01143     SATvalue(v) = value;
01144     SATmakeImplied(v, d);
01145 
01146     if(cm->option->coreGeneration){
01147       st_insert(cm->anteTable,(char *)SATnormalNode(v),(char *)SATnormalNode(v));
01148     }
01149 
01150     if((SATflags(v) & InQueueMask) == 0) {
01151       sat_Enqueue(Q, v);
01152       SATflags(v) |= InQueueMask;
01153     }
01154   }
01155 }
01156 
01169 void
01170 AS_PreProcessing(satManager_t *cm)
01171 {
01172 satLevel_t *d;
01173 
01174 
01176   cm->queue = sat_CreateQueue(1024);
01177   cm->BDDQueue = sat_CreateQueue(1024);
01178   cm->unusedAigQueue = sat_CreateQueue(1024);
01179 
01185   if(cm->variableArray == 0) {
01186     cm->variableArray = ALLOC(satVariable_t, cm->initNumVariables+1);
01187     memset(cm->variableArray, 0,
01188             sizeof(satVariable_t) * (cm->initNumVariables+1));
01189   }
01190 
01191   cm->auxArray = sat_ArrayAlloc(1024);
01192   cm->nonobjUnitLitArray = sat_ArrayAlloc(128);
01193   cm->objUnitLitArray = sat_ArrayAlloc(128);
01194 
01195 
01196 
01197   if(cm->option->AbRf == 0)
01198     sat_CompactFanout(cm);
01199 
01200 
01201   /*assign initial layer score to variables*/
01202    sat_InitLayerScore(cm);
01203 
01205   if(cm->decisionHeadSize == 0) {
01206     cm->decisionHeadSize = 32;
01207     cm->decisionHead = ALLOC(satLevel_t, cm->decisionHeadSize);
01208     memset(cm->decisionHead, 0, sizeof(satLevel_t) * cm->decisionHeadSize);
01209   }
01210   cm->currentDecision = -1;
01211 
01213   SATvalue(2) = 2;
01214   SATflags(0) = 0;
01215 
01216   cm->initNodesArraySize = cm->nodesArraySize;
01217   cm->beginConflict = cm->nodesArraySize;
01218 
01220   if(cm->option->incTraceObjective) {
01221     sat_RestoreForwardedClauses(cm, 0);
01222   }
01223   else if(cm->option->incAll) {
01224     sat_RestoreForwardedClauses(cm, 1);
01225   }
01226 
01227   if(cm->option->incTraceObjective) {
01228     sat_MarkObjectiveFlagToArray(cm, cm->obj);
01229     sat_MarkObjectiveFlagToArray(cm, cm->objCNF);
01230   }
01231 
01232 
01234   d = sat_AllocLevel(cm);
01235 
01236   sat_ApplyForcedAssignmentMain(cm, d);
01237 
01238   if(cm->status == SAT_UNSAT)
01239     return;
01240 
01241 
01242   if(cm->option->coreGeneration){
01243     cm->rm = ALLOC(RTManager_t, 1);
01244     memset(cm->rm,0,sizeof(RTManager_t));
01245   }
01246 
01247 
01248   AS_ImplyArray(cm, d, cm->auxObj);
01249   AS_ImplyArray(cm, d, cm->obj);
01250   AS_ImplyArray(cm,d,cm->assertArray);
01251 
01252 
01253   AS_ImplicationMain(cm, d);
01254   if(d->conflict) {
01255     cm->status = SAT_UNSAT;
01256   }
01257 
01258   if(cm->status == 0) {
01259     if(cm->option->incDistill) {
01260       sat_IncrementalUsingDistill(cm);
01261     }
01262   }
01263 
01264 }
01265 
01266 
01280 void
01281 AS_Backtrack(
01282         satManager_t *cm,
01283         int level)
01284 {
01285 satLevel_t *d;
01286 
01287 
01288   d = SATgetDecision(cm->currentDecision);
01289   while(1) {
01290     if(d->level <= level)
01291       break;
01292 
01293     AS_Undo(cm, d);
01294     cm->currentDecision--;
01295     if(cm->currentDecision == -1)
01296       break;
01297     d = SATgetDecision(cm->currentDecision);
01298   }
01299   return;
01300 }
01301 
01302 
01316 void
01317 AS_UpdateScore(
01318         satManager_t *cm)
01319 {
01320 satArray_t *one, *tmp;
01321 satArray_t *ordered;
01322 satVariable_t *var;
01323 int size, i;
01324 long v;
01325 int value;
01326 
01327  int baseScore,realScore,newNum;
01328 
01329   ordered = cm->orderedVariableArray;
01330 
01331   one = sat_ArrayAlloc(ordered->num);
01332   tmp = sat_ArrayAlloc(ordered->num);
01333 
01334   size = ordered->num;
01335   for(i=0; i<size; i++) {
01336     v = ordered->space[i];
01337 
01338     if(SATvalue(v) < 2 && SATlevel(v) == 0)
01339       continue;
01340 
01341     var = SATgetVariable(v);
01342 
01343     baseScore =(var->scores[0]/SCOREUNIT)*SCOREUNIT;
01344     realScore = var->scores[0]%SCOREUNIT;
01345     newNum = var->litsCount[0] - var->lastLitsCount[0];
01346     var->scores[0] =  baseScore + (realScore>>1) + newNum;
01347     baseScore =(var->scores[1]/SCOREUNIT)*SCOREUNIT;
01348     realScore = var->scores[1]%SCOREUNIT;
01349     newNum = var->litsCount[1] - var->lastLitsCount[1];
01350     var->scores[1] =  baseScore + (realScore>>1) + newNum;
01351 
01352     var->lastLitsCount[0] = var->litsCount[0];
01353     var->lastLitsCount[1] = var->litsCount[1];
01354 
01355 
01356     if((var->scores[0] + var->scores[1]) < 1) {
01357       sat_ArrayInsert(one, v);
01358     }
01359     else {
01360       value = (var->scores[0] > var->scores[1]) ? var->scores[0] : var->scores[1];
01361       sat_ArrayInsert(tmp, v);
01362       sat_ArrayInsert(tmp, value);
01363     }
01364   }
01365 
01366   qsort(tmp->space, (tmp->num)>>1, sizeof(long)*2, ScoreCompare);
01367 
01368   ordered->num = 0;
01369   size = tmp->num;
01370   for(i=0; i<size; i++) {
01371     v = tmp->space[i];
01372     sat_ArrayInsert(ordered, v);
01373     var = SATgetVariable(v);
01374     var->pos = (i>>1);
01375     i++;
01376   }
01377 
01378   size = one->num;
01379   for(i=0; i<size; i++) {
01380     v = one->space[i];
01381     var = SATgetVariable(v);
01382     var->pos = ordered->num;
01383     sat_ArrayInsert(ordered, v);
01384   }
01385 
01386   sat_ArrayFree(one);
01387   sat_ArrayFree(tmp);
01388 
01389   cm->orderedVariableArray = ordered;
01390   cm->currentVarPos = 0;
01391 
01392   for(i=1; i<cm->currentDecision; i++) {
01393     cm->decisionHead[i].currentVarPos = 0;
01394   }
01395 
01396   if(cm->option->verbose > 3)
01397     sat_PrintScore(cm);
01398 
01399 }
01400 
01401 
01414 void
01415 AS_PeriodicFunctions(satManager_t *cm)
01416 {
01417 satStatistics_t *stats;
01418 satOption_t *option;
01419 int nDecisions;
01420 int gap;
01421 
01422   stats = cm->each;
01423   option = cm->option;
01424   nDecisions = stats->nDecisions-stats->nShrinkDecisions;
01425   if(nDecisions && !(nDecisions % option->decisionAgeInterval)) {
01426     if(option->decisionAgeRestart) {
01427       gap = stats->nConflictCl-stats->nOldConflictCl;
01428       if(gap > option->decisionAgeInterval>>2)  {
01429 
01430         AS_UpdateScore(cm);
01431 
01432         AS_Backtrack(cm, cm->lowestBacktrackLevel);
01433         cm->currentTopConflict = cm->literals->last-1;
01434         cm->currentTopConflictCount = 0;
01435         cm->lowestBacktrackLevel = MAXINT;
01436       }
01437       stats->nOldConflictCl = stats->nConflictCl;
01438     }
01439     else {
01440 
01441       AS_UpdateScore(cm);
01442     }
01443 
01444   }
01445 
01446   if(stats->nBacktracks > option->nextClauseDeletion) {
01447     option->nextClauseDeletion += option->clauseDeletionInterval;
01448     sat_ClauseDeletion(cm);
01449   }
01450 }
01451 
01464 satLevel_t *
01465 AS_MakeDecision(satManager_t *cm)
01466 {
01467 satLevel_t *d;
01468 int value;
01469 long v;
01470 satStatistics_t *stats;
01471 
01472   d = SATgetDecision(cm->currentDecision);
01473 
01474   if(cm->queue->size)
01475     return(d);
01476 
01477   d = sat_AllocLevel(cm);
01478 
01479   v = 0;
01480   v = sat_DecisionBasedOnBDD(cm, d);
01481 
01482   if(v == 0)
01483     v = sat_DecisionBasedOnScore(cm, d);
01484 
01485   if(v == 0)
01486     return(0);
01487 
01488 
01489   sat_ASDec(cm,d,v);
01490 
01491   stats = cm->each;
01492 
01493   stats->nDecisions++;
01494 
01495   value = !(SATisInverted(v));
01496   v = SATnormalNode(v);
01497   d->decisionNode = v;
01498 
01499 
01500     if((SATgetVariable(v)->RecVal)!=0){
01501       if(SATgetVariable(v)->RecVal==-1)
01502         value=0;
01503       else{
01504 #if DBG
01505         assert(SATgetVariable(v)->RecVal==1);
01506 #endif
01507         value=1;
01508       }
01509 
01510     }
01511 
01512 
01513   SATvalue(v) = value;
01514 
01515   SATmakeImplied(v, d);
01516 #if DBG
01517 
01518   assert(SATflags(v)&CoiMask);
01519 #endif
01520 
01521   sat_Enqueue(cm->queue, v);
01522   SATflags(v) |= InQueueMask;
01523 
01524   return(d);
01525 }
01526 
01539 void
01540 AS_FindUIP(
01541         satManager_t *cm,
01542         satArray_t *clauseArray,
01543         satLevel_t *d,
01544         int *objectFlag,
01545         int *bLevel,
01546         long *fdaLit)
01547 {
01548 long conflicting;
01549 int nMarked, value;
01550 int first, i,j;
01551 long *space, v,left,right,inverted,result,*tmp;
01552 satArray_t *implied;
01553 satOption_t *option;
01554 RTnode_t  tmprt;
01555 int size = 0;
01556 long *lp, *tmpIP,ante,ante2,node;
01557  RTManager_t * rm = cm->rm;
01558 
01559   conflicting = d->conflict;
01560   nMarked = 0;
01561   option = cm->option;
01562   if(option->incTraceObjective) *objectFlag = 0;
01563   else                          *objectFlag = ObjMask;
01564 
01565   (*objectFlag) |= (SATflags(conflicting) & ObjMask);
01566 
01567   /* find seed from conflicting clause to find unique implication point.
01568    * */
01569   clauseArray->num = 0;
01570   sat_MarkNodeInConflict(
01571           cm, d, &nMarked, objectFlag, bLevel, clauseArray);
01572 
01573   /* Traverse implication graph backward */
01574   first = 1;
01575   implied = d->implied;
01576   space = implied->space+implied->num-1;
01577 
01578 
01579   if(cm->option->coreGeneration){
01580     /*if last conflict is CNF*/
01581     if(SATflags(conflicting)&IsCNFMask){
01582       /*is conflict CNF*/
01583       if(SATflags(conflicting)&IsConflictMask){
01584          if(!st_lookup(cm->RTreeTable,(char*)conflicting,&tmprt)){
01585             fprintf(vis_stderr,"RTree node of conflict cnf %ld is not in RTreeTable\n",ante);
01586             exit(0);
01587           }
01588           else{
01589             rm->root = tmprt;
01590           }
01591       }
01592       /*CNF but not conflict*/
01593       else{
01594         rm->root = RTCreateNode(rm);
01595         RT_oriClsIdx(rm->root) = conflicting;
01596         size = SATnumLits(conflicting);
01597         RT_fSize(rm->root) = size;
01598       }
01599     }
01600     /*if last conflict is AIG*/
01601     else{
01602       rm->root = RTCreateNode(rm);
01603       node = SATnormalNode(conflicting);
01604       left = SATleftChild(node);
01605       right = SATrightChild(node);
01606       result = PureSat_IdentifyConflict(cm,left,right,conflicting);
01607       inverted = SATisInverted(left);
01608       left = SATnormalNode(left);
01609       left = inverted ? SATnot(left) : left;
01610 
01611       inverted = SATisInverted(right);
01612       right = SATnormalNode(right);
01613       right = inverted ? SATnot(right) : right;
01614 
01615       j = node;
01616 
01617       if(result == 1){
01618         tmp = ALLOC(long,3);
01619         tmp[0] = left;
01620         tmp[1] = SATnot(j);
01621         size = 2;
01622       }
01623       else if(result == 2){
01624         tmp = ALLOC(long,3);
01625         tmp[0] = right;
01626         tmp[1] = SATnot(j);
01627         size = 2;
01628       }
01629       else if(result == 3){
01630         tmp = ALLOC(long,4);
01631         tmp[0] = SATnot(left);
01632         tmp[1] = SATnot(right);
01633         tmp[2] = j;
01634         size = 3;
01635       }
01636       else{
01637         fprintf(vis_stderr,"wrong returned result value, exit\n");
01638         exit(0);
01639       }
01640 
01641       lp = tmp;
01642       sat_BuildRT(cm,rm->root, lp, tmpIP,size,1);
01643       FREE(tmp);
01644     }
01645   }
01646 
01647 
01648   for(i=implied->num-1; i>=0; i--, space--) {
01649     v = *space;
01650     if(SATflags(v) & VisitedMask) {
01651       SATflags(v) &= ResetVisitedMask;
01652       --nMarked;
01653 
01654       if(nMarked == 0 && (!first || i==0))  {
01655         value = SATvalue(v);
01656         *fdaLit = v^(!value);
01657         sat_ArrayInsert(clauseArray, *fdaLit);
01658         break;
01659       }
01660 
01661 
01662 
01663       if(cm->option->coreGeneration){
01664         ante = SATante(v);
01665         if(ante==0){
01666           if(!(st_lookup(cm->anteTable, (char *)SATnormalNode(v),&ante))){
01667             fprintf(vis_stderr,"ante = 0 and is not in anteTable %ld\n",v);
01668             exit(0);
01669           }
01670         }
01671 
01672         /*build non-leaf node*/
01673         tmprt = RTCreateNode(rm);
01674         RT_pivot(tmprt) = SATnormalNode(v);
01675         RT_right(tmprt) = rm->root;
01676         rm->root = tmprt;
01677 
01678         if((SATflags(ante)&IsConflictMask)&&(SATflags(ante)&IsCNFMask)){
01679           if(!st_lookup(cm->RTreeTable,(char*)ante,&tmprt)){
01680             fprintf(vis_stderr,"RTree node of conflict cnf %ld is not in RTreeTable\n",ante);
01681             exit(0);
01682           }
01683           else{
01684             RT_left(rm->root) = tmprt;
01685           }
01686         }
01687         else{ /* if not conflict CNF*/
01688           /*left */
01689           tmprt = RTCreateNode(rm);
01690           RT_left(rm->root) = tmprt;
01691           /*left is AIG*/
01692           if(!(SATflags(ante) & IsCNFMask)){
01693             /*generate formula for left*/
01694             tmp = ALLOC(long,3);
01695             value = SATvalue(v);
01696             node = SATnormalNode(v);
01697             node = value==1?node:SATnot(node);
01698             tmp[0] = node;
01699 
01700             size = 1;
01701             if(ante != SATnormalNode(v)){
01702               value = SATvalue(ante);
01703               node = SATnormalNode(ante);
01704               node = value==1?SATnot(node):node;
01705               tmp[1] = node;
01706               size++;
01707               ante2 = SATante2(v);
01708               if(ante2){
01709                 value = SATvalue(ante2);
01710                 node = SATnormalNode(ante2);
01711                 node = value==1?SATnot(node):node;
01712                 tmp[2] = node;
01713                 size++;
01714               }
01715             }
01716             /*generate p1 and p2*/
01717             lp = tmp;
01718             sat_BuildRT(cm,tmprt,lp,tmpIP,size,1);
01719             FREE(tmp);
01720           }
01721           /* left is CNF*/
01722           else{
01723             RT_oriClsIdx(tmprt) = ante;
01724             //generate p1 and p2 for left
01725             lp = (long*)SATfirstLit(ante);
01726             size = SATnumLits(ante);
01727             RT_fSize(tmprt) = size;
01728           }
01729         } /*else{  if not conflict CNF*/
01730 
01731       }/*if(cm->option->coreGeneration){*/
01732 
01733 
01734       sat_MarkNodeInImpliedNode(
01735               cm, d, v, &nMarked, objectFlag, bLevel, clauseArray);
01736       /*Bing:Important for EffIP*/
01737       first = 0;
01738     }
01739     /* first = 0;*/
01740   }
01741 
01742 
01743   /* Minimize conflict clause */
01744   if(option->minimizeConflictClause)
01745     sat_MinimizeConflictClause(cm, clauseArray);
01746   else
01747     sat_ResetFlagForClauseArray(cm, clauseArray);
01748 
01749   return;
01750 }
01751 
01752 
01765 int
01766 AS_ConflictAnalysis(
01767         satManager_t *cm,
01768         satLevel_t *d)
01769 {
01770 satStatistics_t *stats;
01771 satOption_t *option;
01772 satArray_t *clauseArray;
01773 int objectFlag;
01774 int bLevel;
01775 long fdaLit, learned, conflicting;
01776 int inverted;
01777 RTManager_t * rm = cm->rm;
01778 
01779 
01780   conflicting = d->conflict;
01781 
01782   if(d->level == 0) {
01784     if(cm->option->coreGeneration)
01785       sat_ConflictAnalysisForCoreGen(cm, d);
01786     cm->currentDecision--;
01787     return (-1);
01788   }
01789 
01790   stats = cm->each;
01791   option = cm->option;
01792 
01793 
01794   stats->nBacktracks++;
01795 
01796   clauseArray = cm->auxArray;
01797 
01798   bLevel = 0;
01799   fdaLit = -1;
01800   clauseArray->num = 0;
01801 
01802   /*  Find Unique Implication Point */
01803   AS_FindUIP(cm, clauseArray, d, &objectFlag, &bLevel, &fdaLit);
01804   stats->nNonchonologicalBacktracks += (d->level - bLevel);
01805 
01806 
01807   if(clauseArray->num == 0) {
01808     sat_PrintImplicationGraph(cm, d);
01809     sat_PrintNode(cm, conflicting);
01810   }
01811 
01812   /* If we could find UIP then it is critical error...
01813   * at lease the decision node has to be detected as UIP.
01814   */
01815   if(fdaLit == -1) {
01816     fprintf(vis_stdout, "%s ERROR : Illegal unit literal\n", cm->comment);
01817     fflush(vis_stdout);
01818     sat_PrintNode(cm, conflicting);
01819     sat_PrintImplicationGraph(cm, d);
01820     sat_PrintDotForConflict(cm, d, conflicting, 0);
01821     exit(0);
01822   }
01823 
01824   if(bLevel && cm->lowestBacktrackLevel > bLevel)
01825     cm->lowestBacktrackLevel = bLevel;
01826 
01827 
01828   inverted = SATisInverted(fdaLit);
01829   fdaLit = SATnormalNode(fdaLit);
01830 
01831   if(option->verbose > 2) {
01832     if(option->verbose > 4)
01833       sat_PrintNode(cm, conflicting);
01834     fprintf(vis_stdout, "conflict at %3d on %6ld  bLevel %d UnitLit ",
01835             d->level, conflicting, bLevel);
01836     sat_PrintNodeAlone(cm, fdaLit);
01837     fprintf(vis_stdout, "\n");
01838   }
01839 
01840   d->conflict = 0;
01841 
01842   /* unit literal conflict clause */
01843   if(clauseArray->num == 1) {
01844     learned = sat_AddUnitConflictClause(cm, clauseArray, objectFlag);
01845 
01846     stats->nUnitConflictCl++;
01847     cm->currentTopConflict = cm->literals->last-1;
01848     cm->currentTopConflictCount = 0;
01849 
01850     AS_Backtrack(cm, 0);
01851 
01852     d = SATgetDecision(cm->currentDecision);
01853     cm->implicatedSoFar -= d->implied->num;
01854     SATante(fdaLit) = 0;
01855 
01856 
01857     if(!(SATflags(SATnormalNode(fdaLit))&AssignedMask)){
01858       satVariable_t *var = SATgetVariable(fdaLit);
01859       int dfs = var->scores[0]/SCOREUNIT;
01860 #if DBG
01861       assert(dfs==cm->LatchLevel);
01862 #endif
01863       SATflags(SATnormalNode(fdaLit))|=AssignedMask;
01864       cm->assignedArray[dfs]++;
01865       if(cm->assignedArray[dfs]==cm->numArray[dfs])
01866         sat_ASmergeLevel(cm);
01867     }
01868 
01869 
01870     SATvalue(fdaLit) = inverted;
01871     SATmakeImplied(fdaLit, d);
01872 
01873     if((SATflags(fdaLit) & InQueueMask)  == 0) {
01874       sat_Enqueue(cm->queue, fdaLit);
01875       SATflags(fdaLit) |= InQueueMask;
01876     }
01877 
01878     clauseArray->num = 0;
01879 
01880     if(option->incTraceObjective && objectFlag == 0)
01881       sat_ArrayInsert(cm->nonobjUnitLitArray, SATnot(fdaLit));
01882 
01883     if(option->incDistill && objectFlag)
01884       sat_ArrayInsert(cm->objUnitLitArray, SATnot(fdaLit));
01885 
01886     if(objectFlag)
01887       SATflags(fdaLit) |= ObjMask;
01888 
01889     /* Bing: UNSAT core generation */
01890     if(cm->option->coreGeneration){
01891 
01892       st_insert(cm->anteTable,(char*)SATnormalNode(fdaLit),(char*)learned);
01893       st_insert(cm->RTreeTable, (char *)learned, (char *)rm->root);
01894       RT_oriClsIdx(rm->root) = learned;
01895     }
01896 
01897     return(bLevel);
01898   }
01899 
01900   /* add conflict learned clause */
01901   learned = sat_AddConflictClause(cm, clauseArray, objectFlag);
01902 
01903   cm->currentTopConflict = cm->literals->last-1;
01904   cm->currentTopConflictCount = 0;
01905 
01906    /* Bing: UNSAT core generation */
01907   if(cm->option->coreGeneration){
01908    st_insert(cm->RTreeTable, (char *)learned, (char *)rm->root);
01909    RT_oriClsIdx(rm->root) = learned;
01910   }
01911 
01912   if(option->verbose > 2) {
01913     sat_PrintNode(cm, learned);
01914     fflush(vis_stdout);
01915   }
01916 
01917 
01918   /* apply Deepest Variable Hike decision heuristic */
01919   if(option->decisionHeuristic & DVH_DECISION)
01920    sat_UpdateDVH(cm, d, clauseArray, bLevel, fdaLit);
01921 
01922 
01923   /* Backtrack and failure driven assertion */
01924   AS_Backtrack(cm, bLevel);
01925 
01926   d = SATgetDecision(bLevel);
01927   cm->implicatedSoFar -= d->implied->num;
01928 
01929 
01930     if(!(SATflags(SATnormalNode(fdaLit))&AssignedMask)){
01931       satVariable_t *var = SATgetVariable(fdaLit);
01932       int dfs = var->scores[0]/SCOREUNIT;
01933 #if DBG
01934       assert(dfs==cm->LatchLevel);
01935 #endif
01936       SATflags(SATnormalNode(fdaLit))|=AssignedMask;
01937       cm->assignedArray[dfs]++;
01938       if(cm->assignedArray[dfs]==cm->numArray[dfs])
01939         sat_ASmergeLevel(cm);
01940     }
01941 
01942 
01943   SATante(fdaLit) = learned;
01944   SATvalue(fdaLit) = inverted;
01945   SATmakeImplied(fdaLit, d);
01946 
01947   if((SATflags(fdaLit) & InQueueMask)  == 0) {
01948     sat_Enqueue(cm->queue, fdaLit);
01949     SATflags(fdaLit) |= InQueueMask;
01950   }
01951 
01952   clauseArray->num = 0;
01953   if(objectFlag)
01954     SATflags(fdaLit) |= ObjMask;
01955 
01956   return(bLevel);
01957 }
01958 
01959 
01972 void
01973 AS_Solve(satManager_t *cm)
01974 {
01975 satLevel_t *d;
01976 satOption_t *option;
01977 satVariable_t *var;
01978 int level;
01979 
01980   d = SATgetDecision(0);
01981   cm->implicatedSoFar = d->implied->num;
01982   cm->currentTopConflict = 0;
01983 
01984   option = cm->option;
01985   if(option->BDDMonolithic) {
01986     sat_TryToBuildMonolithicBDD(cm);
01987   }
01988 
01989   if(cm->status == SAT_UNSAT) {
01990     AS_Undo(cm, SATgetDecision(0));
01991     return;
01992   }
01993 
01994   while(1) {
01995     AS_PeriodicFunctions(cm);
01996 
01997     if(cm->currentDecision == 0)
01998       sat_BuildLevelZeroHyperImplicationGraph(cm);
01999 
02000     d = AS_MakeDecision(cm);
02001 
02002     if(d == 0)  {
02003       cm->status = SAT_SAT;
02004       return;
02005     }
02006 
02007     while(1) {
02008       AS_ImplicationMain(cm, d);
02009 
02010       if(d->conflict == 0)      {
02011         if(cm->option->verbose > 2) {
02012           var = SATgetVariable(d->decisionNode);
02013           fprintf(vis_stdout, "decision at %3d on %6ld <- %ld score(%d %d) %d %ld implied %.3f %%\n",
02014             d->level, d->decisionNode, SATvalue(d->decisionNode),
02015             var->scores[0], var->scores[1],
02016             cm->each->nDecisions, d->implied->num,
02017             (double)cm->implicatedSoFar/(double)cm->initNumVariables * 100);
02018           fflush(vis_stdout);
02019         }
02020 
02021         break;
02022       }
02023         if(cm->option->verbose > 2) {
02024           var = SATgetVariable(d->decisionNode);
02025           fprintf(vis_stdout, "decision at %3d on %6ld <- %ld score(%d %d) %d %ld implied  %.3f %% Conflict\n",
02026             d->level, d->decisionNode, SATvalue(d->decisionNode),
02027             var->scores[0], var->scores[1],
02028             cm->each->nDecisions, d->implied->num,
02029             (double)cm->implicatedSoFar/(double)cm->initNumVariables * 100);
02030           fflush(vis_stdout);
02031         }
02032 
02033       level = AS_ConflictAnalysis(cm, d);
02034 
02035       if(cm->currentDecision == -1) {
02036         if(cm->option->incDistill) {
02037           sat_BuildTrieForDistill(cm);
02038         }
02039         AS_Undo(cm, SATgetDecision(0));
02040         cm->status = SAT_UNSAT;
02041         return;
02042       }
02043 
02044       d = SATgetDecision(cm->currentDecision);
02045     }
02046 
02047   }
02048 }
02049 
02050 
02063 void AS_Main(satManager_t *cm)
02064 {
02065 long btime, etime;
02066 
02067   btime = util_cpu_time();
02068   AS_PreProcessing(cm);
02069 
02070   if(cm->status == 0)
02071     AS_Solve(cm);
02072 
02074   if(cm->option->coreGeneration && cm->status == SAT_UNSAT){
02075 
02076     sat_GenerateCore_All(cm);
02077   }
02078 
02079   sat_PostProcessing(cm);
02080 
02081   etime = util_cpu_time();
02082   cm->each->satTime = (double)(etime - btime) / 1000.0 ;
02083 
02084 }
02085 
02086 
02100 void PureSatCreateOneLayer(Ntk_Network_t *network,
02101                            PureSat_Manager_t *pm,
02102                            satManager_t *cm,
02103                            array_t * latchArray,
02104                            int layer)
02105 {
02106   mAig_Manager_t * manager = Ntk_NetworkReadMAigManager(network);
02107   st_table * node2MvfAigTable =(st_table *) Ntk_NetworkReadApplInfo(network,
02108                                                          MVFAIG_NETWORK_APPL_KEY);
02109   MvfAig_Function_t *mvfAig;
02110   bAigTimeFrame_t * tf = manager->timeframeWOI;
02111   mAigMvar_t lmVar;
02112   mAigBvar_t bVar;
02113   array_t *bVarList,*mVarList;
02114   int i,j,k,lmAigId,index,index1;
02115   char * name;
02116   Ntk_Node_t * latch;
02117   bAigEdge_t node,v, *li, *bli;
02118   st_table *table = st_init_table(st_numcmp,st_numhash);
02119 
02120   bVarList = mAigReadBinVarList(manager);
02121   mVarList = mAigReadMulVarList(manager);
02122 
02123   arrayForEachItem(char*,latchArray,i,name){
02124 
02125     latch = Ntk_NetworkFindNodeByName(network,name);
02126     st_lookup(node2MvfAigTable,latch,&mvfAig);
02127     for(j=0;j<mvfAig->num;j++){
02128       int retVal;
02129       v = bAig_GetCanonical(manager,MvfAig_FunctionReadComponent(mvfAig,j));
02130       if(v==bAig_Zero||v==bAig_One)
02131         continue;
02132       retVal = st_lookup(tf->li2index,(char *)v,&index);
02133       assert(retVal);
02134       for(k=1;k<=pm->Length;k++){
02135         li = tf->latchInputs[k];
02136         if(li[index]==bAig_Zero||li[index]==bAig_One)
02137           continue;
02138         node = bAig_NonInvertedEdge(li[index]);
02139         st_insert(table,(char*)node,(char*)(long)layer);
02140 
02141       }
02142     }
02143 
02144     lmAigId = Ntk_NodeReadMAigId(latch);
02145     lmVar = array_fetch(mAigMvar_t, mVarList,lmAigId);
02146     for(j=0,index1=lmVar.bVars;j<lmVar.encodeLength;j++,index1++){
02147       int retVal;
02148       bVar = array_fetch(mAigBvar_t,bVarList,index1);
02149       if(bVar.node==bAig_Zero||bVar.node==bAig_One)
02150         continue;
02151       retVal = st_lookup(tf->bli2index,(char *)bVar.node,&index);
02152       assert(retVal);
02153       for(k=1;k<=pm->Length;k++){
02154         bli = tf->blatchInputs[k];
02155         if(bli[index]==bAig_Zero||bli[index]==bAig_One)
02156           continue;
02157         node = bAig_NonInvertedEdge(bli[index]);
02158         st_insert(table,(char*)node,(char*)(long)layer);
02159 
02160       }
02161     }
02162   }
02163 
02164   st_insert(cm->layerTable,table,(char*)(long)layer);
02165 
02166 }
02167 
02168 
02169 
02183 void PureSatCreateLayer(Ntk_Network_t *network,
02184                         PureSat_Manager_t *pm,
02185                         satManager_t *cm,
02186                         array_t *absModel,
02187                         array_t *sufArray)
02188 {
02189   int layer;
02190   char* name;
02191   int i,j,threshold;
02192   array_t *tmpArray;
02193 
02194   if(sufArray->num<4)
02195     layer = 1;
02196   else if(sufArray->num<6)
02197     layer = 2;
02198   else if(sufArray->num<10)
02199     layer = 3;
02200   else if(sufArray->num<20)
02201     layer = 5;
02202   else if(sufArray->num<50)
02203     layer = 8;
02204   else
02205     layer = 10;
02206 
02207   threshold = sufArray->num/layer;
02208   layer = array_n(sufArray)/threshold;
02209   layer = sufArray->num%threshold?layer+1:layer;
02210 
02211   cm->LatchLevel = layer;
02212   cm->OriLevel = layer;
02213   cm->layerTable = st_init_table(st_ptrcmp,st_ptrhash);
02214 
02215   layer = layer+1;
02216   for(i=0;i<array_n(sufArray);i=i+threshold){
02217     if(i==0)
02218       tmpArray = array_dup(absModel);
02219     else
02220       tmpArray = array_alloc(char*,0);
02221     layer = layer-1;
02222     for(j=0;j<threshold;j++){
02223       if(i+j>=array_n(sufArray))
02224         break;
02225       name = array_fetch(char*,sufArray,i+j);
02226       array_insert_last(char*,tmpArray,name);
02227 
02228     }
02229     PureSatCreateOneLayer(network,pm,cm,tmpArray,layer);
02230     array_free(tmpArray);
02231   }
02232   assert(layer==1);
02233   cm->assignedArray = ALLOC(int,cm->LatchLevel+1);
02234   cm->numArray = ALLOC(int,cm->LatchLevel+1);
02235 
02236 }