abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
satSolver2i.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [satSolver2i.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [SAT solver.]
8 
9  Synopsis [Records the trace of SAT solving in the CNF form.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - September 2, 2013.]
16 
17  Revision [$Id: satSolver2i.c,v 1.4 2013/09/02 00:00:00 casem Exp $]
18 
19 ***********************************************************************/
20 
21 #include "satSolver2.h"
22 #include "aig/gia/gia.h"
23 #include "aig/gia/giaAig.h"
24 #include "sat/cnf/cnf.h"
25 
27 
28 
29 ////////////////////////////////////////////////////////////////////////
30 /// DECLARATIONS ///
31 ////////////////////////////////////////////////////////////////////////
32 
33 struct Int2_Man_t_
34 {
35  sat_solver2 * pSat; // user's SAT solver
36  Vec_Int_t * vGloVars; // IDs of global variables
37  Vec_Int_t * vVar2Glo; // mapping of SAT variables into their global IDs
38  Gia_Man_t * pGia; // AIG manager to store the interpolant
39 };
40 
41 ////////////////////////////////////////////////////////////////////////
42 /// FUNCTION DEFINITIONS ///
43 ////////////////////////////////////////////////////////////////////////
44 
45 /**Function*************************************************************
46 
47  Synopsis [Managing interpolation manager.]
48 
49  Description []
50 
51  SideEffects []
52 
53  SeeAlso []
54 
55 ***********************************************************************/
56 Int2_Man_t * Int2_ManStart( sat_solver2 * pSat, int * pGloVars, int nGloVars )
57 {
58  Int2_Man_t * p;
59  int i;
60  p = ABC_CALLOC( Int2_Man_t, 1 );
61  p->pSat = pSat;
62  p->vGloVars = Vec_IntAllocArrayCopy( pGloVars, nGloVars );
63  p->vVar2Glo = Vec_IntInvert( p->vGloVars, -1 );
64  p->pGia = Gia_ManStart( 10 * Vec_IntSize(p->vGloVars) );
65  p->pGia->pName = Abc_UtilStrsav( "interpolant" );
66  for ( i = 0; i < nGloVars; i++ )
67  Gia_ManAppendCi( p->pGia );
68  Gia_ManHashStart( p->pGia );
69  return p;
70 }
72 {
73  if ( p == NULL )
74  return;
75  Gia_ManStopP( &p->pGia );
76  Vec_IntFree( p->vGloVars );
77  Vec_IntFree( p->vVar2Glo );
78  ABC_FREE( p );
79 }
81 {
82  Int2_Man_t * p = pSat->pInt2;
83  Gia_Man_t * pTemp, * pGia = p->pGia; p->pGia = NULL;
84  // return NULL, if the interpolant is not ready (for example, when the solver returned 'sat')
85  if ( pSat->hProofLast == -1 )
86  return NULL;
87  // create AIG with one primary output
88  assert( Gia_ManPoNum(pGia) == 0 );
89  Gia_ManAppendCo( pGia, pSat->hProofLast );
90  pSat->hProofLast = -1;
91  // cleanup the resulting AIG
92  pGia = Gia_ManCleanup( pTemp = pGia );
93  Gia_ManStop( pTemp );
94  return (void *)pGia;
95 }
96 
97 /**Function*************************************************************
98 
99  Synopsis [Computing interpolant for a clause.]
100 
101  Description []
102 
103  SideEffects []
104 
105  SeeAlso []
106 
107 ***********************************************************************/
109 {
110  if ( c->lrn )
111  return veci_begin(&p->pSat->claProofs)[clause_id(c)];
112  if ( !c->partA )
113  return 1;
114  if ( c->lits[c->size] < 0 )
115  {
116  int i, Var, CiId, Res = 0;
117  for ( i = 0; i < (int)c->size; i++ )
118  {
119  // get ID of the global variable
120  if ( Abc_Lit2Var(c->lits[i]) >= Vec_IntSize(p->vVar2Glo) )
121  continue;
122  Var = Vec_IntEntry( p->vVar2Glo, Abc_Lit2Var(c->lits[i]) );
123  if ( Var < 0 )
124  continue;
125  // get literal of the AIG node
126  CiId = Gia_ObjId( p->pGia, Gia_ManCi(p->pGia, Var) );
127  // compute interpolant of the clause
128  Res = Gia_ManHashOr( p->pGia, Res, Abc_Var2Lit(CiId, Abc_LitIsCompl(c->lits[i])) );
129  }
130  c->lits[c->size] = Res;
131  }
132  return c->lits[c->size];
133 }
134 int Int2_ManChainResolve( Int2_Man_t * p, clause * c, int iLit, int varA )
135 {
136  int iLit2 = Int2_ManChainStart( p, c );
137  assert( iLit >= 0 );
138  if ( varA )
139  iLit = Gia_ManHashOr( p->pGia, iLit, iLit2 );
140  else
141  iLit = Gia_ManHashAnd( p->pGia, iLit, iLit2 );
142  return iLit;
143 }
144 
145 /**Function*************************************************************
146 
147  Synopsis [Test for the interpolation procedure.]
148 
149  Description [The input AIG can be any n-input comb circuit with one PO
150  (not necessarily a comb miter). The interpolant depends on n+1 variables
151  and equal to the relation f = F(x0,x1,...,xn).]
152 
153  SideEffects []
154 
155  SeeAlso []
156 
157 ***********************************************************************/
159 {
160  sat_solver2 * pSat;
161  Gia_Man_t * pInter;
162  Aig_Man_t * pMan;
163  Vec_Int_t * vGVars;
164  Cnf_Dat_t * pCnf;
165  Aig_Obj_t * pObj;
166  int Lit, Cid, Var, status, i;
167  abctime clk = Abc_Clock();
168  assert( Gia_ManRegNum(p) == 0 );
169  assert( Gia_ManCoNum(p) == 1 );
170 
171  // derive CNFs
172  pMan = Gia_ManToAigSimple( p );
173  pCnf = Cnf_Derive( pMan, 1 );
174 
175  // start the solver
176  pSat = sat_solver2_new();
177  pSat->fVerbose = 1;
178  sat_solver2_setnvars( pSat, 2*pCnf->nVars+1 );
179 
180  // set A-variables (all used except PI/PO, which will be global variables)
181  Aig_ManForEachObj( pMan, pObj, i )
182  if ( pCnf->pVarNums[pObj->Id] >= 0 && !Aig_ObjIsCi(pObj) && !Aig_ObjIsCo(pObj) )
183  var_set_partA( pSat, pCnf->pVarNums[pObj->Id], 1 );
184 
185  // add clauses of A
186  for ( i = 0; i < pCnf->nClauses; i++ )
187  {
188  Cid = sat_solver2_addclause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1], -1 );
189  clause2_set_partA( pSat, Cid, 1 ); // this API should be called for each clause of A
190  }
191 
192  // add clauses of B (after shifting all CNF variables by pCnf->nVars)
193  Cnf_DataLift( pCnf, pCnf->nVars );
194  for ( i = 0; i < pCnf->nClauses; i++ )
195  sat_solver2_addclause( pSat, pCnf->pClauses[i], pCnf->pClauses[i+1], -1 );
196  Cnf_DataLift( pCnf, -pCnf->nVars );
197 
198  // add PI equality clauses
199  vGVars = Vec_IntAlloc( Aig_ManCoNum(pMan)+1 );
200  Aig_ManForEachCi( pMan, pObj, i )
201  {
202  Var = pCnf->pVarNums[pObj->Id];
203  sat_solver2_add_buffer( pSat, Var, pCnf->nVars + Var, 0, 0, -1 );
204  Vec_IntPush( vGVars, Var );
205  }
206 
207  // add an XOR clause in the end
208  Var = pCnf->pVarNums[Aig_ManCo(pMan,0)->Id];
209  sat_solver2_add_xor( pSat, Var, pCnf->nVars + Var, 2*pCnf->nVars, 0, 0, -1 );
210  Vec_IntPush( vGVars, Var );
211 
212  // start the interpolation manager
213  pSat->pInt2 = Int2_ManStart( pSat, Vec_IntArray(vGVars), Vec_IntSize(vGVars) );
214 
215  // solve the problem
216  Lit = toLitCond( 2*pCnf->nVars, 0 );
217  status = sat_solver2_solve( pSat, &Lit, &Lit + 1, 0, 0, 0, 0 );
218  assert( status == l_False );
219  Sat_Solver2PrintStats( stdout, pSat );
220 
221  // derive interpolant
222  pInter = (Gia_Man_t *)Int2_ManReadInterpolant( pSat );
223  Gia_ManPrintStats( pInter, NULL );
224  Abc_PrintTime( 1, "Total interpolation time", Abc_Clock() - clk );
225 
226  // clean up
227  Vec_IntFree( vGVars );
228  Cnf_DataFree( pCnf );
229  Aig_ManStop( pMan );
230  sat_solver2_delete( pSat );
231 
232  // return interpolant
233  return pInter;
234 }
235 
236 ////////////////////////////////////////////////////////////////////////
237 /// END OF FILE ///
238 ////////////////////////////////////////////////////////////////////////
239 
240 
242 
static int * Vec_IntArray(Vec_Int_t *p)
Definition: vecInt.h:328
Vec_Int_t * vVar2Glo
Definition: satSolver2i.c:37
DECLARATIONS ///.
Definition: int2Int.h:46
void Gia_ManStop(Gia_Man_t *p)
Definition: giaMan.c:77
Aig_Man_t * Gia_ManToAigSimple(Gia_Man_t *p)
Definition: giaAig.c:367
void Gia_ManPrintStats(Gia_Man_t *p, Gps_Par_t *pPars)
Definition: giaMan.c:389
lit lits[0]
Definition: satClause.h:56
static int Gia_ManPoNum(Gia_Man_t *p)
Definition: gia.h:386
Cnf_Dat_t * Cnf_Derive(Aig_Man_t *pAig, int nOutputs)
Definition: cnfCore.c:165
typedefABC_NAMESPACE_HEADER_START struct Aig_Man_t_ Aig_Man_t
INCLUDES ///.
Definition: aig.h:50
static Gia_Obj_t * Gia_ManCi(Gia_Man_t *p, int v)
Definition: gia.h:403
static int Gia_ManAppendCo(Gia_Man_t *p, int iLit0)
Definition: gia.h:703
static Llb_Mgr_t * p
Definition: llb3Image.c:950
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
void Aig_ManStop(Aig_Man_t *p)
Definition: aigMan.c:187
int nClauses
Definition: cnf.h:61
void sat_solver2_delete(sat_solver2 *s)
Definition: satSolver2.c:1225
static int Gia_ManAppendCi(Gia_Man_t *p)
Definition: gia.h:583
int nVars
Definition: cnf.h:59
static int Abc_Var2Lit(int Var, int fCompl)
Definition: abc_global.h:263
static Vec_Int_t * Vec_IntInvert(Vec_Int_t *p, int Fill)
Definition: vecInt.h:1092
#define Aig_ManForEachCi(p, pObj, i)
ITERATORS ///.
Definition: aig.h:393
int * pVarNums
Definition: cnf.h:63
static int sat_solver2_add_xor(sat_solver2 *pSat, int iVarA, int iVarB, int iVarC, int fCompl, int fMark, int Id)
Definition: satSolver2.h:319
Vec_Int_t * vGloVars
Definition: satSolver2i.c:36
Int2_Man_t * pInt2
Definition: satSolver2.h:168
sat_solver2 * sat_solver2_new(void)
Definition: satSolver2.c:1109
void Sat_Solver2PrintStats(FILE *pFile, sat_solver2 *p)
Definition: satUtil.c:210
int sat_solver2_addclause(sat_solver2 *s, lit *begin, lit *end, int Id)
Definition: satSolver2.c:1287
static abctime Abc_Clock()
Definition: abc_global.h:279
Definition: cnf.h:56
sat_solver2 * pSat
Definition: satSolver2i.c:35
static int clause_id(clause *c)
Definition: satClause.h:144
void sat_solver2_setnvars(sat_solver2 *s, int n)
Definition: satSolver2.c:1170
void Gia_ManStopP(Gia_Man_t **p)
Definition: giaMan.c:177
int Int2_ManChainStart(Int2_Man_t *p, clause *c)
Definition: satSolver2i.c:108
static void Abc_PrintTime(int level, const char *pStr, abctime time)
Definition: abc_global.h:367
unsigned partA
Definition: satClause.h:53
static int Aig_ManCoNum(Aig_Man_t *p)
Definition: aig.h:252
int Int2_ManChainResolve(Int2_Man_t *p, clause *c, int iLit, int varA)
Definition: satSolver2i.c:134
static int Abc_LitIsCompl(int Lit)
Definition: abc_global.h:265
int ** pClauses
Definition: cnf.h:62
void var_set_partA(sat_solver2 *s, int v, int partA)
Definition: satSolver2.c:85
typedefABC_NAMESPACE_HEADER_START struct Int2_Man_t_ Int2_Man_t
INCLUDES ///.
Definition: int2Int.h:45
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
static lit toLitCond(int v, int c)
Definition: satVec.h:143
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
Gia_Man_t * pGia
Definition: int2Int.h:51
void Gia_ManHashStart(Gia_Man_t *p)
Definition: giaHash.c:117
Gia_Man_t * Gia_ManStart(int nObjsMax)
DECLARATIONS ///.
Definition: giaMan.c:52
Definition: aig.h:69
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
static int Gia_ObjId(Gia_Man_t *p, Gia_Obj_t *pObj)
Definition: gia.h:410
static Vec_Int_t * Vec_IntAllocArrayCopy(int *pArray, int nSize)
Definition: vecInt.h:192
void Cnf_DataLift(Cnf_Dat_t *p, int nVarsPlus)
Definition: cnfMan.c:205
Gia_Man_t * Gia_ManInterTest(Gia_Man_t *p)
Definition: satSolver2i.c:158
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
static Aig_Obj_t * Aig_ManCo(Aig_Man_t *p, int i)
Definition: aig.h:267
static int sat_solver2_add_buffer(sat_solver2 *pSat, int iVarA, int iVarB, int fCompl, int fMark, int Id)
Definition: satSolver2.h:275
Int2_Man_t * Int2_ManStart(sat_solver2 *pSat, int *pGloVars, int nGloVars)
FUNCTION DEFINITIONS ///.
Definition: satSolver2i.c:56
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
#define Aig_ManForEachObj(p, pObj, i)
Definition: aig.h:403
unsigned size
Definition: satClause.h:55
#define ABC_FREE(obj)
Definition: abc_global.h:232
Definition: gia.h:95
static void clause2_set_partA(sat_solver2 *s, int h, int partA)
Definition: satSolver2.h:183
void * Int2_ManReadInterpolant(sat_solver2 *pSat)
Definition: satSolver2i.c:80
static int Abc_Lit2Var(int Lit)
Definition: abc_global.h:264
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
#define l_False
Definition: SolverTypes.h:85
int Var
Definition: SolverTypes.h:42
void Cnf_DataFree(Cnf_Dat_t *p)
Definition: cnfMan.c:180
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)
Definition: satSolver2.c:1835
unsigned lrn
Definition: satClause.h:51
#define assert(ex)
Definition: util_old.h:213
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
static int Aig_ObjIsCi(Aig_Obj_t *pObj)
Definition: aig.h:275
ABC_INT64_T abctime
Definition: abc_global.h:278
Gia_Man_t * Gia_ManCleanup(Gia_Man_t *p)
Definition: giaScl.c:84
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47
int Gia_ManHashOr(Gia_Man_t *p, int iLit0, int iLit1)
Definition: giaHash.c:611
static int * veci_begin(veci *v)
Definition: satVec.h:45
int Id
Definition: aig.h:85
void Int2_ManStop(Int2_Man_t *p)
Definition: satSolver2i.c:71
static int Aig_ObjIsCo(Aig_Obj_t *pObj)
Definition: aig.h:276
int Gia_ManHashAnd(Gia_Man_t *p, int iLit0, int iLit1)
Definition: giaHash.c:572
static int Gia_ManCoNum(Gia_Man_t *p)
Definition: gia.h:384
static int Gia_ManRegNum(Gia_Man_t *p)
Definition: gia.h:387