1 | #include <stdio.h> |
---|
2 | #include <math.h> |
---|
3 | #include "util.h" |
---|
4 | #include "mdd.h" |
---|
5 | |
---|
6 | /* |
---|
7 | * MDD Package |
---|
8 | * |
---|
9 | * $Id: mdd_util.c,v 1.38 2002/09/21 20:41:33 fabio Exp $ |
---|
10 | * |
---|
11 | * Author: Timothy Kam |
---|
12 | * |
---|
13 | * Copyright 1992 by the Regents of the University of California. |
---|
14 | * |
---|
15 | * All rights reserved. Permission to use, copy, modify and distribute |
---|
16 | * this software is hereby granted, provided that the above copyright |
---|
17 | * notice and this permission notice appear in all copies. This software |
---|
18 | * is made available as is, with no warranties. |
---|
19 | */ |
---|
20 | |
---|
21 | static bdd_t* mddRetOnvalBdd(mdd_manager *mddMgr, int mddId); |
---|
22 | static bdd_t* mddIntRetOnvalBdd(mdd_manager *mddMgr, int valNum, int low, int hi, int level, array_t *bddVarArr); |
---|
23 | static void mddFreeBddArr(array_t *bddArr); |
---|
24 | |
---|
25 | /************************************************************************/ |
---|
26 | #define mddGetVarById( mgr, id ) \ |
---|
27 | array_fetch(mvar_type, mdd_ret_mvar_list((mgr)),(id)) |
---|
28 | |
---|
29 | |
---|
30 | int |
---|
31 | toggle(int x) |
---|
32 | { |
---|
33 | if (x == 0) return 1; |
---|
34 | else { |
---|
35 | if (x == 1) return 0; |
---|
36 | else { |
---|
37 | fail("toggle: invalid boolean value\n"); |
---|
38 | return -1; |
---|
39 | } |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | int |
---|
44 | no_bit_encode(int n) |
---|
45 | { |
---|
46 | int i = 0; |
---|
47 | int j = 1; |
---|
48 | |
---|
49 | if (n < 2) return 1; /* Takes care of mv.values <= 1 */ |
---|
50 | |
---|
51 | while (j < n) { |
---|
52 | j = j * 2; |
---|
53 | i++; |
---|
54 | } |
---|
55 | return i; |
---|
56 | } |
---|
57 | |
---|
58 | void |
---|
59 | print_mvar_list(mdd_manager *mgr) |
---|
60 | { |
---|
61 | mvar_type mv; |
---|
62 | int i; |
---|
63 | int no_mvar; |
---|
64 | array_t *mvar_list = mdd_ret_mvar_list(mgr); |
---|
65 | |
---|
66 | no_mvar = array_n(mvar_list); |
---|
67 | printf("print_mvar_list:\n"); |
---|
68 | printf("id\tname\tvalues\tbits\tstride\tstart_vertex\n"); |
---|
69 | for (i=0; i<no_mvar; i++) { |
---|
70 | mv = array_fetch(mvar_type, mvar_list, i); |
---|
71 | (void) printf("%d\t%s\t%d\t%d\n", |
---|
72 | mv.mvar_id, mv.name, mv.values, |
---|
73 | mv.encode_length); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void |
---|
78 | print_strides(array_t *mvar_strides) |
---|
79 | { |
---|
80 | int i, s; |
---|
81 | |
---|
82 | (void) printf("mvar_strides: "); |
---|
83 | for (i=0; i<array_n(mvar_strides); i++) { |
---|
84 | s = array_fetch(int, mvar_strides, i); |
---|
85 | (void) printf("%d ", s); |
---|
86 | } |
---|
87 | (void) printf("\n"); |
---|
88 | } |
---|
89 | |
---|
90 | void |
---|
91 | print_bdd_list_id(array_t *bdd_list) |
---|
92 | { |
---|
93 | bdd_t *b; |
---|
94 | int i, is_complemented; |
---|
95 | |
---|
96 | (void) printf("bdd_list id's: "); |
---|
97 | for (i=0; i<array_n(bdd_list); i++) { |
---|
98 | b = array_fetch(bdd_t *, bdd_list, i); |
---|
99 | (void)bdd_get_node(b, &is_complemented); |
---|
100 | if (is_complemented) (void) printf("!"); |
---|
101 | (void) printf("%d ", bdd_top_var_id(b)); |
---|
102 | } |
---|
103 | (void) printf("\n"); |
---|
104 | } |
---|
105 | |
---|
106 | void |
---|
107 | print_bvar_list_id(mdd_manager *mgr) |
---|
108 | { |
---|
109 | bvar_type bv; |
---|
110 | int i, is_complemented; |
---|
111 | array_t *bvar_list = mdd_ret_bvar_list(mgr); |
---|
112 | |
---|
113 | (void) printf("bvar_list id's: "); |
---|
114 | for (i=0; i<array_n(bvar_list); i++) { |
---|
115 | bv = array_fetch(bvar_type, bvar_list, i); |
---|
116 | (void)bdd_get_node(bv.node,&is_complemented); |
---|
117 | if (is_complemented) (void) printf("!"); |
---|
118 | (void) printf("%d ", bdd_top_var_id(bv.node)); |
---|
119 | } |
---|
120 | (void) printf("\n"); |
---|
121 | } |
---|
122 | |
---|
123 | void |
---|
124 | print_bdd(bdd_manager *mgr, bdd_t *top) |
---|
125 | { |
---|
126 | |
---|
127 | int is_complemented; |
---|
128 | bdd_t *child, *top_uncomp; |
---|
129 | |
---|
130 | if (bdd_is_tautology(top,1)) { |
---|
131 | (void) printf("ONE "); |
---|
132 | return; |
---|
133 | } |
---|
134 | if (bdd_is_tautology(top,0)) { |
---|
135 | (void) printf("ZERO "); |
---|
136 | return; |
---|
137 | } |
---|
138 | (void)bdd_get_node(top, &is_complemented); |
---|
139 | if (is_complemented != 0) (void) printf("!"); |
---|
140 | (void) printf("%d ", bdd_top_var_id(top)); |
---|
141 | (void) printf("v "); |
---|
142 | (void) printf("< "); |
---|
143 | |
---|
144 | if (is_complemented) top_uncomp = bdd_not(top); |
---|
145 | else top_uncomp = mdd_dup(top); |
---|
146 | |
---|
147 | child = bdd_then(top); |
---|
148 | |
---|
149 | print_bdd(mgr, child); |
---|
150 | (void) printf("> "); |
---|
151 | |
---|
152 | mdd_free(child); |
---|
153 | child = bdd_else(top); |
---|
154 | |
---|
155 | print_bdd(mgr, child); |
---|
156 | (void) printf("^ "); |
---|
157 | |
---|
158 | mdd_free(top_uncomp); |
---|
159 | mdd_free(child); |
---|
160 | |
---|
161 | return; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | mvar_type |
---|
167 | find_mvar_id(mdd_manager *mgr, unsigned short id) |
---|
168 | { |
---|
169 | mvar_type mv; |
---|
170 | bvar_type bv; |
---|
171 | array_t *mvar_list = mdd_ret_mvar_list(mgr); |
---|
172 | array_t *bvar_list = mdd_ret_bvar_list(mgr); |
---|
173 | |
---|
174 | if (id >= array_n(bvar_list)) |
---|
175 | fail("find_mvar_id: invalid parameter range for id\n"); |
---|
176 | bv = array_fetch(bvar_type, bvar_list, id); |
---|
177 | if ((bv.mvar_id < 0) || (bv.mvar_id >= array_n(mvar_list))) |
---|
178 | fail("find_mvar_id: bvar contains invalid mvar_id\n"); |
---|
179 | mv = array_fetch(mvar_type, mvar_list, bv.mvar_id); |
---|
180 | return mv; |
---|
181 | } |
---|
182 | |
---|
183 | void |
---|
184 | clear_all_marks(mdd_manager *mgr) |
---|
185 | { |
---|
186 | int i, j; |
---|
187 | mvar_type mv; |
---|
188 | array_t *mvar_list = mdd_ret_mvar_list(mgr); |
---|
189 | |
---|
190 | |
---|
191 | for (i=0; i<array_n(mvar_list); i++) { |
---|
192 | mv = array_fetch(mvar_type, mvar_list, i); |
---|
193 | for (j=0; j<mv.encode_length; j++) |
---|
194 | mv.encoding[j] = 2; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | void |
---|
199 | mdd_mark( |
---|
200 | mdd_manager *mgr, |
---|
201 | bdd_t *top /**** was bdd_node *bnode; --- changed by Serdar ***/, |
---|
202 | int phase) |
---|
203 | { |
---|
204 | int i, top_id, found = 0; |
---|
205 | int bit_position = 0; /* initialize for lint */ |
---|
206 | mvar_type mv; |
---|
207 | |
---|
208 | top_id = bdd_top_var_id(top); |
---|
209 | mv = find_mvar_id(mgr, top_id); |
---|
210 | |
---|
211 | for (i=0; i<(mv.encode_length); i++){ |
---|
212 | if ( mdd_ret_bvar_id( &mv, i) == top_id ){ |
---|
213 | bit_position = i; |
---|
214 | found = 1; |
---|
215 | break; |
---|
216 | }; |
---|
217 | }; |
---|
218 | |
---|
219 | |
---|
220 | if (found == 0) |
---|
221 | fail("mdd_mark: interleaving error\n"); |
---|
222 | |
---|
223 | mv.encoding[bit_position] = phase; |
---|
224 | |
---|
225 | } |
---|
226 | |
---|
227 | void |
---|
228 | mdd_unmark(mdd_manager *mgr, bdd_t *top) |
---|
229 | { |
---|
230 | int i, top_id, found = 0; |
---|
231 | int bit_position = 0; /* initialize for lint */ |
---|
232 | mvar_type mv; |
---|
233 | |
---|
234 | |
---|
235 | top_id = bdd_top_var_id(top); |
---|
236 | mv = find_mvar_id(mgr, top_id); |
---|
237 | |
---|
238 | for (i=0; i<mv.encode_length; i++) |
---|
239 | if ( mdd_ret_bvar_id( &mv, i) == top_id ){ |
---|
240 | bit_position = i; |
---|
241 | found = 1; |
---|
242 | break; |
---|
243 | }; |
---|
244 | |
---|
245 | if (found == 0) |
---|
246 | fail("mdd_unmark: interleaving error\n"); |
---|
247 | mv.encoding[bit_position] = 2; |
---|
248 | } |
---|
249 | |
---|
250 | mvar_type |
---|
251 | find_mvar(mdd_manager *mgr, char *name) |
---|
252 | { |
---|
253 | int i; |
---|
254 | mvar_type mv; |
---|
255 | array_t *mvar_list = mdd_ret_mvar_list(mgr); |
---|
256 | |
---|
257 | for (i=0; i<array_n(mvar_list); i++) { |
---|
258 | mv = array_fetch(mvar_type, mvar_list, i); |
---|
259 | if (strcmp(mv.name, name) == 0) return mv; |
---|
260 | } |
---|
261 | fail("find_mvar: cannot find name in mvar_list\n"); |
---|
262 | return mv; |
---|
263 | } |
---|
264 | |
---|
265 | array_t * |
---|
266 | mdd_ret_mvar_list(mdd_manager *mgr) |
---|
267 | { |
---|
268 | bdd_external_hooks *hook; |
---|
269 | array_t *mvar_list; |
---|
270 | |
---|
271 | hook = bdd_get_external_hooks(mgr); |
---|
272 | mvar_list = ((mdd_hook_type *)(hook->mdd))->mvar_list; |
---|
273 | |
---|
274 | return mvar_list; |
---|
275 | } |
---|
276 | |
---|
277 | void |
---|
278 | mdd_set_mvar_list(mdd_manager *mgr, array_t *mvar_list) |
---|
279 | { |
---|
280 | bdd_external_hooks *hook; |
---|
281 | |
---|
282 | hook = bdd_get_external_hooks(mgr); |
---|
283 | ((mdd_hook_type *)(hook->mdd))->mvar_list = mvar_list; |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | array_t * |
---|
288 | mdd_ret_bvar_list(mdd_manager *mgr) |
---|
289 | { |
---|
290 | bdd_external_hooks *hook; |
---|
291 | array_t *bvar_list; |
---|
292 | |
---|
293 | hook = bdd_get_external_hooks(mgr); |
---|
294 | bvar_list = ((mdd_hook_type *)(hook->mdd))->bvar_list; |
---|
295 | |
---|
296 | return bvar_list; |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | int |
---|
301 | mdd_ret_bvar_id(mvar_type *mvar_ptr, int i) |
---|
302 | { |
---|
303 | |
---|
304 | return ( array_fetch(int, mvar_ptr->bvars, i) ); |
---|
305 | } |
---|
306 | |
---|
307 | bvar_type |
---|
308 | mdd_ret_bvar(mvar_type *mvar_ptr, int i, array_t *bvar_list) |
---|
309 | { |
---|
310 | int bvar_id; |
---|
311 | |
---|
312 | bvar_id = array_fetch(int, mvar_ptr->bvars, i); |
---|
313 | |
---|
314 | return array_fetch(bvar_type, bvar_list, bvar_id); |
---|
315 | } |
---|
316 | |
---|
317 | /************************************************************************/ |
---|
318 | /* Given an Mdd, returns the num of onset points. By construction of */ |
---|
319 | /* Mdd's, some points not in the range of Mdd vars may be included */ |
---|
320 | /* in the onset. These fake points must first be removed. */ |
---|
321 | /************************************************************************/ |
---|
322 | |
---|
323 | double |
---|
324 | mdd_count_onset( |
---|
325 | mdd_manager *mddMgr, |
---|
326 | mdd_t *aMdd, |
---|
327 | array_t *mddIdArr) |
---|
328 | { |
---|
329 | bdd_t *onvalBdd, *aOnvalBdd, *onsetBdd, *tmpBdd; |
---|
330 | double onsetNum; |
---|
331 | array_t *bddVarArr; |
---|
332 | int i, arrSize, mddId; |
---|
333 | |
---|
334 | arrSize = array_n( mddIdArr ); |
---|
335 | onvalBdd = bdd_one( mddMgr ); |
---|
336 | |
---|
337 | for ( i = 0 ; i < arrSize ; i++ ) { |
---|
338 | mddId = array_fetch( int, mddIdArr, i ); |
---|
339 | aOnvalBdd = mddRetOnvalBdd( mddMgr, mddId ); |
---|
340 | |
---|
341 | tmpBdd = bdd_and( onvalBdd, aOnvalBdd, 1, 1 ); |
---|
342 | bdd_free( onvalBdd ); |
---|
343 | bdd_free( aOnvalBdd ); |
---|
344 | onvalBdd = tmpBdd; |
---|
345 | } |
---|
346 | onsetBdd = bdd_and( onvalBdd, aMdd, 1, 1 ); |
---|
347 | bdd_free( onvalBdd ); |
---|
348 | |
---|
349 | bddVarArr = mdd_id_array_to_bdd_array( mddMgr, mddIdArr ); |
---|
350 | onsetNum = bdd_count_onset( onsetBdd, bddVarArr ); |
---|
351 | bdd_free( onsetBdd ); |
---|
352 | mddFreeBddArr( bddVarArr ); |
---|
353 | return( onsetNum ); |
---|
354 | } /* mdd_count_onset */ |
---|
355 | |
---|
356 | mdd_t * |
---|
357 | mdd_onset_bdd( |
---|
358 | mdd_manager *mddMgr, |
---|
359 | mdd_t *aMdd, |
---|
360 | array_t *mddIdArr) |
---|
361 | { |
---|
362 | bdd_t *onvalBdd, *aOnvalBdd, *onsetBdd, *tmpBdd; |
---|
363 | int i, arrSize, mddId; |
---|
364 | |
---|
365 | arrSize = array_n( mddIdArr ); |
---|
366 | onvalBdd = bdd_one( mddMgr ); |
---|
367 | |
---|
368 | for ( i = 0 ; i < arrSize ; i++ ) { |
---|
369 | mddId = array_fetch( int, mddIdArr, i ); |
---|
370 | aOnvalBdd = mddRetOnvalBdd( mddMgr, mddId ); |
---|
371 | |
---|
372 | tmpBdd = bdd_and( onvalBdd, aOnvalBdd, 1, 1 ); |
---|
373 | bdd_free( onvalBdd ); |
---|
374 | bdd_free( aOnvalBdd ); |
---|
375 | onvalBdd = tmpBdd; |
---|
376 | } |
---|
377 | onsetBdd = bdd_and( onvalBdd, aMdd, 1, 1 ); |
---|
378 | bdd_free( onvalBdd ); |
---|
379 | return( onsetBdd ); |
---|
380 | } /* mdd_onset_bdd */ |
---|
381 | |
---|
382 | int |
---|
383 | mdd_epd_count_onset( |
---|
384 | mdd_manager *mddMgr, |
---|
385 | mdd_t *aMdd, |
---|
386 | array_t *mddIdArr, |
---|
387 | EpDouble *epd) |
---|
388 | { |
---|
389 | bdd_t *onvalBdd, *aOnvalBdd, *onsetBdd, *tmpBdd; |
---|
390 | array_t *bddVarArr; |
---|
391 | int i, arrSize, mddId; |
---|
392 | int status; |
---|
393 | |
---|
394 | arrSize = array_n( mddIdArr ); |
---|
395 | onvalBdd = bdd_one( mddMgr ); |
---|
396 | |
---|
397 | for ( i = 0 ; i < arrSize ; i++ ) { |
---|
398 | mddId = array_fetch( int, mddIdArr, i ); |
---|
399 | aOnvalBdd = mddRetOnvalBdd( mddMgr, mddId ); |
---|
400 | |
---|
401 | tmpBdd = bdd_and( onvalBdd, aOnvalBdd, 1, 1 ); |
---|
402 | bdd_free( onvalBdd ); |
---|
403 | bdd_free( aOnvalBdd ); |
---|
404 | onvalBdd = tmpBdd; |
---|
405 | } |
---|
406 | onsetBdd = bdd_and( onvalBdd, aMdd, 1, 1 ); |
---|
407 | bdd_free( onvalBdd ); |
---|
408 | |
---|
409 | bddVarArr = mdd_id_array_to_bdd_array( mddMgr, mddIdArr ); |
---|
410 | status = bdd_epd_count_onset( onsetBdd, bddVarArr, epd ); |
---|
411 | if (status) |
---|
412 | return(status); |
---|
413 | bdd_free( onsetBdd ); |
---|
414 | mddFreeBddArr( bddVarArr ); |
---|
415 | return(0); |
---|
416 | } /* mdd_epd_count_onset */ |
---|
417 | |
---|
418 | /************************************************************************/ |
---|
419 | static bdd_t* |
---|
420 | mddRetOnvalBdd( |
---|
421 | mdd_manager *mddMgr, |
---|
422 | int mddId) |
---|
423 | { |
---|
424 | bdd_t *onvalBdd; |
---|
425 | mvar_type mVar; |
---|
426 | int valNum, high; |
---|
427 | array_t *bddVarArr; |
---|
428 | |
---|
429 | mVar = mddGetVarById( mddMgr, mddId ); |
---|
430 | valNum = mVar.values; |
---|
431 | high = (int) pow( (double) 2, (double) mVar.encode_length ); |
---|
432 | assert( (valNum == 1) || ( (valNum <= high) && (valNum > high/2) )); |
---|
433 | if ( valNum == high ) |
---|
434 | onvalBdd = bdd_one( mddMgr ); |
---|
435 | else { |
---|
436 | bddVarArr = mdd_id_to_bdd_array( mddMgr, mddId ); |
---|
437 | onvalBdd = mddIntRetOnvalBdd( mddMgr, valNum, 0, high, |
---|
438 | 0, bddVarArr ); |
---|
439 | mddFreeBddArr( bddVarArr ); |
---|
440 | } |
---|
441 | return( onvalBdd ); |
---|
442 | } /* mddRetOnvalBdd */ |
---|
443 | |
---|
444 | /************************************************************************/ |
---|
445 | static bdd_t* |
---|
446 | mddIntRetOnvalBdd( |
---|
447 | mdd_manager *mddMgr, |
---|
448 | int valNum, |
---|
449 | int low, |
---|
450 | int hi, |
---|
451 | int level, |
---|
452 | array_t *bddVarArr) |
---|
453 | { |
---|
454 | int mid; |
---|
455 | bdd_t *curVar, *recBdd; |
---|
456 | bdd_t *onvalBdd = NIL(bdd_t); /* initialized for lint */ |
---|
457 | |
---|
458 | mid = (low + hi) / 2; |
---|
459 | curVar = array_fetch( bdd_t *, bddVarArr, level ); |
---|
460 | |
---|
461 | if ( valNum > mid ) { |
---|
462 | recBdd = mddIntRetOnvalBdd( mddMgr, valNum, mid, hi, |
---|
463 | level+1, bddVarArr ); |
---|
464 | onvalBdd = bdd_or( recBdd, curVar, 1, 0 ); |
---|
465 | bdd_free( recBdd ); |
---|
466 | } |
---|
467 | else if ( valNum < mid ) { |
---|
468 | recBdd = mddIntRetOnvalBdd( mddMgr, valNum, low, mid, |
---|
469 | level+1, bddVarArr ); |
---|
470 | onvalBdd = bdd_and( recBdd, curVar, 1, 0 ); |
---|
471 | bdd_free( recBdd ); |
---|
472 | } |
---|
473 | else if ( valNum == mid ) |
---|
474 | onvalBdd = bdd_not( curVar ); |
---|
475 | return( onvalBdd ); |
---|
476 | } /* mddIntRetOnvalBdd */ |
---|
477 | |
---|
478 | /************************************************************************/ |
---|
479 | /* Given an array of bdd nodes, frees the array. */ |
---|
480 | |
---|
481 | static void |
---|
482 | mddFreeBddArr(array_t *bddArr) |
---|
483 | { |
---|
484 | int i, arrSize; |
---|
485 | |
---|
486 | arrSize = array_n( bddArr ); |
---|
487 | for ( i = 0 ; i < arrSize ; i++ ) |
---|
488 | bdd_free( array_fetch( bdd_t *, bddArr, i ) ); |
---|
489 | array_free( bddArr ); |
---|
490 | } /* mddFreeBddArr */ |
---|
491 | |
---|
492 | array_t * |
---|
493 | mdd_ret_bvars_of_mvar(mvar_type *mvar_ptr) |
---|
494 | { |
---|
495 | return mvar_ptr->bvars; |
---|
496 | } |
---|
497 | |
---|
498 | /************************************************************************/ |
---|
499 | /* mdd_get_care_set returns the care set of the mdd manager */ |
---|
500 | |
---|
501 | static mdd_t *mdd_get_care_set(mdd_manager *mdd_mgr) |
---|
502 | { |
---|
503 | mdd_t *temp; |
---|
504 | mvar_type mv; |
---|
505 | mdd_manager *bdd_mgr; |
---|
506 | |
---|
507 | int mvar_id,i,j,val_j,value; |
---|
508 | array_t *mvar_list; |
---|
509 | bdd_t *care_set, *care_val, *care_cube,*bit_j; |
---|
510 | |
---|
511 | mvar_list = mdd_ret_mvar_list(mdd_mgr); |
---|
512 | bdd_mgr = mdd_mgr; |
---|
513 | |
---|
514 | care_set = bdd_one(bdd_mgr); |
---|
515 | |
---|
516 | for (mvar_id =0; mvar_id < array_n(mvar_list); mvar_id++) |
---|
517 | { |
---|
518 | mv = array_fetch(mvar_type, mvar_list, mvar_id); |
---|
519 | care_val = bdd_zero(bdd_mgr); |
---|
520 | |
---|
521 | for (i=0; i< (mv.values); i++) |
---|
522 | { |
---|
523 | value = i; |
---|
524 | care_cube = bdd_one(bdd_mgr); |
---|
525 | for(j=0; j< mv.encode_length; j++ ) |
---|
526 | { |
---|
527 | bit_j = bdd_get_variable(bdd_mgr,mdd_ret_bvar_id(&mv, j)); |
---|
528 | val_j = value % 2; |
---|
529 | value = value/2; |
---|
530 | temp = care_cube; |
---|
531 | care_cube = bdd_and(temp,bit_j,1,val_j); |
---|
532 | bdd_free(temp); |
---|
533 | } |
---|
534 | temp = care_val; |
---|
535 | care_val = bdd_or(temp,care_cube,1,1); |
---|
536 | bdd_free(temp); |
---|
537 | bdd_free(care_cube); |
---|
538 | } |
---|
539 | temp = care_set; |
---|
540 | care_set = bdd_and(temp,care_val,1,1); |
---|
541 | bdd_free(care_val); |
---|
542 | bdd_free(temp); |
---|
543 | } |
---|
544 | return care_set; |
---|
545 | } |
---|
546 | |
---|
547 | /* Corrected mdd_cproject */ |
---|
548 | /* returns only valid carepoints */ |
---|
549 | |
---|
550 | mdd_t *mdd_cproject( |
---|
551 | mdd_manager *mgr, |
---|
552 | mdd_t *T, |
---|
553 | array_t *mvars) |
---|
554 | { |
---|
555 | mdd_t *care_set, *new_T, *T_proj; |
---|
556 | array_t *bdd_vars; |
---|
557 | int i, j, mv_no; |
---|
558 | mvar_type mv; |
---|
559 | bdd_t *temp; |
---|
560 | array_t *mvar_list = mdd_ret_mvar_list(mgr); |
---|
561 | |
---|
562 | |
---|
563 | care_set = mdd_get_care_set(mgr); |
---|
564 | new_T = bdd_and(T,care_set,1,1); |
---|
565 | bdd_free(care_set); |
---|
566 | |
---|
567 | if ( mvars == NIL(array_t) ) { |
---|
568 | T_proj = bdd_dup(T); |
---|
569 | printf("\nWARNING: Empty Array of Smoothing Variables\n"); |
---|
570 | return T_proj; |
---|
571 | } |
---|
572 | else if ( array_n(mvars) == 0) { |
---|
573 | T_proj = bdd_dup(T); |
---|
574 | printf("\nWARNING: Empty Array of Smoothing Variables\n"); |
---|
575 | return T_proj; |
---|
576 | } |
---|
577 | |
---|
578 | |
---|
579 | bdd_vars = array_alloc(bdd_t*, 0); |
---|
580 | for (i=0; i<array_n(mvars); i++) { |
---|
581 | mv_no = array_fetch(int, mvars, i); |
---|
582 | mv = array_fetch(mvar_type, mvar_list, mv_no); |
---|
583 | if (mv.status == MDD_BUNDLED) { |
---|
584 | (void) fprintf(stderr, |
---|
585 | "\nmdd_smooth: bundled variable %s used\n",mv.name); |
---|
586 | fail(""); |
---|
587 | } |
---|
588 | |
---|
589 | for (j = 0;j < mv.encode_length; j ++) { |
---|
590 | temp = bdd_get_variable(mgr, mdd_ret_bvar_id(&mv,j) ); |
---|
591 | array_insert_last(bdd_t *, bdd_vars, temp); |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | |
---|
596 | T_proj = bdd_cproject(new_T,bdd_vars); |
---|
597 | bdd_free(new_T); |
---|
598 | |
---|
599 | for (i=0; i<array_n(bdd_vars); i++) { |
---|
600 | temp = array_fetch(bdd_t *, bdd_vars, i); |
---|
601 | bdd_free(temp); |
---|
602 | } |
---|
603 | array_free(bdd_vars); |
---|
604 | |
---|
605 | |
---|
606 | return T_proj; |
---|
607 | } |
---|
608 | |
---|
609 | void |
---|
610 | mdd_print_support(mdd_t *f) |
---|
611 | { |
---|
612 | mdd_manager *mgr = bdd_get_manager(f); |
---|
613 | array_t *support_list = mdd_get_support(mgr, f); |
---|
614 | array_t *mvar_list = mdd_ret_mvar_list(mgr); |
---|
615 | int nSupports = array_n(support_list); |
---|
616 | int i, j; |
---|
617 | mvar_type mv; |
---|
618 | int id; |
---|
619 | |
---|
620 | for (i = 0; i < nSupports; i++) { |
---|
621 | id = array_fetch(int, support_list, i); |
---|
622 | mv = array_fetch(mvar_type, mvar_list, id); |
---|
623 | if (id == mv.mvar_id) |
---|
624 | printf("[%d] = %s\n", i, mv.name); |
---|
625 | else { /* needs to be checked */ |
---|
626 | for (j = 0; j < array_n(mvar_list); j++) { |
---|
627 | mv = array_fetch(mvar_type, mvar_list, j); |
---|
628 | if (id == mv.mvar_id) { |
---|
629 | printf(" [%d] = %s\n", i, mv.name); |
---|
630 | break; |
---|
631 | } |
---|
632 | } |
---|
633 | } |
---|
634 | } |
---|
635 | |
---|
636 | array_free(support_list); |
---|
637 | } |
---|
638 | |
---|
639 | void |
---|
640 | mdd_print_support_to_file(FILE *fout, char *format, mdd_t *f) |
---|
641 | { |
---|
642 | mdd_manager *mgr = bdd_get_manager(f); |
---|
643 | array_t *support_list = mdd_get_support(mgr, f); |
---|
644 | array_t *mvar_list = mdd_ret_mvar_list(mgr); |
---|
645 | int nSupports = array_n(support_list); |
---|
646 | int i, j; |
---|
647 | mvar_type mv; |
---|
648 | int id; |
---|
649 | |
---|
650 | for (i = 0; i < nSupports; i++) { |
---|
651 | id = array_fetch(int, support_list, i); |
---|
652 | mv = array_fetch(mvar_type, mvar_list, id); |
---|
653 | if (id == mv.mvar_id) |
---|
654 | fprintf(fout, format, mv.name); |
---|
655 | else { /* needs to be checked */ |
---|
656 | for (j = 0; j < array_n(mvar_list); j++) { |
---|
657 | mv = array_fetch(mvar_type, mvar_list, j); |
---|
658 | if (id == mv.mvar_id) { |
---|
659 | fprintf(fout, format, mv.name); |
---|
660 | break; |
---|
661 | } |
---|
662 | } |
---|
663 | } |
---|
664 | } |
---|
665 | |
---|
666 | array_free(support_list); |
---|
667 | } |
---|
668 | |
---|
669 | char * |
---|
670 | mdd_read_var_name(mdd_t *f) |
---|
671 | { |
---|
672 | mdd_manager *mgr; |
---|
673 | array_t *support_list; |
---|
674 | array_t *mvar_list; |
---|
675 | int i, id; |
---|
676 | mvar_type mv; |
---|
677 | |
---|
678 | if (bdd_size(f) != 2) { |
---|
679 | fprintf(stderr, |
---|
680 | "** mdd error: mdd_read_var_name can be called for a variable\n"); |
---|
681 | return(NIL(char)); |
---|
682 | } |
---|
683 | |
---|
684 | mgr = bdd_get_manager(f); |
---|
685 | support_list = mdd_get_support(mgr, f); |
---|
686 | mvar_list = mdd_ret_mvar_list(mgr); |
---|
687 | |
---|
688 | id = array_fetch(int, support_list, 0); |
---|
689 | mv = array_fetch(mvar_type, mvar_list, id); |
---|
690 | if (id == mv.mvar_id) { |
---|
691 | array_free(support_list); |
---|
692 | return(mv.name); |
---|
693 | } else { /* needs to be checked */ |
---|
694 | for (i = 0; i < array_n(mvar_list); i++) { |
---|
695 | mv = array_fetch(mvar_type, mvar_list, i); |
---|
696 | if (id == mv.mvar_id) { |
---|
697 | array_free(support_list); |
---|
698 | return(mv.name); |
---|
699 | } |
---|
700 | } |
---|
701 | } |
---|
702 | |
---|
703 | array_free(support_list); |
---|
704 | return(NIL(char)); |
---|
705 | } |
---|
706 | |
---|
707 | int |
---|
708 | mdd_read_mdd_id(mdd_t *f) |
---|
709 | { |
---|
710 | mdd_manager *mgr; |
---|
711 | array_t *support_list; |
---|
712 | int id; |
---|
713 | |
---|
714 | if (bdd_size(f) != 2) { |
---|
715 | fprintf(stderr, |
---|
716 | "** mdd error: mdd_read_mdd_id can be called for a variable\n"); |
---|
717 | return(0); |
---|
718 | } |
---|
719 | |
---|
720 | mgr = bdd_get_manager(f); |
---|
721 | support_list = mdd_get_support(mgr, f); |
---|
722 | id = array_fetch(int, support_list, 0); |
---|
723 | array_free(support_list); |
---|
724 | return(id); |
---|
725 | } |
---|
726 | |
---|
727 | /**Function******************************************************************** |
---|
728 | |
---|
729 | Synopsis [Returns an array of BDD ids corresponding to a MDD variable.] |
---|
730 | |
---|
731 | Description [This function takes an MddId. It returns an array of BDD ids |
---|
732 | corresponding to the bits.] |
---|
733 | |
---|
734 | SideEffects [] |
---|
735 | |
---|
736 | ******************************************************************************/ |
---|
737 | array_t * |
---|
738 | mdd_id_to_bdd_id_array(mdd_manager *mddManager, int mddId) |
---|
739 | { |
---|
740 | array_t *bddIdArray; |
---|
741 | mvar_type mddVar; |
---|
742 | array_t *mvar_list; |
---|
743 | int i, j; |
---|
744 | |
---|
745 | mvar_list = mdd_ret_mvar_list(mddManager); |
---|
746 | mddVar = array_fetch(mvar_type, mvar_list, mddId); |
---|
747 | bddIdArray = array_alloc(int, mddVar.encode_length); |
---|
748 | |
---|
749 | for (i=0; i<mddVar.encode_length; i++){ |
---|
750 | j = mdd_ret_bvar_id(&mddVar, i); |
---|
751 | array_insert_last(int, bddIdArray, j); |
---|
752 | } |
---|
753 | return bddIdArray; |
---|
754 | } |
---|
755 | |
---|
756 | /**Function******************************************************************** |
---|
757 | |
---|
758 | Synopsis [Returns an array of Bdd_t's corresponding to a Mdd variable.] |
---|
759 | |
---|
760 | Description [This function takes an MddId. It returns an array of bdd_t's |
---|
761 | corresponding to the bits.] |
---|
762 | |
---|
763 | SideEffects [] |
---|
764 | |
---|
765 | ******************************************************************************/ |
---|
766 | array_t * |
---|
767 | mdd_id_to_bdd_array(mdd_manager *mddManager, int mddId) |
---|
768 | { |
---|
769 | array_t *bddArray; |
---|
770 | mvar_type mddVar; |
---|
771 | int i, id; |
---|
772 | |
---|
773 | mddVar = mddGetVarById(mddManager, mddId); |
---|
774 | bddArray = array_alloc(bdd_t*, mddVar.encode_length); |
---|
775 | |
---|
776 | for (i = 0; i < mddVar.encode_length; i++) { |
---|
777 | id = mdd_ret_bvar_id(&mddVar, i); |
---|
778 | array_insert_last(bdd_t*, bddArray, bdd_get_variable(mddManager, id)); |
---|
779 | } |
---|
780 | return bddArray; |
---|
781 | } |
---|
782 | |
---|
783 | |
---|
784 | /**Function******************************************************************** |
---|
785 | |
---|
786 | Synopsis [Returns an array of binary vars(bdd_t *) for a given mdd |
---|
787 | id array.] |
---|
788 | |
---|
789 | Description [] |
---|
790 | |
---|
791 | SideEffects [] |
---|
792 | |
---|
793 | ******************************************************************************/ |
---|
794 | array_t * |
---|
795 | mdd_id_array_to_bdd_array(mdd_manager *mddManager, array_t *mddIdArray) |
---|
796 | { |
---|
797 | array_t *bddArray; |
---|
798 | int i, j; |
---|
799 | int id, size; |
---|
800 | mvar_type mddVar; |
---|
801 | |
---|
802 | bddArray = array_alloc(bdd_t*, 0); |
---|
803 | size = array_n(mddIdArray); |
---|
804 | |
---|
805 | for (i = 0; i < size; i++) { |
---|
806 | id = array_fetch(int, mddIdArray, i); |
---|
807 | mddVar = mddGetVarById(mddManager, id); |
---|
808 | for (j = 0; j < mddVar.encode_length; j++) { |
---|
809 | id = mdd_ret_bvar_id(&mddVar, j); |
---|
810 | array_insert_last(bdd_t *, bddArray, bdd_get_variable(mddManager, id)); |
---|
811 | } |
---|
812 | } |
---|
813 | return bddArray; |
---|
814 | } |
---|
815 | |
---|
816 | /**Function******************************************************************** |
---|
817 | |
---|
818 | Synopsis [Returns an array of bddId's corresponding to an array of Mdd |
---|
819 | ids.] |
---|
820 | |
---|
821 | Description [This function takes an array of MddId's. For each MddId it |
---|
822 | returns an array of bddId's corresponding to the bits. These arrays of bddId's |
---|
823 | are concatenated together and returned.] |
---|
824 | |
---|
825 | SideEffects [] |
---|
826 | |
---|
827 | ******************************************************************************/ |
---|
828 | array_t * |
---|
829 | mdd_id_array_to_bdd_id_array(mdd_manager *mddManager, array_t *mddIdArray) |
---|
830 | { |
---|
831 | array_t *bddIdArray; |
---|
832 | int i; |
---|
833 | |
---|
834 | bddIdArray = array_alloc(int, 0); |
---|
835 | for (i=0; i<array_n(mddIdArray); i++){ |
---|
836 | int mddId; |
---|
837 | array_t *tmpBddIdArray; |
---|
838 | mddId = array_fetch(int, mddIdArray, i); |
---|
839 | tmpBddIdArray = mdd_id_to_bdd_id_array(mddManager, mddId); |
---|
840 | array_append(bddIdArray, tmpBddIdArray); |
---|
841 | array_free(tmpBddIdArray); |
---|
842 | } |
---|
843 | return bddIdArray; |
---|
844 | } |
---|
845 | |
---|
846 | |
---|
847 | /**Function******************************************************************** |
---|
848 | |
---|
849 | Synopsis [Returns a bdd cube from a given mdd id array.] |
---|
850 | |
---|
851 | Description [] |
---|
852 | |
---|
853 | SideEffects [] |
---|
854 | |
---|
855 | ******************************************************************************/ |
---|
856 | mdd_t * |
---|
857 | mdd_id_array_to_bdd_cube(mdd_manager *mddManager, array_t *mddIdArray) |
---|
858 | { |
---|
859 | int i, j; |
---|
860 | int id, size; |
---|
861 | mvar_type mddVar; |
---|
862 | mdd_t *cube, *var, *tmp; |
---|
863 | int nVars; |
---|
864 | char *vars; |
---|
865 | |
---|
866 | size = array_n(mddIdArray); |
---|
867 | nVars = bdd_num_vars(mddManager); |
---|
868 | vars = ALLOC(char, sizeof(char) * nVars); |
---|
869 | memset(vars, 0, sizeof(char) * nVars); |
---|
870 | |
---|
871 | for (i = 0; i < size; i++) { |
---|
872 | id = array_fetch(int, mddIdArray, i); |
---|
873 | mddVar = mddGetVarById(mddManager, id); |
---|
874 | for (j = 0; j < mddVar.encode_length; j++) { |
---|
875 | id = mdd_ret_bvar_id(&mddVar, j); |
---|
876 | vars[bdd_get_level_from_id(mddManager, id)] = 1; |
---|
877 | } |
---|
878 | } |
---|
879 | cube = mdd_one(mddManager); |
---|
880 | for (i = nVars - 1; i >= 0; i--) { |
---|
881 | if (vars[i] == 0) |
---|
882 | continue; |
---|
883 | id = (int)bdd_get_id_from_level(mddManager, (long)i); |
---|
884 | var = bdd_get_variable(mddManager, id); |
---|
885 | tmp = mdd_and(cube, var, 1, 1); |
---|
886 | mdd_free(cube); |
---|
887 | mdd_free(var); |
---|
888 | cube = tmp; |
---|
889 | } |
---|
890 | FREE(vars); |
---|
891 | return cube; |
---|
892 | } |
---|
893 | |
---|
894 | /**Function******************************************************************** |
---|
895 | |
---|
896 | Synopsis [Returns the number of bdd variables from mdd id array.] |
---|
897 | |
---|
898 | Description [Returns the number of bdd variables from mdd id array.] |
---|
899 | |
---|
900 | SideEffects [] |
---|
901 | |
---|
902 | ******************************************************************************/ |
---|
903 | int |
---|
904 | mdd_get_number_of_bdd_vars(mdd_manager *mddManager, array_t *mddIdArray) |
---|
905 | { |
---|
906 | int i, n; |
---|
907 | |
---|
908 | n = 0; |
---|
909 | for (i=0; i<array_n(mddIdArray); i++){ |
---|
910 | int mddId; |
---|
911 | array_t *tmpBddIdArray; |
---|
912 | mddId = array_fetch(int, mddIdArray, i); |
---|
913 | tmpBddIdArray = mdd_id_to_bdd_id_array(mddManager, mddId); |
---|
914 | n += array_n(tmpBddIdArray); |
---|
915 | array_free(tmpBddIdArray); |
---|
916 | } |
---|
917 | return n; |
---|
918 | } |
---|
919 | |
---|
920 | /**Function******************************************************************** |
---|
921 | |
---|
922 | Synopsis [Returns the number of bdd support of a mdd.] |
---|
923 | |
---|
924 | Description [Returns the number of bdd support of a mdd.] |
---|
925 | |
---|
926 | SideEffects [] |
---|
927 | |
---|
928 | ******************************************************************************/ |
---|
929 | int |
---|
930 | mdd_get_number_of_bdd_support(mdd_manager *mddManager, mdd_t *f) |
---|
931 | { |
---|
932 | array_t *bvar_list = mdd_ret_bvar_list(mddManager); |
---|
933 | var_set_t *vset; |
---|
934 | int i, number = 0; |
---|
935 | |
---|
936 | vset = bdd_get_support(f); |
---|
937 | for (i = 0; i < array_n(bvar_list); i++) { |
---|
938 | if (var_set_get_elt(vset, i) == 1) { |
---|
939 | number++; |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | (void) var_set_free(vset); |
---|
944 | return number; |
---|
945 | } |
---|
946 | |
---|
947 | /**Function******************************************************************** |
---|
948 | |
---|
949 | Synopsis [Given an Mvf representing the functionality of a multi-valued |
---|
950 | variable, it returns an array of Bdd's representing the characteristic |
---|
951 | function of the relation of the various bits of the multi-valued variable.] |
---|
952 | |
---|
953 | Description [Suppose y is a k-valued variable and takes values |
---|
954 | 0,1,..,k-1. Then the input to this function is an array with k |
---|
955 | Mdds each representing the onset of the respective value of the |
---|
956 | variable (the ith Mdd representing the onset when y takes the |
---|
957 | value (i-1). Suppose m bits are needed to encode the k values of |
---|
958 | y. Then internally y is represented as y_0, y_1, ..., |
---|
959 | y_(m-1). Now the functionality of each bit of y can be computed |
---|
960 | by proper boolean operation on the functions representing the |
---|
961 | onsets of various values of y. For instance if y is a 4-valued |
---|
962 | variable. To achieve that we do the following: |
---|
963 | For each bit b{ |
---|
964 | relation = 0; |
---|
965 | For each value j of the variable{ |
---|
966 | Ej = Encoding function of the jth value |
---|
967 | Fj = Onset function of the jth value |
---|
968 | If (b appears in the positive phase in Ej) then |
---|
969 | relation += b * Fj |
---|
970 | else if (b appears in the negative phase in Ej) then |
---|
971 | relation += b'* Fj |
---|
972 | else if (b does not appear in Ej) then |
---|
973 | relation += Fj |
---|
974 | } |
---|
975 | } |
---|
976 | Note that the above algorithm does not handle the case when a |
---|
977 | bit appears in both phases in the encoding of any value of the |
---|
978 | variable. Hence the assumption behind the above algorithm is that |
---|
979 | the values are encoded as cubes. |
---|
980 | The case when the encoding are functions can be handled by more |
---|
981 | complex algorithm. In that case, we will not be able to build the |
---|
982 | relation for each bit separately. Something to be dealt with in |
---|
983 | the later work. |
---|
984 | ] |
---|
985 | SideEffects [] |
---|
986 | |
---|
987 | ******************************************************************************/ |
---|
988 | array_t * |
---|
989 | mdd_fn_array_to_bdd_rel_array( |
---|
990 | mdd_manager *mddManager, |
---|
991 | int mddId, |
---|
992 | array_t *mddFnArray) |
---|
993 | { |
---|
994 | array_t *bddRelationArray, *mddLiteralArray, *valueArray, *bddArray; |
---|
995 | mvar_type mddVar; |
---|
996 | int i, j, numValues, numEncodingBits; |
---|
997 | bdd_t *bdd, *bddRelation, *bddNot; |
---|
998 | bdd_t *mddFn, *posCofactor, *negCofactor, *tmpBddRelation; |
---|
999 | mdd_t *mddLiteral, *literalRelation; |
---|
1000 | array_t *mvar_list; |
---|
1001 | |
---|
1002 | numValues = array_n(mddFnArray); |
---|
1003 | /* simple binary case */ |
---|
1004 | if (numValues == 2) { |
---|
1005 | bdd_t *onRelation, *offRelation; |
---|
1006 | |
---|
1007 | bddArray = mdd_id_to_bdd_array(mddManager, mddId); |
---|
1008 | bdd = array_fetch(bdd_t *, bddArray, 0); |
---|
1009 | array_free(bddArray); |
---|
1010 | mddFn = array_fetch(mdd_t *, mddFnArray, 0); |
---|
1011 | offRelation = bdd_and(bdd, mddFn, 0, 1); |
---|
1012 | mddFn = array_fetch(mdd_t *, mddFnArray, 1); |
---|
1013 | onRelation = bdd_and(bdd, mddFn, 1, 1); |
---|
1014 | bdd_free(bdd); |
---|
1015 | bddRelation = bdd_or(onRelation, offRelation, 1, 1); |
---|
1016 | bdd_free(onRelation); |
---|
1017 | bdd_free(offRelation); |
---|
1018 | bddRelationArray = array_alloc(bdd_t *, 0); |
---|
1019 | array_insert_last(bdd_t *, bddRelationArray, bddRelation); |
---|
1020 | return bddRelationArray; |
---|
1021 | } |
---|
1022 | mvar_list = mdd_ret_mvar_list(mddManager); |
---|
1023 | mddVar = array_fetch(mvar_type, mvar_list, mddId); |
---|
1024 | assert(mddVar.values == numValues); |
---|
1025 | |
---|
1026 | /* |
---|
1027 | * The following is to check whether each encoding is cube or not. |
---|
1028 | * Since Berkeley MDD package always does the cube encoding this checking has |
---|
1029 | * been turned off currently. |
---|
1030 | */ |
---|
1031 | |
---|
1032 | valueArray = array_alloc(int, 1); |
---|
1033 | mddLiteralArray = array_alloc(mdd_t*, 0); |
---|
1034 | for (i=0; i<numValues; i++){ |
---|
1035 | array_insert(int, valueArray, 0, i); |
---|
1036 | /* Form the Mdd corresponding to this value */ |
---|
1037 | mddLiteral = mdd_literal(mddManager, mddId, valueArray); |
---|
1038 | /* Check if this is a cube */ |
---|
1039 | if (bdd_is_cube(mddLiteral) == FALSE){ |
---|
1040 | fprintf(stderr, |
---|
1041 | "The encoding of the variable %s for the value %d isnot a cube.\n", |
---|
1042 | mddVar.name, i); |
---|
1043 | fprintf(stderr, "It can result in wrong answers.\n"); |
---|
1044 | } |
---|
1045 | array_insert_last(mdd_t*, mddLiteralArray, mddLiteral); |
---|
1046 | } |
---|
1047 | array_free(valueArray); |
---|
1048 | |
---|
1049 | bddRelationArray = array_alloc(bdd_t*, 0); |
---|
1050 | numEncodingBits = mddVar.encode_length; |
---|
1051 | bddArray = mdd_id_to_bdd_array(mddManager, mddId); |
---|
1052 | for (i=0; i<numEncodingBits; i++) { |
---|
1053 | bddRelation = bdd_zero((bdd_manager *)mddManager); |
---|
1054 | bdd = array_fetch(bdd_t*, bddArray, i); |
---|
1055 | bddNot = bdd_not(bdd); |
---|
1056 | for (j=0; j<numValues; j++){ |
---|
1057 | mddLiteral = array_fetch(mdd_t*, mddLiteralArray, j); |
---|
1058 | mddFn = array_fetch(mdd_t*, mddFnArray, j); |
---|
1059 | posCofactor = bdd_cofactor(mddLiteral, bdd); |
---|
1060 | if (bdd_is_tautology(posCofactor, 0)) { |
---|
1061 | literalRelation = bdd_and(bddNot, mddFn, 1, 1); |
---|
1062 | bdd_free(posCofactor); |
---|
1063 | } else { |
---|
1064 | negCofactor = bdd_cofactor(mddLiteral, bddNot); |
---|
1065 | if (bdd_is_tautology(negCofactor, 0)) { |
---|
1066 | literalRelation = bdd_and(bdd, mddFn, 1, 1); |
---|
1067 | } else { |
---|
1068 | assert(bdd_equal(posCofactor, negCofactor)); |
---|
1069 | literalRelation = bdd_dup(mddFn); |
---|
1070 | } |
---|
1071 | bdd_free(posCofactor); |
---|
1072 | bdd_free(negCofactor); |
---|
1073 | } |
---|
1074 | tmpBddRelation = bdd_or(bddRelation, literalRelation, 1, 1); |
---|
1075 | bdd_free(literalRelation); |
---|
1076 | bdd_free(bddRelation); |
---|
1077 | bddRelation = tmpBddRelation; |
---|
1078 | } |
---|
1079 | array_insert_last(bdd_t*, bddRelationArray, bddRelation); |
---|
1080 | bdd_free(bdd); |
---|
1081 | bdd_free(bddNot); |
---|
1082 | } |
---|
1083 | /* Free stuff */ |
---|
1084 | mdd_array_free(mddLiteralArray); |
---|
1085 | array_free(bddArray); |
---|
1086 | return bddRelationArray; |
---|
1087 | } |
---|
1088 | |
---|
1089 | /**Function******************************************************************** |
---|
1090 | |
---|
1091 | Synopsis [Given an Mvf representing the functionality of a multi-valued |
---|
1092 | variable, it returns an array of Bdd's representing the characteristic |
---|
1093 | function of the relation of the various bits of the multi-valued variable.] |
---|
1094 | |
---|
1095 | Description [Suppose y is a k-valued variable and takes values |
---|
1096 | 0,1,..,k-1. Then the input to this function is an array with k |
---|
1097 | Mdds each representing the onset of the respective value of the |
---|
1098 | variable (the ith Mdd representing the onset when y takes the |
---|
1099 | value (i-1). Suppose m bits are needed to encode the k values of |
---|
1100 | y. Then internally y is represented as y_0, y_1, ..., |
---|
1101 | y_(m-1). Now the functionality of each bit of y can be computed |
---|
1102 | by proper boolean operation on the functions representing the |
---|
1103 | onsets of various values of y. For instance if y is a 4-valued |
---|
1104 | variable. To achieve that we do the following: |
---|
1105 | For each bit b{ |
---|
1106 | relation = 0; |
---|
1107 | For each value j of the variable{ |
---|
1108 | Ej = Encoding function of the jth value |
---|
1109 | Fj = Onset function of the jth value |
---|
1110 | If (b appears in the positive phase in Ej) then |
---|
1111 | relation += b * Fj |
---|
1112 | else if (b appears in the negative phase in Ej) then |
---|
1113 | relation += b'* Fj |
---|
1114 | else if (b does not appear in Ej) then |
---|
1115 | relation += Fj |
---|
1116 | } |
---|
1117 | } |
---|
1118 | Note that the above algorithm does not handle the case when a |
---|
1119 | bit appears in both phases in the encoding of any value of the |
---|
1120 | variable. Hence the assumption behind the above algorithm is that |
---|
1121 | the values are encoded as cubes. |
---|
1122 | The case when the encoding are functions can be handled by more |
---|
1123 | complex algorithm. In that case, we will not be able to build the |
---|
1124 | relation for each bit separately. Something to be dealt with in |
---|
1125 | the later work. |
---|
1126 | ] |
---|
1127 | SideEffects [] |
---|
1128 | |
---|
1129 | ******************************************************************************/ |
---|
1130 | array_t * |
---|
1131 | mdd_fn_array_to_bdd_fn_array( |
---|
1132 | mdd_manager *mddManager, |
---|
1133 | int mddId, |
---|
1134 | array_t *mddFnArray) |
---|
1135 | { |
---|
1136 | array_t *bddFunctionArray, *mddLiteralArray, *valueArray, *bddArray; |
---|
1137 | mvar_type mddVar; |
---|
1138 | int i, j, numValues, numEncodingBits; |
---|
1139 | bdd_t *bdd, *bddFunction, *bddNot; |
---|
1140 | bdd_t *onSet, *offSet, *dcSet, *lower, *upper; |
---|
1141 | bdd_t *mddFn, *posCofactor, *negCofactor, *tmp; |
---|
1142 | mdd_t *mddLiteral; |
---|
1143 | array_t *mvar_list; |
---|
1144 | |
---|
1145 | numValues = array_n(mddFnArray); |
---|
1146 | /* simple binary case */ |
---|
1147 | if (numValues == 2) { |
---|
1148 | bddFunctionArray = array_alloc(bdd_t *, 0); |
---|
1149 | mddFn = array_fetch(mdd_t *, mddFnArray, 1); |
---|
1150 | bddFunction = mdd_dup(mddFn); |
---|
1151 | array_insert_last(bdd_t *, bddFunctionArray, bddFunction); |
---|
1152 | return bddFunctionArray; |
---|
1153 | } |
---|
1154 | mvar_list = mdd_ret_mvar_list(mddManager); |
---|
1155 | mddVar = array_fetch(mvar_type, mvar_list, mddId); |
---|
1156 | assert(mddVar.values == numValues); |
---|
1157 | |
---|
1158 | /* |
---|
1159 | * The following is to check whether each encoding is cube or not. |
---|
1160 | * Since Berkeley MDD package always does the cube encoding this checking has |
---|
1161 | * been turned off currently. |
---|
1162 | */ |
---|
1163 | |
---|
1164 | valueArray = array_alloc(int, 1); |
---|
1165 | mddLiteralArray = array_alloc(mdd_t*, 0); |
---|
1166 | for (i=0; i<numValues; i++){ |
---|
1167 | array_insert(int, valueArray, 0, i); |
---|
1168 | /* Form the Mdd corresponding to this value */ |
---|
1169 | mddLiteral = mdd_literal(mddManager, mddId, valueArray); |
---|
1170 | /* Check if this is a cube */ |
---|
1171 | if (bdd_is_cube(mddLiteral) == FALSE) { |
---|
1172 | fprintf(stderr, |
---|
1173 | "The encoding of the variable %s for the value %d isnot a cube.\n", |
---|
1174 | mddVar.name, i); |
---|
1175 | fprintf(stderr, "It can result in wrong answers.\n"); |
---|
1176 | } |
---|
1177 | array_insert_last(mdd_t*, mddLiteralArray, mddLiteral); |
---|
1178 | } |
---|
1179 | array_free(valueArray); |
---|
1180 | |
---|
1181 | bddFunctionArray = array_alloc(bdd_t*, 0); |
---|
1182 | numEncodingBits = mddVar.encode_length; |
---|
1183 | bddArray = mdd_id_to_bdd_array(mddManager, mddId); |
---|
1184 | for (i=0; i<numEncodingBits; i++) { |
---|
1185 | onSet = bdd_zero((bdd_manager *)mddManager); |
---|
1186 | offSet = bdd_zero((bdd_manager *)mddManager); |
---|
1187 | dcSet = bdd_zero((bdd_manager *)mddManager); |
---|
1188 | bdd = array_fetch(bdd_t*, bddArray, i); |
---|
1189 | bddNot = bdd_not(bdd); |
---|
1190 | for (j=0; j<numValues; j++) { |
---|
1191 | mddLiteral = array_fetch(mdd_t*, mddLiteralArray, j); |
---|
1192 | posCofactor = bdd_cofactor(mddLiteral, bdd); |
---|
1193 | mddFn = array_fetch(mdd_t*, mddFnArray, j); |
---|
1194 | |
---|
1195 | if (bdd_is_tautology(posCofactor, 0)) { |
---|
1196 | tmp = bdd_or(offSet, mddFn, 1, 1); |
---|
1197 | bdd_free(offSet); |
---|
1198 | offSet = tmp; |
---|
1199 | bdd_free(posCofactor); |
---|
1200 | continue; |
---|
1201 | } |
---|
1202 | |
---|
1203 | negCofactor = bdd_cofactor(mddLiteral, bddNot); |
---|
1204 | if (bdd_is_tautology(negCofactor, 0)) { |
---|
1205 | tmp = bdd_or(onSet, mddFn, 1, 1); |
---|
1206 | bdd_free(onSet); |
---|
1207 | onSet = tmp; |
---|
1208 | bdd_free(posCofactor); |
---|
1209 | bdd_free(negCofactor); |
---|
1210 | continue; |
---|
1211 | } |
---|
1212 | |
---|
1213 | assert(bdd_equal(posCofactor, negCofactor)); |
---|
1214 | bdd_free(posCofactor); |
---|
1215 | bdd_free(negCofactor); |
---|
1216 | |
---|
1217 | tmp = bdd_or(dcSet, mddFn, 1, 1); |
---|
1218 | bdd_free(dcSet); |
---|
1219 | dcSet = tmp; |
---|
1220 | } |
---|
1221 | bdd_free(bdd); |
---|
1222 | bdd_free(bddNot); |
---|
1223 | lower = bdd_and(onSet, offSet, 1, 0); |
---|
1224 | bdd_free(offSet); |
---|
1225 | upper = bdd_or(onSet, dcSet, 1, 1); |
---|
1226 | bdd_free(onSet); |
---|
1227 | bdd_free(dcSet); |
---|
1228 | bddFunction = bdd_between(lower, upper); |
---|
1229 | bdd_free(lower); |
---|
1230 | bdd_free(upper); |
---|
1231 | array_insert_last(bdd_t*, bddFunctionArray, bddFunction); |
---|
1232 | } |
---|
1233 | /* Free stuff */ |
---|
1234 | mdd_array_free(mddLiteralArray); |
---|
1235 | array_free(bddArray); |
---|
1236 | return bddFunctionArray; |
---|
1237 | } |
---|
1238 | |
---|
1239 | |
---|
1240 | array_t * |
---|
1241 | mdd_pick_arbitrary_minterms( |
---|
1242 | mdd_manager *mddMgr, |
---|
1243 | mdd_t *aMdd, |
---|
1244 | array_t *mddIdArr, |
---|
1245 | int n) |
---|
1246 | { |
---|
1247 | bdd_t *onvalBdd, *aOnvalBdd, *onsetBdd, *tmpBdd; |
---|
1248 | array_t *bddVarArr; |
---|
1249 | int i, arrSize, mddId; |
---|
1250 | array_t *mintermArray; |
---|
1251 | |
---|
1252 | arrSize = array_n( mddIdArr ); |
---|
1253 | onvalBdd = bdd_one( mddMgr ); |
---|
1254 | |
---|
1255 | for ( i = 0 ; i < arrSize ; i++ ) { |
---|
1256 | mddId = array_fetch( int, mddIdArr, i ); |
---|
1257 | aOnvalBdd = mddRetOnvalBdd( mddMgr, mddId ); |
---|
1258 | |
---|
1259 | tmpBdd = bdd_and( onvalBdd, aOnvalBdd, 1, 1 ); |
---|
1260 | bdd_free( onvalBdd ); |
---|
1261 | bdd_free( aOnvalBdd ); |
---|
1262 | onvalBdd = tmpBdd; |
---|
1263 | } |
---|
1264 | onsetBdd = bdd_and( onvalBdd, aMdd, 1, 1 ); |
---|
1265 | bdd_free( onvalBdd ); |
---|
1266 | |
---|
1267 | bddVarArr = mdd_id_array_to_bdd_array(mddMgr, mddIdArr); |
---|
1268 | mintermArray = bdd_bdd_pick_arbitrary_minterms(onsetBdd, bddVarArr, |
---|
1269 | array_n(bddVarArr), n); |
---|
1270 | bdd_free(onsetBdd); |
---|
1271 | mddFreeBddArr(bddVarArr); |
---|
1272 | return(mintermArray); |
---|
1273 | } |
---|
1274 | |
---|
1275 | |
---|
1276 | mdd_t * |
---|
1277 | mdd_subset_with_mask_vars( |
---|
1278 | mdd_manager *mddMgr, |
---|
1279 | mdd_t *aMdd, |
---|
1280 | array_t *mddIdArr, |
---|
1281 | array_t *maskIdArr) |
---|
1282 | { |
---|
1283 | bdd_t *onvalBdd, *aOnvalBdd, *onsetBdd, *tmpBdd; |
---|
1284 | array_t *bddVarArr, *maskVarArr; |
---|
1285 | int i, arrSize, mddId; |
---|
1286 | mdd_t *subset; |
---|
1287 | |
---|
1288 | arrSize = array_n( mddIdArr ); |
---|
1289 | onvalBdd = bdd_one( mddMgr ); |
---|
1290 | |
---|
1291 | for ( i = 0 ; i < arrSize ; i++ ) { |
---|
1292 | mddId = array_fetch( int, mddIdArr, i ); |
---|
1293 | aOnvalBdd = mddRetOnvalBdd( mddMgr, mddId ); |
---|
1294 | |
---|
1295 | tmpBdd = bdd_and( onvalBdd, aOnvalBdd, 1, 1 ); |
---|
1296 | bdd_free( onvalBdd ); |
---|
1297 | bdd_free( aOnvalBdd ); |
---|
1298 | onvalBdd = tmpBdd; |
---|
1299 | } |
---|
1300 | onsetBdd = bdd_and( onvalBdd, aMdd, 1, 1 ); |
---|
1301 | bdd_free( onvalBdd ); |
---|
1302 | |
---|
1303 | bddVarArr = mdd_id_array_to_bdd_array(mddMgr, mddIdArr); |
---|
1304 | maskVarArr = mdd_id_array_to_bdd_array(mddMgr, maskIdArr); |
---|
1305 | |
---|
1306 | subset = bdd_subset_with_mask_vars(onsetBdd, bddVarArr, maskVarArr); |
---|
1307 | bdd_free(onsetBdd); |
---|
1308 | mddFreeBddArr(bddVarArr); |
---|
1309 | mddFreeBddArr(maskVarArr); |
---|
1310 | return(subset); |
---|
1311 | } |
---|
1312 | |
---|
1313 | |
---|
1314 | /* Internal macro to access the mvar_type structure for each MDD variable. */ |
---|
1315 | mvar_type |
---|
1316 | mdd_get_var_by_id(mdd_manager *mddMgr, int id) |
---|
1317 | { |
---|
1318 | return(mddGetVarById(mddMgr, id)); |
---|
1319 | } |
---|
1320 | |
---|
1321 | |
---|
1322 | /* checks whether all support variables of mdd appear in supportIdArray. */ |
---|
1323 | int |
---|
1324 | mdd_check_support( |
---|
1325 | mdd_manager *mddMgr, |
---|
1326 | mdd_t *mdd, |
---|
1327 | array_t *supportIdArray) |
---|
1328 | { |
---|
1329 | int i, mddId; |
---|
1330 | st_table *supportTable = st_init_table(st_numcmp, st_numhash); |
---|
1331 | array_t *tmpIdArray; |
---|
1332 | int allSupportFlag = 1; |
---|
1333 | |
---|
1334 | for (i = 0; i < array_n(supportIdArray); i++) { |
---|
1335 | mddId = array_fetch(int, supportIdArray, i); |
---|
1336 | st_insert(supportTable, (char *)(long)mddId, NULL); |
---|
1337 | } |
---|
1338 | |
---|
1339 | tmpIdArray = mdd_get_support(mddMgr, mdd); |
---|
1340 | for (i = 0; i < array_n(tmpIdArray); i++) { |
---|
1341 | mddId = array_fetch(int, tmpIdArray, i); |
---|
1342 | if (!st_lookup(supportTable, (char *)(long)mddId, NULL)) { |
---|
1343 | allSupportFlag = 0; |
---|
1344 | break; |
---|
1345 | } |
---|
1346 | } |
---|
1347 | |
---|
1348 | st_free_table(supportTable); |
---|
1349 | array_free(tmpIdArray); |
---|
1350 | return(allSupportFlag); |
---|
1351 | } |
---|
1352 | |
---|
1353 | |
---|
1354 | boolean |
---|
1355 | mdd_equal_mod_care_set_array(mdd_t *aSet, mdd_t *bSet, array_t *careSetArray) |
---|
1356 | { |
---|
1357 | mdd_t *tmpMdd1, *tmpMdd2; |
---|
1358 | mdd_t *careSet; |
---|
1359 | int i; |
---|
1360 | boolean result; |
---|
1361 | |
---|
1362 | if (mdd_equal(aSet, bSet)) |
---|
1363 | return 1; |
---|
1364 | |
---|
1365 | arrayForEachItem(mdd_t *, careSetArray, i, careSet) { |
---|
1366 | tmpMdd1 = mdd_and(aSet, careSet, 1, 1); |
---|
1367 | tmpMdd2 = mdd_and(bSet, careSet, 1, 1); |
---|
1368 | |
---|
1369 | result = mdd_equal(tmpMdd1, tmpMdd2); |
---|
1370 | mdd_free(tmpMdd1); |
---|
1371 | mdd_free(tmpMdd2); |
---|
1372 | if (result == 1) |
---|
1373 | return 1; |
---|
1374 | } |
---|
1375 | |
---|
1376 | return 0; |
---|
1377 | } |
---|
1378 | |
---|
1379 | |
---|
1380 | boolean |
---|
1381 | mdd_lequal_mod_care_set_array(mdd_t *aSet, mdd_t *bSet, |
---|
1382 | boolean aPhase, boolean bPhase, |
---|
1383 | array_t *careSetArray) |
---|
1384 | { |
---|
1385 | mdd_t *tmpMdd, *careSet; |
---|
1386 | int i, result; |
---|
1387 | |
---|
1388 | if (mdd_lequal(aSet, bSet, aPhase, bPhase)) |
---|
1389 | return 1; |
---|
1390 | |
---|
1391 | arrayForEachItem(mdd_t *, careSetArray, i, careSet) { |
---|
1392 | tmpMdd = mdd_and(aSet, careSet, aPhase, 1); |
---|
1393 | |
---|
1394 | result = mdd_lequal(tmpMdd, bSet, 1, bPhase); |
---|
1395 | mdd_free(tmpMdd); |
---|
1396 | if (result == 1) |
---|
1397 | return 1; |
---|
1398 | } |
---|
1399 | |
---|
1400 | return 0; |
---|
1401 | } |
---|
1402 | |
---|
1403 | |
---|
1404 | /* Return the mdd that represents all valid assignments to the |
---|
1405 | variables in the support. Support is an array of mdd_ids. */ |
---|
1406 | mdd_t * |
---|
1407 | mdd_range_mdd( |
---|
1408 | mdd_manager *mgr, |
---|
1409 | array_t *support |
---|
1410 | ) |
---|
1411 | { |
---|
1412 | int var, varIndex; /* iterates over support */ |
---|
1413 | mdd_t *range; |
---|
1414 | |
---|
1415 | range = mdd_one(mgr); |
---|
1416 | arrayForEachItem(int, support, varIndex, var){ |
---|
1417 | mdd_t *rangeForVar; |
---|
1418 | mdd_t *tmp; |
---|
1419 | |
---|
1420 | rangeForVar = mddRetOnvalBdd(mgr, var); |
---|
1421 | tmp = mdd_and( rangeForVar, range, 1, 1); |
---|
1422 | mdd_free(rangeForVar); |
---|
1423 | mdd_free(range); |
---|
1424 | range = tmp; |
---|
1425 | } |
---|
1426 | |
---|
1427 | return range; |
---|
1428 | } |
---|
1429 | |
---|
1430 | /*---------------------------------------------------------------------------*/ |
---|
1431 | /* Static function prototypes */ |
---|
1432 | /*---------------------------------------------------------------------------*/ |
---|
1433 | |
---|
1434 | |
---|