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.

537 lines
16 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Functions to read in a matrix
  5. @see cudd_addHarwell.c
  6. @author Fabio Somenzi
  7. @copyright@parblock
  8. Copyright (c) 1995-2015, Regents of the University of Colorado
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. Neither the name of the University of Colorado nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. POSSIBILITY OF SUCH DAMAGE.
  33. @endparblock
  34. */
  35. #include "util.h"
  36. #include "cuddInt.h"
  37. /*---------------------------------------------------------------------------*/
  38. /* Constant declarations */
  39. /*---------------------------------------------------------------------------*/
  40. /*---------------------------------------------------------------------------*/
  41. /* Stucture declarations */
  42. /*---------------------------------------------------------------------------*/
  43. /*---------------------------------------------------------------------------*/
  44. /* Type declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /*---------------------------------------------------------------------------*/
  47. /* Variable declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Macro declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /** \cond */
  53. /*---------------------------------------------------------------------------*/
  54. /* Static function prototypes */
  55. /*---------------------------------------------------------------------------*/
  56. /** \endcond */
  57. /*---------------------------------------------------------------------------*/
  58. /* Definition of exported functions */
  59. /*---------------------------------------------------------------------------*/
  60. /**
  61. @brief Reads in a sparse matrix.
  62. @details Reads in a sparse matrix specified in a simple format.
  63. The first line of the input contains the numbers of rows and columns.
  64. The remaining lines contain the elements of the matrix, one per line.
  65. Given a background value
  66. (specified by the background field of the manager), only the values
  67. different from it are explicitly listed. Each foreground element is
  68. described by two integers, i.e., the row and column number, and a
  69. real number, i.e., the value.<p>
  70. Cudd_addRead produces an %ADD that depends on two sets of variables: x
  71. and y. The x variables (x\[0\] ... x\[nx-1\]) encode the row index and
  72. the y variables (y\[0\] ... y\[ny-1\]) encode the column index.
  73. x\[0\] and y\[0\] are the most significant bits in the indices.
  74. The variables may already exist or may be created by the function.
  75. The index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy.<p>
  76. On input, nx and ny hold the numbers
  77. of row and column variables already in existence. On output, they
  78. hold the numbers of row and column variables actually used by the
  79. matrix. When Cudd_addRead creates the variable arrays,
  80. the index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy.
  81. When some variables already exist Cudd_addRead expects the indices
  82. of the existing x variables to be bx+i*sx, and the indices of the
  83. existing y variables to be by+i*sy.<p>
  84. m and n are set to the numbers of rows and columns of the
  85. matrix. Their values on input are immaterial.
  86. The %ADD for the sparse matrix is returned in E, and its reference
  87. count is > 0.
  88. @return 1 in case of success; 0 otherwise.
  89. @sideeffect nx and ny are set to the numbers of row and column
  90. variables. m and n are set to the numbers of rows and columns. x and y
  91. are possibly extended to represent the array of row and column
  92. variables. Similarly for xn and yn_, which hold on return from
  93. Cudd_addRead the complements of the row and column variables.
  94. @see Cudd_addHarwell Cudd_bddRead
  95. */
  96. int
  97. Cudd_addRead(
  98. FILE * fp /**< input file pointer */,
  99. DdManager * dd /**< %DD manager */,
  100. DdNode ** E /**< characteristic function of the graph */,
  101. DdNode *** x /**< array of row variables */,
  102. DdNode *** y /**< array of column variables */,
  103. DdNode *** xn /**< array of complemented row variables */,
  104. DdNode *** yn_ /**< array of complemented column variables */,
  105. int * nx /**< number or row variables */,
  106. int * ny /**< number or column variables */,
  107. int * m /**< number of rows */,
  108. int * n /**< number of columns */,
  109. int bx /**< first index of row variables */,
  110. int sx /**< step of row variables */,
  111. int by /**< first index of column variables */,
  112. int sy /**< step of column variables */)
  113. {
  114. DdNode *one, *zero;
  115. DdNode *w, *neW;
  116. DdNode *minterm1;
  117. int u, v, err, i, nv;
  118. int lnx, lny;
  119. CUDD_VALUE_TYPE val;
  120. DdNode **lx, **ly, **lxn, **lyn;
  121. one = DD_ONE(dd);
  122. zero = DD_ZERO(dd);
  123. err = fscanf(fp, "%d %d", &u, &v);
  124. if (err == EOF) {
  125. return(0);
  126. } else if (err != 2) {
  127. return(0);
  128. }
  129. *m = u;
  130. /* Compute the number of x variables. */
  131. lx = *x; lxn = *xn;
  132. u--; /* row and column numbers start from 0 */
  133. for (lnx=0; u > 0; lnx++) {
  134. u >>= 1;
  135. }
  136. /* Here we rely on the fact that REALLOC of a null pointer is
  137. ** translates to an ALLOC.
  138. */
  139. if (lnx > *nx) {
  140. *x = lx = REALLOC(DdNode *, *x, lnx);
  141. if (lx == NULL) {
  142. dd->errorCode = CUDD_MEMORY_OUT;
  143. return(0);
  144. }
  145. *xn = lxn = REALLOC(DdNode *, *xn, lnx);
  146. if (lxn == NULL) {
  147. dd->errorCode = CUDD_MEMORY_OUT;
  148. return(0);
  149. }
  150. }
  151. *n = v;
  152. /* Compute the number of y variables. */
  153. ly = *y; lyn = *yn_;
  154. v--; /* row and column numbers start from 0 */
  155. for (lny=0; v > 0; lny++) {
  156. v >>= 1;
  157. }
  158. /* Here we rely on the fact that REALLOC of a null pointer is
  159. ** translates to an ALLOC.
  160. */
  161. if (lny > *ny) {
  162. *y = ly = REALLOC(DdNode *, *y, lny);
  163. if (ly == NULL) {
  164. dd->errorCode = CUDD_MEMORY_OUT;
  165. return(0);
  166. }
  167. *yn_ = lyn = REALLOC(DdNode *, *yn_, lny);
  168. if (lyn == NULL) {
  169. dd->errorCode = CUDD_MEMORY_OUT;
  170. return(0);
  171. }
  172. }
  173. /* Create all new variables. */
  174. for (i = *nx, nv = bx + (*nx) * sx; i < lnx; i++, nv += sx) {
  175. do {
  176. dd->reordered = 0;
  177. lx[i] = cuddUniqueInter(dd, nv, one, zero);
  178. } while (dd->reordered == 1);
  179. if (lx[i] == NULL) {
  180. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  181. dd->timeoutHandler(dd, dd->tohArg);
  182. }
  183. return(0);
  184. }
  185. cuddRef(lx[i]);
  186. do {
  187. dd->reordered = 0;
  188. lxn[i] = cuddUniqueInter(dd, nv, zero, one);
  189. } while (dd->reordered == 1);
  190. if (lxn[i] == NULL) {
  191. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  192. dd->timeoutHandler(dd, dd->tohArg);
  193. }
  194. return(0);
  195. }
  196. cuddRef(lxn[i]);
  197. }
  198. for (i = *ny, nv = by + (*ny) * sy; i < lny; i++, nv += sy) {
  199. do {
  200. dd->reordered = 0;
  201. ly[i] = cuddUniqueInter(dd, nv, one, zero);
  202. } while (dd->reordered == 1);
  203. if (ly[i] == NULL) {
  204. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  205. dd->timeoutHandler(dd, dd->tohArg);
  206. }
  207. return(0);
  208. }
  209. cuddRef(ly[i]);
  210. do {
  211. dd->reordered = 0;
  212. lyn[i] = cuddUniqueInter(dd, nv, zero, one);
  213. } while (dd->reordered == 1);
  214. if (lyn[i] == NULL) {
  215. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  216. dd->timeoutHandler(dd, dd->tohArg);
  217. }
  218. return(0);
  219. }
  220. cuddRef(lyn[i]);
  221. }
  222. *nx = lnx;
  223. *ny = lny;
  224. *E = dd->background; /* this call will never cause reordering */
  225. cuddRef(*E);
  226. while (! feof(fp)) {
  227. err = fscanf(fp, "%d %d %lf", &u, &v, &val);
  228. if (err == EOF) {
  229. break;
  230. } else if (err != 3) {
  231. return(0);
  232. } else if (u >= *m || v >= *n || u < 0 || v < 0) {
  233. return(0);
  234. }
  235. minterm1 = one; cuddRef(minterm1);
  236. /* Build minterm1 corresponding to this arc */
  237. for (i = lnx - 1; i>=0; i--) {
  238. if (u & 1) {
  239. w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lx[i]);
  240. } else {
  241. w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lxn[i]);
  242. }
  243. if (w == NULL) {
  244. Cudd_RecursiveDeref(dd, minterm1);
  245. return(0);
  246. }
  247. cuddRef(w);
  248. Cudd_RecursiveDeref(dd, minterm1);
  249. minterm1 = w;
  250. u >>= 1;
  251. }
  252. for (i = lny - 1; i>=0; i--) {
  253. if (v & 1) {
  254. w = Cudd_addApply(dd, Cudd_addTimes, minterm1, ly[i]);
  255. } else {
  256. w = Cudd_addApply(dd, Cudd_addTimes, minterm1, lyn[i]);
  257. }
  258. if (w == NULL) {
  259. Cudd_RecursiveDeref(dd, minterm1);
  260. return(0);
  261. }
  262. cuddRef(w);
  263. Cudd_RecursiveDeref(dd, minterm1);
  264. minterm1 = w;
  265. v >>= 1;
  266. }
  267. /* Create new constant node if necessary.
  268. ** This call will never cause reordering.
  269. */
  270. neW = cuddUniqueConst(dd, val);
  271. if (neW == NULL) {
  272. Cudd_RecursiveDeref(dd, minterm1);
  273. return(0);
  274. }
  275. cuddRef(neW);
  276. w = Cudd_addIte(dd, minterm1, neW, *E);
  277. if (w == NULL) {
  278. Cudd_RecursiveDeref(dd, minterm1);
  279. Cudd_RecursiveDeref(dd, neW);
  280. return(0);
  281. }
  282. cuddRef(w);
  283. Cudd_RecursiveDeref(dd, minterm1);
  284. Cudd_RecursiveDeref(dd, neW);
  285. Cudd_RecursiveDeref(dd, *E);
  286. *E = w;
  287. }
  288. return(1);
  289. } /* end of Cudd_addRead */
  290. /**
  291. @brief Reads in a graph (without labels) given as a list of arcs.
  292. @details Reads in a graph (without labels) given as an adjacency
  293. matrix. The first line of the input contains the numbers of rows and
  294. columns of the adjacency matrix. The remaining lines contain the arcs
  295. of the graph, one per line. Each arc is described by two integers,
  296. i.e., the row and column number, or the indices of the two endpoints.
  297. Cudd_bddRead produces a %BDD that depends on two sets of variables: x
  298. and y. The x variables (x\[0\] ... x\[nx-1\]) encode
  299. the row index and the y variables (y\[0\] ... y\[ny-1\]) encode the
  300. column index. x\[0\] and y\[0\] are the most significant bits in the
  301. indices.
  302. The variables may already exist or may be created by the function.
  303. The index of x\[i\] is bx+i*sx, and the index of y\[i\] is by+i*sy.<p>
  304. On input, nx and ny hold the numbers of row and column variables already
  305. in existence. On output, they hold the numbers of row and column
  306. variables actually used by the matrix. When Cudd_bddRead creates the
  307. variable arrays, the index of x\[i\] is bx+i*sx, and the index of
  308. y\[i\] is by+i*sy. When some variables already exist, Cudd_bddRead
  309. expects the indices of the existing x variables to be bx+i*sx, and the
  310. indices of the existing y variables to be by+i*sy.<p>
  311. m and n are set to the numbers of rows and columns of the
  312. matrix. Their values on input are immaterial. The %BDD for the graph
  313. is returned in E, and its reference count is > 0.
  314. @return 1 in case of success; 0 otherwise.
  315. @sideeffect nx and ny are set to the numbers of row and column
  316. variables. m and n are set to the numbers of rows and columns. x and y
  317. are possibly extended to represent the array of row and column
  318. variables.
  319. @see Cudd_addHarwell Cudd_addRead
  320. */
  321. int
  322. Cudd_bddRead(
  323. FILE * fp /**< input file pointer */,
  324. DdManager * dd /**< DD manager */,
  325. DdNode ** E /**< characteristic function of the graph */,
  326. DdNode *** x /**< array of row variables */,
  327. DdNode *** y /**< array of column variables */,
  328. int * nx /**< number or row variables */,
  329. int * ny /**< number or column variables */,
  330. int * m /**< number of rows */,
  331. int * n /**< number of columns */,
  332. int bx /**< first index of row variables */,
  333. int sx /**< step of row variables */,
  334. int by /**< first index of column variables */,
  335. int sy /**< step of column variables */)
  336. {
  337. DdNode *one, *zero;
  338. DdNode *w;
  339. DdNode *minterm1;
  340. int u, v, err, i, nv;
  341. int lnx, lny;
  342. DdNode **lx, **ly;
  343. one = DD_ONE(dd);
  344. zero = Cudd_Not(one);
  345. err = fscanf(fp, "%d %d", &u, &v);
  346. if (err == EOF) {
  347. return(0);
  348. } else if (err != 2) {
  349. return(0);
  350. }
  351. *m = u;
  352. /* Compute the number of x variables. */
  353. lx = *x;
  354. u--; /* row and column numbers start from 0 */
  355. for (lnx=0; u > 0; lnx++) {
  356. u >>= 1;
  357. }
  358. if (lnx > *nx) {
  359. *x = lx = REALLOC(DdNode *, *x, lnx);
  360. if (lx == NULL) {
  361. dd->errorCode = CUDD_MEMORY_OUT;
  362. return(0);
  363. }
  364. }
  365. *n = v;
  366. /* Compute the number of y variables. */
  367. ly = *y;
  368. v--; /* row and column numbers start from 0 */
  369. for (lny=0; v > 0; lny++) {
  370. v >>= 1;
  371. }
  372. if (lny > *ny) {
  373. *y = ly = REALLOC(DdNode *, *y, lny);
  374. if (ly == NULL) {
  375. dd->errorCode = CUDD_MEMORY_OUT;
  376. return(0);
  377. }
  378. }
  379. /* Create all new variables. */
  380. for (i = *nx, nv = bx + (*nx) * sx; i < lnx; i++, nv += sx) {
  381. do {
  382. dd->reordered = 0;
  383. lx[i] = cuddUniqueInter(dd, nv, one, zero);
  384. } while (dd->reordered == 1);
  385. if (lx[i] == NULL) {
  386. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  387. dd->timeoutHandler(dd, dd->tohArg);
  388. }
  389. return(0);
  390. }
  391. cuddRef(lx[i]);
  392. }
  393. for (i = *ny, nv = by + (*ny) * sy; i < lny; i++, nv += sy) {
  394. do {
  395. dd->reordered = 0;
  396. ly[i] = cuddUniqueInter(dd, nv, one, zero);
  397. } while (dd->reordered == 1);
  398. if (ly[i] == NULL) {
  399. if (dd->errorCode == CUDD_TIMEOUT_EXPIRED && dd->timeoutHandler) {
  400. dd->timeoutHandler(dd, dd->tohArg);
  401. }
  402. return(0);
  403. }
  404. cuddRef(ly[i]);
  405. }
  406. *nx = lnx;
  407. *ny = lny;
  408. *E = zero; /* this call will never cause reordering */
  409. cuddRef(*E);
  410. while (! feof(fp)) {
  411. err = fscanf(fp, "%d %d", &u, &v);
  412. if (err == EOF) {
  413. break;
  414. } else if (err != 2) {
  415. return(0);
  416. } else if (u >= *m || v >= *n || u < 0 || v < 0) {
  417. return(0);
  418. }
  419. minterm1 = one; cuddRef(minterm1);
  420. /* Build minterm1 corresponding to this arc. */
  421. for (i = lnx - 1; i>=0; i--) {
  422. if (u & 1) {
  423. w = Cudd_bddAnd(dd, minterm1, lx[i]);
  424. } else {
  425. w = Cudd_bddAnd(dd, minterm1, Cudd_Not(lx[i]));
  426. }
  427. if (w == NULL) {
  428. Cudd_RecursiveDeref(dd, minterm1);
  429. return(0);
  430. }
  431. cuddRef(w);
  432. Cudd_RecursiveDeref(dd,minterm1);
  433. minterm1 = w;
  434. u >>= 1;
  435. }
  436. for (i = lny - 1; i>=0; i--) {
  437. if (v & 1) {
  438. w = Cudd_bddAnd(dd, minterm1, ly[i]);
  439. } else {
  440. w = Cudd_bddAnd(dd, minterm1, Cudd_Not(ly[i]));
  441. }
  442. if (w == NULL) {
  443. Cudd_RecursiveDeref(dd, minterm1);
  444. return(0);
  445. }
  446. cuddRef(w);
  447. Cudd_RecursiveDeref(dd, minterm1);
  448. minterm1 = w;
  449. v >>= 1;
  450. }
  451. w = Cudd_bddAnd(dd, Cudd_Not(minterm1), Cudd_Not(*E));
  452. if (w == NULL) {
  453. Cudd_RecursiveDeref(dd, minterm1);
  454. return(0);
  455. }
  456. w = Cudd_Not(w);
  457. cuddRef(w);
  458. Cudd_RecursiveDeref(dd, minterm1);
  459. Cudd_RecursiveDeref(dd, *E);
  460. *E = w;
  461. }
  462. return(1);
  463. } /* end of Cudd_bddRead */
  464. /*---------------------------------------------------------------------------*/
  465. /* Definition of internal functions */
  466. /*---------------------------------------------------------------------------*/
  467. /*---------------------------------------------------------------------------*/
  468. /* Definition of static functions */
  469. /*---------------------------------------------------------------------------*/