satSolver2.h 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**************************************************************************************************
MiniSat -- Copyright (c) 2005, Niklas Sorensson
http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko

22 23
#ifndef ABC__sat__bsat__satSolver2_h
#define ABC__sat__bsat__satSolver2_h
24
 
25 26 27 28 29 30 31

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "satVec.h"
32
#include "satClause.h"
33
#include "misc/vec/vecSet.h"
34
#include "satProof2.h"
35 36 37

ABC_NAMESPACE_HEADER_START

38
//#define USE_FLOAT_ACTIVITY2
39 40 41 42 43 44

//=================================================================================================
// Public interface:

struct sat_solver2_t;
typedef struct sat_solver2_t sat_solver2;
45
typedef struct Int2_Man_t_ Int2_Man_t;
46 47

extern sat_solver2* sat_solver2_new(void);
48
extern void         sat_solver2_delete(sat_solver2* s);
49

50
extern int          sat_solver2_addclause(sat_solver2* s, lit* begin, lit* end, int Id);
51 52
extern int          sat_solver2_simplify(sat_solver2* s);
extern int          sat_solver2_solve(sat_solver2* s, lit* begin, lit* end, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, ABC_INT64_T nConfLimitGlobal, ABC_INT64_T nInsLimitGlobal);
53
extern void         sat_solver2_rollback(sat_solver2* s);
54
extern void         sat_solver2_reducedb(sat_solver2* s);
55
extern double       sat_solver2_memory( sat_solver2* s, int fAll );
56
extern double       sat_solver2_memory_proof( sat_solver2* s );
57

58
extern void         sat_solver2_setnvars(sat_solver2* s,int n);
59

60 61 62 63
extern void         Sat_Solver2WriteDimacs( sat_solver2 * p, char * pFileName, lit* assumptionsBegin, lit* assumptionsEnd, int incrementVars );
extern void         Sat_Solver2PrintStats( FILE * pFile, sat_solver2 * p );
extern int *        Sat_Solver2GetModel( sat_solver2 * p, int * pVars, int nVars );
extern void         Sat_Solver2DoubleClauses( sat_solver2 * p, int iVar );
64

65
// global variables
Alan Mishchenko committed
66 67 68
extern int          var_is_assigned(sat_solver2* s, int v);
extern int          var_is_partA   (sat_solver2* s, int v);
extern void         var_set_partA  (sat_solver2* s, int v, int partA);
69

70 71 72
// proof-based APIs
extern void *       Sat_ProofCore( sat_solver2 * s );
extern void *       Sat_ProofInterpolant( sat_solver2 * s, void * pGloVars );
73
extern word *       Sat_ProofInterpolantTruth( sat_solver2 * s, void * pGloVars );
74
extern void         Sat_ProofCheck( sat_solver2 * s );
75

76 77 78 79 80 81 82 83
// interpolation APIs
extern Int2_Man_t * Int2_ManStart( sat_solver2 * pSat, int * pGloVars, int nGloVars );
extern void         Int2_ManStop( Int2_Man_t * p );
extern int          Int2_ManChainStart( Int2_Man_t * p, clause * c );
extern int          Int2_ManChainResolve( Int2_Man_t * p, clause * c, int iLit, int varA );
extern void *       Int2_ManReadInterpolant( sat_solver2 * s );


84 85 86
//=================================================================================================
// Solver representation:

87
struct varinfo_t;
88
typedef struct varinfo2_t varinfo2;
89

90 91
struct sat_solver2_t
{
92 93 94 95 96 97 98 99
    int             size;           // nof variables
    int             cap;            // size of varmaps
    int             qhead;          // Head index of queue.
    int             qtail;          // Tail index of queue.

