satInterA.c 34.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 24
/**CFile****************************************************************

  FileName    [satInter.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT sat_solver.]

  Synopsis    [Interpolation package.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

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

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

Alan Mishchenko committed
26
#include "satStore.h"
27
#include "aig/aig/aig.h"
Alan Mishchenko committed
28

29 30 31
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

// variable assignments 
static const lit    LIT_UNDEF = 0xffffffff;

// interpolation manager
struct Inta_Man_t_
{
    // clauses of the problems
    Sto_Man_t *     pCnf;         // the set of CNF clauses for A and B
    Vec_Int_t *     vVarsAB;      // the array of global variables
    // various parameters
    int             fVerbose;     // verbosiness flag
    int             fProofVerif;  // verifies the proof
    int             fProofWrite;  // writes the proof file
    int             nVarsAlloc;   // the allocated size of var arrays
    int             nClosAlloc;   // the allocated size of clause arrays
    // internal BCP
    int             nRootSize;    // the number of root level assignments
    int             nTrailSize;   // the number of assignments made 
    lit *           pTrail;       // chronological order of assignments (size nVars)
    lit *           pAssigns;     // assignments by variable (size nVars) 
    char *          pSeens;       // temporary mark (size nVars)
    Sto_Cls_t **    pReasons;     // reasons for each assignment (size nVars)          
    Sto_Cls_t **    pWatches;     // watched clauses for each literal (size 2*nVars)          
    // interpolation data
    Aig_Man_t *     pAig;         // the AIG manager for recording the interpolant
    int *           pVarTypes;    // variable type (size nVars) [1=A, 0=B, <0=AB]
    Aig_Obj_t **    pInters;      // storage for interpolants as truth tables (size nClauses)
    int             nIntersAlloc; // the allocated size of truth table array
    // proof recording
    int             Counter;      // counter of resolved clauses
    int *           pProofNums;   // the proof numbers for each clause (size nClauses)
    FILE *          pFile;        // the file for proof recording
    // internal verification
Alan Mishchenko committed
69
    Vec_Int_t *     vResLits;
Alan Mishchenko committed
70
    // runtime stats
71 72 73
    abctime         timeBcp;      // the runtime for BCP
    abctime         timeTrace;    // the runtime of trace construction
    abctime         timeTotal;    // the total runtime of interpolation
Alan Mishchenko committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
};

// procedure to get hold of the clauses' truth table 
static inline Aig_Obj_t ** Inta_ManAigRead( Inta_Man_t * pMan, Sto_Cls_t * pCls )                   { return pMan->pInters + pCls->Id;          }
static inline void         Inta_ManAigClear( Inta_Man_t * pMan, Aig_Obj_t ** p )                    { *p = Aig_ManConst0(pMan->pAig);           }
static inline void         Inta_ManAigFill( Inta_Man_t * pMan, Aig_Obj_t ** p )                     { *p = Aig_ManConst1(pMan->pAig);           }
static inline void         Inta_ManAigCopy( Inta_Man_t * pMan, Aig_Obj_t ** p, Aig_Obj_t ** q )     { *p = *q;                                  }
static inline void         Inta_ManAigAnd( Inta_Man_t * pMan, Aig_Obj_t ** p, Aig_Obj_t ** q )      { *p = Aig_And(pMan->pAig, *p, *q);         }
static inline void         Inta_ManAigOr( Inta_Man_t * pMan, Aig_Obj_t ** p, Aig_Obj_t ** q )       { *p = Aig_Or(pMan->pAig, *p, *q);          }
static inline void         Inta_ManAigOrNot( Inta_Man_t * pMan, Aig_Obj_t ** p, Aig_Obj_t ** q )    { *p = Aig_Or(pMan->pAig, *p, Aig_Not(*q)); }
static inline void         Inta_ManAigOrVar( Inta_Man_t * pMan, Aig_Obj_t ** p, int v )             { *p = Aig_Or(pMan->pAig, *p, Aig_IthVar(pMan->pAig, v));          }
static inline void         Inta_ManAigOrNotVar( Inta_Man_t * pMan, Aig_Obj_t ** p, int v )          { *p = Aig_Or(pMan->pAig, *p, Aig_Not(Aig_IthVar(pMan->pAig, v))); }

// reading/writing the proof for a clause
static inline int          Inta_ManProofGet( Inta_Man_t * p, Sto_Cls_t * pCls )                  { return p->pProofNums[pCls->Id];           }
static inline void         Inta_ManProofSet( Inta_Man_t * p, Sto_Cls_t * pCls, int n )           { p->pProofNums[pCls->Id] = n;              }

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

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

  Synopsis    [Allocate proof manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Inta_Man_t * Inta_ManAlloc()
{
    Inta_Man_t * p;
    // allocate the manager
Alan Mishchenko committed
110
    p = (Inta_Man_t *)ABC_ALLOC( char, sizeof(Inta_Man_t) );
Alan Mishchenko committed
111 112
    memset( p, 0, sizeof(Inta_Man_t) );
    // verification
Alan Mishchenko committed
113
    p->vResLits = Vec_IntAlloc( 1000 );
Alan Mishchenko committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    // parameters
    p->fProofWrite = 0;
    p->fProofVerif = 1;
    return p;    
}

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

  Synopsis    [Count common variables in the clauses of A and B.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Inta_ManGlobalVars( Inta_Man_t * p )
{
    Sto_Cls_t * pClause;
    int LargeNum = -100000000;
    int Var, nVarsAB, v;

    // mark the variable encountered in the clauses of A
    Sto_ManForEachClauseRoot( p->pCnf, pClause )
    {
        if ( !pClause->fA )
            break;
        for ( v = 0; v < (int)pClause->nLits; v++ )
            p->pVarTypes[lit_var(pClause->pLits[v])] = 1;
    }

    // check variables that appear in clauses of B
    nVarsAB = 0;
    Sto_ManForEachClauseRoot( p->pCnf, pClause )
    {
        if ( pClause->fA )
            continue;
        for ( v = 0; v < (int)pClause->nLits; v++ )
        {
            Var = lit_var(pClause->pLits[v]);
            if ( p->pVarTypes[Var] == 1 ) // var of A
            {
                // change it into a global variable
                nVarsAB++;
                p->pVarTypes[Var] = LargeNum;
            }
        }
    }
    assert( nVarsAB <= Vec_IntSize(p->vVarsAB) );

    // order global variables
    nVarsAB = 0;
    Vec_IntForEachEntry( p->vVarsAB, Var, v )
        p->pVarTypes[Var] = -(1+nVarsAB++);

    // check that there is no extra global variables
    for ( v = 0; v < p->pCnf->nVars; v++ )
        assert( p->pVarTypes[v] != LargeNum );
    return nVarsAB;
}

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

  Synopsis    [Resize proof manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Inta_ManResize( Inta_Man_t * p )
{
189
    p->Counter = 0;
Alan Mishchenko committed
190 191 192 193 194 195 196 197 198
    // check if resizing is needed
    if ( p->nVarsAlloc < p->pCnf->nVars )
    {
        // find the new size
        if ( p->nVarsAlloc == 0 )
            p->nVarsAlloc = 1;
        while ( p->nVarsAlloc < p->pCnf->nVars ) 
            p->nVarsAlloc *= 2;
        // resize the arrays
Alan Mishchenko committed
199 200 201 202 203 204
        p->pTrail    = ABC_REALLOC(lit,         p->pTrail,    p->nVarsAlloc );
        p->pAssigns  = ABC_REALLOC(lit,         p->pAssigns,  p->nVarsAlloc );
        p->pSeens    = ABC_REALLOC(char,        p->pSeens,    p->nVarsAlloc );
        p->pVarTypes = ABC_REALLOC(int,         p->pVarTypes, p->nVarsAlloc );
        p->pReasons  = ABC_REALLOC(Sto_Cls_t *, p->pReasons,  p->nVarsAlloc );
        p->pWatches  = ABC_REALLOC(Sto_Cls_t *, p->pWatches,  p->nVarsAlloc*2 );
Alan Mishchenko committed
205 206
    }

207
    // clean the free space
Alan Mishchenko committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    memset( p->pAssigns , 0xff, sizeof(lit) * p->pCnf->nVars );
    memset( p->pSeens   , 0,    sizeof(char) * p->pCnf->nVars );
    memset( p->pVarTypes, 0,    sizeof(int) * p->pCnf->nVars );
    memset( p->pReasons , 0,    sizeof(Sto_Cls_t *) * p->pCnf->nVars );
    memset( p->pWatches , 0,    sizeof(Sto_Cls_t *) * p->pCnf->nVars*2 );

    // compute the number of common variables
    Inta_ManGlobalVars( p );

    // check if resizing of clauses is needed
    if ( p->nClosAlloc < p->pCnf->nClauses )
    {
        // find the new size
        if ( p->nClosAlloc == 0 )
            p->nClosAlloc = 1;
        while ( p->nClosAlloc < p->pCnf->nClauses ) 
            p->nClosAlloc *= 2;
        // resize the arrays
Alan Mishchenko committed
226
        p->pProofNums = ABC_REALLOC( int, p->pProofNums,  p->nClosAlloc );
Alan Mishchenko committed
227 228 229 230 231 232 233
    }
    memset( p->pProofNums, 0, sizeof(int) * p->pCnf->nClauses );

    // check if resizing of truth tables is needed
    if ( p->nIntersAlloc < p->pCnf->nClauses )
    {
        p->nIntersAlloc = p->pCnf->nClauses;
Alan Mishchenko committed
234
        p->pInters = ABC_REALLOC( Aig_Obj_t *, p->pInters, p->nIntersAlloc );
Alan Mishchenko committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    }
    memset( p->pInters, 0, sizeof(Aig_Obj_t *) * p->pCnf->nClauses );
}

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

  Synopsis    [Deallocate proof manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Inta_ManFree( Inta_Man_t * p )
{
/*
    printf( "Runtime stats:\n" );
Alan Mishchenko committed
254 255 256
ABC_PRT( "BCP     ", p->timeBcp   );
ABC_PRT( "Trace   ", p->timeTrace );
ABC_PRT( "TOTAL   ", p->timeTotal );
Alan Mishchenko committed
257
*/
Alan Mishchenko committed
258 259 260 261 262 263 264 265
    ABC_FREE( p->pInters );
    ABC_FREE( p->pProofNums );
    ABC_FREE( p->pTrail );
    ABC_FREE( p->pAssigns );
    ABC_FREE( p->pSeens );
    ABC_FREE( p->pVarTypes );
    ABC_FREE( p->pReasons );
    ABC_FREE( p->pWatches );
