bmcEnum.c 7.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 69 70 71 72 73 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 110 111 112 113 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
/**CFile****************************************************************

  FileName    [bmcEnum.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT-based bounded model checking.]

  Synopsis    [Enumeration.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

  Revision    [$Id: bmcEnum.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

#include "bmc.h"
#include "sat/cnf/cnf.h"
#include "sat/bsat/satSolver.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDeriveOne( Gia_Man_t * p, Vec_Int_t * vValues )
{
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj; int i, fPhase0, fPhase1;
    assert( Vec_IntSize(vValues) == Gia_ManCiNum(p) );
    // propagate forward
    Gia_ManForEachCi( p, pObj, i )
        pObj->fPhase = Vec_IntEntry(vValues, i);
    Gia_ManForEachAnd( p, pObj, i )
    {
        fPhase0 = Gia_ObjPhase(Gia_ObjFanin0(pObj)) ^ Gia_ObjFaninC0(pObj);
        fPhase1 = Gia_ObjPhase(Gia_ObjFanin1(pObj)) ^ Gia_ObjFaninC1(pObj);
        pObj->fPhase = fPhase0 & fPhase1;
    }
    // propagate backward
    Gia_ManCleanMark0(p);
    Gia_ManForEachCo( p, pObj, i )
        Gia_ObjFanin0(pObj)->fMark0 = 1;
    Gia_ManForEachAndReverse( p, pObj, i )
    {
        if ( !pObj->fMark0 )
            continue;
        fPhase0 = Gia_ObjPhase(Gia_ObjFanin0(pObj)) ^ Gia_ObjFaninC0(pObj);
        fPhase1 = Gia_ObjPhase(Gia_ObjFanin1(pObj)) ^ Gia_ObjFaninC1(pObj);
        if ( fPhase0 == fPhase1 )
        {
            assert( (int)pObj->fPhase == fPhase0 );
            Gia_ObjFanin0(pObj)->fMark0 = 1;
            Gia_ObjFanin1(pObj)->fMark0 = 1;
        }
        else if ( fPhase0 )
        {
            assert( fPhase1 == 0 );
            assert( pObj->fPhase == 0 );
            Gia_ObjFanin1(pObj)->fMark0 = 1;
        }
        else if ( fPhase1 )
        {
            assert( fPhase0 == 0 );
            assert( pObj->fPhase == 0 );
            Gia_ObjFanin0(pObj)->fMark0 = 1;
        }
    }
    // create new
    Gia_ManFillValue( p );
    pNew = Gia_ManStart( Gia_ManObjNum(p) );
    pNew->pName = Abc_UtilStrsav( p->pName );
    pNew->pSpec = Abc_UtilStrsav( p->pSpec );
    Gia_ManConst0(p)->Value = 0;
    Gia_ManForEachCi( p, pObj, i )
        pObj->Value = Abc_LitNotCond( Gia_ManAppendCi(pNew), !Vec_IntEntry(vValues, i) );
    Gia_ManForEachAnd( p, pObj, i )
    {
        if ( !pObj->fMark0 )
            continue;
        fPhase0 = Gia_ObjPhase(Gia_ObjFanin0(pObj)) ^ Gia_ObjFaninC0(pObj);
        fPhase1 = Gia_ObjPhase(Gia_ObjFanin1(pObj)) ^ Gia_ObjFaninC1(pObj);
        if ( fPhase0 == fPhase1 )
        {
            assert( (int)pObj->fPhase == fPhase0 );
            if ( pObj->fPhase )
                pObj->Value = Gia_ManAppendAnd( pNew, Gia_ObjFanin0(pObj)->Value, Gia_ObjFanin1(pObj)->Value );
            else
                pObj->Value = Gia_ManAppendOr( pNew, Gia_ObjFanin0(pObj)->Value, Gia_ObjFanin1(pObj)->Value );
        }
        else if ( fPhase0 )
        {
            assert( fPhase1 == 0 );
            assert( pObj->fPhase == 0 );
            pObj->Value = Gia_ObjFanin1(pObj)->Value;
        }
        else if ( fPhase1 )
        {
            assert( fPhase0 == 0 );
            assert( pObj->fPhase == 0 );
            pObj->Value = Gia_ObjFanin0(pObj)->Value;
        }
    }
    Gia_ManForEachCo( p, pObj, i )
        Gia_ManAppendCo( pNew, Gia_ObjFanin0(pObj)->Value );
    Gia_ManSetRegNum( pNew, Gia_ManRegNum(p) );
    Gia_ManCleanMark0(p);
    return pNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Gia_Man_t * Gia_ManDeriveOneTest2( Gia_Man_t * p )
{
    Gia_Man_t * pNew;
    Vec_Int_t * vValues = Vec_IntStart( Gia_ManCiNum(p) );
    //Vec_IntFill( vValues, Gia_ManCiNum(p), 1 );
    pNew = Gia_ManDeriveOne( p, vValues );
    Vec_IntFree( vValues );
    return pNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Gia_ManDeriveOneTest( Gia_Man_t * p )
{
    int fVerbose = 1;
    Gia_Man_t * pNew;
    Gia_Obj_t * pObj, * pRoot;
    Vec_Int_t * vValues = Vec_IntStart( Gia_ManCiNum(p) );
170
    Cnf_Dat_t * pCnf = (Cnf_Dat_t *)Mf_ManGenerateCnf( p, 8, 0, 0, 0, 0 );
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    int i, iVar, nIter, iPoVarBeg = pCnf->nVars - Gia_ManCiNum(p);
    sat_solver * pSat = (sat_solver *)Cnf_DataWriteIntoSolver( pCnf, 1, 0 );
    Vec_Int_t * vLits = Vec_IntAlloc( 100 );
    Vec_IntPush( vLits, Abc_Var2Lit( 1, 1 ) );
    for ( nIter = 0; nIter < 10000; nIter++ )
    {
        int status = sat_solver_solve( pSat, Vec_IntArray(vLits), Vec_IntLimit(vLits), 0, 0, 0, 0 );
        if ( status == l_False ) 
            break;
        // derive new set
        assert( status == l_True );
        for ( i = 0; i < Gia_ManCiNum(p); i++ )
        {
            Vec_IntWriteEntry( vValues, i, sat_solver_var_value(pSat, iPoVarBeg+i) );
            printf( "%d", sat_solver_var_value(pSat, iPoVarBeg+i) );
        }
        printf( " : " );

        pNew = Gia_ManDeriveOne( p, vValues );
        // assign variables
        Gia_ManForEachCi( pNew, pObj, i )
            pObj->Value = iPoVarBeg+i;
        iVar = sat_solver_nvars(pSat);
        Gia_ManForEachAnd( pNew, pObj, i )
            pObj->Value = iVar++;
        sat_solver_setnvars( pSat, iVar );
        // create new clauses
        Gia_ManForEachAnd( pNew, pObj, i )
            sat_solver_add_and( pSat, pObj->Value, Gia_ObjFanin0(pObj)->Value, Gia_ObjFanin1(pObj)->Value, Gia_ObjFaninC0(pObj), Gia_ObjFaninC1(pObj), 0 );
        // add to the assumptions
        pRoot = Gia_ManCo(pNew, 0);
        Vec_IntPush( vLits, Abc_Var2Lit(Gia_ObjFanin0(pRoot)->Value, !Gia_ObjFaninC0(pRoot)) );
        if ( fVerbose )
        {
            printf( "Iter = %5d : ", nIter );
            printf( "And = %4d ", Gia_ManAndNum(pNew) );
            printf( "\n" );
        }
        Gia_ManStop( pNew );
    }
    Vec_IntFree( vLits );
    Vec_IntFree( vValues );
    Cnf_DataFree( pCnf );
    sat_solver_delete( pSat );
}

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


ABC_NAMESPACE_IMPL_END