    int             root_level;     // Level of first proper decision.
    double          random_seed;
    double          progress_estimate;
100
    int             verbosity;      // Verbosity level. 0=silent, 1=some progress report, 2=everything    // activities
101

102
#ifdef USE_FLOAT_ACTIVITY2
103 104 105 106 107
    double          var_inc;        // Amount to bump next variable with.
    double          var_decay;      // INVERSE decay factor for variable activity: stores 1/decay. 
    float           cla_inc;        // Amount to bump next clause with.
    float           cla_decay;      // INVERSE decay factor for clause activity: stores 1/decay.
    double*         activity;       // A heuristic measurement of the activity of a variable.
108
#else
109
    int             var_inc;        // Amount to bump next variable with.
110
    int             var_inc2;       // Amount to bump next variable with.
111
    int             cla_inc;        // Amount to bump next clause with.
112 113
    unsigned*       activity;       // A heuristic measurement of the activity of a variable
    unsigned*       activity2;      // backup variable activity
114
#endif
115

116 117
    int             nUnits;         // the total number of unit clauses
    int             nof_learnts;    // the number of clauses to trigger reduceDB
118
    int             nLearntMax;     // enables using reduce DB method
119 120 121 122
    int             nLearntStart;   // starting learned clause limit
    int             nLearntDelta;   // delta of learned clause limit
    int             nLearntRatio;   // ratio percentage of learned clauses
    int             nDBreduces;     // number of DB reductions
123
    int             fNotUseRandom;  // do not allow random decisions with a fixed probability
124
    int             fSkipSimplify;  // set to one to skip simplification of the clause database
125
    int             fProofLogging;  // enable proof-logging
126
    int             fVerbose;
127 128

    // clauses
129
    Sat_Mem_t       Mem;
130
    veci*           wlists;         // watcher lists (for each literal)
131
    veci            act_clas;       // clause activities
132
    veci            claProofs;      // clause proofs
133

134 135 136 137
    // rollback
    int             iVarPivot;      // the pivot for variables
    int             iTrailPivot;    // the pivot for trail
    int             hProofPivot;    // the pivot for proof records
138 139

    // internal state
140
    varinfo2 *      vi;             // variable information
141 142
    int*            levels;         // 
    char*           assigns;        // 
143 144 145 146
    lit*            trail;          // sequence of assignment and implications
    int*            orderpos;       // Index in variable order.
    cla*            reasons;        // reason clauses
    cla*            units;          // unit clauses
147
    int*            model;          // If problem is solved, this vector contains the model (contains: lbool).
148 149 150 151 152 153 154 155 156 157 158

    veci            tagged;         // (contains: var)
    veci            stack;          // (contains: var)
    veci            order;          // Variable order. (heap) (contains: var)
    veci            trail_lim;      // Separator indices for different decision levels in 'trail'. (contains: int)
    veci            temp_clause;    // temporary storage for a CNF clause
    veci            conf_final;     // If problem is unsatisfiable (possibly under assumptions),
                                    // this vector represent the final conflict clause expressed in the assumptions.
    veci            mark_levels;    // temporary storage for labeled levels
    veci            min_lit_order;  // ordering of removable literals
    veci            min_step_order; // ordering of resolution steps
159
    veci            learnt_live;    // remaining clauses after reduce DB
160 161

    // proof logging
162
    Vec_Set_t *     pPrf1;          // sequence of proof records
163
    veci            temp_proof;     // temporary place to store proofs
164 165
    int             hLearntLast;    // in proof-logging mode, the ID of the final conflict clause (conf_final)
    int             hProofLast;     // in proof-logging mode, the ID of the final conflict clause (conf_final)
166 167
    Prf_Man_t *     pPrf2;          // another proof manager
    double          dPrfMemory;     // memory used by the proof-logger
168 169
    Int2_Man_t *    pInt2;          // interpolation manager
    int             tempInter;      // temporary storage for the interpolant
170

171
    // statistics
172 173 174
    stats_t         stats;
    ABC_INT64_T     nConfLimit;     // external limit on the number of conflicts
    ABC_INT64_T     nInsLimit;      // external limit on the number of implications
175
    abctime         nRuntimeLimit;  // external limit on runtime
176
};
177

178
static inline clause * clause2_read( sat_solver2 * s, cla h )                  { return Sat_MemClauseHand( &s->Mem, h ); }
179
static inline int      clause2_proofid(sat_solver2* s, clause* c, int varA)    { return c->lrn ? (veci_begin(&s->claProofs)[clause_id(c)]<<2) | (varA<<1) : (clause_id(c)<<2) | (varA<<1) | 1; }
180

