satUtil.c 11.3 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**CFile****************************************************************

  FileName    [satUtil.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [C-language MiniSat solver.]

  Synopsis    [Additional SAT solver procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: satUtil.c,v 1.4 2005/09/16 22:55:03 casem Exp $]

***********************************************************************/

#include <stdio.h>
#include <assert.h>
#include "satSolver.h"
24
#include "satSolver2.h"
Alan Mishchenko committed
25

26 27 28
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
29 30 31 32
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

33
static void Sat_SolverClauseWriteDimacs( FILE * pFile, clause * pC, int fIncrement );
Alan Mishchenko committed
34 35 36 37 38 39 40

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  Synopsis    [Writes the given clause in a file in DIMACS format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_SolverClauseWriteDimacs( FILE * pFile, clause * pC, int fIncrement )
{
    int i;
    for ( i = 0; i < (int)pC->size; i++ )
        fprintf( pFile, "%s%d ", (lit_sign(pC->lits[i])? "-": ""),  lit_var(pC->lits[i]) + (fIncrement>0) );
    if ( fIncrement )
        fprintf( pFile, "0" );
    fprintf( pFile, "\n" );
}

/**Function*************************************************************

Alan Mishchenko committed
62 63
  Synopsis    [Write the clauses in the solver into a file in DIMACS format.]

64 65 66 67
  Description [This procedure by default does not print binary clauses. To enable 
  printing of binary clauses, please set fUseBinaryClauses to 0 in the body of 
  procedure sat_solver_clause_new(sat_solver* s, lit* begin, lit* end, int learnt)
  in file "satSolver.c".]
Alan Mishchenko committed
68 69 70 71 72 73
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
74
void Sat_SolverWriteDimacs( sat_solver * p, char * pFileName, lit* assumpBegin, lit* assumpEnd, int incrementVars )
Alan Mishchenko committed
75
{
76
    Sat_Mem_t * pMem = &p->Mem;
Alan Mishchenko committed
77
    FILE * pFile;
78
    clause * c;
79 80 81 82 83
    int i, k, nUnits;

    // count the number of unit clauses
    nUnits = 0;
    for ( i = 0; i < p->size; i++ )
84
        if ( p->levels[i] == 0 && p->assigns[i] != 3 )
85
            nUnits++;
86 87

    // start the file
88
    pFile = pFileName ? fopen( pFileName, "wb" ) : stdout;
89 90 91 92 93 94
    if ( pFile == NULL )
    {
        printf( "Sat_SolverWriteDimacs(): Cannot open the ouput file.\n" );
        return;
    }
//    fprintf( pFile, "c CNF generated by ABC on %s\n", Extra_TimeStamp() );
95
    fprintf( pFile, "p cnf %d %d\n", p->size, Sat_MemEntryNum(&p->Mem, 0)-1+Sat_MemEntryNum(&p->Mem, 1)+nUnits+(int)(assumpEnd-assumpBegin) );
96 97 98 99 100 101

    // write the original clauses
    Sat_MemForEachClause( pMem, c, i, k )
        Sat_SolverClauseWriteDimacs( pFile, c, incrementVars );

    // write the learned clauses
102 103
//    Sat_MemForEachLearned( pMem, c, i, k )
//        Sat_SolverClauseWriteDimacs( pFile, c, incrementVars );
Alan Mishchenko committed
104

105
    // write zero-level assertions
Alan Mishchenko committed
106
    for ( i = 0; i < p->size; i++ )
107
        if ( p->levels[i] == 0 && p->assigns[i] != 3 ) // varX
108
            fprintf( pFile, "%s%d%s\n",
109
                     (p->assigns[i] == 1)? "-": "",    // var0
110 111 112
                     i + (int)(incrementVars>0),
                     (incrementVars) ? " 0" : "");

113 114 115
    // write the assump
    if (assumpBegin) {
        for (; assumpBegin != assumpEnd; assumpBegin++) {
116
            fprintf( pFile, "%s%d%s\n",
117 118
                     lit_sign(*assumpBegin)? "-": "",
                     lit_var(*assumpBegin) + (int)(incrementVars>0),
119 120 121 122 123
                     (incrementVars) ? " 0" : "");
        }
    }

    fprintf( pFile, "\n" );
124
    if ( pFileName ) fclose( pFile );
125
} 
126
void Sat_Solver2WriteDimacs( sat_solver2 * p, char * pFileName, lit* assumpBegin, lit* assumpEnd, int incrementVars )
127 128 129 130
{
    Sat_Mem_t * pMem = &p->Mem;
    FILE * pFile;
    clause * c;
131 132 133 134 135
    int i, k, nUnits;

    // count the number of unit clauses
    nUnits = 0;
    for ( i = 0; i < p->size; i++ )
136
        if ( p->levels[i] == 0 && p->assigns[i] != 3 )
137
            nUnits++;
Alan Mishchenko committed
138 139 140 141 142 143 144 145

    // start the file
    pFile = fopen( pFileName, "wb" );
    if ( pFile == NULL )
    {
        printf( "Sat_SolverWriteDimacs(): Cannot open the ouput file.\n" );
        return;
    }
Alan Mishchenko committed
146
//    fprintf( pFile, "c CNF generated by ABC on %s\n", Extra_TimeStamp() );
147
    fprintf( pFile, "p cnf %d %d\n", p->size, Sat_MemEntryNum(&p->Mem, 0)-1+Sat_MemEntryNum(&p->Mem, 1)+nUnits+(int)(assumpEnd-assumpBegin) );
Alan Mishchenko committed
148 149

    // write the original clauses
150 151
    Sat_MemForEachClause2( pMem, c, i, k )
        Sat_SolverClauseWriteDimacs( pFile, c, incrementVars );
Alan Mishchenko committed
152 153

    // write the learned clauses
154 155
//    Sat_MemForEachLearned( pMem, c, i, k )
//        Sat_SolverClauseWriteDimacs( pFile, c, incrementVars );
Alan Mishchenko committed
156 157 158

    // write zero-level assertions
    for ( i = 0; i < p->size; i++ )
159
        if ( p->levels[i] == 0 && p->assigns[i] != 3 ) // varX
Alan Mishchenko committed
160
            fprintf( pFile, "%s%d%s\n",
161
                     (p->assigns[i] == 1)? "-": "",    // var0
Alan Mishchenko committed
162 163 164
                     i + (int)(incrementVars>0),
                     (incrementVars) ? " 0" : "");

165 166 167
    // write the assump
    if (assumpBegin) {
        for (; assumpBegin != assumpEnd; assumpBegin++) {
Alan Mishchenko committed
168
            fprintf( pFile, "%s%d%s\n",
169 170
                     lit_sign(*assumpBegin)? "-": "",
                     lit_var(*assumpBegin) + (int)(incrementVars>0),
Alan Mishchenko committed
171 172 173 174 175 176 177
                     (incrementVars) ? " 0" : "");
        }
    }

    fprintf( pFile, "\n" );
    fclose( pFile );
}   
178
  
Alan Mishchenko committed
179 180 181 182 183 184 185 186 187 188 189 190

/**Function*************************************************************

  Synopsis    [Writes the given clause in a file in DIMACS format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
191 192
static inline double Sat_Wrd2Dbl( word w )  { return (double)(unsigned)(w&0x3FFFFFFF) + (double)(1<<30)*(unsigned)(w>>30); }

Alan Mishchenko committed
193 194
void Sat_SolverPrintStats( FILE * pFile, sat_solver * p )
{
195 196 197 198
    printf( "starts        : %16.0f\n", Sat_Wrd2Dbl(p->stats.starts) );
    printf( "conflicts     : %16.0f\n", Sat_Wrd2Dbl(p->stats.conflicts) );
    printf( "decisions     : %16.0f\n", Sat_Wrd2Dbl(p->stats.decisions) );
    printf( "propagations  : %16.0f\n", Sat_Wrd2Dbl(p->stats.propagations) );
199
//    printf( "inspects      : %10d\n", (int)p->stats.inspects );
200 201 202 203 204 205 206 207 208 209 210 211 212 213
//    printf( "inspects2     : %10d\n", (int)p->stats.inspects2 );
}

/**Function*************************************************************

  Synopsis    [Writes the given clause in a file in DIMACS format.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
214
void Sat_Solver2PrintStats( FILE * pFile, sat_solver2 * s )
215
{
216 217 218 219 220 221
    printf( "starts        : %10d\n", (int)s->stats.starts );
    printf( "conflicts     : %10d\n", (int)s->stats.conflicts );
    printf( "decisions     : %10d\n", (int)s->stats.decisions );
    printf( "propagations  : %10d\n", (int)s->stats.propagations );
//    printf( "inspects      : %10d\n", (int)s->stats.inspects );
//    printf( "inspects2     : %10d\n", (int)s->stats.inspects2 );
222
/*
223
    printf( "memory for variables %.1f MB (free %6.2f %%) and clauses %.1f MB (free %6.2f %%)\n", 
224 225 226 227 228 229
        1.0 * Sat_Solver2GetVarMem(s) * s->size / (1<<20),
        100.0 * (s->cap - s->size) / s->cap,
        4.0 * (s->clauses.cap + s->learnts.cap) / (1<<20),
        100.0 * (s->clauses.cap - s->clauses.size + 
                 s->learnts.cap - s->learnts.size) / 
                (s->clauses.cap + s->learnts.cap) );
230
*/
Alan Mishchenko committed
231 232
}

Alan Mishchenko committed
233 234
/**Function*************************************************************

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  Synopsis    [Returns the number of bytes used for each variable.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sat_Solver2GetVarMem( sat_solver2 * s )
{
    int Mem = 0;
    Mem += (sizeof(s->activity[0]) == 4) ? sizeof(unsigned) : sizeof(double);  // activity
    Mem += 2 * sizeof(veci); // wlists
    Mem += sizeof(int);      // vi (variable info)
    Mem += sizeof(int);      // trail
    Mem += sizeof(int);      // orderpos
    Mem += sizeof(int);      // reasons
    Mem += sizeof(int);      // units
    Mem += sizeof(int);      // order
    Mem += sizeof(int);      // model
    return Mem;
}

/**Function*************************************************************

Alan Mishchenko committed
261 262 263 264 265 266 267 268 269 270 271 272 273
  Synopsis    [Returns a counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Sat_SolverGetModel( sat_solver * p, int * pVars, int nVars )
{
    int * pModel;
    int i;
274
    pModel = ABC_CALLOC( int, nVars+1 );
Alan Mishchenko committed
275
    for ( i = 0; i < nVars; i++ )
276
        pModel[i] = sat_solver_var_value(p, pVars[i]);
Alan Mishchenko committed
277 278 279 280 281
    return pModel;    
}

/**Function*************************************************************

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  Synopsis    [Returns a counter-example.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int * Sat_Solver2GetModel( sat_solver2 * p, int * pVars, int nVars )
{
    int * pModel;
    int i;
    pModel = ABC_CALLOC( int, nVars+1 );
    for ( i = 0; i < nVars; i++ )
297
        pModel[i] = sat_solver2_var_value(p, pVars[i]);
298 299 300 301 302
    return pModel;    
}

/**Function*************************************************************

Alan Mishchenko committed
303 304 305 306 307 308 309 310 311 312 313
  Synopsis    [Duplicates all clauses, complements unit clause of the given var.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_SolverDoubleClauses( sat_solver * p, int iVar )
{
314
    assert( 0 );
315
/*
Alan Mishchenko committed
316
    clause * pClause;
Alan Mishchenko committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    lit Lit, * pLits;
    int RetValue, nClauses, nVarsOld, nLitsOld, nLits, c, v;
    // get the number of variables
    nVarsOld = p->size;
    nLitsOld = 2 * p->size;
    // extend the solver to depend on two sets of variables
    sat_solver_setnvars( p, 2 * p->size );
    // duplicate implications
    for ( v = 0; v < nVarsOld; v++ )
        if ( p->assigns[v] != l_Undef )
        {
            Lit = nLitsOld + toLitCond( v, p->assigns[v]==l_False );
            if ( v == iVar )
                Lit = lit_neg(Lit);
            RetValue = sat_solver_addclause( p, &Lit, &Lit + 1 );
            assert( RetValue );
        }
    // duplicate clauses
    nClauses = vecp_size(&p->clauses);
    for ( c = 0; c < nClauses; c++ )
    {
338
        pClause = (clause *)p->clauses.ptr[c];
Alan Mishchenko committed
339 340
        nLits = clause_size(pClause);
        pLits = clause_begin(pClause);
Alan Mishchenko committed
341 342 343 344 345 346 347
        for ( v = 0; v < nLits; v++ )
            pLits[v] += nLitsOld;
        RetValue = sat_solver_addclause( p, pLits, pLits + nLits );
        assert( RetValue );
        for ( v = 0; v < nLits; v++ )
            pLits[v] -= nLitsOld;
    }
348
*/
Alan Mishchenko committed
349 350
}

Alan Mishchenko committed
351 352 353 354 355 356

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


357 358
ABC_NAMESPACE_IMPL_END