VIS

src/baig/baigAllSat.c

Go to the documentation of this file.
00001 
00019 #include "baig.h"
00020 #include "baigInt.h"
00021 #include "ntk.h"
00022 #include "bmc.h"
00023 #include "sat.h"
00024 
00025 static char rcsid[] UNUSED = "$Id: baigAllSat.c,v 1.4 2009/04/11 02:40:01 fabio Exp $";
00026 static satManager_t *SATcm;
00027 
00028 /*---------------------------------------------------------------------------*/
00029 /* Constant declarations                                                     */
00030 /*---------------------------------------------------------------------------*/
00031 
00034 /*---------------------------------------------------------------------------*/
00035 /* Static function prototypes                                                */
00036 /*---------------------------------------------------------------------------*/
00037 static int nodenameCompare( const void * node1, const void * node2);
00038 static int levelCompare( const void * node1, const void * node2);
00039 static int indexCompare( const void * node1, const void * node2);
00040 static int StringCheckIsInteger(char *string, int *value);
00041 
00045 /*---------------------------------------------------------------------------*/
00046 /* Definition of exported functions                                          */
00047 /*---------------------------------------------------------------------------*/
00048 
00049 
00061 bAigTransition_t *
00062 bAigCreateTransitionRelation(
00063         Ntk_Network_t *network,
00064         mAig_Manager_t *manager)
00065 {
00066 bAigTransition_t *t;
00067 array_t   *bVarList, *mVarList;
00068 array_t *latcharr;
00069 Ntk_Node_t *node, *dataInput;
00070 mAigMvar_t mVar;
00071 mAigBvar_t bVar;
00072 st_table *node2MvfAigTable;
00073 lsGen gen;
00074 int nLatches, nInputs;
00075 int i, j, index, index1;
00076 int mAigId, maxCS;
00077 bAigEdge_t v;
00078 bAigEdge_t *cstates, *nstates, *inputs;
00079 
00080   t = ALLOC(bAigTransition_t, 1);
00081   memset(t, 0, sizeof(bAigTransition_t));
00082   t->network = network;
00083   t->manager = manager;
00084 
00085   bVarList = mAigReadBinVarList(manager);
00086   mVarList = mAigReadMulVarList(manager);
00087 
00088   latcharr = array_alloc(Ntk_Node_t *, 16);
00089   nLatches = 0;
00090   Ntk_NetworkForEachLatch(network, gen, node) {
00091     mAigId = Ntk_NodeReadMAigId(node);
00092     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00093     nLatches += mVar.encodeLength;
00094     array_insert_last(Ntk_Node_t *, latcharr, node);
00095   }
00096   t->nLatches = nLatches;
00097 
00098   node2MvfAigTable = 
00099         (st_table *)Ntk_NetworkReadApplInfo(network, MVFAIG_NETWORK_APPL_KEY);
00100 
00101   array_sort(latcharr, nodenameCompare);
00102 
00103   cstates = ALLOC(bAigEdge_t, nLatches);
00104   nstates = ALLOC(bAigEdge_t, nLatches);
00105   t->tstates = ALLOC(bAigEdge_t, nLatches);
00106   t->initials = ALLOC(bAigEdge_t, nLatches);
00107 
00108   nLatches = 0;
00109   maxCS = 0;
00110   for(j=0; j<array_n(latcharr); j++) {
00111     node = array_fetch(Ntk_Node_t *, latcharr, j);
00112     mAigId = Ntk_NodeReadMAigId(node);
00113     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00114     index = nLatches;
00115     for(i=0, index1=mVar.bVars; i<mVar.encodeLength; i++) {
00116       bVar = array_fetch(mAigBvar_t, bVarList, index1++);
00117       v = bVar.node;
00118       cstates[index++] = v;
00119       if(maxCS < v)
00120         maxCS = v;
00121     }
00122 
00123     dataInput = Ntk_LatchReadDataInput(node);
00124     mAigId = Ntk_NodeReadMAigId(dataInput);
00125     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00126     index = nLatches;
00127     for(i=0, index1=mVar.bVars; i<mVar.encodeLength; i++) {
00128       bVar = array_fetch(mAigBvar_t, bVarList, index1++);
00129       v = bVar.node;
00130       v = bAig_GetCanonical(manager, v);
00131       nstates[index++] = v;
00132     }
00133 
00134     dataInput = Ntk_LatchReadInitialInput(node);
00135     mAigId = Ntk_NodeReadMAigId(dataInput);
00136     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00137     index = nLatches;
00138     for(i=0, index1=mVar.bVars; i<mVar.encodeLength; i++) {
00139       bVar = array_fetch(mAigBvar_t, bVarList, index1++);
00140       v = bVar.node;
00141       v = bAig_GetCanonical(manager, v);
00142       t->initials[index++] = v;
00143     }
00144     nLatches = index;
00145   }
00146 
00147   nInputs = 0;
00148   Ntk_NetworkForEachPrimaryInput(network, gen, node) {
00149     mAigId = Ntk_NodeReadMAigId(node);
00150     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00151     nInputs += mVar.encodeLength;
00152   }
00153   Ntk_NetworkForEachPseudoInput(network, gen, node) {
00154     mAigId = Ntk_NodeReadMAigId(node);
00155     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00156     nInputs += mVar.encodeLength;
00157   }
00158   t->nInputs = nInputs;
00159 
00160   inputs = ALLOC(bAigEdge_t, nInputs);
00161   nInputs = 0;
00162   Ntk_NetworkForEachPrimaryInput(network, gen, node) {
00163     mAigId = Ntk_NodeReadMAigId(node);
00164     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00165     for(i=0, index1=mVar.bVars; i<mVar.encodeLength; i++) {
00166       bVar = array_fetch(mAigBvar_t, bVarList, index1++);
00167       v = bVar.node;
00168       inputs[nInputs++] = v;
00169     }
00170   }
00171   Ntk_NetworkForEachPseudoInput(network, gen, node) {
00172     mAigId = Ntk_NodeReadMAigId(node);
00173     mVar = array_fetch(mAigMvar_t, mVarList, mAigId);
00174     for(i=0, index1=mVar.bVars; i<mVar.encodeLength; i++) {
00175       bVar = array_fetch(mAigBvar_t, bVarList, index1++);
00176       v = bVar.node;
00177       inputs[nInputs++] = v;
00178     }
00179   }
00180 
00181   t->cstates = cstates;
00182   t->nstates = nstates;
00183   t->inputs = inputs;
00184 
00185   maxCS /= bAigNodeSize;
00186   maxCS++;
00187   t->csize = maxCS;
00188   t->cobj = ALLOC(bAigEdge_t, maxCS);
00189   memset(t->cobj, 0, sizeof(bAigEdge_t)*maxCS);
00190   t->c2n = ALLOC(bAigEdge_t, maxCS);
00191   memset(t->c2n, 0, sizeof(bAigEdge_t)*maxCS);
00192   nLatches = t->nLatches;
00193   for(i=0; i<nLatches; i++) {
00194     v = t->cstates[i];
00195     t->c2n[SATnodeID(v)] = t->nstates[i];
00196   }
00197   memcpy(t->tstates, cstates, sizeof(bAigEdge_t)*t->nLatches);
00198 
00199   t->vinputs = ALLOC(int, t->nInputs);
00200   memset(t->vinputs, 0, sizeof(int)*t->nInputs);
00201   t->vtstates = ALLOC(int, t->nLatches);
00202   memset(t->vtstates, 0, sizeof(int)*t->nLatches);
00203   
00204   t->avgLits = 0;
00205   t->sum = 0;
00206   t->interval = 10;
00207   t->period = 100;
00208   array_free(latcharr);
00209 
00210   return(t);
00211 }
00212 
00213 
00225 bAigEdge_t
00226 bAig_CreatebAigForInvariant(
00227         Ntk_Network_t *network,
00228         bAig_Manager_t *manager,
00229         Ctlsp_Formula_t *inv)
00230 {
00231 bAigEdge_t result, left, right;
00232 st_table *nodeToMvfAigTable;
00233 int nodeValue;
00234 int check;    
00235 char *nodeNameString;
00236 char *nodeValueString;
00237 Ntk_Node_t *node;
00238 Var_Variable_t *nodeVar;
00239 MvfAig_Function_t  *tmpMvfAig;
00240 
00241 
00242   if (inv == NIL(Ctlsp_Formula_t))      return mAig_NULL; 
00243   if (inv->type == Ctlsp_TRUE_c)        return mAig_One; 
00244   if (inv->type == Ctlsp_FALSE_c)       return mAig_Zero;
00245 
00246   assert(Ctlsp_isPropositionalFormula(inv));
00247 
00248   if (inv->type == Ctlsp_ID_c){
00249     nodeNameString  = Ctlsp_FormulaReadVariableName(inv);
00250     nodeValueString = Ctlsp_FormulaReadValueName(inv);
00251     node = Ntk_NetworkFindNodeByName(network, nodeNameString);
00252       
00253     if (node == NIL(Ntk_Node_t)) {
00254       fprintf(vis_stderr, "sat_inv error: Could not find node corresponding to the name\t %s\n", nodeNameString);
00255       return mAig_NULL;
00256     }
00257 
00258     nodeToMvfAigTable = (st_table *) Ntk_NetworkReadApplInfo(network, MVFAIG_NETWORK_APPL_KEY);
00259     if (nodeToMvfAigTable == NIL(st_table)){
00260       fprintf(vis_stderr, 
00261               "sat_inv error: please run build_partiton_maigs first");
00262       return mAig_NULL;
00263     }
00264     tmpMvfAig = Bmc_ReadMvfAig(node, nodeToMvfAigTable);
00265     if (tmpMvfAig ==  NIL(MvfAig_Function_t)){
00266       tmpMvfAig = Bmc_NodeBuildMVF(network, node);
00267       array_free(tmpMvfAig);
00268       tmpMvfAig = Bmc_ReadMvfAig(node, nodeToMvfAigTable);
00269     }
00270       
00271     nodeVar = Ntk_NodeReadVariable(node);
00272     if (Var_VariableTestIsSymbolic(nodeVar)) {
00273       nodeValue = Var_VariableReadIndexFromSymbolicValue(nodeVar, nodeValueString);
00274       if ( nodeValue == -1 ) {
00275         fprintf(vis_stderr, 
00276                 "Value specified in RHS is not in domain of variable\n");
00277         fprintf(vis_stderr,"%s = %s\n", nodeNameString, nodeValueString);
00278         return mAig_NULL;
00279       }
00280     }
00281     else { 
00282       check = StringCheckIsInteger(nodeValueString, &nodeValue);
00283       if( check == 0 ) {
00284         fprintf(vis_stderr,"Illegal value in the RHS\n");
00285         fprintf(vis_stderr,"%s = %s\n", nodeNameString, nodeValueString);
00286         return mAig_NULL;
00287       }
00288       if( check == 1 ) {
00289         fprintf(vis_stderr,"Value in the RHS is out of range of int\n");
00290         fprintf(vis_stderr,"%s = %s", nodeNameString, nodeValueString);
00291         return mAig_NULL;
00292       }
00293       if ( !(Var_VariableTestIsValueInRange(nodeVar, nodeValue))) {
00294         fprintf(vis_stderr,"Value specified in RHS is not in domain of variable\n");
00295         fprintf(vis_stderr,"%s = %s\n", nodeNameString, nodeValueString);
00296         return mAig_NULL;
00297       }
00298     }
00299     result = bAig_GetCanonical(manager,
00300               MvfAig_FunctionReadComponent(tmpMvfAig, nodeValue));
00301     return result;
00302   }
00303 
00304   left = bAig_CreatebAigForInvariant(network, manager, inv->left);
00305   if (left == mAig_NULL){
00306     return mAig_NULL;
00307   }  
00308   right = bAig_CreatebAigForInvariant(network, manager, inv->right);
00309 
00310   if (right == mAig_NULL && inv->type ==Ctlsp_NOT_c ){
00311     return mAig_Not(left);
00312   }  
00313   else if(right == mAig_NULL) {
00314     return mAig_NULL;
00315   }
00316 
00317   switch(inv->type) {
00318     case Ctlsp_OR_c:
00319       result = mAig_Or(manager, left, right);
00320       break;
00321     case Ctlsp_AND_c:
00322       result = mAig_And(manager, left, right);
00323       break;
00324     case Ctlsp_THEN_c:
00325       result = mAig_Then(manager, left, right); 
00326       break;
00327     case Ctlsp_EQ_c:
00328       result = mAig_Eq(manager, left, right);
00329       break;
00330     case Ctlsp_XOR_c:
00331       result = mAig_Xor(manager, left, right);
00332       break;
00333     default:
00334       fail("Unexpected LTL type");
00335   }
00336   return result;
00337 
00338 }
00339 
00351 int
00352 bAigCheckInvariantWithAG(
00353         bAigTransition_t *t,
00354         bAigEdge_t objective)
00355 {
00356 int included, returnFlag;
00357 bAig_Manager_t *manager;
00358 satManager_t *cm;
00359 long frontierNodesBegin;
00360 
00361   manager = t->manager;
00362   bAigCleanUpDataFromPreviousExecution(t);
00363 
00365   objective = bAig_Not(objective);
00366   t->objective = objective;
00367   returnFlag = 0;
00368 
00369   while(1) {
00376     if(t->verbose > 1) 
00377       fprintf(vis_stdout, "** SAT_INV : %d'th pre-image is being computed\n", t->iteration+1);
00378 
00379     frontierNodesBegin = manager->nodesArraySize;
00380     if(t->iteration > 0) {
00381       bAigBuildObjectiveFromFrontierSet(t);
00382       if(t->frontier->num <= 1) {
00383         returnFlag = 1; 
00384         break;
00385       }
00386     }
00387     
00388     cm = bAigCirCUsInterfaceForAX(t);
00389     cm->frontierNodesBegin = frontierNodesBegin;
00390 
00391     t->allsat = cm;
00392   
00393 
00394     sat_CleanDatabase(cm);
00395 
00396     bAigMarkConeOfInfluenceForAX(t, cm);
00397 
00398     fflush(vis_stdout);
00399 
00400     bAig_ComputeAX(t);
00401 
00402     if(t->verbose > 1) {
00403       sat_ReportStatistics(cm, cm->each);
00404     }
00405 
00406     included = bAigInclusionTestOnInitialStates(t);
00407     if(included == 0) {
00408       returnFlag = 2; 
00409     }
00410 
00412     if(t->frontier->num <= 1) {
00413       returnFlag = 1; 
00414     }
00415 
00416 
00417     bAig_PostProcessForAX(t, cm);
00418     t->manager->nodesArraySize = frontierNodesBegin;
00419     t->allsat = 0;
00420 
00421     if(t->lifting) {
00422       sat_FreeManager(t->lifting);
00423       t->lifting = 0;
00424     }
00425 
00426     if(t->originalFrontier) {
00427       sat_ArrayFree(t->originalFrontier);
00428       t->originalFrontier = 0;
00429     }
00430 
00431     if(returnFlag) {
00432       break;
00433     }
00434     t->iteration++;
00435   } 
00436 
00437   if(t->verbose > 0) {
00438     fprintf(vis_stdout, "** SAT_INV : Total %d pre-image is computed\n", t->iteration+1);
00439   }
00440 
00441   sat_ArrayFree(t->frontier);
00442   t->frontier = 0;
00443   sat_ArrayFree(t->reached);
00444   t->reached = 0;
00445 
00446   fflush(vis_stdout);
00447 
00448   return(returnFlag);
00449 }
00450 
00462 void
00463 bAigReduceBlockingClauseWithUnsatCore(bAigTransition_t *t)
00464 {
00465 satArray_t *cnfArray;
00466 satArray_t *coreArray;
00467 satArray_t *rootArray;
00468 satOption_t *option;
00469 satManager_t *cm;
00470 st_table *mappedTable;
00471 char filename[1024];
00472 int flag, i, size;
00473 bAigEdge_t v, nv, *plit;
00474 
00475   cnfArray = sat_ArrayAlloc(1024);
00476   mappedTable = st_init_table(st_numcmp, st_numhash);
00477 
00478   sat_ArrayInsert(cnfArray, 0);
00479   cm = t->allsat;
00481   for(v = cm->frontierNodesBegin; v<cm->frontierNodesEnd; v+= satNodeSize) {
00482     if(!(SATflags(v) & IsCNFMask))      continue;
00483     size = SATnumLits(v);
00484     plit = (long*)SATfirstLit(v);
00485     for(i=0; i<size; i++, plit++) {
00486       nv = SATgetNode(*plit);
00487       sat_ArrayInsert(cnfArray, nv);
00488       nv = SATnormalNode(nv);
00489       SATflags(nv) |= VisitedMask;
00490     }
00491     sat_ArrayInsert(cnfArray, 0);
00492   }
00493 
00495   rootArray = sat_ArrayAlloc(t->nLatches);
00496   for(i=0; i<t->nLatches; i++) {
00497     v = t->nstates[i];
00498     v = SATnormalNode(v);
00499     if(v>1 && (SATflags(v) & VisitedMask))
00500       sat_ArrayInsert(rootArray, v);
00501   }
00502   for(i=0; i<rootArray->num; i++) {
00503     v = rootArray->space[i];
00504     SATflags(v) &= ResetVisitedMask;
00505   }
00506 
00507   if(cm->maxNodesArraySize > t->manager->maxNodesArraySize) {
00508     t->manager->maxNodesArraySize = cm->maxNodesArraySize;
00509     t->manager->nameList   = REALLOC(char *, t->manager->nameList  , t->manager->maxNodesArraySize/bAigNodeSize);
00510     t->manager->bddIdArray = REALLOC(int ,   t->manager->bddIdArray  , t->manager->maxNodesArraySize/bAigNodeSize);
00511     t->manager->bddArray   = REALLOC(bdd_t *, t->manager->bddArray  , t->manager->maxNodesArraySize/bAigNodeSize);
00512   }
00513   t->manager->maxNodesArraySize = cm->maxNodesArraySize;
00514   t->manager->NodesArray = cm->nodesArray;
00515   bAig_CreateCNFFromAIG(t->manager, rootArray, cnfArray);
00516   sat_ArrayFree(rootArray);
00517 
00519   for(v = cm->frontierNodesEnd; v<cm->nodesArraySize; v+= satNodeSize) {
00520     if(!(SATflags(v) & IsCNFMask))      continue;
00521     if(!(SATflags(v) & IsBlockingMask)) continue;
00522     size = SATnumLits(v);
00523     plit = (long*)SATfirstLit(v);
00524     for(i=0; i<size; i++, plit++) {
00525       nv = SATgetNode(*plit);
00526       sat_ArrayInsert(cnfArray, nv);
00527     }
00528     sat_ArrayInsert(cnfArray, -v);
00529   }
00530 
00532   sat_ArrayInsert(cnfArray, t->objective);
00533   sat_ArrayInsert(cnfArray, 0);
00534 
00535   option = sat_InitOption();
00536 
00537   if(t->verbose > 2) {
00538     sprintf(filename, "core%d.cnf", t->iteration);
00539     sat_WriteCNFFromArray(cnfArray, filename);
00540   }
00541   coreArray = sat_ArrayAlloc(1024);
00542   flag = sat_CNFMainWithArray(option, cnfArray, 1, coreArray, mappedTable);
00543 
00544   if(flag == SAT_SAT)   {
00545     sprintf(filename, "core%d.cnf", t->iteration);
00546     fprintf(stdout, "ERROR : this instance %s should be UNSAT\n", filename);
00547     sat_WriteCNFFromArray(cnfArray, filename);
00548   }
00549 
00550   for(i=0; i<coreArray->num; i++) {
00551     v = coreArray->space[i];
00552     if(st_lookup(mappedTable, (char *)v, &nv)) {
00553       /* If a clause is blocking clause then it is in the table **/
00554       SATflags(nv) |= InCoreMask;    
00555     }
00561   }
00562   for(v=satNodeSize; v<cm->nodesArraySize; v+=satNodeSize) {
00563     if(!(SATflags(v) & IsCNFMask))
00564       continue;
00565     if(!(SATflags(v) & IsBlockingMask))
00566       continue;
00567     if(!(SATflags(v) & IsFrontierMask))
00568       continue;
00569     if((SATflags(v) & InCoreMask))
00570       continue;
00571 
00572     SATresetInUse(v);
00573     if(t->verbose > 4) {
00574       fprintf(vis_stdout, "NOTICE : deleted blocking clause\n");
00575       sat_PrintNode(cm, v);
00576     }
00577   }
00578 
00579 
00580   st_free_table(mappedTable);
00581   sat_ArrayFree(cnfArray);
00582   sat_ArrayFree(coreArray);
00583 }
00584 
00597 int
00598 bAigInclusionTestOnInitialStates(bAigTransition_t *t)
00599 {
00600 satArray_t *cnfArray;
00601 satOption_t *option;
00602 char filename[1024];
00603 int flag;
00604 
00605   if(t->inclusionInitial == 0)  return(1);
00606 
00607   cnfArray = bAigCreateCNFInstanceForInclusionTestOnInitialStates(t);
00608   option = sat_InitOption();
00609 
00610   if(t->verbose > 2) {
00611     sprintf(filename, "init%d.cnf", t->iteration);
00612     sat_WriteCNFFromArray(cnfArray, filename);
00613   }
00614   flag = sat_CNFMainWithArray(option, cnfArray, 0, 0, 0);
00615   sat_ArrayFree(cnfArray);
00616 
00617   if(flag == SAT_UNSAT) return(1);
00618   else                  return(0);
00619 
00620   return(1);
00621 }
00622 
00634 satArray_t *
00635 bAigCreateCNFInstanceForInclusionTestOnInitialStates(bAigTransition_t *t)
00636 {
00637 satArray_t *cnfArray, *frontier;
00638 satArray_t *andArr, *orArr, *fandArr, *clause;
00639 satArray_t *rootArray;
00640 bAigEdge_t v, cv, out, objective;
00641 bAigEdge_t maxIndex;
00642 long *space;
00643 int i, j;
00644 int nCls;
00645 
00646   maxIndex = 0;
00647   for(i=0; i<t->nLatches; i++) {
00648     v = t->cstates[i];
00649     if(maxIndex < v)    maxIndex = v;
00650   }
00651   maxIndex += bAigNodeSize;
00652  
00653   cnfArray = sat_ArrayAlloc(2048);
00654   andArr = sat_ArrayAlloc(1024);
00655   orArr = sat_ArrayAlloc(1024);
00656   fandArr = sat_ArrayAlloc(1024);
00657   clause = sat_ArrayAlloc(1024);
00658  
00659   nCls = 0;
00660   frontier = t->frontier; 
00661   sat_ArrayInsert(cnfArray, 0);
00662   for(i=0, space=frontier->space; i<frontier->num; i++, space++) {
00663     if(*space <= 0) { 
00664       space++;
00665       i++;
00666 
00667       if(i >= frontier->num) {
00668         break;
00669       }
00670 
00671       fandArr->num = 0;
00672       andArr->num = 0;
00673       while(*space > 0) {
00674         sat_ArrayInsert(fandArr, *space);
00675         i++;
00676         space++;
00677       }
00678       i--;
00679       space--;
00680 
00681 
00682       for(j=0; j<fandArr->num; j++) {
00683         v = fandArr->space[j];
00684         sat_ArrayInsert(andArr, v);
00685       }
00686 
00687       out = maxIndex;
00688       maxIndex += bAigNodeSize;
00689       sat_ArrayInsert(orArr, out);
00690 
00691       for(j=0; j<andArr->num; j++) {
00692         v = andArr->space[j];
00693         sat_ArrayInsert(cnfArray, SATnot(v));
00694         sat_ArrayInsert(cnfArray, SATnot(out));
00695         sat_ArrayInsert(cnfArray, -1); 
00696         nCls++;
00697       }
00698 
00699       for(j=0; j<andArr->num; j++) {
00700         v = andArr->space[j];
00701         sat_ArrayInsert(cnfArray, v);
00702       }
00703       sat_ArrayInsert(cnfArray, out);
00704       sat_ArrayInsert(cnfArray, -1);
00705       nCls++;
00706     }
00707   }
00708 
00709   objective = maxIndex;
00710   maxIndex += bAigNodeSize;
00711   for(i=0; i<orArr->num; i++) {
00712     v = orArr->space[i];
00713     sat_ArrayInsert(cnfArray, SATnot(v));
00714     sat_ArrayInsert(cnfArray, objective);
00715     sat_ArrayInsert(cnfArray, -1);
00716     nCls++;
00717   }
00718   for(i=0; i<orArr->num; i++) {
00719     v = orArr->space[i];
00720     sat_ArrayInsert(cnfArray, v);
00721   }
00722 
00723   sat_ArrayInsert(cnfArray, SATnot(objective));
00724 
00725   sat_ArrayInsert(cnfArray, -1);
00726   nCls++;
00727 
00729   sat_ArrayInsert(cnfArray, objective);
00730   sat_ArrayInsert(cnfArray, -1);
00731   nCls++;
00732 
00733   sat_ArrayFree(andArr);
00734   sat_ArrayFree(orArr);
00735   sat_ArrayFree(fandArr);
00736   sat_ArrayFree(clause);
00737 
00738 
00740   rootArray = sat_ArrayAlloc(t->nLatches);
00741   for(i=0; i<t->nLatches; i++) {
00742     v = t->initials[i];
00743     if(v>1)
00744       sat_ArrayInsert(rootArray, v);
00745   }
00746 
00747   if(t->allsat->maxNodesArraySize > t->manager->maxNodesArraySize) {
00748     t->manager->maxNodesArraySize = t->allsat->maxNodesArraySize;
00749     t->manager->nameList   = REALLOC(char *, t->manager->nameList  , t->manager->maxNodesArraySize/bAigNodeSize);
00750     t->manager->bddIdArray = REALLOC(int ,   t->manager->bddIdArray  , t->manager->maxNodesArraySize/bAigNodeSize);
00751     t->manager->bddArray   = REALLOC(bdd_t *, t->manager->bddArray  , t->manager->maxNodesArraySize/bAigNodeSize);
00752   }
00753   t->manager->maxNodesArraySize = t->allsat->maxNodesArraySize;
00754   t->manager->NodesArray = t->allsat->nodesArray;
00755   bAig_CreateCNFFromAIG(t->manager, rootArray, cnfArray);
00756   sat_ArrayFree(rootArray);
00757 
00762   for(i=0; i<t->nLatches; i++) {
00763     v = t->initials[i];
00764     cv = t->cstates[i];
00765     if(v == 0) {
00766       sat_ArrayInsert(cnfArray, SATnot(cv));
00767       sat_ArrayInsert(cnfArray, -1);
00768     }
00769     else if(v == 1) {
00770       sat_ArrayInsert(cnfArray, cv);
00771       sat_ArrayInsert(cnfArray, -1);
00772     }
00773     else {
00774       sat_ArrayInsert(cnfArray, SATnot(v));
00775       sat_ArrayInsert(cnfArray, (cv));
00776       sat_ArrayInsert(cnfArray, -1);
00777       sat_ArrayInsert(cnfArray, (v));
00778       sat_ArrayInsert(cnfArray, SATnot(cv));
00779       sat_ArrayInsert(cnfArray, -1);
00780     }
00781   }
00782 
00783   return(cnfArray);
00784 }
00785 
00797 void
00798 bAig_CreateCNFFromAIG(
00799         bAig_Manager_t *manager,
00800         satArray_t *rootArray,
00801         satArray_t *cnfArray)
00802 {
00803 int i;
00804 bAigEdge_t v, left, right;
00805 
00806   for(i=0; i<rootArray->num; i++) {
00807     v = rootArray->space[i];
00808     bAig_SetMaskTransitiveFanin(manager, v, VisitedMask);
00809   }
00810 
00811   for(v=bAigFirstNodeIndex ; v<manager->nodesArraySize; v+=bAigNodeSize){
00812     if(flags(v) & IsCNFMask) 
00813       continue;
00814     if(flags(v) & VisitedMask) {
00816       left = leftChild(v);
00817       if(left == 2)
00818         continue;
00819       right = rightChild(v);
00820 
00821       sat_ArrayInsert(cnfArray, SATnot(left));
00822       sat_ArrayInsert(cnfArray, SATnot(right));
00823       sat_ArrayInsert(cnfArray, (v));
00824       sat_ArrayInsert(cnfArray, -1);
00825       sat_ArrayInsert(cnfArray, (left));
00826       sat_ArrayInsert(cnfArray, SATnot(v));
00827       sat_ArrayInsert(cnfArray, -1);
00828       sat_ArrayInsert(cnfArray, (right));
00829       sat_ArrayInsert(cnfArray, SATnot(v));
00830       sat_ArrayInsert(cnfArray, -1);
00831     }
00832   }
00833 
00834   for(i=0; i<rootArray->num; i++) {
00835     v = rootArray->space[i];
00836     bAig_ResetMaskTransitiveFanin(manager, v, VisitedMask, ResetVisitedMask);
00837   }
00838   return;
00839 }
00840                       
00841 
00842 
00854 void
00855 bAig_ComputeAX(bAigTransition_t *t)
00856 {
00857 satManager_t *cm;
00858 long btime, etime;
00859 
00860   btime = util_cpu_time();
00861 
00862   cm = t->allsat;
00863   sat_PreProcessingForMixed(cm);
00864 
00865   if(cm->status == 0) {
00866     if(t->constrain)
00867       bAigCreateSatManagerForLifting(t);
00868     else 
00869       bAigCreateSatManagerForLiftingUnconstrained(t);
00872     bAigSolveAllSatWithLifting(t);
00873     if(t->iteration > 0 && t->reductionUsingUnsat)
00874       bAigReduceBlockingClauseWithUnsatCore(t);
00875   }
00876 
00877   sat_PostProcessing(cm);
00878 
00880   t->reached = cm->reached;
00881   t->frontier = cm->frontier;
00882   cm->reached = 0;
00883   cm->frontier = 0;
00884 
00885   etime = util_cpu_time();
00886   cm->each->satTime = (double)(etime - btime) / 1000.0 ;
00887   fflush(vis_stdout);
00888   return;
00889   
00890 }
00891 
00892 
00904 void
00905 bAigSolveAllSatWithLifting(bAigTransition_t *t)
00906 {
00907 satManager_t *cm;
00908 satLevel_t *d;
00909 satOption_t *option;
00910 int level;
00911 
00912   cm = t->allsat;
00913 
00914   d = SATgetDecision(0);
00915   cm->implicatedSoFar = d->implied->num;
00916   cm->currentTopConflict = 0;
00917 
00918   option = cm->option;
00919 
00925   if(cm->status == SAT_UNSAT) {
00926     sat_Undo(cm, SATgetDecision(0));
00927     return;
00928   }
00929 
00930   while(1) {
00931     sat_PeriodicFunctions(cm);
00932 
00933     d = sat_MakeDecision(cm);
00934 
00935     if(d == 0)  {
00936       bAigBlockingClauseAnalysisBasedOnLifting(t, cm);
00937 
00938       if(cm->currentDecision == -1) {
00939         sat_Undo(cm, SATgetDecision(0));
00940         cm->status = SAT_UNSAT;
00941         return;
00942       }
00943       d = SATgetDecision(cm->currentDecision);
00944     }
00945 
00946     while(1) {
00947       sat_ImplicationMain(cm, d);
00948 
00949       if(d->conflict == 0)      
00950         break;
00951 
00952       level = sat_ConflictAnalysis(cm, d);
00953 
00954       if(cm->currentDecision == -1) {
00955         sat_Undo(cm, SATgetDecision(0));
00956         cm->status = SAT_UNSAT;
00957         return;
00958       }
00959 
00960       d = SATgetDecision(cm->currentDecision);
00961     }
00962   }
00963 
00964   return;
00965 }
00966 
00978 void
00979 bAigBlockingClauseAnalysisBasedOnLifting(bAigTransition_t *t, satManager_t *allsat)
00980 {
00981 satManager_t *cm;
00982 satLevel_t *d;
00983 satArray_t *clauseArray;
00984 bAigEdge_t v, obj, blocked, fdaLit;
00985 int objInverted, inverted;
00986 int i, satisfied;
00987 int value, tvalue;
00988 int mLevel, bLevel;
00989 
00990   cm = allsat;
00991   for(i=0; i<t->nInputs; i++) {
00992     v = t->inputs[i];
00993     t->vinputs[i] = SATvalue(v);
00994   }
00995 
00996   SATcm = allsat;
00997   qsort(t->tstates, t->nLatches, sizeof(bAigEdge_t), levelCompare);
00998 
00999   for(i=0; i<t->nLatches; i++) {
01000     v = t->tstates[i];
01001     t->vtstates[i] = SATvalue(v);
01002   }
01003 
01005   cm = t->lifting;
01006 
01007   obj = (bAigEdge_t )cm->obj->space[0];
01008   objInverted = SATisInverted(obj);
01009   obj = SATnormalNode(obj);
01010 
01011   d = sat_AllocLevel(cm);
01012   satisfied = 0;
01013 
01014   clauseArray = sat_ArrayAlloc(256);
01015   clauseArray->num = 0;
01016 
01021   for(i=0; i<t->nInputs; i++) {
01022     if(satisfied)       break;
01023     v = t->inputs[i];
01024     if(!(SATflags(v) & CoiMask))        continue;
01025 
01026     value = t->vinputs[i];
01027     if(value > 1)       continue;
01028     tvalue = SATvalue(v);
01029     if(tvalue < 2)      continue;
01030 
01031     SATvalue(v) = value;
01032     SATmakeImplied(v, d);
01033     sat_Enqueue(cm->queue, v);
01034     SATflags(v) |= InQueueMask;
01035 
01036     sat_ImplicationMain(cm, d);
01037 
01038     value = SATvalue(obj);
01039     if(value < 2)       satisfied = 1;  
01040   }
01041 
01046   if(satisfied == 0) {
01047     d = sat_AllocLevel(cm);
01048     for(i=0; i<t->nLatches; i++) {
01049       if(satisfied)     break;
01050       v = t->tstates[i];
01051       if(!(SATflags(v) & CoiMask))      continue;
01052 
01053       value = t->vtstates[i];
01054       if(value > 1)     continue; 
01055       tvalue = SATvalue(v);
01056   
01061       sat_ArrayInsert(clauseArray, v^(!value));
01062   
01063       if(tvalue > 1) {
01064         SATvalue(v) = value;
01065         SATmakeImplied(v, d);
01066         sat_Enqueue(cm->queue, v);
01067         SATflags(v) |= InQueueMask;
01068     
01069         sat_ImplicationMain(cm, d);
01070         value = SATvalue(obj);
01071         if(value < 2)   satisfied = 1;  
01072       }
01073     }
01074   }
01075 
01076   value = SATvalue(obj);
01077   if(value > 1) {
01078     fprintf(stdout, "ERROR : Can't justify objective %ld\n", t->objective);
01079     exit(0);
01080   }
01081 
01082   if(clauseArray->num == 0) { 
01084     sat_Backtrack(cm, 0);
01085     cm->currentDecision--;
01086 
01087     sat_Backtrack(allsat, 0);
01088     allsat->currentDecision--;
01089     return;
01090   }
01091 
01092 
01093   bAigCollectAntecdentOfObjective(t, cm, t->objective, clauseArray);
01094 
01095   if(clauseArray->num == 0) { 
01097     fprintf(stdout, "ERROR : This might be the bug after greedy heuristic\n");
01098     sat_Backtrack(cm, 0);
01099     cm->currentDecision--;
01100 
01101     return;
01102   }
01103 
01104   sat_Backtrack(cm, 0);
01105   d = SATgetDecision(cm->currentDecision);
01106   sat_Undo(cm, d);
01107   cm->currentDecision--;
01108 
01112   if(t->disableLifting == 0)
01113     bAigMinimizationBasedOnLiftingAllAtOnce(t, t->objective, clauseArray);
01114 
01115   if(clauseArray->num == 0) { 
01121     if(cm->currentDecision >= 0) {
01122       sat_Backtrack(cm, 0);
01123       cm->currentDecision--;
01124     }
01125 
01126     sat_Backtrack(allsat, 0);
01127     allsat->currentDecision--;
01128 
01129     return;
01130   }
01131 
01133   cm = t->allsat;
01134 
01135   mLevel = 0;
01136   for(i=0; i<clauseArray->num; i++) {
01137     v = clauseArray->space[i];
01138     v = SATnormalNode(v);
01139     if(mLevel < SATlevel(v))
01140       mLevel = SATlevel(v);
01141   }
01142 
01143   blocked = sat_AddBlockingClause(cm, clauseArray);
01144   SATflags(blocked) |= IsFrontierMask;
01145 
01146   if(t->verbose > 3)
01147     sat_PrintNode(cm, blocked);
01148 
01149   sat_Backtrack(cm, mLevel);
01150   d = SATgetDecision(cm->currentDecision);
01151 
01152   if(t->verbose > 3) {
01153     qsort(clauseArray->space, clauseArray->num, sizeof(long), indexCompare);
01154     for(i=0; i<clauseArray->num; i++) {
01155       fprintf(stdout, "%ld ", clauseArray->space[i]);
01156     }
01157     fprintf(stdout, "\n");
01158   }
01159 
01160   if(bAigCheckExistenceOfUIP(cm, clauseArray, mLevel, &fdaLit, &bLevel)) {
01161     sat_Backtrack(cm, bLevel);
01162 
01163     if(SATlevel(fdaLit) == 0) {
01164       sat_Backtrack(cm, 0);
01165       cm->currentDecision = -1;
01166       sat_ArrayFree(clauseArray);
01167       return;
01168     }
01169 
01170     d = SATgetDecision(cm->currentDecision);
01171     inverted = SATisInverted(fdaLit);
01172     fdaLit = SATnormalNode(fdaLit);
01173     SATante(fdaLit) = blocked;
01174     SATvalue(fdaLit) = inverted;
01175     SATmakeImplied(fdaLit, d);
01176     
01177     if((SATflags(fdaLit) & InQueueMask)  == 0) {
01178       sat_Enqueue(cm->queue, fdaLit);
01179       SATflags(fdaLit) |= InQueueMask;
01180     }
01181   }
01182   else {
01183     d->conflict = blocked;
01184     sat_ConflictAnalysisWithBlockingClause(cm, d);
01185   }
01186 
01187   sat_ArrayFree(clauseArray);
01188   return;
01189 }
01190 
01202 int
01203 bAigCheckExistenceOfUIP(
01204         satManager_t *cm, 
01205         satArray_t *clauseArray, 
01206         int mLevel, 
01207         bAigEdge_t *fdaLit, 
01208         int *bLevel)
01209 {
01210 int i, nLit, level;
01211 int uipFlag;
01212 bAigEdge_t v, ante;
01213 
01214   nLit = 0;
01215   *bLevel = 0;
01216   *fdaLit = 0;
01217   uipFlag = 0;
01218   for(i=0; i<clauseArray->num; i++) {
01219     v = clauseArray->space[i];
01220     v = SATnormalNode(v);
01221     level = SATlevel(v);
01222     if(level == mLevel) {
01223       nLit++;
01224       if(nLit > 1)
01225         break;
01226 
01227       ante = SATante(v);
01228       if(ante == 0) {
01229         uipFlag = 1;
01230         *fdaLit = clauseArray->space[i];
01231       }
01232     }
01233     else if(*bLevel < level)
01234       *bLevel = level;
01235   }
01236 
01237   if(nLit > 1)
01238     uipFlag = 0;
01239 
01240   if(uipFlag)   return(1);
01241   else          return(0);
01242 }
01243 
01255 void
01256 bAigMinimizationBasedOnLifting(
01257         bAigTransition_t *t, 
01258         bAigEdge_t obj,
01259         satArray_t *orderArray)
01260 {
01261 satManager_t *allsat, *cm;
01262 satLevel_t *d;
01263 satArray_t *tmpArray;
01264 satArray_t *notLiftableArray;
01265 bAigEdge_t v, tv, *plit;
01266 bAigEdge_t lastNode, lastLit, ante;
01267 int inverted, inserted;
01268 int tvalue, value, bLevel;
01269 int i, j, size;
01270 
01271   cm = t->lifting;
01272   allsat = t->allsat;
01277   cm->option->includeLevelZeroLiteral = 1;
01278   lastLit = cm->literals->last-cm->literals->begin;
01279   lastNode = cm->nodesArraySize;
01280 
01287   d = sat_AllocLevel(cm);
01288   for(i=0; i<t->nInputs; i++) {
01289     v = t->inputs[i];
01290     if(!(SATflags(v) & CoiMask))        continue;
01291     value = t->vinputs[i];
01292 
01293     SATvalue(v) = value;
01294     SATmakeImplied(v, d);
01295     sat_Enqueue(cm->queue, v);
01296     SATflags(v) |= InQueueMask;
01297 
01298   }
01299   sat_ImplyArray(cm, d, cm->assertion);
01300   sat_ImplyArray(cm, d, cm->unitLits);
01301   sat_ImplyArray(cm, d, cm->pureLits);
01302   sat_ImplyArray(cm, d, cm->auxObj);
01303   sat_ImplyArray(cm, d, cm->nonobjUnitLitArray);
01304 
01305   value = SATisInverted(obj);
01306   v = SATnormalNode(obj);
01307   SATvalue(v) = value;
01308   SATmakeImplied(v, d);
01309   if((SATflags(v) & InQueueMask) == 0) {
01310     sat_Enqueue(cm->queue, v);
01311     SATflags(v) |= InQueueMask;
01312   }
01313   sat_ImplicationMain(cm, d);
01314 
01316   for(i=0; i<orderArray->num; i++) {
01317     cm->status = 0;
01318 
01321     tv = orderArray->space[i];
01322     v = SATnormalNode(tv);
01323     value = SATvalue(v);
01324     if(value < 2) { 
01325       continue;
01326     }
01327     value = !SATisInverted(tv);
01328     SATvalue(v) = value;
01329 
01330     d = sat_AllocLevel(cm);
01331 
01332     SATmakeImplied(v, d);
01333     if((SATflags(v) & InQueueMask) == 0) {
01334       sat_Enqueue(cm->queue, v);
01335       SATflags(v) |= InQueueMask;
01336     }
01337     sat_ImplicationMain(cm, d);
01338   }
01339 
01341   notLiftableArray = sat_ArrayAlloc(orderArray->num);
01342   for(i=orderArray->num-1; i>=0; i--) {
01344     while(1) {
01345       if(i<0) break;
01346       tv = orderArray->space[i];
01347       v = SATnormalNode(tv);
01348       value = SATvalue(v);
01349       ante = SATante(v);
01350       if(ante) { 
01351         tvalue = !SATisInverted(tv);
01352         if(tvalue == value)
01353           sat_ArrayInsert(notLiftableArray, tv);
01354         i--;
01355         continue;
01356       }
01357       else { 
01358         sat_Backtrack(cm, SATlevel(v)-1);
01359         break;
01360       }
01361     }
01362     if(i<0)     break;
01363 
01364     bLevel = cm->currentDecision;
01365 
01366     tv = orderArray->space[i];
01367     value = SATisInverted(tv); 
01368     SATvalue(v) = value;
01369 
01370     d = sat_AllocLevel(cm);
01371     SATmakeImplied(v, d);
01372     if((SATflags(v) & InQueueMask) == 0) {
01373       sat_Enqueue(cm->queue, v);
01374       SATflags(v) |= InQueueMask;
01375     }
01376     sat_ImplicationMain(cm, d);
01377 
01378     if(d->conflict) { 
01379       sat_Backtrack(cm, bLevel);
01380       continue;
01381     }
01382 
01383     for(j=0; j<notLiftableArray->num; j++) {
01384       v = notLiftableArray->space[j];
01385       value = !SATisInverted(v);
01386       v = SATnormalNode(v);
01387       tvalue = SATvalue(v);
01388       if(tvalue < 2 && tvalue != value) {
01390         sat_Backtrack(cm, bLevel);
01391         d->conflict = v;
01392         continue;
01393       }
01394       SATvalue(v) = value;
01395       SATmakeImplied(v, d);
01396       if((SATflags(v) & InQueueMask) == 0) {
01397         sat_Enqueue(cm->queue, v);
01398         SATflags(v) |= InQueueMask;
01399       }
01400       sat_ImplicationMain(cm, d);
01401       if(d->conflict) { 
01402         break;
01403       }
01404     }
01405 
01406     if(d->conflict) { 
01407       sat_Backtrack(cm, bLevel);
01408       continue;
01409     }
01410 
01411     bAigSolverForLifting(cm, cm->currentDecision); 
01413     sat_CleanImplicationQueue(cm);
01414     if(cm->status == SAT_SAT) {
01415       sat_ArrayInsert(notLiftableArray, tv); 
01416     }
01417     sat_Backtrack(cm, bLevel);
01418   }
01419 
01420   memcpy(orderArray->space, notLiftableArray->space, sizeof(long)*notLiftableArray->num);
01421   orderArray->num = notLiftableArray->num;
01422   sat_ArrayFree(notLiftableArray);
01423 
01424   sat_Backtrack(cm, 0);
01425   d = SATgetDecision(cm->currentDecision);
01426   sat_Undo(cm, d);
01427   cm->currentDecision--;
01428   cm->status = 0;
01429 
01430 
01433   tmpArray = sat_ArrayAlloc(64);
01434   for(i=lastNode; i<cm->nodesArraySize; i+=satNodeSize) {
01435     size = SATnumLits(i);
01436     plit = (long*)SATfirstLit(i);
01437     inserted = 0;
01438     tmpArray->num = 0;
01439     for(j=0; j<size; j++, plit++) {
01440       v = SATgetNode(*plit);
01441       inverted = SATisInverted(v);
01442       v = SATnormalNode(v);
01443       value = (allsat->nodesArray[v+satValue]) ;
01444       value = value ^ inverted;
01446       if(value > 0)
01447         inserted = 1;
01448       sat_ArrayInsert(tmpArray, v^(!inverted));
01449     }
01450     if(inserted)
01451       sat_AddConflictClause(allsat, tmpArray, 0);
01452   }
01453   sat_ArrayFree(tmpArray);
01454 
01455   return;
01456 }
01457 
01469 void
01470 bAigMinimizationBasedOnLiftingAllAtOnce(
01471         bAigTransition_t *t, 
01472         bAigEdge_t obj,
01473         satArray_t *orderArray)
01474 {
01475 satManager_t *allsat, *cm;
01476 satLevel_t *d;
01477 satArray_t *implied;
01478 satArray_t *notLiftableArray;
01479 bAigEdge_t v, tv;
01480 bAigEdge_t lastNode, lastLit;
01481 long *space;
01482 int value;
01483 int i, j, size, num;
01484 
01485   cm = t->lifting;
01486   allsat = t->allsat;
01491   cm->option->includeLevelZeroLiteral = 1;
01492   lastLit = cm->literals->last-cm->literals->begin;
01493   lastNode = cm->nodesArraySize;
01494 
01501   d = sat_AllocLevel(cm);
01502   for(i=0; i<t->nInputs; i++) {
01503     v = t->inputs[i];
01504     if(!(SATflags(v) & CoiMask))        continue;
01505     value = t->vinputs[i];
01506 
01507     SATvalue(v) = value;
01508     SATmakeImplied(v, d);
01509     sat_Enqueue(cm->queue, v);
01510     SATflags(v) |= InQueueMask;
01511 
01512   }
01513   sat_ImplyArray(cm, d, cm->assertion);
01514   sat_ImplyArray(cm, d, cm->unitLits);
01515   sat_ImplyArray(cm, d, cm->pureLits);
01516   sat_ImplyArray(cm, d, cm->auxObj);
01517   sat_ImplyArray(cm, d, cm->nonobjUnitLitArray);
01518 
01519   value = SATisInverted(obj);
01520   v = SATnormalNode(obj);
01521   SATvalue(v) = value;
01522   SATmakeImplied(v, d);
01523   if((SATflags(v) & InQueueMask) == 0) {
01524     sat_Enqueue(cm->queue, v);
01525     SATflags(v) |= InQueueMask;
01526   }
01527   sat_ImplicationMain(cm, d);
01528 
01530   num = d->implied->num;
01531   notLiftableArray = sat_ArrayAlloc(orderArray->num);
01532   for(i=0; i<orderArray->num; i++) {
01533     cm->status = 0;
01534     d->conflict = 0;
01535 
01538     tv = orderArray->space[i];
01539     v = SATnormalNode(tv);
01540     value = SATisInverted(tv);
01541     SATvalue(v) = value;
01542 
01543     SATmakeImplied(v, d);
01544     if((SATflags(v) & InQueueMask) == 0) {
01545       sat_Enqueue(cm->queue, v);
01546       SATflags(v) |= InQueueMask;
01547     }
01548 
01549     for(j=i+1; j<orderArray->num; j++) {
01550       v = orderArray->space[j];
01551       value = !SATisInverted(v);
01552       v = SATnormalNode(v);
01553       SATvalue(v) = value;
01554       SATmakeImplied(v, d);
01555       if((SATflags(v) & InQueueMask) == 0) {
01556         sat_Enqueue(cm->queue, v);
01557         SATflags(v) |= InQueueMask;
01558       }
01559     }
01560 
01561     for(j=0; j<notLiftableArray->num; j++) {
01562       v = notLiftableArray->space[j];
01563       value = !SATisInverted(v);
01564       v = SATnormalNode(v);
01565       SATvalue(v) = value;
01566       SATmakeImplied(v, d);
01567       if((SATflags(v) & InQueueMask) == 0) {
01568         sat_Enqueue(cm->queue, v);
01569         SATflags(v) |= InQueueMask;
01570       }
01571     }
01572     sat_ImplicationMain(cm, d);
01573 
01574 
01575     if(d->conflict == 0) {
01576       bAigSolverForLifting(cm, 0); 
01578       sat_CleanImplicationQueue(cm);
01579     }
01580     else  {
01581       cm->status = SAT_UNSAT;
01582     }
01583 
01584     if(cm->status == SAT_SAT) {
01585       sat_ArrayInsert(notLiftableArray, tv); 
01586       sat_Backtrack(cm, 0);
01587     }
01588 
01589     d = SATgetDecision(0);
01590     implied = d->implied;
01591     space = implied->space;
01592     size = implied->num;
01593     for(j=num; j<size; j++) {
01594       v = space[j];
01595   
01596       SATvalue(v) = 2;
01597       SATflags(v) &= ResetNewVisitedObjInQueueMask;
01598       SATante(v) = 0;
01599       SATante2(v) = 0;
01600       SATlevel(v) = -1;
01601     }
01602     implied->num = num;
01603     cm->currentDecision = 0;
01604 
01605   }
01606 
01607   memcpy(orderArray->space, notLiftableArray->space, sizeof(long)*notLiftableArray->num);
01608   orderArray->num = notLiftableArray->num;
01609   sat_ArrayFree(notLiftableArray);
01610 
01611 
01612   d = SATgetDecision(0);
01613   sat_Undo(cm, d);
01614   cm->status = 0;
01615   cm->currentDecision = -1;
01616 
01617 #if 0
01618 
01620   tmpArray = sat_ArrayAlloc(64);
01621   for(i=lastNode; i<cm->nodesArraySize; i+=satNodeSize) {
01622     size = SATnumLits(i);
01623     plit = (long*)SATfirstLit(i);
01624     inserted = 0;
01625     tmpArray->num = 0;
01626     for(j=0; j<size; j++, plit++) {
01627       tv = SATgetNode(*plit);
01628       inverted = SATisInverted(tv);
01629       v = SATnormalNode(tv);
01630       value = (allsat->nodesArray[v+satValue]) ;
01631       value = value ^ inverted;
01633       if(value > 0)
01634         inserted = 1;
01635       sat_ArrayInsert(tmpArray, SATnot(tv));
01636     }
01637     if(inserted)
01638       sat_AddConflictClause(allsat, tmpArray, 0);
01639   }
01640   sat_ArrayFree(tmpArray);
01641 #endif
01642 
01643   return;
01644 }
01645 
01657 void
01658 bAigSolverForLifting(satManager_t *cm, int tLevel)
01659 {
01660 satLevel_t *d;
01661 int level;
01662 
01663   d = SATgetDecision(0);
01664   cm->implicatedSoFar = d->implied->num;
01665 
01666   while(1) {
01667     d = sat_MakeDecision(cm);
01668 
01669     if(d == 0)  {
01670       cm->status = SAT_SAT;
01671       return;
01672     }
01673 
01674     while(1) {
01675       sat_ImplicationMain(cm, d);
01676 
01677       if(d->conflict == 0)      
01678         break;
01679 
01680       level = sat_ConflictAnalysisForLifting(cm, d);
01681 
01682       if(cm->currentDecision <= -1) {
01683         cm->status = SAT_UNSAT;
01684         return;
01685       }
01686 
01687       d = SATgetDecision(cm->currentDecision);
01688     }
01689   }
01690   return;
01691 }
01692 
01704 void
01705 bAigCollectAntecdentOfObjective(
01706         bAigTransition_t *t, 
01707         satManager_t *cm, 
01708         bAigEdge_t obj, 
01709         satArray_t *clauseArray)
01710 {
01711 int i, value;
01712 bAigEdge_t v;
01713 
01714   bAigCollectAntecdentOfObjectiveAux(cm, obj);
01715 
01716   clauseArray->num = 0;
01717   for(i=0; i<t->nLatches; i++) {
01718     v = t->tstates[i];
01719     if(SATflags(v) & VisitedMask) {
01720       value = t->vtstates[i];
01721       sat_ArrayInsert(clauseArray, v^(!value));
01722     }
01723   }
01728 }
01729 
01741 void
01742 bAigCollectAntecdentOfObjectiveAux(satManager_t *cm, bAigEdge_t v)
01743 {
01744 int i, size, completeness;
01745 int value, inverted;
01746 bAigEdge_t ante, nv, *plit;
01747 
01748   if(v == 2)    
01749     return;
01750 
01751   v = SATnormalNode(v);
01752   if(SATflags(v) & VisitedMask)
01753     return;
01754 
01755   SATflags(v) |= VisitedMask;
01756   ante = SATante(v);
01757   if(ante == 0) 
01758     return;
01759 
01760   if(SATflags(ante) & IsCNFMask) {
01761 
01762     size = SATnumLits(ante);
01763 
01764     completeness = 1;
01765     for(i=0, plit=(bAigEdge_t*)SATfirstLit(ante); i<size; i++, plit++) {
01766       nv = SATgetNode(*plit);
01767       inverted = SATisInverted(nv);
01768       nv = SATnormalNode(nv);
01769       value = SATvalue(nv) ^ inverted;
01770       if(v == nv) {
01771         if(value == 0)  completeness = 0;
01772       }
01773       else {
01774         if(value == 1)  completeness = 0;
01775       }
01776     }
01777     if(completeness == 0) {
01778       fprintf(stdout, "ERROR : incomplete implication graph\n");
01779     }
01780 
01781     for(i=0, plit=(bAigEdge_t*)SATfirstLit(ante); i<size; i++, plit++) {
01782       nv = SATgetNode(*plit);
01783       nv = SATnormalNode(nv);
01784       if(SATflags(nv) & VisitedMask)
01785         continue;
01786       bAigCollectAntecdentOfObjectiveAux(cm, nv);
01787     }
01788   }
01789   else {
01790     bAigCollectAntecdentOfObjectiveAux(cm, ante);
01791     ante = SATante2(v);
01792     if(ante)
01793       bAigCollectAntecdentOfObjectiveAux(cm, ante);
01794   }
01795 
01796 }
01797 
01809 void
01810 bAigCreateSatManagerForLiftingUnconstrained(bAigTransition_t *t)
01811 {
01812 satManager_t *lifting, *allsat, *cm;
01813 bAigEdge_t *lastLit;
01814 satOption_t *option;
01815 satLiteralDB_t *literals;
01816 long objective;
01817 
01818   allsat = t->allsat;
01819   lifting = sat_InitManager(0);
01820   t->lifting = lifting;
01821 
01823   cm = allsat;
01824   lastLit = 0;
01825   
01827   lifting->nodesArraySize = allsat->frontierNodesBegin;
01828 
01829   lifting->initNodesArraySize = lifting->nodesArraySize;
01830   lifting->maxNodesArraySize = lifting->nodesArraySize * 2;
01831 
01832   lifting->nodesArray = ALLOC(long, lifting->maxNodesArraySize);
01833   memcpy(lifting->nodesArray, allsat->nodesArray, sizeof(long) * lifting->nodesArraySize);
01834 
01835   lifting->HashTable = ALLOC(long, bAig_HashTableSize);
01836   memcpy(lifting->HashTable, allsat->HashTable, sizeof(long)*bAig_HashTableSize);
01837 
01839   sat_AllocLiteralsDB(lifting);
01840   literals = lifting->literals;
01841 
01842   lifting->comment = ALLOC(char, 2);
01843   lifting->comment[0] = ' ';
01844   lifting->comment[1] = '\0';
01845   lifting->stdErr = allsat->stdErr;
01846   lifting->stdOut = allsat->stdOut;
01847   lifting->status = 0;
01848   lifting->orderedVariableArray = 0;
01849   lifting->unitLits = sat_ArrayAlloc(16);
01850   lifting->pureLits = sat_ArrayAlloc(16);
01851   lifting->option = 0;
01852   lifting->each = 0;
01853   lifting->decisionHead = 0;
01854   lifting->variableArray = 0;
01855   lifting->queue = 0;
01856   lifting->BDDQueue = 0;
01857   lifting->unusedAigQueue = 0;
01858 
01859   option = sat_InitOption();
01860   lifting->option = option;
01861   option->verbose = 0;
01862 
01864   option->decisionHeuristic = 0;
01865   option->decisionHeuristic |= DVH_DECISION;
01866 
01867   lifting->each = sat_InitStatistics();
01868 
01869   if(t->originalFrontier || t->allsat->reached) {
01870     objective = bAigBuildComplementedObjectiveWithCNF(
01871           t, lifting, t->originalFrontier, t->allsat->reached);
01872     objective = SATnot(objective);
01873   }
01874   else {
01875     lifting->initNumVariables = lifting->nodesArraySize;
01876     if(lifting->variableArray == 0) {
01877       lifting->variableArray = ALLOC(satVariable_t, lifting->initNumVariables+1);
01878       memset(lifting->variableArray, 0, 
01879               sizeof(satVariable_t) * (lifting->initNumVariables+1));
01880     }
01881     sat_CleanDatabase(lifting);
01882 
01883     if(t->allsat->assertion) t->lifting->assertion = sat_ArrayDuplicate(t->allsat->assertion);
01884     if(t->allsat->auxObj)    t->lifting->auxObj = sat_ArrayDuplicate(t->allsat->auxObj);
01885     objective = allsat->obj->space[0];
01886     sat_MarkTransitiveFaninForNode(lifting, objective, CoiMask);
01887   }
01888 
01889 
01890   sat_PreProcessingForMixedNoCompact(lifting);
01891 
01892   if(lifting->obj)      sat_ArrayFree(lifting->obj);    
01893   lifting->obj = sat_ArrayAlloc(1);
01894   sat_ArrayInsert(lifting->obj, (objective));
01895 
01896   /* 
01897    * reset score of PI 
01898    * Since the scores of PI and current state variables are high than 
01899    * other variables because of blocking clauses that forwarded from
01900    * prevous image step.
01901   for(i=0; i<t->nInputs; i++) {
01902     v = t->inputs[i];
01903     var = lifting->variableArray[SATnodeID(v)];
01904     var.scores[0] = 0;
01905     var.scores[1] = 0;
01906   }
01907   for(i=0; i<t->nLatches; i++) {
01908     v = t->cstates[i];
01909     var = lifting->variableArray[SATnodeID(v)];
01910     var.scores[0] = 0;
01911     var.scores[1] = 0;
01912   }
01913    **/
01914 
01922   lifting->currentDecision = -1;
01923 
01924 }
01925 
01937 void
01938 bAigCreateSatManagerForLifting(bAigTransition_t *t)
01939 {
01940 satManager_t *lifting, *allsat, *cm;
01941 bAigEdge_t *lastLit, v;
01942 satOption_t *option;
01943 satLiteralDB_t *literals;
01944 int size, dir;
01945 int nCls, nLits, index;
01946 long *space;
01947 
01948   allsat = t->allsat;
01949   lifting = sat_InitManager(0);
01950   t->lifting = lifting;
01951 
01953   cm = allsat;
01954   lastLit = 0;
01955   
01956   if(t->constrain) {
01966     if(allsat->nodesArraySize != allsat->frontierNodesEnd){
01967       v = allsat->nodesArraySize-satNodeSize;
01968       lastLit = (bAigEdge_t *) SATfirstLit(v);
01969       lastLit += SATnumLits(v);
01970       lastLit++;
01971     }
01972     lifting->nodesArraySize = allsat->nodesArraySize;
01973   
01974   }
01975   else {
01981     lifting->nodesArraySize = allsat->frontierNodesBegin;
01982   }
01983 
01988   lifting->initNodesArraySize = lifting->nodesArraySize;
01989   lifting->maxNodesArraySize = lifting->nodesArraySize * 2;
01990 
01991   lifting->nodesArray = ALLOC(long, lifting->maxNodesArraySize);
01992   memcpy(lifting->nodesArray, allsat->nodesArray, sizeof(long) * lifting->nodesArraySize);
01993 
01994   lifting->HashTable = ALLOC(long, bAig_HashTableSize);
01995   memcpy(lifting->HashTable, allsat->HashTable, sizeof(long)*bAig_HashTableSize);
01996 
01997   literals = ALLOC(satLiteralDB_t, 1);
01998   lifting->literals = literals;
01999 
02000   if(lastLit > 0) {
02001     size = lastLit - allsat->literals->begin;
02002   }
02003   else {
02004     size = 0;
02005   }
02006 
02007   if(size > 1) { 
02008     literals->begin = ALLOC(long, size*4);
02009     literals->end = literals->begin + size*4;
02010     memcpy(literals->begin, allsat->literals->begin, sizeof(long)*size);
02011     literals->last = literals->begin + size;
02012     literals->initialSize = literals->begin+size-1;
02013   }
02014   else { 
02015     size = 1024 * 1024;
02016     literals->begin = ALLOC(long, size);
02017     *(literals->begin) = 0;
02018     literals->last = literals->begin + 1;
02019     literals->end = literals->begin + size;
02020     literals->initialSize = literals->last;
02021   }
02022 
02023   lifting->initNumVariables = allsat->initNumVariables;
02024   lifting->initNumClauses = allsat->initNumClauses;
02025   lifting->initNumLiterals = allsat->initNumLiterals;
02026   lifting->comment = ALLOC(char, 2);
02027   lifting->comment[0] = ' ';
02028   lifting->comment[1] = '\0';
02029   lifting->stdErr = allsat->stdErr;
02030   lifting->stdOut = allsat->stdOut;
02031   lifting->status = 0;
02032   lifting->orderedVariableArray = 0;
02033   lifting->unitLits = sat_ArrayAlloc(16);
02034   lifting->pureLits = sat_ArrayAlloc(16);
02035   lifting->option = 0;
02036   lifting->each = 0;
02037   lifting->decisionHead = 0;
02038   lifting->variableArray = 0;
02039   lifting->queue = 0;
02040   lifting->BDDQueue = 0;
02041   lifting->unusedAigQueue = 0;
02042 
02043   option = sat_InitOption();
02044   lifting->option = option;
02045   option->verbose = 0;
02046 
02048   option->decisionHeuristic = 0;
02049   option->decisionHeuristic |= DVH_DECISION;
02050 
02051   lifting->each = sat_InitStatistics();
02052 
02053   sat_CleanDatabase(lifting);
02054 
02055   if(lifting->variableArray == 0) {
02056     lifting->variableArray = ALLOC(satVariable_t, lifting->initNumVariables+1);
02057     memset(lifting->variableArray, 0, 
02058             sizeof(satVariable_t) * (lifting->initNumVariables+1));
02059   }
02060 
02061   cm = lifting;
02062   nCls = nLits = 0;
02063   for(space = literals->begin; space < literals->last; space++) {
02064     if(*space < 0) {
02065       v = -(*space);
02066       space++;
02067       SATfirstLit(v) = (long) space;
02068       index = 0;
02069       while(1) {
02070         if(*space < 0)  break;
02071         dir = SATgetDir(*space);
02072         nLits++;
02073         if(dir == -2){
02074           space++;
02075           index++;
02076           continue;
02077         }
02078         SATunsetWL(space);
02079         sat_AddWL(cm, v, index, dir);
02080         space++;
02081         index++;
02082       }
02083       nCls++;
02084     }
02085   }
02086   lifting->initNumClauses = nCls;
02087   lifting->initNumLiterals = nLits;
02088 
02089   if(allsat->assertion) lifting->assertion = sat_ArrayDuplicate(allsat->assertion);
02090   if(allsat->auxObj)    lifting->auxObj = sat_ArrayDuplicate(allsat->auxObj);
02091 
02092   bAigMarkConeOfInfluenceForAX(t, lifting);
02093 
02094 
02095   bAigPreProcessingForLiftingInstance(t, lifting) ;
02096 
02097   /* 
02098    * reset score of PI 
02099    * Since the scores of PI and current state variables are high than 
02100    * other variables because of blocking clauses that forwarded from
02101    * prevous image step.
02102   for(i=0; i<t->nInputs; i++) {
02103     v = t->inputs[i];
02104     var = lifting->variableArray[SATnodeID(v)];
02105     var.scores[0] = 0;
02106     var.scores[1] = 0;
02107   }
02108   for(i=0; i<t->nLatches; i++) {
02109     v = t->cstates[i];
02110     var = lifting->variableArray[SATnodeID(v)];
02111     var.scores[0] = 0;
02112     var.scores[1] = 0;
02113   }
02114    **/
02115 
02122   if(lifting->obj)      sat_ArrayFree(lifting->obj);    
02123   lifting->obj = sat_ArrayDuplicate(allsat->obj);
02124 
02125   lifting->currentDecision = -1;
02126 
02127 }
02139 void
02140 bAigPreProcessingForLiftingInstance(bAigTransition_t *t, satManager_t *cm) 
02141 {
02142 satLevel_t *d;
02143 int i;
02144 long v;
02145 
02146 
02148   cm->queue = sat_CreateQueue(1024);
02149   cm->BDDQueue = sat_CreateQueue(1024);
02150   cm->unusedAigQueue = sat_CreateQueue(1024);
02151 
02157   if(cm->variableArray == 0) {
02158     cm->variableArray = ALLOC(satVariable_t, cm->initNumVariables+1);
02159     memset(cm->variableArray, 0, 
02160             sizeof(satVariable_t) * (cm->initNumVariables+1));
02161   }
02162 
02163   if(cm->auxArray == 0)
02164     cm->auxArray = sat_ArrayAlloc(1024);
02165   if(cm->nonobjUnitLitArray == 0)
02166     cm->nonobjUnitLitArray = sat_ArrayAlloc(128);
02167   if(cm->objUnitLitArray == 0)
02168     cm->objUnitLitArray = sat_ArrayAlloc(128);
02169 
02174   cm->initNodesArraySize = cm->nodesArraySize;
02175   cm->beginConflict = cm->nodesArraySize;
02176 
02177   if(cm->option->allSatMode) {
02178     sat_RestoreFrontierClauses(cm);
02179     sat_RestoreBlockingClauses(cm);
02180   }
02181 
02183   sat_InitScoreForMixed(cm);
02184 
02186   if(cm->decisionHeadSize == 0) {
02187     cm->decisionHeadSize = 32;
02188     cm->decisionHead = ALLOC(satLevel_t, cm->decisionHeadSize);
02189     memset(cm->decisionHead, 0, sizeof(satLevel_t) * cm->decisionHeadSize);
02190   }
02191   cm->currentDecision = -1;
02192 
02194   SATvalue(2) = 2;
02195   SATflags(0) = 0;
02196 
02198   if(cm->option->incTraceObjective) { 
02199     sat_RestoreForwardedClauses(cm, 0);
02200   }
02201   else if(cm->option->incAll) {
02202     sat_RestoreForwardedClauses(cm, 1);
02203   }
02204 
02205   if(cm->option->incTraceObjective) {
02206     sat_MarkObjectiveFlagToArray(cm, cm->obj);
02207     sat_MarkObjectiveFlagToArray(cm, cm->objCNF);
02208   }
02209 
02211   d = sat_AllocLevel(cm);
02212 
02213   sat_ApplyForcedAssignmentMain(cm, d);
02214 
02215   if(cm->status == SAT_UNSAT)
02216     return;
02217 
02221   for(i=0; i<cm->pureLits->num; i++) {
02222     v = cm->pureLits->space[i];
02223     if(v == t->objective) {
02224       for(;i<cm->pureLits->num; i++) {
02225         cm->pureLits->space[i] = cm->pureLits->space[i+1];
02226       }
02227       cm->pureLits->num--;
02228       break;
02229     }
02230   }
02231 
02232   sat_ImplyArray(cm, d, cm->assertion);
02233   sat_ImplyArray(cm, d, cm->unitLits);
02234   sat_ImplyArray(cm, d, cm->pureLits);
02235   sat_ImplyArray(cm, d, cm->auxObj);
02236   sat_ImplyArray(cm, d, cm->nonobjUnitLitArray);
02237   sat_ImplyArray(cm, d, cm->obj);
02238 
02239   sat_ImplicationMain(cm, d);
02240   if(d->conflict) {
02241     cm->status = SAT_UNSAT;
02242   }
02243 
02244   if(cm->status == 0) {
02245     if(cm->option->incDistill) {
02246       sat_IncrementalUsingDistill(cm);
02247     }
02248   }
02249 
02250 }
02251 
02263 void
02264 bAig_PostProcessForAX(bAigTransition_t *t, satManager_t *cm)
02265 {
02266 bAig_Manager_t *manager;
02267 
02268   manager = t->manager;
02269   if(cm->maxNodesArraySize > manager->maxNodesArraySize) {
02270     manager->maxNodesArraySize = cm->maxNodesArraySize;
02271     manager->nameList   = REALLOC(char *, manager->nameList  , manager->maxNodesArraySize/bAigNodeSize);
02272     manager->bddIdArray = REALLOC(int ,   manager->bddIdArray  , manager->maxNodesArraySize/bAigNodeSize);
02273     manager->bddArray   = REALLOC(bdd_t *, manager->bddArray  , manager->maxNodesArraySize/bAigNodeSize);
02274   }
02275 
02276   manager->maxNodesArraySize = cm->maxNodesArraySize;
02277   manager->NodesArray = cm->nodesArray;
02278   manager->literals = cm->literals;
02279   cm->literals->last = cm->literals->initialSize;
02280 
02281   cm->nodesArray = 0;
02282   cm->literals = 0;
02283   cm->HashTable = 0;
02284 
02285   sat_FreeManager(cm);
02286 
02288   t->objective = 0; 
02289 }
02290 
02302 void
02303 bAigMarkConeOfInfluenceForAX(bAigTransition_t *t, satManager_t *cm)
02304 {
02305 satArray_t *arr;
02306 int i;
02307 bAigEdge_t v;
02308 
02309   sat_MarkTransitiveFaninForNode(cm, t->objective, CoiMask);
02310 
02311   if(t->tVariables) {
02312     arr = t->tVariables;
02313     for(i=0; i<arr->num; i++) {
02314       v = arr->space[i];
02315       SATflags(v) |= CoiMask;
02316     }
02317   }
02318 
02319   if(t->auxObj) {
02320     arr = t->auxObj;
02321     for(i=0; i<arr->num; i++) {
02322       v = arr->space[i];
02323       sat_MarkTransitiveFaninForNode(cm, v, CoiMask);
02324     }
02325   }
02326 
02327   if(t->objArr) {
02328     arr = t->objArr;
02329     for(i=0; i<arr->num; i++) {
02330       v = arr->space[i];
02331       sat_MarkTransitiveFaninForNode(cm, v, CoiMask);
02332     }
02333   }
02334 
02335 }
02336 
02348 satManager_t *
02349 bAigCirCUsInterfaceForAX(bAigTransition_t *t)
02350 {
02351 satManager_t *cm;
02352 bAig_Manager_t *manager;
02353 satOption_t *option;
02354 int i;
02355 bAigEdge_t v;
02356 
02357   manager = t->manager;
02358   cm = sat_InitManager(0);
02359   memset(cm, 0, sizeof(satManager_t));
02360 
02361   cm->nodesArraySize = manager->nodesArraySize;
02362   cm->initNodesArraySize = manager->nodesArraySize;
02363   cm->maxNodesArraySize = manager->maxNodesArraySize;
02364   cm->nodesArray = manager->NodesArray;
02365   cm->HashTable = manager->HashTable;
02366   cm->literals = manager->literals;
02367   cm->initNumVariables = (manager->nodesArraySize/bAigNodeSize);
02368   cm->initNumClauses = 0;
02369   cm->initNumLiterals = 0;
02370   cm->comment = ALLOC(char, 2);
02371   cm->comment[0] = ' ';
02372   cm->comment[1] = '\0';
02373   cm->stdErr = vis_stderr;
02374   cm->stdOut = vis_stdout;
02375   cm->status = 0;
02376   cm->orderedVariableArray = 0;
02377   cm->unitLits = sat_ArrayAlloc(16);
02378   cm->pureLits = sat_ArrayAlloc(16);
02379   cm->option = 0;
02380   cm->each = 0;
02381   cm->decisionHead = 0;
02382   cm->variableArray = 0;
02383   cm->queue = 0;
02384   cm->BDDQueue = 0;
02385   cm->unusedAigQueue = 0;
02386 
02387   option = sat_InitOption();
02388   cm->option = option;
02389   option->verbose = 0;
02390 
02391   cm->each = sat_InitStatistics();
02392   sat_AllocLiteralsDB(cm);
02393 
02394   cm->obj = sat_ArrayAlloc(1);
02395   sat_ArrayInsert(cm->obj, t->objective);
02396 
02397   if(t->auxObj && t->auxObj->num) {
02398     cm->auxObj = sat_ArrayAlloc(t->auxObj->num);
02399     for(i=0; i<t->auxObj->num; i++) {
02400       v = t->auxObj->space[i];
02401       sat_ArrayInsert(cm->auxObj, v);
02402     }
02403   }
02404   cm->option->allSatMode = 1;
02405 
02410   cm->reached = t->reached;
02411   cm->frontier = t->frontier;
02412   t->reached = 0;
02413   t->frontier = 0;
02414 
02415   return(cm);
02416 
02417 }
02418 
02431 bAigEdge_t
02432 bAigBuildObjectiveFromFrontierSet(bAigTransition_t *t)
02433 {
02434 mAig_Manager_t *manager;
02435 satArray_t *frontier;
02436 satArray_t *clause;
02437 satArray_t *andArr, *orArr;
02438 satArray_t *fandArr;
02439 satArray_t *fArr;
02440 int i, j, nCls, removeFlag;
02441 int inverted;
02442 long *space, index;
02443 bAigEdge_t v, tv, out, objective;
02444   
02445   manager = t->manager;
02446 
02447   andArr = sat_ArrayAlloc(1024);
02448   fandArr = sat_ArrayAlloc(1024);
02449   clause = sat_ArrayAlloc(1024);
02450 
02451   if(t->coiStates == 0) 
02452     t->coiStates = ALLOC(bAigEdge_t, sizeof(bAigEdge_t) * t->csize);
02453   memset(t->coiStates, 0, sizeof(bAigEdge_t)*t->csize);
02454 
02455   if(t->tVariables) t->tVariables->num = 0;
02456   else              t->tVariables = sat_ArrayAlloc(1024);
02457   fArr = sat_ArrayAlloc(1024);
02458   orArr = t->tVariables;
02459 
02460   nCls = 0;
02461   frontier = t->frontier; 
02462   sat_ArrayInsert(fArr, 0);
02463   for(i=0, space=frontier->space; i<frontier->num; i++, space++) {
02464     if(*space <= 0) { 
02465       space++;
02466       i++;
02467 
02468       if(i >= frontier->num) {
02469         break;
02470       }
02471 
02472       removeFlag = 0;
02473       fandArr->num = 0;
02474       andArr->num = 0;
02475       while(*space > 0) {
02476 #if 0
02477         v = *space;
02478         inverted = SATisInverted(v);
02479         tv = SATnormalNode(v);
02480         index = SATnodeID(tv);
02481         if(index > t->csize) {
02482           fprintf(stdout, "ERROR : %ld is not current state variable\n", tv);
02483           exit(0);
02484         }
02485         v = t->c2n[index];
02486         v = v ^ (inverted);
02487         if(v == 0) {  
02488         }
02489         else if(v == 1)  
02490           removeFlag = 1;
02491         else if(removeFlag == 0) {
02492           sat_ArrayInsert(andArr, v);
02493           t->coiStates[index] = 1;
02494         }
02495 #endif
02496 
02497         sat_ArrayInsert(fandArr, *space);
02498         i++;
02499         space++;
02500       }
02501       i--;
02502       space--;
02503 
02504 
02505       removeFlag = 0;
02506       for(j=0; j<fandArr->num; j++) {
02507         v = fandArr->space[j];
02508         inverted = SATisInverted(v);
02509         tv = SATnormalNode(v);
02510         index = SATnodeID(tv);
02511         if(index > t->csize) {
02512           fprintf(stdout, "ERROR : %ld is not current state variable\n", tv);
02513           exit(0);
02514         }
02515         v = t->c2n[index];
02516         v = v ^ (inverted);
02517 
02518         if(t->verbose > 4)
02519           fprintf(stdout, "%ld(%ld) ",fandArr->space[j], v); 
02520         if(v == 0) {  
02521           andArr->space[j] = 0;
02522         }
02523         else if(v == 1)  
02524           removeFlag = 1;
02525         else  {
02526           sat_ArrayInsert(andArr, v);
02527           t->coiStates[index] = 1;
02528         }
02529       }
02530       if(t->verbose > 4)
02531         fprintf(stdout, "\n");
02532 
02533       if(removeFlag)
02534         continue;
02535 
02536      
02537       if(t->verbose > 4) {
02538         fprintf(stdout, "%ld-> ", andArr->num);
02539         for(j=0; j<andArr->num; j++) {
02540           v = andArr->space[j];
02541           fprintf(stdout, "%ld ", v);
02542         }
02543         fprintf(stdout, "\n");
02544       }
02545 
02546       out = bAig_CreateNode(manager, 2, 2);
02547       sat_ArrayInsert(orArr, out);
02548 
02549       for(j=0; j<andArr->num; j++) {
02550         v = andArr->space[j];
02551         sat_ArrayInsert(fArr, SATnot(v));
02552         sat_ArrayInsert(fArr, SATnot(out));
02553         sat_ArrayInsert(fArr, -1); 
02554         nCls++;
02555       }
02556 
02557       for(j=0; j<andArr->num; j++) {
02558         v = andArr->space[j];
02559         sat_ArrayInsert(fArr, v);
02560       }
02561       sat_ArrayInsert(fArr, out);
02562       sat_ArrayInsert(fArr, -1);
02563       nCls++;
02564     }
02565   }
02566 
02567   objective = bAig_CreateNode(manager, 2, 2);
02568   for(i=0; i<orArr->num; i++) {
02569     v = orArr->space[i];
02570     sat_ArrayInsert(fArr, SATnot(v));
02571     sat_ArrayInsert(fArr, objective);
02572     sat_ArrayInsert(fArr, -1);
02573     nCls++;
02574   }
02575   for(i=0; i<orArr->num; i++) {
02576     v = orArr->space[i];
02577     sat_ArrayInsert(fArr, v);
02578   }
02579 
02580   sat_ArrayInsert(fArr, SATnot(objective));
02581 
02582   sat_ArrayInsert(fArr, -1);
02583   nCls++;
02584 
02585   if(orArr->num == 0) {
02586     fArr->num = 0;
02587   }
02588 
02589   if(t->verbose > 0) {
02590     fprintf(vis_stdout, "** SAT_INV : %ld number of frontier blocking clauses are processed\n", orArr->num);
02591     fprintf(vis_stdout, "** SAT_INV : %d number of clauses are added to build objective\n", nCls);
02592   }
02593 
02594   sat_ArrayFree(andArr);
02595   sat_ArrayFree(fandArr);
02596   sat_ArrayFree(clause);
02597 
02598   t->originalFrontier = frontier;
02599   t->frontier = fArr;
02600   t->objective = objective;
02601 
02602   if(t->objArr == 0) 
02603     t->objArr = sat_ArrayAlloc(t->nLatches);
02604   t->objArr->num = 0;
02605   for(i=0; i<t->csize; i++) {
02606     if(t->coiStates[i]) {
02607       sat_ArrayInsert(t->objArr, SATnormalNode(t->c2n[i]));
02608     }
02609   }
02610 
02611 
02612   return(objective);
02613 }
02614 
02615 bAigEdge_t
02616 bAigBuildComplementedObjectiveWithCNF(
02617         bAigTransition_t *t, 
02618         satManager_t *cm,
02619         satArray_t *narr,
02620         satArray_t *carr)
02621 {
02622 satArray_t *clause;
02623 satArray_t *andArr, *orArr;
02624 satArray_t *fandArr;
02625 satArray_t *fArr, *arr;
02626 satArray_t *frontier;
02627 int i, j, nCls, removeFlag;
02628 int inverted;
02629 long *space, index;
02630 bAigEdge_t v, tv, out, objective;
02631 bAigEdge_t obj1, obj2;
02632   
02633   andArr = sat_ArrayAlloc(1024);
02634   fandArr = sat_ArrayAlloc(1024);
02635   clause = sat_ArrayAlloc(1024);
02636 
02637   if(t->coiStates == 0) 
02638     t->coiStates = ALLOC(bAigEdge_t, sizeof(bAigEdge_t) * t->csize);
02639   memset(t->coiStates, 0, sizeof(bAigEdge_t)*t->csize);
02640 
02641   if(t->tVariables) t->tVariables->num = 0;
02642   else              t->tVariables = sat_ArrayAlloc(1024);
02643   fArr = sat_ArrayAlloc(1024);
02644 
02645   orArr =sat_ArrayAlloc(1024);
02646 
02647   sat_ArrayInsert(fArr, 0);
02648   nCls = 0;
02649   frontier = narr;
02650 
02651 
02652   if(frontier) {
02653   for(i=0, space=frontier->space; i<frontier->num; i++, space++) {
02654     if(*space <= 0) { 
02655       space++;
02656       i++;
02657 
02658       if(i >= frontier->num) {
02659         break;
02660       }
02661 
02662       removeFlag = 0;
02663       fandArr->num = 0;
02664       andArr->num = 0;
02665       while(*space > 0) {
02666         sat_ArrayInsert(fandArr, *space);
02667         i++;
02668         space++;
02669       }
02670       i--;
02671       space--;
02672 
02673 
02674       removeFlag = 0;
02675       for(j=0; j<fandArr->num; j++) {
02676         v = fandArr->space[j];
02677         inverted = SATisInverted(v);
02678         tv = SATnormalNode(v);
02679         index = SATnodeID(tv);
02680         if(index > t->csize) {
02681           fprintf(stdout, "ERROR : %ld is not current state variable\n", tv);
02682           exit(0);
02683         }
02684         v = t->c2n[index];
02685         v = v ^ (inverted);
02686 
02687         if(t->verbose > 4)
02688           fprintf(stdout, "%ld(%ld) ",fandArr->space[j], v); 
02689         if(v == 0) {  
02690           andArr->space[j] = 0;
02691         }
02692         else if(v == 1)  
02693           removeFlag = 1;
02694         else  {
02695           sat_ArrayInsert(andArr, v);
02696           t->coiStates[index] = 1;
02697         }
02698       }
02699       if(t->verbose > 4)
02700         fprintf(stdout, "\n");
02701 
02702       if(removeFlag)
02703         continue;
02704 
02705      
02706       if(t->verbose > 4) {
02707         fprintf(stdout, "%ld-> ", andArr->num);
02708         for(j=0; j<andArr->num; j++) {
02709           v = andArr->space[j];
02710           fprintf(stdout, "%ld ", v);
02711         }
02712         fprintf(stdout, "\n");
02713       }
02714 
02715       out = sat_CreateNode(cm, 2, 2);
02716       sat_ArrayInsert(orArr, out);
02717       sat_ArrayInsert(t->tVariables, out);
02718 
02719       for(j=0; j<andArr->num; j++) {
02720         v = andArr->space[j];
02721         sat_ArrayInsert(fArr, SATnot(v));
02722         sat_ArrayInsert(fArr, SATnot(out));
02723         sat_ArrayInsert(fArr, -1); 
02724         nCls++;
02725       }
02726 
02727       for(j=0; j<andArr->num; j++) {
02728         v = andArr->space[j];
02729         sat_ArrayInsert(fArr, v);
02730       }
02731       sat_ArrayInsert(fArr, out);
02732       sat_ArrayInsert(fArr, -1);
02733       nCls++;
02734     }
02735   }
02736   }
02737 
02738   obj1 = sat_CreateNode(cm, 2, 2);
02739   for(i=0; i<orArr->num; i++) {
02740     v = orArr->space[i];
02741     sat_ArrayInsert(fArr, SATnot(v));
02742     sat_ArrayInsert(fArr, obj1);
02743     sat_ArrayInsert(fArr, -1);
02744     nCls++;
02745   }
02746   for(i=0; i<orArr->num; i++) {
02747     v = orArr->space[i];
02748     sat_ArrayInsert(fArr, v);
02749   }
02750 
02751   sat_ArrayInsert(fArr, SATnot(obj1));
02752 
02753   sat_ArrayInsert(fArr, -1);
02754   nCls++;
02755 
02756 #if 1
02757   frontier = carr;
02758   if(frontier) {
02759   for(i=0, space=frontier->space; i<frontier->num; i++, space++) {
02760     if(*space <= 0) { 
02761       space++;
02762       i++;
02763 
02764       if(i >= frontier->num) {
02765         break;
02766       }
02767 
02768       fandArr->num = 0;
02769       andArr->num = 0;
02770       while(*space > 0) {
02771         sat_ArrayInsert(fandArr, *space);
02772         i++;
02773         space++;
02774       }
02775       i--;
02776       space--;
02777 
02778 
02779       if(t->verbose > 4) {
02780         fprintf(stdout, "%ld-> ", andArr->num);
02781         for(j=0; j<andArr->num; j++) {
02782           v = andArr->space[j];
02783           fprintf(stdout, "%ld ", v);
02784         }
02785         fprintf(stdout, "\n");
02786       }
02787 
02788       out = sat_CreateNode(cm, 2, 2);
02789       sat_ArrayInsert(orArr, out);
02790       sat_ArrayInsert(t->tVariables, out);
02791 
02792       for(j=0; j<andArr->num; j++) {
02793         v = andArr->space[j];
02794         sat_ArrayInsert(fArr, SATnot(v));
02795         sat_ArrayInsert(fArr, SATnot(out));
02796         sat_ArrayInsert(fArr, -1); 
02797         nCls++;
02798       }
02799 
02800       for(j=0; j<andArr->num; j++) {
02801         v = andArr->space[j];
02802         sat_ArrayInsert(fArr, v);
02803       }
02804       sat_ArrayInsert(fArr, out);
02805       sat_ArrayInsert(fArr, -1);
02806       nCls++;
02807     }
02808   }
02809   }
02810 
02811   obj2 = sat_CreateNode(cm, 2, 2);
02812   for(i=0; i<orArr->num; i++) {
02813     v = orArr->space[i];
02814     sat_ArrayInsert(fArr, SATnot(v));
02815     sat_ArrayInsert(fArr, obj2);
02816     sat_ArrayInsert(fArr, -1);
02817     nCls++;
02818   }
02819   for(i=0; i<orArr->num; i++) {
02820     v = orArr->space[i];
02821     sat_ArrayInsert(fArr, v);
02822   }
02823 
02824   sat_ArrayInsert(fArr, SATnot(obj2));
02825 
02826   sat_ArrayInsert(fArr, -1);
02827   nCls++;
02828   #endif
02829 
02830   sat_ArrayInsert(t->tVariables, obj1);
02831   sat_ArrayInsert(t->tVariables, obj2);
02832   objective = sat_CreateNode(cm, 2, 2);
02833   sat_ArrayInsert(fArr, SATnot(obj1));
02834   sat_ArrayInsert(fArr, SATnot(obj2));
02835   sat_ArrayInsert(fArr, objective);
02836   sat_ArrayInsert(fArr, -1);
02837   sat_ArrayInsert(fArr, (obj1));
02838   sat_ArrayInsert(fArr, SATnot(objective));
02839   sat_ArrayInsert(fArr, -1);
02840   sat_ArrayInsert(fArr, (obj2));
02841   sat_ArrayInsert(fArr, SATnot(objective));
02842   sat_ArrayInsert(fArr, -1);
02843 
02844   if(orArr->num == 0) {
02845     fArr->num = 0;
02846   }
02847 
02848   if(t->verbose > 0) {
02849     fprintf(vis_stdout, "** SAT_INV : %ld number of frontier blocking clauses are processed\n", orArr->num);
02850     fprintf(vis_stdout, "** SAT_INV : %d number of clauses are added to build objective\n", nCls);
02851   }
02852 
02853   sat_ArrayFree(andArr);
02854   sat_ArrayFree(fandArr);
02855   sat_ArrayFree(clause);
02856 
02857   cm->initNumVariables = cm->nodesArraySize;
02858   if(cm->variableArray == 0) {
02859     cm->variableArray = ALLOC(satVariable_t, cm->initNumVariables+1);
02860     memset(cm->variableArray, 0, 
02861             sizeof(satVariable_t) * (cm->initNumVariables+1));
02862   }
02863   sat_RestoreClauses(cm, fArr);
02864   sat_CleanDatabase(cm);
02865 
02866 
02867   if(t->allsat->assertion) t->lifting->assertion = sat_ArrayDuplicate(t->allsat->assertion);
02868   if(t->allsat->auxObj)    t->lifting->auxObj = sat_ArrayDuplicate(t->allsat->auxObj);
02869 
02870   sat_MarkTransitiveFaninForNode(cm, objective, CoiMask);
02871 
02872   for(i=0; i<t->csize; i++) {
02873     if(t->coiStates[i]) {
02874       sat_MarkTransitiveFaninForNode(cm, SATnormalNode(t->c2n[i]), CoiMask);
02875     }
02876   }
02877 
02878   if(t->tVariables) {
02879     arr = t->tVariables;
02880     for(i=0; i<arr->num; i++) {
02881       v = arr->space[i];
02882       SATflags(v) |= CoiMask;
02883     }
02884   }
02885 
02886   if(t->auxObj) {
02887     arr = t->auxObj;
02888     for(i=0; i<arr->num; i++) {
02889       v = arr->space[i];
02890       sat_MarkTransitiveFaninForNode(cm, v, CoiMask);
02891     }
02892   }
02893   return(objective);
02894 }
02895 
02907 void
02908 bAigCleanUpDataFromPreviousExecution(bAigTransition_t *t)
02909 {
02910   if(t->frontier) {
02911     sat_ArrayFree(t->frontier);
02912     t->frontier = 0;
02913   }
02914   if(t->reached) {
02915     sat_ArrayFree(t->reached);
02916     t->reached = 0;
02917   }
02918 
02919   t->objective = 0;
02920   t->iteration = 0;
02921   t->nBlocked = 0;
02922   t->sum = 0;
02923   t->avgLits = 0;
02924 
02925 }
02937 void
02938 bAigPrintTransitionInfo(bAigTransition_t *t)
02939 {
02940 int i;
02941 
02942   fprintf(vis_stdout, "Transition relation information in terms of AIG\n");
02943   fprintf(vis_stdout, "objective : %ld\n", t->objective);
02944   fprintf(vis_stdout, "number of primary inputs : %d\n", t->nInputs);
02945   fprintf(vis_stdout, "number of states variables : %d\n", t->nLatches);
02946 
02947   fprintf(vis_stdout, "primary inputs :");
02948   for(i=0; i<t->nInputs; i++) {
02949     fprintf(vis_stdout, "%5ld ", t->inputs[i]);
02950     if((i+1)%10 == 0 && i > 0) 
02951       fprintf(vis_stdout, "\n                ");
02952   }
02953   fprintf(vis_stdout, "\n");
02954 
02955   fprintf(vis_stdout, "state variables :");
02956   for(i=0; i<t->nLatches; i++) {
02957     fprintf(vis_stdout, "%5ld(%5ld):%5ld ", 
02958             t->cstates[i], t->initials[i], t->nstates[i]);
02959     if((i+1)%3 == 0 && i > 0) 
02960       fprintf(vis_stdout, "\n                 ");
02961   }
02962   fprintf(vis_stdout, "\n");
02963 }
02964 
02976 static int
02977 nodenameCompare(
02978   const void * node1,
02979   const void * node2)
02980 {
02981 Ntk_Node_t *v1, *v2;
02982 char *name1, *name2;
02983 
02984   v1 = *(Ntk_Node_t **)(node1);
02985   v2 = *(Ntk_Node_t **)(node2);
02986   name1 = Ntk_NodeReadName(v1);
02987   name2 = Ntk_NodeReadName(v2);
02988   
02989   return (strcmp(name1, name2));
02990 } 
02991 
03003 static int
03004 StringCheckIsInteger(
03005   char *string,
03006   int *value)
03007 {
03008   char *ptr;
03009   long l;
03010   
03011   l = strtol (string, &ptr, 0) ;
03012   if(*ptr != '\0')
03013     return 0;
03014   if ((l > MAXINT) || (l < -1 - MAXINT))
03015     return 1 ;
03016   *value = (int) l;
03017   return 2 ;
03018 }
03019 
03031 static int
03032 levelCompare(
03033   const void * node1,
03034   const void * node2)
03035 {
03036   bAigEdge_t v1, v2;
03037   int l1, l2;
03038 
03039   v1 = *(bAigEdge_t *)(node1);
03040   v2 = *(bAigEdge_t *)(node2);
03041   l1 = SATcm->variableArray[SATnodeID(v1)].level;
03042   l2 = SATcm->variableArray[SATnodeID(v2)].level;
03043   
03044   if(l1 == l2)  return(v1 > v2);
03045   return (l1 > l2);
03046 } 
03058 static int
03059 indexCompare(
03060   const void * node1,
03061   const void * node2)
03062 {
03063   bAigEdge_t v1, v2;
03064 
03065   v1 = *(bAigEdge_t *)(node1);
03066   v2 = *(bAigEdge_t *)(node2);
03067   
03068   return(v1 > v2);
03069 } 
03070