The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

620 lines
15 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief %ADD ITE function and satellites.
  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. static void addVarToConst (DdNode *f, DdNode **gp, DdNode **hp, DdNode *one, DdNode *zero);
  56. /** \endcond */
  57. /*---------------------------------------------------------------------------*/
  58. /* Definition of exported functions */
  59. /*---------------------------------------------------------------------------*/
  60. /**
  61. @brief Implements ITE(f,g,h).
  62. @details This procedure assumes that f is a 0-1 %ADD.
  63. @return a pointer to the resulting %ADD if successful; NULL
  64. otherwise.
  65. @sideeffect None
  66. @see Cudd_bddIte Cudd_addIteConstant Cudd_addApply
  67. */
  68. DdNode *
  69. Cudd_addIte(
  70. DdManager * dd,
  71. DdNode * f,
  72. DdNode * g,
  73. DdNode * h)
  74. {
  75. DdNode *res;
  76. do {
  77. dd->reordered = 0;
  78. res = cuddAddIteRecur(dd,f,g,h);
  79. } while (dd->reordered == 1);
  80. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  81. dd->timeoutHandler(dd, dd->tohArg);
  82. }
  83. return(res);
  84. } /* end of Cudd_addIte */
  85. /**
  86. @brief Implements ITEconstant for %ADDs.
  87. @details f must be a 0-1 %ADD. No new nodes are created. This
  88. function can be used, for instance, to check that g has a constant
  89. value (specified by h) whenever f is 1. If the constant value is
  90. unknown, then one should use Cudd_addEvalConst.
  91. @return a pointer to the resulting %ADD (which may or may not be
  92. constant) or DD_NON_CONSTANT.
  93. @sideeffect None
  94. @see Cudd_addIte Cudd_addEvalConst Cudd_bddIteConstant
  95. */
  96. DdNode *
  97. Cudd_addIteConstant(
  98. DdManager * dd,
  99. DdNode * f,
  100. DdNode * g,
  101. DdNode * h)
  102. {
  103. DdNode *one,*zero;
  104. DdNode *Fv,*Fnv,*Gv,*Gnv,*Hv,*Hnv,*r,*t,*e;
  105. int topf,topg,toph,v;
  106. statLine(dd);
  107. /* Trivial cases. */
  108. if (f == (one = DD_ONE(dd))) { /* ITE(1,G,H) = G */
  109. return(g);
  110. }
  111. if (f == (zero = DD_ZERO(dd))) { /* ITE(0,G,H) = H */
  112. return(h);
  113. }
  114. /* From now on, f is known not to be a constant. */
  115. addVarToConst(f,&g,&h,one,zero);
  116. /* Check remaining one variable cases. */
  117. if (g == h) { /* ITE(F,G,G) = G */
  118. return(g);
  119. }
  120. if (cuddIsConstant(g) && cuddIsConstant(h)) {
  121. return(DD_NON_CONSTANT);
  122. }
  123. topf = cuddI(dd,f->index);
  124. topg = cuddI(dd,g->index);
  125. toph = cuddI(dd,h->index);
  126. v = ddMin(topg,toph);
  127. /* ITE(F,G,H) = (x,G,H) (non constant) if F = (x,1,0), x < top(G,H). */
  128. if (topf < v && cuddIsConstant(cuddT(f)) && cuddIsConstant(cuddE(f))) {
  129. return(DD_NON_CONSTANT);
  130. }
  131. /* Check cache. */
  132. r = cuddConstantLookup(dd,DD_ADD_ITE_CONSTANT_TAG,f,g,h);
  133. if (r != NULL) {
  134. return(r);
  135. }
  136. /* Compute cofactors. */
  137. if (topf <= v) {
  138. v = ddMin(topf,v); /* v = top_var(F,G,H) */
  139. Fv = cuddT(f); Fnv = cuddE(f);
  140. } else {
  141. Fv = Fnv = f;
  142. }
  143. if (topg == v) {
  144. Gv = cuddT(g); Gnv = cuddE(g);
  145. } else {
  146. Gv = Gnv = g;
  147. }
  148. if (toph == v) {
  149. Hv = cuddT(h); Hnv = cuddE(h);
  150. } else {
  151. Hv = Hnv = h;
  152. }
  153. /* Recursive step. */
  154. t = Cudd_addIteConstant(dd,Fv,Gv,Hv);
  155. if (t == DD_NON_CONSTANT || !cuddIsConstant(t)) {
  156. cuddCacheInsert(dd, DD_ADD_ITE_CONSTANT_TAG, f, g, h, DD_NON_CONSTANT);
  157. return(DD_NON_CONSTANT);
  158. }
  159. e = Cudd_addIteConstant(dd,Fnv,Gnv,Hnv);
  160. if (e == DD_NON_CONSTANT || !cuddIsConstant(e) || t != e) {
  161. cuddCacheInsert(dd, DD_ADD_ITE_CONSTANT_TAG, f, g, h, DD_NON_CONSTANT);
  162. return(DD_NON_CONSTANT);
  163. }
  164. cuddCacheInsert(dd, DD_ADD_ITE_CONSTANT_TAG, f, g, h, t);
  165. return(t);
  166. } /* end of Cudd_addIteConstant */
  167. /**
  168. @brief Checks whether %ADD g is constant whenever %ADD f is 1.
  169. @details f must be a 0-1 %ADD. If f is identically 0, the check is
  170. assumed to be successful, and the background value is returned. No
  171. new nodes are created.
  172. @return a pointer to the resulting %ADD (which may or may not be
  173. constant) or DD_NON_CONSTANT.
  174. @sideeffect None
  175. @see Cudd_addIteConstant Cudd_addLeq
  176. */
  177. DdNode *
  178. Cudd_addEvalConst(
  179. DdManager * dd,
  180. DdNode * f,
  181. DdNode * g)
  182. {
  183. DdNode *zero;
  184. DdNode *Fv,*Fnv,*Gv,*Gnv,*r,*t,*e;
  185. int topf,topg;
  186. #ifdef DD_DEBUG
  187. assert(!Cudd_IsComplement(f));
  188. #endif
  189. statLine(dd);
  190. /* Terminal cases. */
  191. if (f == DD_ONE(dd) || cuddIsConstant(g)) {
  192. return(g);
  193. }
  194. if (f == (zero = DD_ZERO(dd))) {
  195. return(dd->background);
  196. }
  197. #ifdef DD_DEBUG
  198. assert(!cuddIsConstant(f));
  199. #endif
  200. /* From now on, f and g are known not to be constants. */
  201. topf = cuddI(dd,f->index);
  202. topg = cuddI(dd,g->index);
  203. /* Check cache. */
  204. r = cuddConstantLookup(dd,DD_ADD_EVAL_CONST_TAG,f,g,g);
  205. if (r != NULL) {
  206. return(r);
  207. }
  208. /* Compute cofactors. */
  209. if (topf <= topg) {
  210. Fv = cuddT(f); Fnv = cuddE(f);
  211. } else {
  212. Fv = Fnv = f;
  213. }
  214. if (topg <= topf) {
  215. Gv = cuddT(g); Gnv = cuddE(g);
  216. } else {
  217. Gv = Gnv = g;
  218. }
  219. /* Recursive step. */
  220. if (Fv != zero) {
  221. t = Cudd_addEvalConst(dd,Fv,Gv);
  222. if (t == DD_NON_CONSTANT || !cuddIsConstant(t)) {
  223. cuddCacheInsert2(dd, Cudd_addEvalConst, f, g, DD_NON_CONSTANT);
  224. return(DD_NON_CONSTANT);
  225. }
  226. if (Fnv != zero) {
  227. e = Cudd_addEvalConst(dd,Fnv,Gnv);
  228. if (e == DD_NON_CONSTANT || !cuddIsConstant(e) || t != e) {
  229. cuddCacheInsert2(dd, Cudd_addEvalConst, f, g, DD_NON_CONSTANT);
  230. return(DD_NON_CONSTANT);
  231. }
  232. }
  233. cuddCacheInsert2(dd,Cudd_addEvalConst,f,g,t);
  234. return(t);
  235. } else { /* Fnv must be != zero */
  236. e = Cudd_addEvalConst(dd,Fnv,Gnv);
  237. cuddCacheInsert2(dd, Cudd_addEvalConst, f, g, e);
  238. return(e);
  239. }
  240. } /* end of Cudd_addEvalConst */
  241. /**
  242. @brief Computes the complement of an %ADD a la C language.
  243. @details The complement of 0 is 1 and the complement of everything
  244. else is 0.
  245. @return a pointer to the resulting %ADD if successful; NULL otherwise.
  246. @sideeffect None
  247. @see Cudd_addNegate
  248. */
  249. DdNode *
  250. Cudd_addCmpl(
  251. DdManager * dd,
  252. DdNode * f)
  253. {
  254. DdNode *res;
  255. do {
  256. dd->reordered = 0;
  257. res = cuddAddCmplRecur(dd,f);
  258. } while (dd->reordered == 1);
  259. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  260. dd->timeoutHandler(dd, dd->tohArg);
  261. }
  262. return(res);
  263. } /* end of Cudd_addCmpl */
  264. /**
  265. @brief Determines whether f is less than or equal to g.
  266. @details No new nodes are created. This procedure works for arbitrary ADDs.
  267. For 0-1 ADDs Cudd_addEvalConst is more efficient.
  268. @return 1 if f is less than or equal to g; 0 otherwise.
  269. @sideeffect None
  270. @see Cudd_addIteConstant Cudd_addEvalConst Cudd_bddLeq
  271. */
  272. int
  273. Cudd_addLeq(
  274. DdManager * dd,
  275. DdNode * f,
  276. DdNode * g)
  277. {
  278. DdNode *tmp, *fv, *fvn, *gv, *gvn;
  279. int topf, topg, res;
  280. /* Terminal cases. */
  281. if (f == g) return(1);
  282. statLine(dd);
  283. if (cuddIsConstant(f)) {
  284. if (cuddIsConstant(g)) return(cuddV(f) <= cuddV(g));
  285. if (f == DD_MINUS_INFINITY(dd)) return(1);
  286. if (f == DD_PLUS_INFINITY(dd)) return(0); /* since f != g */
  287. }
  288. if (g == DD_PLUS_INFINITY(dd)) return(1);
  289. if (g == DD_MINUS_INFINITY(dd)) return(0); /* since f != g */
  290. /* Check cache. */
  291. tmp = cuddCacheLookup2(dd,(DD_CTFP)Cudd_addLeq,f,g);
  292. if (tmp != NULL) {
  293. return(tmp == DD_ONE(dd));
  294. }
  295. /* Compute cofactors. One of f and g is not constant. */
  296. topf = cuddI(dd,f->index);
  297. topg = cuddI(dd,g->index);
  298. if (topf <= topg) {
  299. fv = cuddT(f); fvn = cuddE(f);
  300. } else {
  301. fv = fvn = f;
  302. }
  303. if (topg <= topf) {
  304. gv = cuddT(g); gvn = cuddE(g);
  305. } else {
  306. gv = gvn = g;
  307. }
  308. res = Cudd_addLeq(dd,fvn,gvn) && Cudd_addLeq(dd,fv,gv);
  309. /* Store result in cache and return. */
  310. cuddCacheInsert2(dd,(DD_CTFP) Cudd_addLeq,f,g,
  311. Cudd_NotCond(DD_ONE(dd),res==0));
  312. return(res);
  313. } /* end of Cudd_addLeq */
  314. /*---------------------------------------------------------------------------*/
  315. /* Definition of internal functions */
  316. /*---------------------------------------------------------------------------*/
  317. /**
  318. @brief Implements the recursive step of Cudd_addIte(f,g,h).
  319. @return a pointer to the resulting %ADD if successful; NULL
  320. otherwise.
  321. @sideeffect None
  322. @see Cudd_addIte
  323. */
  324. DdNode *
  325. cuddAddIteRecur(
  326. DdManager * dd,
  327. DdNode * f,
  328. DdNode * g,
  329. DdNode * h)
  330. {
  331. DdNode *one,*zero;
  332. DdNode *r,*Fv,*Fnv,*Gv,*Gnv,*Hv,*Hnv,*t,*e;
  333. int topf,topg,toph,v;
  334. unsigned int index;
  335. statLine(dd);
  336. /* Trivial cases. */
  337. /* One variable cases. */
  338. if (f == (one = DD_ONE(dd))) { /* ITE(1,G,H) = G */
  339. return(g);
  340. }
  341. if (f == (zero = DD_ZERO(dd))) { /* ITE(0,G,H) = H */
  342. return(h);
  343. }
  344. /* From now on, f is known to not be a constant. */
  345. addVarToConst(f,&g,&h,one,zero);
  346. /* Check remaining one variable cases. */
  347. if (g == h) { /* ITE(F,G,G) = G */
  348. return(g);
  349. }
  350. if (g == one) { /* ITE(F,1,0) = F */
  351. if (h == zero) return(f);
  352. }
  353. topf = cuddI(dd,f->index);
  354. topg = cuddI(dd,g->index);
  355. toph = cuddI(dd,h->index);
  356. v = ddMin(topg,toph);
  357. /* A shortcut: ITE(F,G,H) = (x,G,H) if F=(x,1,0), x < top(G,H). */
  358. if (topf < v && cuddT(f) == one && cuddE(f) == zero) {
  359. r = cuddUniqueInter(dd,(int)f->index,g,h);
  360. return(r);
  361. }
  362. if (topf < v && cuddT(f) == zero && cuddE(f) == one) {
  363. r = cuddUniqueInter(dd,(int)f->index,h,g);
  364. return(r);
  365. }
  366. /* Check cache. */
  367. r = cuddCacheLookup(dd,DD_ADD_ITE_TAG,f,g,h);
  368. if (r != NULL) {
  369. return(r);
  370. }
  371. checkWhetherToGiveUp(dd);
  372. /* Compute cofactors. */
  373. index = f->index;
  374. if (topf <= v) {
  375. v = ddMin(topf,v); /* v = top_var(F,G,H) */
  376. Fv = cuddT(f); Fnv = cuddE(f);
  377. } else {
  378. Fv = Fnv = f;
  379. }
  380. if (topg == v) {
  381. index = g->index;
  382. Gv = cuddT(g); Gnv = cuddE(g);
  383. } else {
  384. Gv = Gnv = g;
  385. }
  386. if (toph == v) {
  387. index = h->index;
  388. Hv = cuddT(h); Hnv = cuddE(h);
  389. } else {
  390. Hv = Hnv = h;
  391. }
  392. /* Recursive step. */
  393. t = cuddAddIteRecur(dd,Fv,Gv,Hv);
  394. if (t == NULL) return(NULL);
  395. cuddRef(t);
  396. e = cuddAddIteRecur(dd,Fnv,Gnv,Hnv);
  397. if (e == NULL) {
  398. Cudd_RecursiveDeref(dd,t);
  399. return(NULL);
  400. }
  401. cuddRef(e);
  402. r = (t == e) ? t : cuddUniqueInter(dd,index,t,e);
  403. if (r == NULL) {
  404. Cudd_RecursiveDeref(dd,t);
  405. Cudd_RecursiveDeref(dd,e);
  406. return(NULL);
  407. }
  408. cuddDeref(t);
  409. cuddDeref(e);
  410. cuddCacheInsert(dd,DD_ADD_ITE_TAG,f,g,h,r);
  411. return(r);
  412. } /* end of cuddAddIteRecur */
  413. /**
  414. @brief Performs the recursive step of Cudd_addCmpl.
  415. @return a pointer to the resulting %ADD if successful; NULL
  416. otherwise.
  417. @sideeffect None
  418. @see Cudd_addCmpl
  419. */
  420. DdNode *
  421. cuddAddCmplRecur(
  422. DdManager * dd,
  423. DdNode * f)
  424. {
  425. DdNode *one,*zero;
  426. DdNode *r,*Fv,*Fnv,*t,*e;
  427. statLine(dd);
  428. one = DD_ONE(dd);
  429. zero = DD_ZERO(dd);
  430. if (cuddIsConstant(f)) {
  431. if (f == zero) {
  432. return(one);
  433. } else {
  434. return(zero);
  435. }
  436. }
  437. r = cuddCacheLookup1(dd,Cudd_addCmpl,f);
  438. if (r != NULL) {
  439. return(r);
  440. }
  441. checkWhetherToGiveUp(dd);
  442. Fv = cuddT(f);
  443. Fnv = cuddE(f);
  444. t = cuddAddCmplRecur(dd,Fv);
  445. if (t == NULL) return(NULL);
  446. cuddRef(t);
  447. e = cuddAddCmplRecur(dd,Fnv);
  448. if (e == NULL) {
  449. Cudd_RecursiveDeref(dd,t);
  450. return(NULL);
  451. }
  452. cuddRef(e);
  453. r = (t == e) ? t : cuddUniqueInter(dd,(int)f->index,t,e);
  454. if (r == NULL) {
  455. Cudd_RecursiveDeref(dd, t);
  456. Cudd_RecursiveDeref(dd, e);
  457. return(NULL);
  458. }
  459. cuddDeref(t);
  460. cuddDeref(e);
  461. cuddCacheInsert1(dd,Cudd_addCmpl,f,r);
  462. return(r);
  463. } /* end of cuddAddCmplRecur */
  464. /*---------------------------------------------------------------------------*/
  465. /* Definition of static functions */
  466. /*---------------------------------------------------------------------------*/
  467. /**
  468. @brief Replaces variables with constants if possible (part of
  469. canonical form).
  470. @sideeffect None
  471. */
  472. static void
  473. addVarToConst(
  474. DdNode * f,
  475. DdNode ** gp,
  476. DdNode ** hp,
  477. DdNode * one,
  478. DdNode * zero)
  479. {
  480. DdNode *g = *gp;
  481. DdNode *h = *hp;
  482. if (f == g) { /* ITE(F,F,H) = ITE(F,1,H) = F + H */
  483. *gp = one;
  484. }
  485. if (f == h) { /* ITE(F,G,F) = ITE(F,G,0) = F * G */
  486. *hp = zero;
  487. }
  488. } /* end of addVarToConst */