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.

578 lines
14 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief This program tests selected features of CUDD.
  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 "epd.h"
  36. #include "cudd.h"
  37. #include <setjmp.h>
  38. /** \cond */
  39. static int testBdd(int verbosity);
  40. static int testAdd(int verbosity);
  41. static int testZdd(int verbosity);
  42. static int testApa(int verbosity);
  43. static int testCount(int verbosity);
  44. static int testLdbl(int verbosity);
  45. static int testTimeout(int verbosity);
  46. static void timeoutHandler(DdManager * dd, void * arg);
  47. /** \endcond */
  48. /**
  49. * @brief Main program for testextra.
  50. */
  51. int main(int argc, char const * const * argv)
  52. {
  53. int verbosity = 0;
  54. if (argc == 2) {
  55. int nread;
  56. int ret = sscanf(argv[1], "%d%n", &verbosity, &nread);
  57. if (ret != 1 || argv[1][nread]) {
  58. fprintf(stderr, "Usage: %s [verbosity]\n", argv[0]);
  59. return -1;
  60. }
  61. }
  62. if (testBdd(verbosity) != 0)
  63. return -1;
  64. if (testAdd(verbosity) != 0)
  65. return -1;
  66. if (testZdd(verbosity) != 0)
  67. return -1;
  68. if (testApa(verbosity) != 0)
  69. return -1;
  70. if (testCount(verbosity) != 0)
  71. return -1;
  72. if (testLdbl(verbosity) != 0)
  73. return -1;
  74. if (testTimeout(verbosity) != 0)
  75. return -1;
  76. return 0;
  77. }
  78. /**
  79. * @brief Basic BDD test.
  80. * @return 0 if successful; -1 otherwise.
  81. */
  82. static int
  83. testBdd(int verbosity)
  84. {
  85. DdManager *dd;
  86. DdNode *f, *var, *tmp;
  87. int i, ret;
  88. dd = Cudd_Init(0, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0);
  89. if (!dd) {
  90. if (verbosity) {
  91. printf("initialization failed\n");
  92. }
  93. return -1;
  94. }
  95. if (verbosity) {
  96. printf("Started CUDD version ");
  97. Cudd_PrintVersion(stdout);
  98. }
  99. f = Cudd_ReadOne(dd);
  100. Cudd_Ref(f);
  101. for (i = 3; i >= 0; i--) {
  102. var = Cudd_bddIthVar(dd, i);
  103. tmp = Cudd_bddAnd(dd, Cudd_Not(var), f);
  104. if (!tmp) {
  105. if (verbosity) {
  106. printf("computation failed\n");
  107. }
  108. return -1;
  109. }
  110. Cudd_Ref(tmp);
  111. Cudd_RecursiveDeref(dd, f);
  112. f = tmp;
  113. }
  114. if (verbosity) {
  115. Cudd_bddPrintCover(dd, f, f);
  116. }
  117. Cudd_RecursiveDeref(dd, f);
  118. ret = Cudd_CheckZeroRef(dd);
  119. if (ret != 0 && verbosity) {
  120. printf("%d unexpected non-zero references\n", ret);
  121. }
  122. Cudd_Quit(dd);
  123. return 0;
  124. }
  125. /**
  126. * @brief Basic ADD test.
  127. * @return 0 if successful; -1 otherwise.
  128. */
  129. static int
  130. testAdd(int verbosity)
  131. {
  132. DdManager *manager;
  133. DdNode *f, *var, *tmp, *bg;
  134. int i, ret;
  135. CUDD_VALUE_TYPE pinf;
  136. manager = Cudd_Init(0, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0);
  137. if (!manager) {
  138. if (verbosity) {
  139. printf("initialization failed\n");
  140. }
  141. return -1;
  142. }
  143. pinf = Cudd_V(Cudd_ReadPlusInfinity(manager));
  144. if (verbosity) {
  145. printf("Plus infinity is %g\n", pinf);
  146. }
  147. f = Cudd_addConst(manager,5);
  148. Cudd_Ref(f);
  149. for (i = 3; i >= 0; i--) {
  150. var = Cudd_addIthVar(manager,i);
  151. Cudd_Ref(var);
  152. tmp = Cudd_addApply(manager,Cudd_addTimes,var,f);
  153. Cudd_Ref(tmp);
  154. Cudd_RecursiveDeref(manager,f);
  155. Cudd_RecursiveDeref(manager,var);
  156. f = tmp;
  157. }
  158. if (verbosity) {
  159. Cudd_PrintMinterm(manager, f);
  160. printf("\n");
  161. }
  162. Cudd_RecursiveDeref(manager, f);
  163. bg = Cudd_ReadBackground(manager);
  164. if (verbosity) {
  165. printf("background (%g) minterms : ", Cudd_V(bg));
  166. Cudd_ApaPrintMinterm(Cudd_ReadStdout(manager), manager, bg, 0);
  167. }
  168. ret = Cudd_CheckZeroRef(manager);
  169. if (ret != 0 && verbosity) {
  170. printf("%d non-zero ADD reference counts after dereferencing\n", ret);
  171. }
  172. Cudd_Quit(manager);
  173. return ret != 0;
  174. }
  175. /**
  176. * @brief Basic test of ZDDs.
  177. * @return 0 if successful; -1 otherwise.
  178. */
  179. static int
  180. testZdd(int verbosity)
  181. {
  182. DdManager *manager;
  183. DdNode *f, *var, *tmp;
  184. int i, ret;
  185. manager = Cudd_Init(0,4,CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS,0);
  186. if (!manager) {
  187. if (verbosity) {
  188. printf("initialization failed\n");
  189. }
  190. return -1;
  191. }
  192. tmp = Cudd_ReadZddOne(manager,0);
  193. Cudd_Ref(tmp);
  194. for (i = 3; i >= 0; i--) {
  195. var = Cudd_zddIthVar(manager,i);
  196. Cudd_Ref(var);
  197. f = Cudd_zddIntersect(manager,var,tmp);
  198. Cudd_Ref(f);
  199. Cudd_RecursiveDerefZdd(manager,tmp);
  200. Cudd_RecursiveDerefZdd(manager,var);
  201. tmp = f;
  202. }
  203. f = Cudd_zddDiff(manager,Cudd_ReadZddOne(manager,0),tmp);
  204. Cudd_Ref(f);
  205. Cudd_RecursiveDerefZdd(manager,tmp);
  206. if (verbosity) {
  207. Cudd_zddPrintMinterm(manager,f);
  208. printf("\n");
  209. }
  210. Cudd_RecursiveDerefZdd(manager,f);
  211. ret = Cudd_CheckZeroRef(manager);
  212. if (ret != 0 && verbosity) {
  213. printf("%d unexpected non-zero references\n", ret);
  214. }
  215. Cudd_Quit(manager);
  216. return 0;
  217. }
  218. /**
  219. * @brief Basic test of arbitrary-precision arithmetic.
  220. * @return 0 if successful; -1 otherwise.
  221. */
  222. static int
  223. testApa(int verbosity)
  224. {
  225. if (verbosity) {
  226. printf("DD_APA_BITS = %" PRIszt "\n", sizeof(DdApaDigit) * 8);
  227. }
  228. DdApaNumber an = Cudd_NewApaNumber(3);
  229. Cudd_ApaSetToLiteral(3, an, (DdApaDigit) 0x0fa5);
  230. Cudd_ApaAdd(3, an, an, an);
  231. if (verbosity) {
  232. Cudd_ApaPrintHex(stdout, 3, an);
  233. printf("\n");
  234. }
  235. DdApaDigit numbers[] = {1283805, 1283815, 15983557, 1598354, 15999999};
  236. size_t i;
  237. for (i = 0; i < sizeof(numbers)/sizeof(DdApaDigit); i++) {
  238. Cudd_ApaSetToLiteral(3, an, numbers[i]);
  239. if (verbosity) {
  240. Cudd_ApaPrintDecimal(stdout, 3, an);
  241. printf(" -> ");
  242. Cudd_ApaPrintExponential(stdout, 3, an, 6);
  243. printf("\n");
  244. }
  245. }
  246. Cudd_FreeApaNumber(an);
  247. return 0;
  248. }
  249. /**
  250. * @brief Basic test of Cudd_CountMinterm().
  251. * @return 0 if successful; -1 otherwise.
  252. */
  253. static int
  254. testCount(int verbosity)
  255. {
  256. DdManager *dd;
  257. DdNode *h;
  258. int i, ret;
  259. int const N = 2044; /* number of variables */
  260. dd = Cudd_Init(N, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0);
  261. if (!dd) {
  262. if (verbosity) {
  263. printf("initialization failed\n");
  264. }
  265. return -1;
  266. }
  267. /* Build a cube with N/2 variables. */
  268. h = Cudd_ReadOne(dd);
  269. Cudd_Ref(h);
  270. for (i = 0; i < N; i += 2) {
  271. DdNode *var, *tmp;
  272. var = Cudd_bddIthVar(dd, N-i-1);
  273. tmp = Cudd_bddAnd(dd, h, var);
  274. if (!tmp) {
  275. Cudd_Quit(dd);
  276. return -1;
  277. }
  278. Cudd_Ref(tmp);
  279. Cudd_RecursiveDeref(dd, h);
  280. h = tmp;
  281. }
  282. if (verbosity) {
  283. printf("h (dbl) ");
  284. Cudd_PrintDebug(dd, h, N, 1);
  285. printf("h (apa) ");
  286. Cudd_PrintSummary(dd, h, N, 1);
  287. }
  288. Cudd_RecursiveDeref(dd, h);
  289. if (verbosity) {
  290. printf("one[%d] (dbl) ", N);
  291. Cudd_PrintDebug(dd, Cudd_ReadOne(dd), N, 1);
  292. printf("one[%d] (apa) ", N);
  293. Cudd_PrintSummary(dd, Cudd_ReadOne(dd), N, 1);
  294. ret = Cudd_CheckZeroRef(dd);
  295. printf("one[%d] (dbl) ", N+1);
  296. Cudd_PrintDebug(dd, Cudd_ReadOne(dd), N+1, 1);
  297. printf("one[%d] (apa) ", N+1);
  298. Cudd_PrintSummary(dd, Cudd_ReadOne(dd), N+1, 1);
  299. ret = Cudd_CheckZeroRef(dd);
  300. }
  301. if (verbosity && ret != 0) {
  302. printf("%d non-zero references\n", ret);
  303. }
  304. Cudd_Quit(dd);
  305. return 0;
  306. }
  307. /**
  308. * @brief Basic test of long double and EPD minterm computation.
  309. * @return 0 if successful; -1 otherwise.
  310. */
  311. static int
  312. testLdbl(int verbosity)
  313. {
  314. DdManager *dd;
  315. DdNode *f, *g;
  316. int i, ret;
  317. int const N = 12; /* half the number of variables */
  318. long double cnt;
  319. dd = Cudd_Init(2*N, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0);
  320. if (!dd) {
  321. if (verbosity) {
  322. printf("initialization failed\n");
  323. }
  324. return -1;
  325. }
  326. f = g = Cudd_ReadOne(dd);
  327. Cudd_Ref(f);
  328. Cudd_Ref(g);
  329. for (i = 0; i < N; i++) {
  330. DdNode *var1, *var2, *clause, *tmp;
  331. var1 = Cudd_bddIthVar(dd, i);
  332. var2 = Cudd_bddIthVar(dd, i+N);
  333. clause = Cudd_bddOr(dd, var1, var2);
  334. if (!clause) {
  335. Cudd_Quit(dd);
  336. return -1;
  337. }
  338. Cudd_Ref(clause);
  339. tmp = Cudd_bddAnd(dd, f, clause);
  340. if (!tmp) {
  341. Cudd_Quit(dd);
  342. return -1;
  343. }
  344. Cudd_Ref(tmp);
  345. Cudd_RecursiveDeref(dd, clause);
  346. Cudd_RecursiveDeref(dd, f);
  347. f = tmp;
  348. clause = Cudd_bddOr(dd, Cudd_Not(var1), Cudd_Not(var2));
  349. if (!clause) {
  350. Cudd_Quit(dd);
  351. return -1;
  352. }
  353. Cudd_Ref(clause);
  354. tmp = Cudd_bddAnd(dd, g, clause);
  355. if (!tmp) {
  356. Cudd_Quit(dd);
  357. return -1;
  358. }
  359. Cudd_Ref(tmp);
  360. Cudd_RecursiveDeref(dd, clause);
  361. Cudd_RecursiveDeref(dd, g);
  362. g = tmp;
  363. }
  364. if (verbosity) {
  365. printf("f");
  366. Cudd_PrintSummary(dd, f, 2*N, 0);
  367. }
  368. cnt = Cudd_LdblCountMinterm(dd, f, 2*N);
  369. if (verbosity) {
  370. printf("f has %Lg minterms\n", cnt);
  371. }
  372. if (verbosity) {
  373. printf("EPD count for f = ");
  374. ret = Cudd_EpdPrintMinterm(dd, f, 2*N);
  375. printf("\n");
  376. if (!ret) {
  377. printf("problem with EPD\n");
  378. }
  379. }
  380. Cudd_RecursiveDeref(dd, f);
  381. if (verbosity) {
  382. printf("g");
  383. Cudd_PrintSummary(dd, g, 2*N, 0);
  384. }
  385. cnt = Cudd_LdblCountMinterm(dd, g, 2*N);
  386. if (verbosity) {
  387. printf("g has %Lg minterms\n", cnt);
  388. }
  389. if (verbosity) {
  390. printf("EPD count for g = ");
  391. ret = Cudd_EpdPrintMinterm(dd, g, 2*N);
  392. printf("\n");
  393. if (!ret) {
  394. printf("problem with EPD\n");
  395. }
  396. }
  397. Cudd_RecursiveDeref(dd, g);
  398. ret = Cudd_CheckZeroRef(dd);
  399. if (verbosity && ret != 0) {
  400. printf("%d non-zero references\n", ret);
  401. }
  402. Cudd_Quit(dd);
  403. return 0;
  404. }
  405. /**
  406. * @brief Basic test of timeout handler.
  407. *
  408. * @details Sets a short timeout and then tries to build a function
  409. * with a large BDD. Strives to avoid leaking nodes.
  410. *
  411. * @return 0 if successful; -1 otherwise.
  412. */
  413. static int
  414. testTimeout(int verbosity)
  415. {
  416. DdManager *dd;
  417. /* Declare these "volatile" to prevent clobbering by longjmp. */
  418. DdNode * volatile f;
  419. DdNode * volatile clause = NULL;
  420. DdNode * var1, * var2;
  421. int i, ret, count;
  422. int const N = 20; /* half the number of variables in f */
  423. unsigned long timeout = 100UL; /* in milliseconds */
  424. jmp_buf timeoutEnv;
  425. dd = Cudd_Init(0, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0);
  426. if (!dd) {
  427. if (verbosity) {
  428. printf("initialization failed\n");
  429. }
  430. return -1;
  431. }
  432. /* Set up timeout handling. */
  433. if (setjmp(timeoutEnv) > 0) {
  434. if (verbosity) {
  435. printf("caught timeout\n");
  436. }
  437. /* The nodes of clause may be leaked if the timeout was
  438. * detected while conjoining the clause to f. We set
  439. * clause to NULL when it's not in use to be able to
  440. * detect this case.
  441. */
  442. if (clause)
  443. Cudd_RecursiveDeref(dd, clause);
  444. goto finally;
  445. }
  446. (void) Cudd_RegisterTimeoutHandler(dd, timeoutHandler, (void *) &timeoutEnv);
  447. (void) Cudd_SetTimeLimit(dd, timeout);
  448. /* Try to build function. This is expected to run out of time. */
  449. f = Cudd_ReadOne(dd);
  450. Cudd_Ref(f);
  451. for (i = 0; i < N; i++) {
  452. DdNode * tmp;
  453. var1 = Cudd_bddIthVar(dd, i);
  454. if (!var1) {
  455. if (verbosity) {
  456. printf("computation failed\n");
  457. return -1;
  458. }
  459. }
  460. var2 = Cudd_bddIthVar(dd, i+N);
  461. if (!var2) {
  462. if (verbosity) {
  463. printf("computation failed\n");
  464. return -1;
  465. }
  466. }
  467. clause = Cudd_bddOr(dd, var1, var2);
  468. if (!clause) {
  469. if (verbosity) {
  470. printf("computation failed\n");
  471. }
  472. return -1;
  473. }
  474. Cudd_Ref(clause);
  475. tmp = Cudd_bddAnd(dd, f, clause);
  476. if (!tmp) {
  477. if (verbosity) {
  478. printf("computation failed\n");
  479. }
  480. return -1;
  481. }
  482. Cudd_Ref(tmp);
  483. Cudd_RecursiveDeref(dd, clause);
  484. clause = NULL;
  485. Cudd_RecursiveDeref(dd, f);
  486. f = tmp;
  487. }
  488. if (verbosity > 1) {
  489. Cudd_bddPrintCover(dd, f, f);
  490. }
  491. finally:
  492. if (verbosity) {
  493. printf("so far");
  494. Cudd_PrintSummary(dd, f, 2*N, 0);
  495. }
  496. count = 0;
  497. for (i = 0; i < N-1; i += 2) {
  498. var1 = Cudd_bddIthVar(dd, i);
  499. if (!var1) {
  500. printf("computation failed\n");
  501. return -1;
  502. }
  503. var2 = Cudd_bddIthVar(dd, i+1);
  504. if (!var2) {
  505. printf("computation failed\n");
  506. return -1;
  507. }
  508. clause = Cudd_bddOr(dd, var1, var2);
  509. if (!clause) {
  510. printf("computation failed\n");
  511. return -1;
  512. }
  513. Cudd_Ref(clause);
  514. if (Cudd_bddLeq(dd, f, clause)) {
  515. count++;
  516. }
  517. Cudd_RecursiveDeref(dd, clause);
  518. }
  519. if (verbosity) {
  520. printf("f implies %d clauses\n", count);
  521. }
  522. Cudd_RecursiveDeref(dd, f);
  523. ret = Cudd_CheckZeroRef(dd);
  524. if (verbosity) {
  525. Cudd_PrintInfo(dd, stdout);
  526. if (ret != 0) {
  527. printf("%d non-zero references\n", ret);
  528. }
  529. }
  530. Cudd_Quit(dd);
  531. return 0;
  532. }
  533. /**
  534. * @brief Timeout handler.
  535. */
  536. static void
  537. timeoutHandler(DdManager * dd, void * arg)
  538. {
  539. jmp_buf * timeoutEnv = (jmp_buf *) arg;
  540. /* Reset manager. */
  541. Cudd_ClearErrorCode(dd);
  542. Cudd_UnsetTimeLimit(dd);
  543. Cudd_RegisterTimeoutHandler(dd, NULL, NULL);
  544. longjmp(*timeoutEnv, 1);
  545. }