You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
11 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddAndAbs.c]
  3. PackageName [cudd]
  4. Synopsis [Combined AND and existential abstraction for BDDs]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_bddAndAbstract()
  8. <li> Cudd_bddAndAbstractLimit()
  9. </ul>
  10. Internal procedures included in this module:
  11. <ul>
  12. <li> cuddBddAndAbstractRecur()
  13. </ul>]
  14. Author [Fabio Somenzi]
  15. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  16. All rights reserved.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in the
  24. documentation and/or other materials provided with the distribution.
  25. Neither the name of the University of Colorado nor the names of its
  26. contributors may be used to endorse or promote products derived from
  27. this software without specific prior written permission.
  28. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  31. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  32. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  33. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  34. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. POSSIBILITY OF SUCH DAMAGE.]
  40. ******************************************************************************/
  41. #include "util.h"
  42. #include "cuddInt.h"
  43. /*---------------------------------------------------------------------------*/
  44. /* Constant declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /*---------------------------------------------------------------------------*/
  47. /* Stucture declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Type declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Variable declarations */
  54. /*---------------------------------------------------------------------------*/
  55. #ifndef lint
  56. static char rcsid[] DD_UNUSED = "$Id: cuddAndAbs.c,v 1.20 2012/02/05 01:07:18 fabio Exp $";
  57. #endif
  58. /*---------------------------------------------------------------------------*/
  59. /* Macro declarations */
  60. /*---------------------------------------------------------------------------*/
  61. /**AutomaticStart*************************************************************/
  62. /*---------------------------------------------------------------------------*/
  63. /* Static function prototypes */
  64. /*---------------------------------------------------------------------------*/
  65. /**AutomaticEnd***************************************************************/
  66. /*---------------------------------------------------------------------------*/
  67. /* Definition of exported functions */
  68. /*---------------------------------------------------------------------------*/
  69. /**Function********************************************************************
  70. Synopsis [Takes the AND of two BDDs and simultaneously abstracts the
  71. variables in cube.]
  72. Description [Takes the AND of two BDDs and simultaneously abstracts
  73. the variables in cube. The variables are existentially abstracted.
  74. Returns a pointer to the result is successful; NULL otherwise.
  75. Cudd_bddAndAbstract implements the semiring matrix multiplication
  76. algorithm for the boolean semiring.]
  77. SideEffects [None]
  78. SeeAlso [Cudd_addMatrixMultiply Cudd_addTriangle Cudd_bddAnd]
  79. ******************************************************************************/
  80. DdNode *
  81. Cudd_bddAndAbstract(
  82. DdManager * manager,
  83. DdNode * f,
  84. DdNode * g,
  85. DdNode * cube)
  86. {
  87. DdNode *res;
  88. do {
  89. manager->reordered = 0;
  90. res = cuddBddAndAbstractRecur(manager, f, g, cube);
  91. } while (manager->reordered == 1);
  92. return(res);
  93. } /* end of Cudd_bddAndAbstract */
  94. /**Function********************************************************************
  95. Synopsis [Takes the AND of two BDDs and simultaneously abstracts the
  96. variables in cube. Returns NULL if too many nodes are required.]
  97. Description [Takes the AND of two BDDs and simultaneously abstracts
  98. the variables in cube. The variables are existentially abstracted.
  99. Returns a pointer to the result is successful; NULL otherwise.
  100. In particular, if the number of new nodes created exceeds
  101. <code>limit</code>, this function returns NULL.]
  102. SideEffects [None]
  103. SeeAlso [Cudd_bddAndAbstract]
  104. ******************************************************************************/
  105. DdNode *
  106. Cudd_bddAndAbstractLimit(
  107. DdManager * manager,
  108. DdNode * f,
  109. DdNode * g,
  110. DdNode * cube,
  111. unsigned int limit)
  112. {
  113. DdNode *res;
  114. unsigned int saveLimit = manager->maxLive;
  115. manager->maxLive = (manager->keys - manager->dead) +
  116. (manager->keysZ - manager->deadZ) + limit;
  117. do {
  118. manager->reordered = 0;
  119. res = cuddBddAndAbstractRecur(manager, f, g, cube);
  120. } while (manager->reordered == 1);
  121. manager->maxLive = saveLimit;
  122. return(res);
  123. } /* end of Cudd_bddAndAbstractLimit */
  124. /*---------------------------------------------------------------------------*/
  125. /* Definition of internal functions */
  126. /*---------------------------------------------------------------------------*/
  127. /**Function********************************************************************
  128. Synopsis [Takes the AND of two BDDs and simultaneously abstracts the
  129. variables in cube.]
  130. Description [Takes the AND of two BDDs and simultaneously abstracts
  131. the variables in cube. The variables are existentially abstracted.
  132. Returns a pointer to the result is successful; NULL otherwise.]
  133. SideEffects [None]
  134. SeeAlso [Cudd_bddAndAbstract]
  135. ******************************************************************************/
  136. DdNode *
  137. cuddBddAndAbstractRecur(
  138. DdManager * manager,
  139. DdNode * f,
  140. DdNode * g,
  141. DdNode * cube)
  142. {
  143. DdNode *F, *ft, *fe, *G, *gt, *ge;
  144. DdNode *one, *zero, *r, *t, *e;
  145. unsigned int topf, topg, topcube, top, index;
  146. statLine(manager);
  147. one = DD_ONE(manager);
  148. zero = Cudd_Not(one);
  149. /* Terminal cases. */
  150. if (f == zero || g == zero || f == Cudd_Not(g)) return(zero);
  151. if (f == one && g == one) return(one);
  152. if (cube == one) {
  153. return(cuddBddAndRecur(manager, f, g));
  154. }
  155. if (f == one || f == g) {
  156. return(cuddBddExistAbstractRecur(manager, g, cube));
  157. }
  158. if (g == one) {
  159. return(cuddBddExistAbstractRecur(manager, f, cube));
  160. }
  161. /* At this point f, g, and cube are not constant. */
  162. if (f > g) { /* Try to increase cache efficiency. */
  163. DdNode *tmp = f;
  164. f = g;
  165. g = tmp;
  166. }
  167. /* Here we can skip the use of cuddI, because the operands are known
  168. ** to be non-constant.
  169. */
  170. F = Cudd_Regular(f);
  171. G = Cudd_Regular(g);
  172. topf = manager->perm[F->index];
  173. topg = manager->perm[G->index];
  174. top = ddMin(topf, topg);
  175. topcube = manager->perm[cube->index];
  176. while (topcube < top) {
  177. cube = cuddT(cube);
  178. if (cube == one) {
  179. return(cuddBddAndRecur(manager, f, g));
  180. }
  181. topcube = manager->perm[cube->index];
  182. }
  183. /* Now, topcube >= top. */
  184. /* Check cache. */
  185. if (F->ref != 1 || G->ref != 1) {
  186. r = cuddCacheLookup(manager, DD_BDD_AND_ABSTRACT_TAG, f, g, cube);
  187. if (r != NULL) {
  188. return(r);
  189. }
  190. }
  191. if (topf == top) {
  192. index = F->index;
  193. ft = cuddT(F);
  194. fe = cuddE(F);
  195. if (Cudd_IsComplement(f)) {
  196. ft = Cudd_Not(ft);
  197. fe = Cudd_Not(fe);
  198. }
  199. } else {
  200. index = G->index;
  201. ft = fe = f;
  202. }
  203. if (topg == top) {
  204. gt = cuddT(G);
  205. ge = cuddE(G);
  206. if (Cudd_IsComplement(g)) {
  207. gt = Cudd_Not(gt);
  208. ge = Cudd_Not(ge);
  209. }
  210. } else {
  211. gt = ge = g;
  212. }
  213. if (topcube == top) { /* quantify */
  214. DdNode *Cube = cuddT(cube);
  215. t = cuddBddAndAbstractRecur(manager, ft, gt, Cube);
  216. if (t == NULL) return(NULL);
  217. /* Special case: 1 OR anything = 1. Hence, no need to compute
  218. ** the else branch if t is 1. Likewise t + t * anything == t.
  219. ** Notice that t == fe implies that fe does not depend on the
  220. ** variables in Cube. Likewise for t == ge.
  221. */
  222. if (t == one || t == fe || t == ge) {
  223. if (F->ref != 1 || G->ref != 1)
  224. cuddCacheInsert(manager, DD_BDD_AND_ABSTRACT_TAG,
  225. f, g, cube, t);
  226. return(t);
  227. }
  228. cuddRef(t);
  229. /* Special case: t + !t * anything == t + anything. */
  230. if (t == Cudd_Not(fe)) {
  231. e = cuddBddExistAbstractRecur(manager, ge, Cube);
  232. } else if (t == Cudd_Not(ge)) {
  233. e = cuddBddExistAbstractRecur(manager, fe, Cube);
  234. } else {
  235. e = cuddBddAndAbstractRecur(manager, fe, ge, Cube);
  236. }
  237. if (e == NULL) {
  238. Cudd_IterDerefBdd(manager, t);
  239. return(NULL);
  240. }
  241. if (t == e) {
  242. r = t;
  243. cuddDeref(t);
  244. } else {
  245. cuddRef(e);
  246. r = cuddBddAndRecur(manager, Cudd_Not(t), Cudd_Not(e));
  247. if (r == NULL) {
  248. Cudd_IterDerefBdd(manager, t);
  249. Cudd_IterDerefBdd(manager, e);
  250. return(NULL);
  251. }
  252. r = Cudd_Not(r);
  253. cuddRef(r);
  254. Cudd_DelayedDerefBdd(manager, t);
  255. Cudd_DelayedDerefBdd(manager, e);
  256. cuddDeref(r);
  257. }
  258. } else {
  259. t = cuddBddAndAbstractRecur(manager, ft, gt, cube);
  260. if (t == NULL) return(NULL);
  261. cuddRef(t);
  262. e = cuddBddAndAbstractRecur(manager, fe, ge, cube);
  263. if (e == NULL) {
  264. Cudd_IterDerefBdd(manager, t);
  265. return(NULL);
  266. }
  267. if (t == e) {
  268. r = t;
  269. cuddDeref(t);
  270. } else {
  271. cuddRef(e);
  272. if (Cudd_IsComplement(t)) {
  273. r = cuddUniqueInter(manager, (int) index,
  274. Cudd_Not(t), Cudd_Not(e));
  275. if (r == NULL) {
  276. Cudd_IterDerefBdd(manager, t);
  277. Cudd_IterDerefBdd(manager, e);
  278. return(NULL);
  279. }
  280. r = Cudd_Not(r);
  281. } else {
  282. r = cuddUniqueInter(manager,(int)index,t,e);
  283. if (r == NULL) {
  284. Cudd_IterDerefBdd(manager, t);
  285. Cudd_IterDerefBdd(manager, e);
  286. return(NULL);
  287. }
  288. }
  289. cuddDeref(e);
  290. cuddDeref(t);
  291. }
  292. }
  293. if (F->ref != 1 || G->ref != 1)
  294. cuddCacheInsert(manager, DD_BDD_AND_ABSTRACT_TAG, f, g, cube, r);
  295. return (r);
  296. } /* end of cuddBddAndAbstractRecur */
  297. /*---------------------------------------------------------------------------*/
  298. /* Definition of static functions */
  299. /*---------------------------------------------------------------------------*/