00001
00021 #include "msatInt.h"
00022
00026
00027 static void Msat_SolverUndoOne( Msat_Solver_t * p );
00028 static void Msat_SolverCancel( Msat_Solver_t * p );
00029 static Msat_Clause_t * Msat_SolverRecord( Msat_Solver_t * p, Msat_IntVec_t * vLits );
00030 static void Msat_SolverAnalyze( Msat_Solver_t * p, Msat_Clause_t * pC, Msat_IntVec_t * vLits_out, int * pLevel_out );
00031 static void Msat_SolverReduceDB( Msat_Solver_t * p );
00032
00036
00048 bool Msat_SolverAssume( Msat_Solver_t * p, Msat_Lit_t Lit )
00049 {
00050 assert( Msat_QueueReadSize(p->pQueue) == 0 );
00051 if ( p->fVerbose )
00052 printf(L_IND"assume("L_LIT")\n", L_ind, L_lit(Lit));
00053 Msat_IntVecPush( p->vTrailLim, Msat_IntVecReadSize(p->vTrail) );
00054
00055
00056 return Msat_SolverEnqueue( p, Lit, NULL );
00057 }
00058
00070 void Msat_SolverUndoOne( Msat_Solver_t * p )
00071 {
00072 Msat_Lit_t Lit;
00073 Msat_Var_t Var;
00074 Lit = Msat_IntVecPop( p->vTrail );
00075 Var = MSAT_LIT2VAR(Lit);
00076 p->pAssigns[Var] = MSAT_VAR_UNASSIGNED;
00077 p->pReasons[Var] = NULL;
00078 p->pLevel[Var] = -1;
00079
00080 Msat_OrderVarUnassigned( p->pOrder, Var );
00081
00082 if ( p->fVerbose )
00083 printf(L_IND"unbind("L_LIT")\n", L_ind, L_lit(Lit));
00084 }
00085
00097 void Msat_SolverCancel( Msat_Solver_t * p )
00098 {
00099 int c;
00100 assert( Msat_QueueReadSize(p->pQueue) == 0 );
00101 if ( p->fVerbose )
00102 {
00103 if ( Msat_IntVecReadSize(p->vTrail) != Msat_IntVecReadEntryLast(p->vTrailLim) )
00104 {
00105 Msat_Lit_t Lit;
00106 Lit = Msat_IntVecReadEntry( p->vTrail, Msat_IntVecReadEntryLast(p->vTrailLim) );
00107 printf(L_IND"cancel("L_LIT")\n", L_ind, L_lit(Lit));
00108 }
00109 }
00110 for ( c = Msat_IntVecReadSize(p->vTrail) - Msat_IntVecPop( p->vTrailLim ); c != 0; c-- )
00111 Msat_SolverUndoOne( p );
00112 }
00113
00125 void Msat_SolverCancelUntil( Msat_Solver_t * p, int Level )
00126 {
00127 while ( Msat_IntVecReadSize(p->vTrailLim) > Level )
00128 Msat_SolverCancel(p);
00129 }
00130
00131
00143 Msat_Clause_t * Msat_SolverRecord( Msat_Solver_t * p, Msat_IntVec_t * vLits )
00144 {
00145 Msat_Clause_t * pC;
00146 int Value;
00147 assert( Msat_IntVecReadSize(vLits) != 0 );
00148 Value = Msat_ClauseCreate( p, vLits, 1, &pC );
00149 assert( Value );
00150 Value = Msat_SolverEnqueue( p, Msat_IntVecReadEntry(vLits,0), pC );
00151 assert( Value );
00152 if ( pC )
00153 Msat_ClauseVecPush( p->vLearned, pC );
00154 return pC;
00155 }
00156
00170 bool Msat_SolverEnqueue( Msat_Solver_t * p, Msat_Lit_t Lit, Msat_Clause_t * pC )
00171 {
00172 Msat_Var_t Var = MSAT_LIT2VAR(Lit);
00173
00174
00175 if ( !Msat_IntVecReadEntry( p->vVarsUsed, Var ) )
00176 return 1;
00177
00178
00179
00180
00181
00182 if ( p->pAssigns[Var] != MSAT_VAR_UNASSIGNED )
00183 return p->pAssigns[Var] == Lit;
00184
00185 if ( p->fVerbose )
00186 {
00187
00188 printf(L_IND"bind("L_LIT") ", L_ind, L_lit(Lit));
00189 Msat_ClausePrintSymbols( pC );
00190 }
00191 p->pAssigns[Var] = Lit;
00192 p->pLevel[Var] = Msat_IntVecReadSize(p->vTrailLim);
00193
00194 p->pReasons[Var] = pC;
00195 Msat_IntVecPush( p->vTrail, Lit );
00196 Msat_QueueInsert( p->pQueue, Lit );
00197
00198 Msat_OrderVarAssigned( p->pOrder, Var );
00199 return 1;
00200 }
00201
00214 Msat_Clause_t * Msat_SolverPropagate( Msat_Solver_t * p )
00215 {
00216 Msat_ClauseVec_t ** pvWatched = p->pvWatched;
00217 Msat_Clause_t ** pClauses;
00218 Msat_Clause_t * pConflict;
00219 Msat_Lit_t Lit, Lit_out;
00220 int i, j, nClauses;
00221
00222
00223 while ( (Lit = Msat_QueueExtract( p->pQueue )) >= 0 )
00224 {
00225 p->Stats.nPropagations++;
00226
00227 nClauses = Msat_ClauseVecReadSize( pvWatched[Lit] );
00228 pClauses = Msat_ClauseVecReadArray( pvWatched[Lit] );
00229
00230 for ( i = j = 0; i < nClauses; i++ )
00231 {
00232 p->Stats.nInspects++;
00233
00234 Lit_out = -1;
00235
00236 if ( !Msat_ClausePropagate( pClauses[i], Lit, p->pAssigns, &Lit_out ) )
00237 {
00238
00239 if ( Msat_SolverEnqueue( p, Lit_out, pClauses[i] ) )
00240 {
00241
00242 pClauses[j++] = pClauses[i];
00243 continue;
00244 }
00245
00246 pConflict = pClauses[i];
00247
00248 for ( ; i < nClauses; i++ )
00249 pClauses[j++] = pClauses[i];
00250 Msat_ClauseVecShrink( pvWatched[Lit], j );
00251
00252 Msat_QueueClear( p->pQueue );
00253 return pConflict;
00254 }
00255
00256
00257 if ( Lit_out >= 0 )
00258 Msat_ClauseVecPush( pvWatched[Lit_out], pClauses[i] );
00259 else
00260 pClauses[j++] = pClauses[i];
00261 }
00262 Msat_ClauseVecShrink( pvWatched[Lit], j );
00263 }
00264 return NULL;
00265 }
00266
00279 bool Msat_SolverSimplifyDB( Msat_Solver_t * p )
00280 {
00281 Msat_ClauseVec_t * vClauses;
00282 Msat_Clause_t ** pClauses;
00283 int nClauses, Type, i, j;
00284 int * pAssigns;
00285 int Counter;
00286
00287 assert( Msat_SolverReadDecisionLevel(p) == 0 );
00288 if ( Msat_SolverPropagate(p) != NULL )
00289 return 0;
00290
00291
00292
00293
00294
00295 Counter = 0;
00296 pAssigns = Msat_SolverReadAssignsArray( p );
00297 for ( Type = 0; Type < 2; Type++ )
00298 {
00299 vClauses = Type? p->vLearned : p->vClauses;
00300 nClauses = Msat_ClauseVecReadSize( vClauses );
00301 pClauses = Msat_ClauseVecReadArray( vClauses );
00302 for ( i = j = 0; i < nClauses; i++ )
00303 if ( Msat_ClauseSimplify( pClauses[i], pAssigns ) )
00304 Msat_ClauseFree( p, pClauses[i], 1 );
00305 else
00306 {
00307 pClauses[j++] = pClauses[i];
00308 Msat_ClauseSetNum( pClauses[i], Counter++ );
00309 }
00310 Msat_ClauseVecShrink( vClauses, j );
00311 }
00312 p->nClauses = Counter;
00313 return 1;
00314 }
00315
00329 void Msat_SolverReduceDB( Msat_Solver_t * p )
00330 {
00331 Msat_Clause_t ** pLearned;
00332 int nLearned, i, j;
00333 double dExtraLim = p->dClaInc / Msat_ClauseVecReadSize(p->vLearned);
00334
00335
00336
00337 Msat_SolverSortDB( p );
00338
00339
00340 nLearned = Msat_ClauseVecReadSize( p->vLearned );
00341 pLearned = Msat_ClauseVecReadArray( p->vLearned );
00342 for ( i = j = 0; i < nLearned / 2; i++ )
00343 if ( !Msat_ClauseIsLocked( p, pLearned[i]) )
00344 Msat_ClauseFree( p, pLearned[i], 1 );
00345 else
00346 pLearned[j++] = pLearned[i];
00347
00348 for ( ; i < nLearned; i++ )
00349 if ( !Msat_ClauseIsLocked( p, pLearned[i] ) &&
00350 Msat_ClauseReadActivity(pLearned[i]) < dExtraLim )
00351 Msat_ClauseFree( p, pLearned[i], 1 );
00352 else
00353 pLearned[j++] = pLearned[i];
00354 Msat_ClauseVecShrink( p->vLearned, j );
00355 }
00356
00368 void Msat_SolverRemoveLearned( Msat_Solver_t * p )
00369 {
00370 Msat_Clause_t ** pLearned;
00371 int nLearned, i;
00372
00373
00374 nLearned = Msat_ClauseVecReadSize( p->vLearned );
00375 pLearned = Msat_ClauseVecReadArray( p->vLearned );
00376 for ( i = 0; i < nLearned; i++ )
00377 {
00378 assert( !Msat_ClauseIsLocked( p, pLearned[i]) );
00379
00380 Msat_ClauseFree( p, pLearned[i], 1 );
00381 }
00382 Msat_ClauseVecShrink( p->vLearned, 0 );
00383 p->nClauses = Msat_ClauseVecReadSize(p->vClauses);
00384
00385 for ( i = 0; i < p->nVarsAlloc; i++ )
00386 p->pReasons[i] = NULL;
00387 }
00388
00400 void Msat_SolverRemoveMarked( Msat_Solver_t * p )
00401 {
00402 Msat_Clause_t ** pLearned, ** pClauses;
00403 int nLearned, nClauses, i;
00404
00405
00406 nClauses = Msat_ClauseVecReadSize( p->vClauses );
00407 pClauses = Msat_ClauseVecReadArray( p->vClauses );
00408 for ( i = p->nClausesStart; i < nClauses; i++ )
00409 {
00410
00411 Msat_ClauseFree( p, pClauses[i], 1 );
00412 }
00413 Msat_ClauseVecShrink( p->vClauses, p->nClausesStart );
00414
00415
00416 nLearned = Msat_ClauseVecReadSize( p->vLearned );
00417 pLearned = Msat_ClauseVecReadArray( p->vLearned );
00418 for ( i = 0; i < nLearned; i++ )
00419 {
00420
00421 Msat_ClauseFree( p, pLearned[i], 1 );
00422 }
00423 Msat_ClauseVecShrink( p->vLearned, 0 );
00424 p->nClauses = Msat_ClauseVecReadSize(p->vClauses);
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437 }
00438
00439
00440
00452 void Msat_SolverAnalyze( Msat_Solver_t * p, Msat_Clause_t * pC, Msat_IntVec_t * vLits_out, int * pLevel_out )
00453 {
00454 Msat_Lit_t LitQ, Lit = MSAT_LIT_UNASSIGNED;
00455 Msat_Var_t VarQ, Var;
00456 int * pReasonArray, nReasonSize;
00457 int j, pathC = 0, nLevelCur = Msat_IntVecReadSize(p->vTrailLim);
00458 int iStep = Msat_IntVecReadSize(p->vTrail) - 1;
00459
00460
00461 p->nSeenId++;
00462
00463 Msat_IntVecClear( vLits_out );
00464 Msat_IntVecPush( vLits_out, -1 );
00465 *pLevel_out = 0;
00466 do {
00467 assert( pC != NULL );
00468
00469 Msat_ClauseCalcReason( p, pC, Lit, p->vReason );
00470 nReasonSize = Msat_IntVecReadSize( p->vReason );
00471 pReasonArray = Msat_IntVecReadArray( p->vReason );
00472 for ( j = 0; j < nReasonSize; j++ ) {
00473 LitQ = pReasonArray[j];
00474 VarQ = MSAT_LIT2VAR(LitQ);
00475 if ( p->pSeen[VarQ] != p->nSeenId ) {
00476 p->pSeen[VarQ] = p->nSeenId;
00477
00478
00479 Msat_SolverVarBumpActivity( p, LitQ );
00480
00481
00482 if ( p->pLevel[VarQ] == nLevelCur )
00483 pathC++;
00484 else if ( p->pLevel[VarQ] > 0 ) {
00485
00486
00487 Msat_IntVecPush( vLits_out, MSAT_LITNOT(LitQ) );
00488 if ( *pLevel_out < p->pLevel[VarQ] )
00489 *pLevel_out = p->pLevel[VarQ];
00490 }
00491 }
00492 }
00493
00494 do {
00495
00496 Lit = Msat_IntVecReadEntry( p->vTrail, iStep-- );
00497 Var = MSAT_LIT2VAR(Lit);
00498 pC = p->pReasons[Var];
00499
00500 } while ( p->pSeen[Var] != p->nSeenId );
00501 pathC--;
00502 } while ( pathC > 0 );
00503
00504
00505
00506 Msat_IntVecWriteEntry( vLits_out, 0, MSAT_LITNOT(Lit) );
00507 if ( p->fVerbose )
00508 {
00509 printf( L_IND"Learnt {", L_ind );
00510 nReasonSize = Msat_IntVecReadSize( vLits_out );
00511 pReasonArray = Msat_IntVecReadArray( vLits_out );
00512 for ( j = 0; j < nReasonSize; j++ )
00513 printf(" "L_LIT, L_lit(pReasonArray[j]));
00514 printf(" } at level %d\n", *pLevel_out);
00515 }
00516 }
00517
00532 Msat_Type_t Msat_SolverSearch( Msat_Solver_t * p, int nConfLimit, int nLearnedLimit, int nBackTrackLimit, Msat_SearchParams_t * pPars )
00533 {
00534 Msat_Clause_t * pConf;
00535 Msat_Var_t Var;
00536 int nLevelBack, nConfs, nAssigns, Value;
00537 int i;
00538
00539 assert( Msat_SolverReadDecisionLevel(p) == p->nLevelRoot );
00540 p->Stats.nStarts++;
00541 p->dVarDecay = 1 / pPars->dVarDecay;
00542 p->dClaDecay = 1 / pPars->dClaDecay;
00543
00544
00545 for ( i = 0; i < p->nVars; i++ )
00546 p->pdActivity[i] = (double)p->pFactors[i];
00547
00548
00549 nConfs = 0;
00550 while ( 1 )
00551 {
00552 pConf = Msat_SolverPropagate( p );
00553 if ( pConf != NULL ){
00554
00555 if ( p->fVerbose )
00556 {
00557
00558 printf(L_IND"**CONFLICT** ", L_ind);
00559 Msat_ClausePrintSymbols( pConf );
00560 }
00561
00562 p->Stats.nConflicts++;
00563 nConfs++;
00564
00565
00566 if ( Msat_SolverReadDecisionLevel(p) == p->nLevelRoot )
00567 return MSAT_FALSE;
00568
00569
00570 Msat_SolverAnalyze( p, pConf, p->vTemp, &nLevelBack );
00571 Msat_SolverCancelUntil( p, (p->nLevelRoot > nLevelBack)? p->nLevelRoot : nLevelBack );
00572 Msat_SolverRecord( p, p->vTemp );
00573
00574
00575
00576 Msat_SolverVarDecayActivity( p );
00577 Msat_SolverClaDecayActivity( p );
00578
00579 }
00580 else{
00581
00582 if ( Msat_IntVecReadSize(p->vTrailLim) == 0 ) {
00583
00584
00585
00586 }
00587 nAssigns = Msat_IntVecReadSize( p->vTrail );
00588 if ( nLearnedLimit >= 0 && Msat_ClauseVecReadSize(p->vLearned) >= nLearnedLimit + nAssigns ) {
00589
00590 Msat_SolverReduceDB(p);
00591 }
00592
00593 Var = Msat_OrderVarSelect( p->pOrder );
00594 if ( Var == MSAT_ORDER_UNKNOWN ) {
00595
00596 memcpy( p->pModel, p->pAssigns, sizeof(int) * p->nVars );
00597 Msat_QueueClear( p->pQueue );
00598 Msat_SolverCancelUntil( p, p->nLevelRoot );
00599 return MSAT_TRUE;
00600 }
00601 if ( nConfLimit > 0 && nConfs > nConfLimit ) {
00602
00603 p->dProgress = Msat_SolverProgressEstimate( p );
00604 Msat_QueueClear( p->pQueue );
00605 Msat_SolverCancelUntil( p, p->nLevelRoot );
00606 return MSAT_UNKNOWN;
00607 }
00608 else if ( nBackTrackLimit > 0 && (int)p->Stats.nConflicts - p->nBackTracks > nBackTrackLimit ) {
00609
00610 Msat_QueueClear( p->pQueue );
00611 Msat_SolverCancelUntil( p, p->nLevelRoot );
00612 return MSAT_UNKNOWN;
00613 }
00614 else{
00615
00616 p->Stats.nDecisions++;
00617 assert( Var != MSAT_ORDER_UNKNOWN && Var >= 0 && Var < p->nVars );
00618 Value = Msat_SolverAssume(p, MSAT_VAR2LIT(Var,0) );
00619 assert( Value );
00620 }
00621 }
00622 }
00623 }
00624
00628
00629