abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
msatOrderH.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [msatOrder.c]
4 
5  PackageName [A C version of SAT solver MINISAT, originally developed
6  in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
7  Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
8 
9  Synopsis [The manager of variable assignment.]
10 
11  Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - January 1, 2004.]
16 
17  Revision [$Id: msatOrder.c,v 1.0 2005/05/30 1:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #include "msatInt.h"
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////
27 /// DECLARATIONS ///
28 ////////////////////////////////////////////////////////////////////////
29 
30 // the variable package data structure
32 {
33  Msat_Solver_t * pSat; // the SAT solver
34  Msat_IntVec_t * vIndex; // the heap
35  Msat_IntVec_t * vHeap; // the mapping of var num into its heap num
36 };
37 
38 //The solver can communicate to the variable order the following parts:
39 //- the array of current assignments (pSat->pAssigns)
40 //- the array of variable activities (pSat->pdActivity)
41 //- the array of variables currently in the cone (pSat->vConeVars)
42 //- the array of arrays of variables adjucent to each(pSat->vAdjacents)
43 
44 #define HLEFT(i) ((i)<<1)
45 #define HRIGHT(i) (((i)<<1)+1)
46 #define HPARENT(i) ((i)>>1)
47 #define HCOMPARE(p, i, j) ((p)->pSat->pdActivity[i] > (p)->pSat->pdActivity[j])
48 #define HHEAP(p, i) ((p)->vHeap->pArray[i])
49 #define HSIZE(p) ((p)->vHeap->nSize)
50 #define HOKAY(p, i) ((i) >= 0 && (i) < (p)->vIndex->nSize)
51 #define HINHEAP(p, i) (HOKAY(p, i) && (p)->vIndex->pArray[i] != 0)
52 #define HEMPTY(p) (HSIZE(p) == 1)
53 
54 static int Msat_HeapCheck_rec( Msat_Order_t * p, int i );
55 static int Msat_HeapGetTop( Msat_Order_t * p );
56 static void Msat_HeapInsert( Msat_Order_t * p, int n );
57 static void Msat_HeapIncrease( Msat_Order_t * p, int n );
58 static void Msat_HeapPercolateUp( Msat_Order_t * p, int i );
59 static void Msat_HeapPercolateDown( Msat_Order_t * p, int i );
60 
61 extern abctime timeSelect;
62 
63 ////////////////////////////////////////////////////////////////////////
64 /// FUNCTION DEFINITIONS ///
65 ////////////////////////////////////////////////////////////////////////
66 
67 /**Function*************************************************************
68 
69  Synopsis [Allocates the ordering structure.]
70 
71  Description []
72 
73  SideEffects []
74 
75  SeeAlso []
76 
77 ***********************************************************************/
79 {
80  Msat_Order_t * p;
81  p = ABC_ALLOC( Msat_Order_t, 1 );
82  memset( p, 0, sizeof(Msat_Order_t) );
83  p->pSat = pSat;
84  p->vIndex = Msat_IntVecAlloc( 0 );
85  p->vHeap = Msat_IntVecAlloc( 0 );
86  Msat_OrderSetBounds( p, pSat->nVarsAlloc );
87  return p;
88 }
89 
90 /**Function*************************************************************
91 
92  Synopsis [Sets the bound of the ordering structure.]
93 
94  Description [Should be called whenever the SAT solver is resized.]
95 
96  SideEffects []
97 
98  SeeAlso []
99 
100 ***********************************************************************/
101 void Msat_OrderSetBounds( Msat_Order_t * p, int nVarsMax )
102 {
103  Msat_IntVecGrow( p->vIndex, nVarsMax );
104  Msat_IntVecGrow( p->vHeap, nVarsMax + 1 );
105  p->vIndex->nSize = nVarsMax;
106  p->vHeap->nSize = 0;
107 }
108 
109 /**Function*************************************************************
110 
111  Synopsis [Cleans the ordering structure.]
112 
113  Description []
114 
115  SideEffects []
116 
117  SeeAlso []
118 
119 ***********************************************************************/
121 {
122  int i;
123  for ( i = 0; i < p->vIndex->nSize; i++ )
124  p->vIndex->pArray[i] = 0;
125  for ( i = 0; i < vCone->nSize; i++ )
126  {
127  assert( i+1 < p->vHeap->nCap );
128  p->vHeap->pArray[i+1] = vCone->pArray[i];
129 
130  assert( vCone->pArray[i] < p->vIndex->nSize );
131  p->vIndex->pArray[vCone->pArray[i]] = i+1;
132  }
133  p->vHeap->nSize = vCone->nSize + 1;
134 }
135 
136 /**Function*************************************************************
137 
138  Synopsis [Checks that the J-boundary is okay.]
139 
140  Description []
141 
142  SideEffects []
143 
144  SeeAlso []
145 
146 ***********************************************************************/
148 {
149  return Msat_HeapCheck_rec( p, 1 );
150 }
151 
152 /**Function*************************************************************
153 
154  Synopsis [Frees the ordering structure.]
155 
156  Description []
157 
158  SideEffects []
159 
160  SeeAlso []
161 
162 ***********************************************************************/
164 {
165  Msat_IntVecFree( p->vHeap );
166  Msat_IntVecFree( p->vIndex );
167  ABC_FREE( p );
168 }
169 
170 
171 
172 /**Function*************************************************************
173 
174  Synopsis [Selects the next variable to assign.]
175 
176  Description []
177 
178  SideEffects []
179 
180  SeeAlso []
181 
182 ***********************************************************************/
184 {
185  // Activity based decision:
186 // while (!heap.empty()){
187 // Var next = heap.getmin();
188 // if (toLbool(assigns[next]) == l_Undef)
189 // return next;
190 // }
191 // return var_Undef;
192 
193  int Var;
194  abctime clk = Abc_Clock();
195 
196  while ( !HEMPTY(p) )
197  {
198  Var = Msat_HeapGetTop(p);
199  if ( (p)->pSat->pAssigns[Var] == MSAT_VAR_UNASSIGNED )
200  {
201 //assert( Msat_OrderCheck(p) );
202 timeSelect += Abc_Clock() - clk;
203  return Var;
204  }
205  }
206  return MSAT_ORDER_UNKNOWN;
207 }
208 
209 /**Function*************************************************************
210 
211  Synopsis [Updates J-boundary when the variable is assigned.]
212 
213  Description []
214 
215  SideEffects []
216 
217  SeeAlso []
218 
219 ***********************************************************************/
221 {
222 }
223 
224 /**Function*************************************************************
225 
226  Synopsis [Updates the order after a variable is unassigned.]
227 
228  Description []
229 
230  SideEffects []
231 
232  SeeAlso []
233 
234 ***********************************************************************/
236 {
237 // if (!heap.inHeap(x))
238 // heap.insert(x);
239 
240  abctime clk = Abc_Clock();
241  if ( !HINHEAP(p,Var) )
242  Msat_HeapInsert( p, Var );
243 timeSelect += Abc_Clock() - clk;
244 }
245 
246 /**Function*************************************************************
247 
248  Synopsis [Updates the order after a variable changed weight.]
249 
250  Description []
251 
252  SideEffects []
253 
254  SeeAlso []
255 
256 ***********************************************************************/
258 {
259 // if (heap.inHeap(x))
260 // heap.increase(x);
261 
262  abctime clk = Abc_Clock();
263  if ( HINHEAP(p,Var) )
264  Msat_HeapIncrease( p, Var );
265 timeSelect += Abc_Clock() - clk;
266 }
267 
268 
269 
270 
271 /**Function*************************************************************
272 
273  Synopsis [Checks the heap property recursively.]
274 
275  Description []
276 
277  SideEffects []
278 
279  SeeAlso []
280 
281 ***********************************************************************/
283 {
284  return i >= HSIZE(p) ||
285  (
286  ( HPARENT(i) == 0 || !HCOMPARE(p, HHEAP(p, i), HHEAP(p, HPARENT(i))) ) &&
287 
288  Msat_HeapCheck_rec( p, HLEFT(i) ) &&
289 
290  Msat_HeapCheck_rec( p, HRIGHT(i) )
291  );
292 }
293 
294 /**Function*************************************************************
295 
296  Synopsis [Retrieves the minimum element.]
297 
298  Description []
299 
300  SideEffects []
301 
302  SeeAlso []
303 
304 ***********************************************************************/
306 {
307  int Result, NewTop;
308  Result = HHEAP(p, 1);
309  NewTop = Msat_IntVecPop( p->vHeap );
310  p->vHeap->pArray[1] = NewTop;
311  p->vIndex->pArray[NewTop] = 1;
312  p->vIndex->pArray[Result] = 0;
313  if ( p->vHeap->nSize > 1 )
314  Msat_HeapPercolateDown( p, 1 );
315  return Result;
316 }
317 
318 /**Function*************************************************************
319 
320  Synopsis [Inserts the new element.]
321 
322  Description []
323 
324  SideEffects []
325 
326  SeeAlso []
327 
328 ***********************************************************************/
330 {
331  assert( HOKAY(p, n) );
332  p->vIndex->pArray[n] = HSIZE(p);
333  Msat_IntVecPush( p->vHeap, n );
334  Msat_HeapPercolateUp( p, p->vIndex->pArray[n] );
335 }
336 
337 /**Function*************************************************************
338 
339  Synopsis [Inserts the new element.]
340 
341  Description []
342 
343  SideEffects []
344 
345  SeeAlso []
346 
347 ***********************************************************************/
349 {
350  Msat_HeapPercolateUp( p, p->vIndex->pArray[n] );
351 }
352 
353 /**Function*************************************************************
354 
355  Synopsis [Moves the entry up.]
356 
357  Description []
358 
359  SideEffects []
360 
361  SeeAlso []
362 
363 ***********************************************************************/
365 {
366  int x = HHEAP(p, i);
367  while ( HPARENT(i) != 0 && HCOMPARE(p, x, HHEAP(p, HPARENT(i))) )
368  {
369  p->vHeap->pArray[i] = HHEAP(p, HPARENT(i));
370  p->vIndex->pArray[HHEAP(p, i)] = i;
371  i = HPARENT(i);
372  }
373  p->vHeap->pArray[i] = x;
374  p->vIndex->pArray[x] = i;
375 }
376 
377 /**Function*************************************************************
378 
379  Synopsis [Moves the entry down.]
380 
381  Description []
382 
383  SideEffects []
384 
385  SeeAlso []
386 
387 ***********************************************************************/
389 {
390  int x = HHEAP(p, i);
391  int Child;
392  while ( HLEFT(i) < HSIZE(p) )
393  {
394  if ( HRIGHT(i) < HSIZE(p) && HCOMPARE(p, HHEAP(p, HRIGHT(i)), HHEAP(p, HLEFT(i))) )
395  Child = HRIGHT(i);
396  else
397  Child = HLEFT(i);
398  if ( !HCOMPARE(p, HHEAP(p, Child), x) )
399  break;
400  p->vHeap->pArray[i] = HHEAP(p, Child);
401  p->vIndex->pArray[HHEAP(p, i)] = i;
402  i = Child;
403  }
404  p->vHeap->pArray[i] = x;
405  p->vIndex->pArray[x] = i;
406 }
407 
408 
409 ////////////////////////////////////////////////////////////////////////
410 /// END OF FILE ///
411 ////////////////////////////////////////////////////////////////////////
412 
413 
415 
char * memset()
static void Msat_HeapPercolateUp(Msat_Order_t *p, int i)
Definition: msatOrderH.c:364
static void Msat_HeapPercolateDown(Msat_Order_t *p, int i)
Definition: msatOrderH.c:388
int Msat_OrderVarSelect(Msat_Order_t *p)
Definition: msatOrderH.c:183
#define MSAT_VAR_UNASSIGNED
Definition: msatInt.h:68
#define MSAT_ORDER_UNKNOWN
Definition: msatInt.h:70
static void Msat_HeapIncrease(Msat_Order_t *p, int n)
Definition: msatOrderH.c:348
static Llb_Mgr_t * p
Definition: llb3Image.c:950
#define HINHEAP(p, i)
Definition: msatOrderH.c:51
#define HPARENT(i)
Definition: msatOrderH.c:46
DECLARATIONS ///.
Definition: msatOrderH.c:31
#define HCOMPARE(p, i, j)
Definition: msatOrderH.c:47
Msat_IntVec_t * vIndex
Definition: msatOrderH.c:34
void Msat_OrderFree(Msat_Order_t *p)
Definition: msatOrderH.c:163
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static abctime Abc_Clock()
Definition: abc_global.h:279
void Msat_OrderClean(Msat_Order_t *p, Msat_IntVec_t *vCone)
Definition: msatOrderH.c:120
void Msat_OrderVarAssigned(Msat_Order_t *p, int Var)
Definition: msatOrderH.c:220
for(p=first;p->value< newval;p=p->next)
static int Msat_HeapCheck_rec(Msat_Order_t *p, int i)
Definition: msatOrderH.c:282
static void Msat_HeapInsert(Msat_Order_t *p, int n)
Definition: msatOrderH.c:329
void Msat_OrderSetBounds(Msat_Order_t *p, int nVarsMax)
Definition: msatOrderH.c:101
void Msat_IntVecPush(Msat_IntVec_t *p, int Entry)
Definition: msatVec.c:351
#define HRIGHT(i)
Definition: msatOrderH.c:45
#define HOKAY(p, i)
Definition: msatOrderH.c:50
int Msat_IntVecPop(Msat_IntVec_t *p)
Definition: msatVec.c:425
#define HLEFT(i)
Definition: msatOrderH.c:44
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
Msat_IntVec_t * vHeap
Definition: msatOrderH.c:35
static int Msat_HeapGetTop(Msat_Order_t *p)
Definition: msatOrderH.c:305
Msat_IntVec_t * Msat_IntVecAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: msatVec.c:48
void Msat_IntVecFree(Msat_IntVec_t *p)
Definition: msatVec.c:160
int Msat_OrderCheck(Msat_Order_t *p)
Definition: msatOrderH.c:147
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
void Msat_OrderUpdate(Msat_Order_t *p, int Var)
Definition: msatOrderH.c:257
#define HSIZE(p)
Definition: msatOrderH.c:49
Msat_Order_t * Msat_OrderAlloc(Msat_Solver_t *pSat)
FUNCTION DEFINITIONS ///.
Definition: msatOrderH.c:78
#define HEMPTY(p)
Definition: msatOrderH.c:52
#define ABC_FREE(obj)
Definition: abc_global.h:232
int Var
Definition: SolverTypes.h:42
#define assert(ex)
Definition: util_old.h:213
void Msat_OrderVarUnassigned(Msat_Order_t *p, int Var)
Definition: msatOrderH.c:235
void Msat_IntVecGrow(Msat_IntVec_t *p, int nCapMin)
Definition: msatVec.c:299
int * pArray
Definition: msatInt.h:164
ABC_INT64_T abctime
Definition: abc_global.h:278
typedefABC_NAMESPACE_HEADER_START struct Msat_Solver_t_ Msat_Solver_t
INCLUDES ///.
Definition: msat.h:42
abctime timeSelect
DECLARATIONS ///.
Definition: fraigMan.c:28
#define HHEAP(p, i)
Definition: msatOrderH.c:48
Msat_Solver_t * pSat
Definition: msatOrderH.c:33