181
// these two only work after creating a clause before the solver is called
182 183 184 185
static inline int   clause2_is_partA (sat_solver2* s, int h)                   { return clause2_read(s, h)->partA;       }
static inline void  clause2_set_partA(sat_solver2* s, int h, int partA)        { clause2_read(s, h)->partA = partA;      }
static inline int   clause2_id(sat_solver2* s, int h)                          { return clause_id(clause2_read(s, h));   }
static inline void  clause2_set_id(sat_solver2* s, int h, int id)              { clause_set_id(clause2_read(s, h), id);  }
186 187 188 189

//=================================================================================================
// Public APIs:

190 191 192 193 194 195 196 197 198 199
static inline int sat_solver2_nvars(sat_solver2* s)
{
    return s->size;
}

static inline int sat_solver2_nclauses(sat_solver2* s)
{
    return (int)s->stats.clauses;
}

200 201 202 203 204
static inline int sat_solver2_nlearnts(sat_solver2* s)
{
    return (int)s->stats.learnts;
}

205 206 207 208 209 210
static inline int sat_solver2_nconflicts(sat_solver2* s)
{
    return (int)s->stats.conflicts;
}

static inline int sat_solver2_var_value( sat_solver2* s, int v ) 
211
{
212 213
    assert( v >= 0 && v < s->size );
    return (int)(s->model[v] == l_True);
214
}
215
static inline int sat_solver2_var_literal( sat_solver2* s, int v ) 
216
{
217 218
    assert( v >= 0 && v < s->size );
    return toLitCond( v, s->model[v] != l_True );
219
}
220
static inline void sat_solver2_act_var_clear(sat_solver2* s) 
221 222 223 224 225 226 227
{
    int i;
    for (i = 0; i < s->size; i++)
        s->activity[i] = 0;//.0;
    s->var_inc = 1.0;
}

228
static inline int sat_solver2_final(sat_solver2* s, int ** ppArray)
229 230 231 232 233
{
    *ppArray = s->conf_final.ptr;
    return s->conf_final.size;
}

234
static inline abctime sat_solver2_set_runtime_limit(sat_solver2* s, abctime Limit)
235
{
236
    abctime temp = s->nRuntimeLimit;
237
    s->nRuntimeLimit = Limit;
238
    return temp;
239 240
}

241 242 243 244 245
static inline int sat_solver2_set_learntmax(sat_solver2* s, int nLearntMax)
{
    int temp = s->nLearntMax;
    s->nLearntMax = nLearntMax;
    return temp;
246 247
}

248
static inline void sat_solver2_bookmark(sat_solver2* s)
249
{
250
    assert( s->qhead == s->qtail );
251
    s->iVarPivot    = s->size;
252
    s->iTrailPivot  = s->qhead;
253 254
    if ( s->pPrf1 )
        s->hProofPivot  = Vec_SetHandCurrent(s->pPrf1);
255
    Sat_MemBookMark( &s->Mem );
256
    if ( s->activity2 )
257 258
    {
        s->var_inc2 = s->var_inc;
259
        memcpy( s->activity2, s->activity, sizeof(unsigned) * s->iVarPivot );
260
    }
261 262
}

263
static inline int sat_solver2_add_const( sat_solver2 * pSat, int iVar, int fCompl, int fMark, int Id )
264 265 266 267 268 269
{
    lit Lits[1];
    int Cid;
    assert( iVar >= 0 );

    Lits[0] = toLitCond( iVar, fCompl );
270
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 1, Id );
271
    if ( fMark )
272
        clause2_set_partA( pSat, Cid, 1 );
273 274
    return 1;
}
275
static inline int sat_solver2_add_buffer( sat_solver2 * pSat, int iVarA, int iVarB, int fCompl, int fMark, int Id )
276 277 278 279 280 281 282
{
    lit Lits[2];
    int Cid;
    assert( iVarA >= 0 && iVarB >= 0 );

    Lits[0] = toLitCond( iVarA, 0 );
    Lits[1] = toLitCond( iVarB, !fCompl );
283
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 2, Id );
284
    if ( fMark )
