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.

517 lines
16 KiB

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