00001
00021 #include "cnf.h"
00022 #include "satSolver.h"
00023
00027
00028 static inline int Cnf_Lit2Var( int Lit ) { return (Lit & 1)? -(Lit >> 1)-1 : (Lit >> 1)+1; }
00029 static inline int Cnf_Lit2Var2( int Lit ) { return (Lit & 1)? -(Lit >> 1) : (Lit >> 1); }
00030
00034
00046 Cnf_Man_t * Cnf_ManStart()
00047 {
00048 Cnf_Man_t * p;
00049 int i;
00050
00051 p = ALLOC( Cnf_Man_t, 1 );
00052 memset( p, 0, sizeof(Cnf_Man_t) );
00053
00054 Cnf_ReadMsops( &p->pSopSizes, &p->pSops );
00055
00056 p->pMemCuts = Aig_MmFlexStart();
00057 p->nMergeLimit = 10;
00058
00059 p->pTruths[0] = ALLOC( unsigned, 4 * Aig_TruthWordNum(p->nMergeLimit) );
00060 for ( i = 1; i < 4; i++ )
00061 p->pTruths[i] = p->pTruths[i-1] + Aig_TruthWordNum(p->nMergeLimit);
00062 p->vMemory = Vec_IntAlloc( 1 << 18 );
00063 return p;
00064 }
00065
00077 void Cnf_ManStop( Cnf_Man_t * p )
00078 {
00079 Vec_IntFree( p->vMemory );
00080 free( p->pTruths[0] );
00081 Aig_MmFlexStop( p->pMemCuts, 0 );
00082 free( p->pSopSizes );
00083 free( p->pSops[1] );
00084 free( p->pSops );
00085 free( p );
00086 }
00087
00099 Vec_Int_t * Cnf_DataCollectPiSatNums( Cnf_Dat_t * pCnf, Aig_Man_t * p )
00100 {
00101 Vec_Int_t * vCiIds;
00102 Aig_Obj_t * pObj;
00103 int i;
00104 vCiIds = Vec_IntAlloc( Aig_ManPiNum(p) );
00105 Aig_ManForEachPi( p, pObj, i )
00106 Vec_IntPush( vCiIds, pCnf->pVarNums[pObj->Id] );
00107 return vCiIds;
00108 }
00109
00121 void Cnf_DataFree( Cnf_Dat_t * p )
00122 {
00123 if ( p == NULL )
00124 return;
00125 free( p->pClauses[0] );
00126 free( p->pClauses );
00127 free( p->pVarNums );
00128 free( p );
00129 }
00130
00142 void Cnf_DataWriteIntoFile( Cnf_Dat_t * p, char * pFileName, int fReadable )
00143 {
00144 FILE * pFile;
00145 int * pLit, * pStop, i;
00146 pFile = fopen( pFileName, "w" );
00147 if ( pFile == NULL )
00148 {
00149 printf( "Cnf_WriteIntoFile(): Output file cannot be opened.\n" );
00150 return;
00151 }
00152 fprintf( pFile, "c Result of efficient AIG-to-CNF conversion using package CNF\n" );
00153 fprintf( pFile, "p %d %d\n", p->nVars, p->nClauses );
00154 for ( i = 0; i < p->nClauses; i++ )
00155 {
00156 for ( pLit = p->pClauses[i], pStop = p->pClauses[i+1]; pLit < pStop; pLit++ )
00157 fprintf( pFile, "%d ", fReadable? Cnf_Lit2Var2(*pLit) : Cnf_Lit2Var(*pLit) );
00158 fprintf( pFile, "0\n" );
00159 }
00160 fprintf( pFile, "\n" );
00161 fclose( pFile );
00162 }
00163
00175 void * Cnf_DataWriteIntoSolver( Cnf_Dat_t * p )
00176 {
00177 sat_solver * pSat;
00178 int i, status;
00179 pSat = sat_solver_new();
00180 sat_solver_setnvars( pSat, p->nVars );
00181 for ( i = 0; i < p->nClauses; i++ )
00182 {
00183 if ( !sat_solver_addclause( pSat, p->pClauses[i], p->pClauses[i+1] ) )
00184 {
00185 sat_solver_delete( pSat );
00186 return NULL;
00187 }
00188 }
00189 status = sat_solver_simplify(pSat);
00190 if ( status == 0 )
00191 {
00192 sat_solver_delete( pSat );
00193 return NULL;
00194 }
00195 return pSat;
00196 }
00197
00201
00202