285
        clause2_set_partA( pSat, Cid, 1 );
286 287 288

    Lits[0] = toLitCond( iVarA, 1 );
    Lits[1] = toLitCond( iVarB, fCompl );
289
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 2, Id );
290
    if ( fMark )
291
        clause2_set_partA( pSat, Cid, 1 );
292 293
    return 2;
}
294
static inline int sat_solver2_add_and( sat_solver2 * pSat, int iVar, int iVar0, int iVar1, int fCompl0, int fCompl1, int fMark, int Id )
295 296 297 298 299 300
{
    lit Lits[3];
    int Cid;

    Lits[0] = toLitCond( iVar, 1 );
    Lits[1] = toLitCond( iVar0, fCompl0 );
301
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 2, Id );
302
    if ( fMark )
303
        clause2_set_partA( pSat, Cid, 1 );
304 305 306

    Lits[0] = toLitCond( iVar, 1 );
    Lits[1] = toLitCond( iVar1, fCompl1 );
307
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 2, Id );
308
    if ( fMark )
309
        clause2_set_partA( pSat, Cid, 1 );
310 311 312 313

    Lits[0] = toLitCond( iVar, 0 );
    Lits[1] = toLitCond( iVar0, !fCompl0 );
    Lits[2] = toLitCond( iVar1, !fCompl1 );
314
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 3, Id );
315
    if ( fMark )
316
        clause2_set_partA( pSat, Cid, 1 );
317 318
    return 3;
}
319
static inline int sat_solver2_add_xor( sat_solver2 * pSat, int iVarA, int iVarB, int iVarC, int fCompl, int fMark, int Id )
320 321 322 323 324 325 326 327
{
    lit Lits[3];
    int Cid;
    assert( iVarA >= 0 && iVarB >= 0 && iVarC >= 0 );

    Lits[0] = toLitCond( iVarA, !fCompl );
    Lits[1] = toLitCond( iVarB, 1 );
    Lits[2] = toLitCond( iVarC, 1 );
328
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 3, Id );
329
    if ( fMark )
330
        clause2_set_partA( pSat, Cid, 1 );
331 332 333 334

    Lits[0] = toLitCond( iVarA, !fCompl );
    Lits[1] = toLitCond( iVarB, 0 );
    Lits[2] = toLitCond( iVarC, 0 );
335
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 3, Id );
336
    if ( fMark )
337
        clause2_set_partA( pSat, Cid, 1 );
338 339 340 341

    Lits[0] = toLitCond( iVarA, fCompl );
    Lits[1] = toLitCond( iVarB, 1 );
    Lits[2] = toLitCond( iVarC, 0 );
342
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 3, Id );
343
    if ( fMark )
344
        clause2_set_partA( pSat, Cid, 1 );
345 346 347 348

    Lits[0] = toLitCond( iVarA, fCompl );
    Lits[1] = toLitCond( iVarB, 0 );
    Lits[2] = toLitCond( iVarC, 1 );
349
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 3, Id );
350
    if ( fMark )
351
        clause2_set_partA( pSat, Cid, 1 );
352 353
    return 4;
}
354
static inline int sat_solver2_add_constraint( sat_solver2 * pSat, int iVar, int iVar2, int fCompl, int fMark, int Id )
355 356 357 358 359 360
{
    lit Lits[2];
    int Cid;
    assert( iVar >= 0 );

    Lits[0] = toLitCond( iVar, fCompl );
361
    Lits[1] = toLitCond( iVar2, 0 );
362
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 2, Id );
363
    if ( fMark )
364
        clause2_set_partA( pSat, Cid, 1 );
365 366

    Lits[0] = toLitCond( iVar, fCompl );
367
    Lits[1] = toLitCond( iVar2, 1 );
368
    Cid = sat_solver2_addclause( pSat, Lits, Lits + 2, Id );
369
    if ( fMark )
370
        clause2_set_partA( pSat, Cid, 1 );
371 372 373 374
    return 2;
}


375 376 377
ABC_NAMESPACE_HEADER_END

#endif