00001
00021 #include <stdio.h>
00022 #include <assert.h>
00023 #include "satSolver.h"
00024 #include "extra.h"
00025
00029
00030 struct clause_t
00031 {
00032 int size_learnt;
00033 lit lits[0];
00034 };
00035
00036 static inline int clause_size( clause* c ) { return c->size_learnt >> 1; }
00037 static inline lit* clause_begin( clause* c ) { return c->lits; }
00038
00039 static void Sat_SolverClauseWriteDimacs( FILE * pFile, clause * pC, bool fIncrement );
00040
00044
00056 void Sat_SolverWriteDimacs( sat_solver * p, char * pFileName, lit* assumptionsBegin, lit* assumptionsEnd, int incrementVars )
00057 {
00058 FILE * pFile;
00059 void ** pClauses;
00060 int nClauses, i;
00061
00062
00063 nClauses = p->clauses.size + p->learnts.size;
00064 for ( i = 0; i < p->size; i++ )
00065 if ( p->levels[i] == 0 && p->assigns[i] != l_Undef )
00066 nClauses++;
00067
00068
00069 pFile = fopen( pFileName, "wb" );
00070 if ( pFile == NULL )
00071 {
00072 printf( "Sat_SolverWriteDimacs(): Cannot open the ouput file.\n" );
00073 return;
00074 }
00075 fprintf( pFile, "c CNF generated by ABC on %s\n", Extra_TimeStamp() );
00076 fprintf( pFile, "p cnf %d %d\n", p->size, nClauses );
00077
00078
00079 nClauses = p->clauses.size;
00080 pClauses = p->clauses.ptr;
00081 for ( i = 0; i < nClauses; i++ )
00082 Sat_SolverClauseWriteDimacs( pFile, pClauses[i], incrementVars );
00083
00084
00085 nClauses = p->learnts.size;
00086 pClauses = p->learnts.ptr;
00087 for ( i = 0; i < nClauses; i++ )
00088 Sat_SolverClauseWriteDimacs( pFile, pClauses[i], incrementVars );
00089
00090
00091 for ( i = 0; i < p->size; i++ )
00092 if ( p->levels[i] == 0 && p->assigns[i] != l_Undef )
00093 fprintf( pFile, "%s%d%s\n",
00094 (p->assigns[i] == l_False)? "-": "",
00095 i + (int)(incrementVars>0),
00096 (incrementVars) ? " 0" : "");
00097
00098
00099 if (assumptionsBegin) {
00100 for (; assumptionsBegin != assumptionsEnd; assumptionsBegin++) {
00101 fprintf( pFile, "%s%d%s\n",
00102 lit_sign(*assumptionsBegin)? "-": "",
00103 lit_var(*assumptionsBegin) + (int)(incrementVars>0),
00104 (incrementVars) ? " 0" : "");
00105 }
00106 }
00107
00108 fprintf( pFile, "\n" );
00109 fclose( pFile );
00110 }
00111
00123 void Sat_SolverClauseWriteDimacs( FILE * pFile, clause * pC, bool fIncrement )
00124 {
00125 lit * pLits = clause_begin(pC);
00126 int nLits = clause_size(pC);
00127 int i;
00128
00129 for ( i = 0; i < nLits; i++ )
00130 fprintf( pFile, "%s%d ", (lit_sign(pLits[i])? "-": ""), lit_var(pLits[i]) + (int)(fIncrement>0) );
00131 if ( fIncrement )
00132 fprintf( pFile, "0" );
00133 fprintf( pFile, "\n" );
00134 }
00135
00147 void Sat_SolverPrintStats( FILE * pFile, sat_solver * p )
00148 {
00149
00150 printf( "starts : %8d\n", (int)p->stats.starts );
00151 printf( "conflicts : %8d\n", (int)p->stats.conflicts );
00152 printf( "decisions : %8d\n", (int)p->stats.decisions );
00153 printf( "propagations : %8d\n", (int)p->stats.propagations );
00154 printf( "inspects : %8d\n", (int)p->stats.inspects );
00155
00156 }
00157
00169 int * Sat_SolverGetModel( sat_solver * p, int * pVars, int nVars )
00170 {
00171 int * pModel;
00172 int i;
00173 pModel = ALLOC( int, nVars );
00174 for ( i = 0; i < nVars; i++ )
00175 {
00176 assert( pVars[i] >= 0 && pVars[i] < p->size );
00177 pModel[i] = (int)(p->model.ptr[pVars[i]] == l_True);
00178 }
00179 return pModel;
00180 }
00181
00193 void Sat_SolverDoubleClauses( sat_solver * p, int iVar )
00194 {
00195 clause * pClause;
00196 lit Lit, * pLits;
00197 int RetValue, nClauses, nVarsOld, nLitsOld, nLits, c, v;
00198
00199 nVarsOld = p->size;
00200 nLitsOld = 2 * p->size;
00201
00202 sat_solver_setnvars( p, 2 * p->size );
00203
00204 for ( v = 0; v < nVarsOld; v++ )
00205 if ( p->assigns[v] != l_Undef )
00206 {
00207 Lit = nLitsOld + toLitCond( v, p->assigns[v]==l_False );
00208 if ( v == iVar )
00209 Lit = lit_neg(Lit);
00210 RetValue = sat_solver_addclause( p, &Lit, &Lit + 1 );
00211 assert( RetValue );
00212 }
00213
00214 nClauses = vecp_size(&p->clauses);
00215 for ( c = 0; c < nClauses; c++ )
00216 {
00217 pClause = p->clauses.ptr[c];
00218 nLits = clause_size(pClause);
00219 pLits = clause_begin(pClause);
00220 for ( v = 0; v < nLits; v++ )
00221 pLits[v] += nLitsOld;
00222 RetValue = sat_solver_addclause( p, pLits, pLits + nLits );
00223 assert( RetValue );
00224 for ( v = 0; v < nLits; v++ )
00225 pLits[v] -= nLitsOld;
00226 }
00227 }
00228
00229
00233
00234