Alan Mishchenko committed
266
    Vec_IntFree( p->vResLits );
Alan Mishchenko committed
267
    ABC_FREE( p );
Alan Mishchenko committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
}




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

  Synopsis    [Prints the clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Inta_ManPrintClause( Inta_Man_t * p, Sto_Cls_t * pClause )
{
    int i;
    printf( "Clause ID = %d. Proof = %d. {", pClause->Id, Inta_ManProofGet(p, pClause) );
    for ( i = 0; i < (int)pClause->nLits; i++ )
        printf( " %d", pClause->pLits[i] );
    printf( " }\n" );
}

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

  Synopsis    [Prints the resolvent.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
304
void Inta_ManPrintResolvent( Vec_Int_t * vResLits )
Alan Mishchenko committed
305
{
Alan Mishchenko committed
306
    int i, Entry;
Alan Mishchenko committed
307
    printf( "Resolvent: {" );
Alan Mishchenko committed
308 309
    Vec_IntForEachEntry( vResLits, Entry, i )
        printf( " %d", Entry );
Alan Mishchenko committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    printf( " }\n" );
}

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

  Synopsis    [Prints the interpolant for one clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Inta_ManPrintInterOne( Inta_Man_t * p, Sto_Cls_t * pClause )
{
    printf( "Clause %2d :  ", pClause->Id );
//    Extra_PrintBinary___( stdout, Inta_ManAigRead(p, pClause), (1 << p->nVarsAB) );
    printf( "\n" );
}



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

  Synopsis    [Adds one clause to the watcher list.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Inta_ManWatchClause( Inta_Man_t * p, Sto_Cls_t * pClause, lit Lit )
{
    assert( lit_check(Lit, p->pCnf->nVars) );
    if ( pClause->pLits[0] == Lit )
        pClause->pNext0 = p->pWatches[lit_neg(Lit)];  
    else
    {
        assert( pClause->pLits[1] == Lit );
        pClause->pNext1 = p->pWatches[lit_neg(Lit)];  
    }
    p->pWatches[lit_neg(Lit)] = pClause;
}


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

  Synopsis    [Records implication.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Inta_ManEnqueue( Inta_Man_t * p, lit Lit, Sto_Cls_t * pReason )
{
    int Var = lit_var(Lit);
    if ( p->pAssigns[Var] != LIT_UNDEF )
        return p->pAssigns[Var] == Lit;
    p->pAssigns[Var] = Lit;
    p->pReasons[Var] = pReason;
    p->pTrail[p->nTrailSize++] = Lit;
//printf( "assigning var %d value %d\n", Var, !lit_sign(Lit) );
    return 1;
}

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

  Synopsis    [Records implication.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Inta_ManCancelUntil( Inta_Man_t * p, int Level )
{
    lit Lit;
    int i, Var;
    for ( i = p->nTrailSize - 1; i >= Level; i-- )
    {
        Lit = p->pTrail[i];
        Var = lit_var( Lit );
        p->pReasons[Var] = NULL;
        p->pAssigns[Var] = LIT_UNDEF;
//printf( "cancelling var %d\n", Var );
    }
    p->nTrailSize = Level;
}

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

  Synopsis    [Propagate one assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Sto_Cls_t * Inta_ManPropagateOne( Inta_Man_t * p, lit Lit )
{
    Sto_Cls_t ** ppPrev, * pCur, * pTemp;
    lit LitF = lit_neg(Lit);
    int i;
    // iterate through the literals
    ppPrev = p->pWatches + Lit;
    for ( pCur = p->pWatches[Lit]; pCur; pCur = *ppPrev )
    {
        // make sure the false literal is in the second literal of the clause
        if ( pCur->pLits[0] == LitF )
        {
            pCur->pLits[0] = pCur->pLits[1];
            pCur->pLits[1] = LitF;
            pTemp = pCur->pNext0;
            pCur->pNext0 = pCur->pNext1;
            pCur->pNext1 = pTemp;
        }
        assert( pCur->pLits[1] == LitF );

        // if the first literal is true, the clause is satisfied
        if ( pCur->pLits[0] == p->pAssigns[lit_var(pCur->pLits[0])] )
        {
            ppPrev = &pCur->pNext1;
            continue;
        }

        // look for a new literal to watch
        for ( i = 2; i < (int)pCur->nLits; i++ )
        {
            // skip the case when the literal is false
            if ( lit_neg(pCur->pLits[i]) == p->pAssigns[lit_var(pCur->pLits[i])] )
                continue;
            // the literal is either true or unassigned - watch it
            pCur->pLits[1] = pCur->pLits[i];
            pCur->pLits[i] = LitF;
            // remove this clause from the watch list of Lit
            *ppPrev = pCur->pNext1;
            // add this clause to the watch list of pCur->pLits[i] (now it is pCur->pLits[1])
            Inta_ManWatchClause( p, pCur, pCur->pLits[1] );
            break;
        }
        if ( i < (int)pCur->nLits ) // found new watch
            continue;

        // clause is unit - enqueue new implication
        if ( Inta_ManEnqueue(p, pCur->pLits[0], pCur) )
        {
            ppPrev = &pCur->pNext1;
            continue;
        }

        // conflict detected - return the conflict clause
        return pCur;
    }
    return NULL;
}

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

  Synopsis    [Propagate the current assignments.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Sto_Cls_t * Inta_ManPropagate( Inta_Man_t * p, int Start )
{
    Sto_Cls_t * pClause;
    int i;
491
    abctime clk = Abc_Clock();
Alan Mishchenko committed
492 493 494 495 496
    for ( i = Start; i < p->nTrailSize; i++ )
    {
        pClause = Inta_ManPropagateOne( p, p->pTrail[i] );
        if ( pClause )
        {
497
p->timeBcp += Abc_Clock() - clk;
Alan Mishchenko committed
498 499 500
            return pClause;
        }
    }
501
p->timeBcp += Abc_Clock() - clk;
Alan Mishchenko committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    return NULL;
}


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

  Synopsis    [Writes one root clause into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Inta_ManProofWriteOne( Inta_Man_t * p, Sto_Cls_t * pClause )
{
    Inta_ManProofSet( p, pClause, ++p->Counter );

    if ( p->fProofWrite )
    {
        int v;
        fprintf( p->pFile, "%d", Inta_ManProofGet(p, pClause) );
        for ( v = 0; v < (int)pClause->nLits; v++ )
            fprintf( p->pFile, " %d", lit_print(pClause->pLits[v]) );
        fprintf( p->pFile, " 0 0\n" );
    }
}

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

  Synopsis    [Traces the proof for one clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Inta_ManProofTraceOne( Inta_Man_t * p, Sto_Cls_t * pConflict, Sto_Cls_t * pFinal )
{
    Sto_Cls_t * pReason;
    int i, v, Var, PrevId;
    int fPrint = 0;
547
    abctime clk = Abc_Clock();
Alan Mishchenko committed
548

Alan Mishchenko committed
549 550 551
    // collect resolvent literals
    if ( p->fProofVerif )
    {
Alan Mishchenko committed
552 553 554
        Vec_IntClear( p->vResLits );
        for ( i = 0; i < (int)pConflict->nLits; i++ )
            Vec_IntPush( p->vResLits, pConflict->pLits[i] );
Alan Mishchenko committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    }

    // mark all the variables in the conflict as seen
    for ( v = 0; v < (int)pConflict->nLits; v++ )
        p->pSeens[lit_var(pConflict->pLits[v])] = 1;

    // start the anticedents
//    pFinal->pAntis = Vec_PtrAlloc( 32 );
//    Vec_PtrPush( pFinal->pAntis, pConflict );

    if ( p->pCnf->nClausesA )
        Inta_ManAigCopy( p, Inta_ManAigRead(p, pFinal), Inta_ManAigRead(p, pConflict) );

    // follow the trail backwards
    PrevId = Inta_ManProofGet(p, pConflict);
    for ( i = p->nTrailSize - 1; i >= 0; i-- )
    {
        // skip literals that are not involved
        Var = lit_var(p->pTrail[i]);
        if ( !p->pSeens[Var] )
            continue;
        p->pSeens[Var] = 0;
Alan Mishchenko committed
577

Alan Mishchenko committed
578 579 580 581 582
        // skip literals of the resulting clause
        pReason = p->pReasons[Var];
        if ( pReason == NULL )
            continue;
        assert( p->pTrail[i] == pReason->pLits[0] );
Alan Mishchenko committed
583

Alan Mishchenko committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
        // add the variables to seen
        for ( v = 1; v < (int)pReason->nLits; v++ )
            p->pSeens[lit_var(pReason->pLits[v])] = 1;

        // record the reason clause
        assert( Inta_ManProofGet(p, pReason) > 0 );
        p->Counter++;
        if ( p->fProofWrite )
            fprintf( p->pFile, "%d * %d %d 0\n", p->Counter, PrevId, Inta_ManProofGet(p, pReason) );
        PrevId = p->Counter;

        if ( p->pCnf->nClausesA )
        {
            if ( p->pVarTypes[Var] == 1 ) // var of A
                Inta_ManAigOr( p, Inta_ManAigRead(p, pFinal), Inta_ManAigRead(p, pReason) );
            else
                Inta_ManAigAnd( p, Inta_ManAigRead(p, pFinal), Inta_ManAigRead(p, pReason) );
        }
 
        // resolve the temporary resolvent with the reason clause
        if ( p->fProofVerif )
        {
Alan Mishchenko committed
606
            int v1, v2, Entry = -1; 
Alan Mishchenko committed
607
            if ( fPrint )
Alan Mishchenko committed
608
                Inta_ManPrintResolvent( p->vResLits );
Alan Mishchenko committed
609
            // check that the var is present in the resolvent
Alan Mishchenko committed
610 611
            Vec_IntForEachEntry( p->vResLits, Entry, v1 )
                if ( lit_var(Entry) == Var )
Alan Mishchenko committed
612
                    break;
Alan Mishchenko committed
613
            if ( v1 == Vec_IntSize(p->vResLits) )
Alan Mishchenko committed
614
                printf( "Recording clause %d: Cannot find variable %d in the temporary resolvent.\n", pFinal->Id, Var );
Alan Mishchenko committed
615
            if ( Entry != lit_neg(pReason->pLits[0]) )
Alan Mishchenko committed
616
                printf( "Recording clause %d: The resolved variable %d is in the wrong polarity.\n", pFinal->Id, Var );
Alan Mishchenko committed
617 618 619
            // remove variable v1 from the resolvent
            assert( lit_var(Entry) == Var );
            Vec_IntRemove( p->vResLits, Entry );
Alan Mishchenko committed
620 621 622
            // add variables of the reason clause
            for ( v2 = 1; v2 < (int)pReason->nLits; v2++ )
            {
Alan Mishchenko committed
623 624
                Vec_IntForEachEntry( p->vResLits, Entry, v1 )
                    if ( lit_var(Entry) == lit_var(pReason->pLits[v2]) )
Alan Mishchenko committed
625 626
                        break;
                // if it is a new variable, add it to the resolvent
Alan Mishchenko committed
627
                if ( v1 == Vec_IntSize(p->vResLits) ) 
Alan Mishchenko committed
628
                {
Alan Mishchenko committed
629
                    Vec_IntPush( p->vResLits, pReason->pLits[v2] );
Alan Mishchenko committed
630 631 632
                    continue;
                }
                // if the variable is the same, the literal should be the same too
Alan Mishchenko committed
633
                if ( Entry == pReason->pLits[v2] )
Alan Mishchenko committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
                    continue;
                // the literal is different
                printf( "Recording clause %d: Trying to resolve the clause with more than one opposite literal.\n", pFinal->Id );
            }
        }
//        Vec_PtrPush( pFinal->pAntis, pReason );
    }

    // unmark all seen variables
//    for ( i = p->nTrailSize - 1; i >= 0; i-- )
//        p->pSeens[lit_var(p->pTrail[i])] = 0;
    // check that the literals are unmarked
//    for ( i = p->nTrailSize - 1; i >= 0; i-- )
//        assert( p->pSeens[lit_var(p->pTrail[i])] == 0 );

    // use the resulting clause to check the correctness of resolution
    if ( p->fProofVerif )
    {
Alan Mishchenko committed
652
        int v1, v2, Entry = -1; 
Alan Mishchenko committed
653
        if ( fPrint )
Alan Mishchenko committed
654 655
            Inta_ManPrintResolvent( p->vResLits );
        Vec_IntForEachEntry( p->vResLits, Entry, v1 )
Alan Mishchenko committed
656 657
        {
            for ( v2 = 0; v2 < (int)pFinal->nLits; v2++ )
Alan Mishchenko committed
658
                if ( pFinal->pLits[v2] == Entry )
Alan Mishchenko committed
659 660 661 662 663
                    break;
            if ( v2 < (int)pFinal->nLits )
                continue;
            break;
        }
Alan Mishchenko committed
664
        if ( v1 < Vec_IntSize(p->vResLits) )
Alan Mishchenko committed
665 666 667
        {
            printf( "Recording clause %d: The final resolvent is wrong.\n", pFinal->Id );
            Inta_ManPrintClause( p, pConflict );
Alan Mishchenko committed
668
            Inta_ManPrintResolvent( p->vResLits );
Alan Mishchenko committed
669 670
            Inta_ManPrintClause( p, pFinal );
        }
Alan Mishchenko committed
671 672 673 674

        // if there are literals in the clause that are not in the resolvent
        // it means that the derived resolvent is stronger than the clause
        // we can replace the clause with the resolvent by removing these literals
Alan Mishchenko committed
675
        if ( Vec_IntSize(p->vResLits) != (int)pFinal->nLits )
Alan Mishchenko committed
676 677 678
        {
            for ( v1 = 0; v1 < (int)pFinal->nLits; v1++ )
            {
Alan Mishchenko committed
679 680
                Vec_IntForEachEntry( p->vResLits, Entry, v2 )
                    if ( pFinal->pLits[v1] == Entry )
Alan Mishchenko committed
681
                        break;
Alan Mishchenko committed
682
                if ( v2 < Vec_IntSize(p->vResLits) )
Alan Mishchenko committed
683 684 685 686 687 688 689
                    continue;
                // remove literal v1 from the final clause
                pFinal->nLits--;
                for ( v2 = v1; v2 < (int)pFinal->nLits; v2++ )
                    pFinal->pLits[v2] = pFinal->pLits[v2+1];
                v1--;
            }
Alan Mishchenko committed
690
            assert( Vec_IntSize(p->vResLits) == (int)pFinal->nLits );
Alan Mishchenko committed
691
        }
Alan Mishchenko committed
692
    }
693
p->timeTrace += Abc_Clock() - clk;
Alan Mishchenko committed
694 695 696 697 698 699 700

    // return the proof pointer 
    if ( p->pCnf->nClausesA )
    {
//        Inta_ManPrintInterOne( p, pFinal );
    }
    Inta_ManProofSet( p, pFinal, p->Counter );
Alan Mishchenko committed
701 702 703 704
    // make sure the same proof ID is not asssigned to two consecutive clauses
    assert( p->pProofNums[pFinal->Id-1] != p->Counter );
//    if ( p->pProofNums[pFinal->Id] == p->pProofNums[pFinal->Id-1] )
//        p->pProofNums[pFinal->Id] = p->pProofNums[pConflict->Id];
Alan Mishchenko committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    return p->Counter;
}

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

  Synopsis    [Records the proof for one clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Inta_ManProofRecordOne( Inta_Man_t * p, Sto_Cls_t * pClause )
{
    Sto_Cls_t * pConflict;
    int i;

    // empty clause never ends up there
    assert( pClause->nLits > 0 );
    if ( pClause->nLits == 0 )
        printf( "Error: Empty clause is attempted.\n" );

    assert( !pClause->fRoot );
    assert( p->nTrailSize == p->nRootSize );
Alan Mishchenko committed
731 732 733 734 735 736 737 738

    // if any of the clause literals are already assumed
    // it means that the clause is redundant and can be skipped
    for ( i = 0; i < (int)pClause->nLits; i++ )
        if ( p->pAssigns[lit_var(pClause->pLits[i])] == pClause->pLits[i] )
            return 1;

    // add assumptions to the trail
Alan Mishchenko committed
739 740 741 742 743 744 745 746 747 748 749 750 751
    for ( i = 0; i < (int)pClause->nLits; i++ )
        if ( !Inta_ManEnqueue( p, lit_neg(pClause->pLits[i]), NULL ) )
        {
            assert( 0 ); // impossible
            return 0;
        }

    // propagate the assumptions
    pConflict = Inta_ManPropagate( p, p->nRootSize );
    if ( pConflict == NULL )
    {
        assert( 0 ); // cannot prove
        return 0;
Alan Mishchenko committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    } 

    // skip the clause if it is weaker or the same as the conflict clause
    if ( pClause->nLits >= pConflict->nLits )
    {
        // check if every literal of conflict clause can be found in the given clause
        int j;
        for ( i = 0; i < (int)pConflict->nLits; i++ )
        {
            for ( j = 0; j < (int)pClause->nLits; j++ )
                if ( pConflict->pLits[i] == pClause->pLits[j] )
                    break;
            if ( j == (int)pClause->nLits ) // literal pConflict->pLits[i] is not found
                break;
        }
        if ( i == (int)pConflict->nLits ) // all lits are found
        {
            // undo to the root level
            Inta_ManCancelUntil( p, p->nRootSize );
            return 1;
        }
Alan Mishchenko committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    }

    // construct the proof
    Inta_ManProofTraceOne( p, pConflict, pClause );

    // undo to the root level
    Inta_ManCancelUntil( p, p->nRootSize );

    // add large clauses to the watched lists
    if ( pClause->nLits > 1 )
    {
        Inta_ManWatchClause( p, pClause, pClause->pLits[0] );
        Inta_ManWatchClause( p, pClause, pClause->pLits[1] );
        return 1;
    }
    assert( pClause->nLits == 1 );

    // if the clause proved is unit, add it and propagate
    if ( !Inta_ManEnqueue( p, pClause->pLits[0], pClause ) )
    {
        assert( 0 ); // impossible
        return 0;
    }

    // propagate the assumption
    pConflict = Inta_ManPropagate( p, p->nRootSize );
    if ( pConflict )
    {
        // construct the proof
        Inta_ManProofTraceOne( p, pConflict, p->pCnf->pEmpty );
//        if ( p->fVerbose )
//            printf( "Found last conflict after adding unit clause number %d!\n", pClause->Id );
        return 0;
    }

    // update the root level
    p->nRootSize = p->nTrailSize;
    return 1;
}

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

  Synopsis    [Propagate the root clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Inta_ManProcessRoots( Inta_Man_t * p )
{
    Sto_Cls_t * pClause;
    int Counter;

    // make sure the root clauses are preceeding the learnt clauses
    Counter = 0;
    Sto_ManForEachClause( p->pCnf, pClause )
    {
        assert( (int)pClause->fA    == (Counter < (int)p->pCnf->nClausesA) );
        assert( (int)pClause->fRoot == (Counter < (int)p->pCnf->nRoots)    );
        Counter++;
    }
    assert( p->pCnf->nClauses == Counter );

    // make sure the last clause if empty
    assert( p->pCnf->pTail->nLits == 0 );

    // go through the root unit clauses
    p->nTrailSize = 0;
    Sto_ManForEachClauseRoot( p->pCnf, pClause )
    {
        // create watcher lists for the root clauses
        if ( pClause->nLits > 1 )
        {
            Inta_ManWatchClause( p, pClause, pClause->pLits[0] );
            Inta_ManWatchClause( p, pClause, pClause->pLits[1] );
        }
        // empty clause and large clauses
        if ( pClause->nLits != 1 )
            continue;
        // unit clause
        assert( lit_check(pClause->pLits[0], p->pCnf->nVars) );
        if ( !Inta_ManEnqueue( p, pClause->pLits[0], pClause ) )
        {
            // detected root level conflict
Alan Mishchenko committed
860 861 862 863 864 865
//            printf( "Error in Inta_ManProcessRoots(): Detected a root-level conflict too early!\n" );
//            assert( 0 );
            // detected root level conflict
            Inta_ManProofTraceOne( p, pClause, p->pCnf->pEmpty );
            if ( p->fVerbose )
                printf( "Found root level conflict!\n" );
Alan Mishchenko committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
            return 0;
        }
    }

    // propagate the root unit clauses
    pClause = Inta_ManPropagate( p, 0 );
    if ( pClause )
    {
        // detected root level conflict
        Inta_ManProofTraceOne( p, pClause, p->pCnf->pEmpty );
        if ( p->fVerbose )
            printf( "Found root level conflict!\n" );
        return 0;
    }

    // set the root level
    p->nRootSize = p->nTrailSize;
    return 1;
}

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

  Synopsis    [Records the proof.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Inta_ManPrepareInter( Inta_Man_t * p )
{
    Sto_Cls_t * pClause;
    int Var, VarAB, v;

    // set interpolants for root clauses
    Sto_ManForEachClauseRoot( p->pCnf, pClause )
    {
        if ( !pClause->fA ) // clause of B
        {
            Inta_ManAigFill( p, Inta_ManAigRead(p, pClause) );
//            Inta_ManPrintInterOne( p, pClause );
            continue;
        }
        // clause of A
        Inta_ManAigClear( p, Inta_ManAigRead(p, pClause) );
        for ( v = 0; v < (int)pClause->nLits; v++ )
        {
            Var = lit_var(pClause->pLits[v]);
            if ( p->pVarTypes[Var] < 0 ) // global var
            {
                VarAB = -p->pVarTypes[Var]-1;
                assert( VarAB >= 0 && VarAB < Vec_IntSize(p->vVarsAB) );
                if ( lit_sign(pClause->pLits[v]) ) // negative var
                    Inta_ManAigOrNotVar( p, Inta_ManAigRead(p, pClause), VarAB );
                else
                    Inta_ManAigOrVar( p, Inta_ManAigRead(p, pClause), VarAB );
            }
        }
//        Inta_ManPrintInterOne( p, pClause );
    }
}

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

  Synopsis    [Computes interpolant for the given CNF.]

  Description [Takes the interpolation manager, the CNF deriving by the SAT
  solver, which includes ClausesA, ClausesB, and learned clauses. Additional
  arguments are the vector of variables common to AB and the verbosiness flag.
  Returns the AIG manager with a single output, containing the interpolant.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
944
void * Inta_ManInterpolate( Inta_Man_t * p, Sto_Man_t * pCnf, abctime TimeToStop, void * vVarsAB, int fVerbose )
Alan Mishchenko committed
945 946
{
    Aig_Man_t * pRes;
Alan Mishchenko committed
947
    Aig_Obj_t * pObj;
Alan Mishchenko committed
948 949
    Sto_Cls_t * pClause;
    int RetValue = 1;
950
    abctime clkTotal = Abc_Clock();
Alan Mishchenko committed
951

952
    if ( TimeToStop && Abc_Clock() > TimeToStop )
953 954
        return NULL;

Alan Mishchenko committed
955 956 957 958
    // check that the CNF makes sense
    assert( pCnf->nVars > 0 && pCnf->nClauses > 0 );
    p->pCnf = pCnf;
    p->fVerbose = fVerbose;
959
    p->vVarsAB = (Vec_Int_t *)vVarsAB;
Alan Mishchenko committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
    p->pAig = pRes = Aig_ManStart( 10000 );
    Aig_IthVar( p->pAig, Vec_IntSize(p->vVarsAB) - 1 );

    // adjust the manager
    Inta_ManResize( p );

    // prepare the interpolant computation
    Inta_ManPrepareInter( p );

    // construct proof for each clause
    // start the proof
    if ( p->fProofWrite )
    {
        p->pFile = fopen( "proof.cnf_", "w" );
        p->Counter = 0;
    }

    // write the root clauses
    Sto_ManForEachClauseRoot( p->pCnf, pClause )
979
    {
Alan Mishchenko committed
980
        Inta_ManProofWriteOne( p, pClause );
981 982 983 984 985 986 987
        if ( TimeToStop && Abc_Clock() > TimeToStop )
        {
            Aig_ManStop( pRes );
            p->pAig = NULL;
            return NULL;
        }
    }
Alan Mishchenko committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

    // propagate root level assignments
    if ( Inta_ManProcessRoots( p ) )
    {
        // if there is no conflict, consider learned clauses
        Sto_ManForEachClause( p->pCnf, pClause )
        {
            if ( pClause->fRoot )
                continue;
            if ( !Inta_ManProofRecordOne( p, pClause ) )
            {
                RetValue = 0;
                break;
            }
1002 1003 1004 1005 1006 1007
            if ( TimeToStop && Abc_Clock() > TimeToStop )
            {
                Aig_ManStop( pRes );
                p->pAig = NULL;
                return NULL;
            }
Alan Mishchenko committed
1008 1009 1010 1011 1012
        }
    }

    // stop the proof
    if ( p->fProofWrite )
Alan Mishchenko committed
1013
    { 
Alan Mishchenko committed
1014
        fclose( p->pFile );
Alan Mishchenko committed
1015
//        Sat_ProofChecker( "proof.cnf_" );
Alan Mishchenko committed
1016 1017 1018 1019 1020
        p->pFile = NULL;    
    }

    if ( fVerbose )
    {
1021
//        ABC_PRT( "Interpo", Abc_Clock() - clkTotal );
1022
    printf( "Vars = %d. Roots = %d. Learned = %d. Resol steps = %d.  Ave = %.2f.  Mem = %.2f MB\n", 
Alan Mishchenko committed
1023 1024 1025
        p->pCnf->nVars, p->pCnf->nRoots, p->pCnf->nClauses-p->pCnf->nRoots, p->Counter,  
        1.0*(p->Counter-p->pCnf->nRoots)/(p->pCnf->nClauses-p->pCnf->nRoots), 
        1.0*Sto_ManMemoryReport(p->pCnf)/(1<<20) );
1026
p->timeTotal += Abc_Clock() - clkTotal;
Alan Mishchenko committed
1027 1028
    }

Alan Mishchenko committed
1029
    pObj = *Inta_ManAigRead( p, p->pCnf->pTail );
1030
    Aig_ObjCreateCo( pRes, pObj );
Alan Mishchenko committed
1031 1032 1033 1034 1035 1036 1037
    Aig_ManCleanup( pRes );

    p->pAig = NULL;
    return pRes;
    
}

Alan Mishchenko committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Inta_ManDeriveClauses( Inta_Man_t * pMan, Sto_Man_t * pCnf, int fClausesA )
{
    Aig_Man_t * p;
    Aig_Obj_t * pMiter, * pSum, * pLit;
    Sto_Cls_t * pClause;
    int Var, VarAB, v;
    p = Aig_ManStart( 10000 );
    pMiter = Aig_ManConst1(p);
    Sto_ManForEachClauseRoot( pCnf, pClause )
    {
        if ( fClausesA ^ pClause->fA ) // clause of B
            continue;
        // clause of A
        pSum = Aig_ManConst0(p);
        for ( v = 0; v < (int)pClause->nLits; v++ )
        {
            Var = lit_var(pClause->pLits[v]);
            if ( pMan->pVarTypes[Var] < 0 ) // global var
            {
                VarAB = -pMan->pVarTypes[Var]-1;
                assert( VarAB >= 0 && VarAB < Vec_IntSize(pMan->vVarsAB) );
                pLit = Aig_NotCond( Aig_IthVar(p, VarAB), lit_sign(pClause->pLits[v]) );
            }
            else
                pLit = Aig_NotCond( Aig_IthVar(p, Vec_IntSize(pMan->vVarsAB)+1+Var), lit_sign(pClause->pLits[v]) );
            pSum = Aig_Or( p, pSum, pLit );
        }
        pMiter = Aig_And( p, pMiter, pSum );
    }
1079
    Aig_ObjCreateCo( p, pMiter );
Alan Mishchenko committed
1080 1081 1082
    return p;
}

Alan Mishchenko committed
1083 1084 1085 1086 1087
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1088 1089
ABC_NAMESPACE_IMPL_END