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.

364 lines
10 KiB

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