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.

556 lines
17 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup nanotrav
  4. @brief Symbolic shortest paths algorithms.
  5. @details This file contains the functions that implement the
  6. symbolic version of several shortest path algorithms described in the
  7. JFM paper on ADDs.
  8. @author Fabio Somenzi, Iris Bahar
  9. @copyright@parblock
  10. Copyright (c) 1995-2015, Regents of the University of Colorado
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions
  14. are met:
  15. Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. Neither the name of the University of Colorado nor the names of its
  21. contributors may be used to endorse or promote products derived from
  22. this software without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. POSSIBILITY OF SUCH DAMAGE.
  35. @endparblock
  36. */
  37. #include "ntr.h"
  38. #include "cuddInt.h"
  39. /*---------------------------------------------------------------------------*/
  40. /* Constant declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Stucture declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Type declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Variable declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /*---------------------------------------------------------------------------*/
  52. /* Macro declarations */
  53. /*---------------------------------------------------------------------------*/
  54. /** \cond */
  55. /*---------------------------------------------------------------------------*/
  56. /* Static function prototypes */
  57. /*---------------------------------------------------------------------------*/
  58. static DdNode * ntrBellman (DdManager *dd, DdNode *D, DdNode *source, DdNode **x, DdNode **y, int vars, int pr);
  59. static DdNode * ntrWarshall (DdManager *dd, DdNode *D, DdNode **x, DdNode **y, int vars, int pr);
  60. static DdNode * ntrSquare (DdManager *dd, DdNode *D, DdNode **x, DdNode **y, DdNode **z, int vars, int pr, int st);
  61. /** \endcond */
  62. /*---------------------------------------------------------------------------*/
  63. /* Definition of exported functions */
  64. /*---------------------------------------------------------------------------*/
  65. /**
  66. @brief Computes shortest paths in a state graph.
  67. @details Computes shortest paths in the state transition graph of
  68. a network. Three methods are availabe:
  69. <ul>
  70. <li> Bellman-Ford algorithm for single-source shortest paths; the
  71. algorithm computes the distance (number of transitions) from the initial
  72. states to all states.
  73. <li> Floyd-Warshall algorithm for all-pair shortest paths.
  74. <li> Repeated squaring algorithm for all-pair shortest paths.
  75. </ul>
  76. @return 1 in case of success; 0 otherwise.
  77. @sideeffect %ADD variables are created in the manager.
  78. */
  79. int
  80. Ntr_ShortestPaths(
  81. DdManager * dd,
  82. BnetNetwork * net,
  83. NtrOptions * option)
  84. {
  85. NtrPartTR *TR;
  86. DdNode *edges, *source, *res, *r, *q, *bddSource;
  87. DdNode **xadd, **yadd, **zadd;
  88. int i;
  89. int pr = option->verb;
  90. int algorithm = option->shortPath;
  91. int selectiveTrace = option->selectiveTrace;
  92. int nvars = net->nlatches;
  93. /* Set background to infinity for shortest paths. */
  94. Cudd_SetBackground(dd,Cudd_ReadPlusInfinity(dd));
  95. /* Build the monolithic TR. */
  96. TR = Ntr_buildTR(dd,net,option,NTR_IMAGE_MONO);
  97. /* Build the ADD variable vectors for x and y. */
  98. xadd = ALLOC(DdNode *, nvars);
  99. yadd = ALLOC(DdNode *, nvars);
  100. for(i = 0; i < nvars; i++) {
  101. q = Cudd_addIthVar(dd,TR->x[i]->index);
  102. Cudd_Ref(q);
  103. xadd[i] = q;
  104. q = Cudd_addIthVar(dd,TR->y[i]->index);
  105. Cudd_Ref(q);
  106. yadd[i] = q;
  107. }
  108. /* Convert the transition relation BDD into an ADD... */
  109. q = Cudd_BddToAdd(dd,TR->part[0]);
  110. Cudd_Ref(q);
  111. /* ...replacing zeroes with infinities... */
  112. r = Cudd_addIte(dd,q,Cudd_ReadOne(dd),Cudd_ReadPlusInfinity(dd));
  113. Cudd_Ref(r);
  114. Cudd_RecursiveDeref(dd,q);
  115. /* ...and zeroing the diagonal. */
  116. q = Cudd_addXeqy(dd,nvars,xadd,yadd);
  117. Cudd_Ref(q);
  118. edges = Cudd_addIte(dd,q,Cudd_ReadZero(dd),r);
  119. Cudd_Ref(edges);
  120. Cudd_RecursiveDeref(dd,r);
  121. Cudd_RecursiveDeref(dd,q);
  122. switch(algorithm) {
  123. case NTR_SHORT_BELLMAN:
  124. bddSource = Ntr_initState(dd,net,option);
  125. source = Cudd_BddToAdd(dd,bddSource);
  126. Cudd_Ref(source);
  127. res = ntrBellman(dd,edges,source,xadd,yadd,nvars,pr);
  128. if (res == NULL) return(0);
  129. Cudd_Ref(res);
  130. Cudd_RecursiveDeref(dd,source);
  131. Cudd_RecursiveDeref(dd,bddSource);
  132. if (pr >= 0) {
  133. (void) fprintf(stdout,"Distance Matrix");
  134. Cudd_PrintDebug(dd,res,nvars,pr);
  135. }
  136. break;
  137. case NTR_SHORT_FLOYD:
  138. res = ntrWarshall(dd,edges,xadd,yadd,nvars,pr);
  139. if (res == NULL) return(0);
  140. Cudd_Ref(res);
  141. if (pr >= 0) {
  142. (void) fprintf(stdout,"Distance Matrix");
  143. Cudd_PrintDebug(dd,res,2*nvars,pr);
  144. }
  145. break;
  146. case NTR_SHORT_SQUARE:
  147. /* Create a third set of ADD variables. */
  148. zadd = ALLOC(DdNode *, nvars);
  149. for(i = 0; i < nvars; i++) {
  150. int level;
  151. level = Cudd_ReadIndex(dd,TR->x[i]->index);
  152. q = Cudd_addNewVarAtLevel(dd,level);
  153. Cudd_Ref(q);
  154. zadd[i] = q;
  155. }
  156. /* Compute the shortest paths. */
  157. res = ntrSquare(dd,edges,zadd,yadd,xadd,nvars,pr,selectiveTrace);
  158. if (res == NULL) return(0);
  159. Cudd_Ref(res);
  160. /* Dispose of the extra variables. */
  161. for(i = 0; i < nvars; i++) {
  162. Cudd_RecursiveDeref(dd,zadd[i]);
  163. }
  164. FREE(zadd);
  165. if (pr >= 0) {
  166. (void) fprintf(stdout,"Distance Matrix");
  167. Cudd_PrintDebug(dd,res,2*nvars,pr);
  168. }
  169. break;
  170. default:
  171. (void) printf("Unrecognized method. Try again.\n");
  172. return(0);
  173. }
  174. /* Here we should compute the paths. */
  175. /* Clean up. */
  176. Ntr_freeTR(dd,TR);
  177. Cudd_RecursiveDeref(dd,edges);
  178. Cudd_RecursiveDeref(dd,res);
  179. for(i = 0; i < nvars; i++) {
  180. Cudd_RecursiveDeref(dd,xadd[i]);
  181. Cudd_RecursiveDeref(dd,yadd[i]);
  182. }
  183. FREE(xadd);
  184. FREE(yadd);
  185. if (option->autoDyn & 1) {
  186. (void) printf("Order after short path computation\n");
  187. if (!Bnet_PrintOrder(net,dd)) return(0);
  188. }
  189. return(1);
  190. } /* end of Ntr_ShortestPaths */
  191. /*---------------------------------------------------------------------------*/
  192. /* Definition of internal functions */
  193. /*---------------------------------------------------------------------------*/
  194. /*---------------------------------------------------------------------------*/
  195. /* Definition of static functions */
  196. /*---------------------------------------------------------------------------*/
  197. /**
  198. @brief Bellman-Ford algorithm for single-source shortest paths.
  199. @return the vector of the distances of all states from the initial
  200. states.
  201. @details In case of multiple initial states the distance for
  202. each state is from the nearest initial state. Negative-weight
  203. cycles are detected, though only in the naive way. (Lack of
  204. convergence after nodes-1 iterations.) In such a case, a constant
  205. %ADD with value minus infinity is returned. Bellman-Ford is based on
  206. matrix-vector multiplication. The matrix is the distance matrix
  207. D(x,y), such that D(a,b) is the length of the arc connecting state a
  208. to state b. The vector V(x) stores the distances of all states from
  209. the initial states. The actual vector used in the matrix-vector
  210. multiplication is diff(x), that holds those distances that have
  211. changed during the last update.
  212. @see ntrWarshall ntrSquare
  213. */
  214. static DdNode *
  215. ntrBellman(
  216. DdManager *dd,
  217. DdNode *D,
  218. DdNode *source,
  219. DdNode **x,
  220. DdNode **y,
  221. int vars,
  222. int pr)
  223. {
  224. DdNode *u, *w, *V, *min, *diff;
  225. DdApaNumber i, nodes, one;
  226. int digits = vars + 1;
  227. /* To avoid overflow when there are many variables, use APA. */
  228. nodes = Cudd_NewApaNumber(digits);
  229. Cudd_ApaPowerOfTwo(digits,nodes,vars);
  230. i = Cudd_NewApaNumber(digits);
  231. one = Cudd_NewApaNumber(digits);
  232. Cudd_ApaSetToLiteral(digits,one,1);
  233. #if 0
  234. /* Find the distances from the initial state along paths using one
  235. ** arc. */
  236. w = Cudd_Cofactor(dd,D,source); /* works only if source is a cube */
  237. Cudd_Ref(w);
  238. V = Cudd_addSwapVariables(dd,w,x,y,vars);
  239. Cudd_Ref(V);
  240. Cudd_RecursiveDeref(dd,w);
  241. #endif
  242. /* The initial states are at distance 0. The other states are
  243. ** initially at infinite distance. */
  244. V = Cudd_addIte(dd,source,Cudd_ReadZero(dd),Cudd_ReadPlusInfinity(dd));
  245. Cudd_Ref(V);
  246. /* Selective trace algorithm. For the next update, only consider the
  247. ** nodes whose distance has changed in the last update. */
  248. diff = V;
  249. Cudd_Ref(diff);
  250. for (Cudd_ApaSetToLiteral(digits,i,1);
  251. Cudd_ApaCompare(digits,i,digits,nodes) < 0;
  252. Cudd_ApaAdd(digits,i,one,i)) {
  253. if (pr>2) {(void) printf("V"); Cudd_PrintDebug(dd,V,vars,pr);}
  254. /* Compute the distances via triangulation as a function of x. */
  255. w = Cudd_addTriangle(dd,diff,D,x,vars);
  256. Cudd_Ref(w);
  257. Cudd_RecursiveDeref(dd,diff);
  258. u = Cudd_addSwapVariables(dd,w,x,y,vars);
  259. Cudd_Ref(u);
  260. Cudd_RecursiveDeref(dd,w);
  261. if (pr>2) {(void) printf("u"); Cudd_PrintDebug(dd,u,vars,pr);}
  262. /* Take the minimum of the previous distances and those just
  263. ** computed. */
  264. min = Cudd_addApply(dd,Cudd_addMinimum,V,u);
  265. Cudd_Ref(min);
  266. Cudd_RecursiveDeref(dd,u);
  267. if (pr>2) {(void) printf("min"); Cudd_PrintDebug(dd,min,vars,pr);}
  268. if (V == min) { /* convergence */
  269. Cudd_RecursiveDeref(dd,min);
  270. if (pr>0) {
  271. (void) printf("Terminating after ");
  272. Cudd_ApaPrintDecimal(stdout,digits,i);
  273. (void) printf(" iterations\n");
  274. }
  275. break;
  276. }
  277. /* Find the distances that decreased. */
  278. diff = Cudd_addApply(dd,Cudd_addDiff,V,min);
  279. Cudd_Ref(diff);
  280. if (pr>2) {(void) printf("diff"); Cudd_PrintDebug(dd,diff,vars,pr);}
  281. Cudd_RecursiveDeref(dd,V);
  282. V = min;
  283. }
  284. /* Negative cycle detection. */
  285. if (Cudd_ApaCompare(digits,i,digits,nodes) == 0 &&
  286. diff != Cudd_ReadPlusInfinity(dd)) {
  287. (void) printf("Negative cycle\n");
  288. Cudd_RecursiveDeref(dd,diff);
  289. Cudd_RecursiveDeref(dd,V);
  290. V = Cudd_ReadMinusInfinity(dd);
  291. Cudd_Ref(V);
  292. }
  293. Cudd_Deref(V);
  294. FREE(i);
  295. FREE(nodes);
  296. FREE(one);
  297. return(V);
  298. } /* end of ntrBellman */
  299. /**
  300. @brief Floyd-Warshall algorithm for all-pair shortest paths.
  301. */
  302. static DdNode *
  303. ntrWarshall(
  304. DdManager *dd,
  305. DdNode *D,
  306. DdNode **x,
  307. DdNode **y,
  308. int vars,
  309. int pr)
  310. {
  311. DdNode *one, *zero;
  312. DdNode *xminterm, *w, *V, *V2;
  313. DdNode *P, *R;
  314. int i;
  315. int nodes;
  316. int k,u;
  317. long start_time;
  318. if (vars > 30)
  319. nodes = 1000000000;
  320. else
  321. nodes = 1 << vars;
  322. one = DD_ONE(dd);
  323. zero = DD_ZERO(dd);
  324. Cudd_Ref(R = D); /* make copy of original matrix */
  325. /* Extract pivot row and column from D */
  326. start_time = util_cpu_time();
  327. for (k = 0; k < nodes; k++) {
  328. if (k % 10000 == 0) {
  329. (void) printf("Starting iteration %d at time %s\n",
  330. k,util_print_time(util_cpu_time() - start_time));
  331. }
  332. Cudd_Ref(xminterm = one);
  333. u = k;
  334. for (i = vars-1; i >= 0; i--) {
  335. if (u&1) {
  336. Cudd_Ref(w = Cudd_addIte(dd,x[i],xminterm,zero));
  337. } else {
  338. Cudd_Ref(w = Cudd_addIte(dd,x[i],zero,xminterm));
  339. }
  340. Cudd_RecursiveDeref(dd,xminterm);
  341. xminterm = w;
  342. u >>= 1;
  343. }
  344. Cudd_Ref(V = Cudd_Cofactor(dd,R,xminterm));
  345. Cudd_RecursiveDeref(dd,xminterm);
  346. if (pr>2) {(void) printf("V"); Cudd_PrintDebug(dd,V,vars,pr);}
  347. Cudd_Ref(xminterm = one);
  348. u = k;
  349. for (i = vars-1; i >= 0; i--) {
  350. if (u&1) {
  351. Cudd_Ref(w = Cudd_addIte(dd,y[i],xminterm,zero));
  352. } else {
  353. Cudd_Ref(w = Cudd_addIte(dd,y[i],zero,xminterm));
  354. }
  355. Cudd_RecursiveDeref(dd,xminterm);
  356. xminterm = w;
  357. u >>= 1;
  358. }
  359. Cudd_Ref(V2 = Cudd_Cofactor(dd,R,xminterm));
  360. Cudd_RecursiveDeref(dd,xminterm);
  361. if (pr>2) {(void) printf("V2"); Cudd_PrintDebug(dd,V2,vars,pr);}
  362. Cudd_Ref(P = Cudd_addOuterSum(dd,R,V,V2));
  363. Cudd_RecursiveDeref(dd,V);
  364. Cudd_RecursiveDeref(dd,V2);
  365. Cudd_RecursiveDeref(dd,R);
  366. R = P;
  367. if (pr>2) {(void) printf("R"); Cudd_PrintDebug(dd,R,vars,pr);}
  368. }
  369. Cudd_Deref(R);
  370. return(R);
  371. } /* end of ntrWarshall */
  372. /**
  373. @brief Repeated squaring algorithm for all-pairs shortest paths.
  374. */
  375. static DdNode *
  376. ntrSquare(
  377. DdManager *dd /**< manager */,
  378. DdNode *D /**< D(z,y): distance matrix */,
  379. DdNode **x /**< array of x variables */,
  380. DdNode **y /**< array of y variables */,
  381. DdNode **z /**< array of z variables */,
  382. int vars /**< number of variables in each of the three arrays */,
  383. int pr /**< verbosity level */,
  384. int st /**< use the selective trace algorithm */)
  385. {
  386. DdNode *zero;
  387. DdNode *I; /* identity matirix */
  388. DdNode *w, *V, *P, *M, *R, *RT;
  389. DdNode *diff, *min, *minDiag;
  390. int n;
  391. int neg;
  392. long start_time;
  393. zero = Cudd_ReadZero(dd);
  394. /* Make a working copy of the original matrix. */
  395. R = D;
  396. Cudd_Ref(R);
  397. I = Cudd_addXeqy(dd,vars,z,y); /* identity matrix */
  398. Cudd_Ref(I);
  399. /* Make a copy of the matrix for the selective trace algorithm. */
  400. diff = R;
  401. Cudd_Ref(diff);
  402. start_time = util_cpu_time();
  403. for (n = vars; n >= 0; n--) {
  404. printf("Starting iteration %d at time %s\n",vars-n,
  405. util_print_time(util_cpu_time() - start_time));
  406. /* Check for negative cycles: They are identified by negative
  407. ** elements on the diagonal.
  408. */
  409. /* Extract values from the diagonal. */
  410. Cudd_Ref(w = Cudd_addIte(dd,I,R,zero));
  411. minDiag = Cudd_addFindMin(dd,w); /* no need to ref */
  412. neg = Cudd_V(minDiag) < 0;
  413. Cudd_RecursiveDeref(dd,w);
  414. if (neg) {
  415. Cudd_RecursiveDeref(dd,diff);
  416. (void) printf("Negative cycle after %d iterations!\n",vars-n);
  417. break;
  418. }
  419. /* Prepare the first operand of matrix multiplication:
  420. ** diff(z,y) -> RT(x,y) -> V(x,z)
  421. */
  422. /* RT(x,y) */
  423. Cudd_Ref(RT = Cudd_addSwapVariables(dd,diff,x,z,vars));
  424. Cudd_RecursiveDeref(dd,diff);
  425. /* V(x,z) */
  426. Cudd_Ref(V = Cudd_addSwapVariables(dd,RT,y,z,vars));
  427. Cudd_RecursiveDeref(dd,RT);
  428. if (pr > 0) {
  429. double pathcount;
  430. (void) printf("V"); Cudd_PrintDebug(dd,V,2*vars,pr);
  431. pathcount = Cudd_CountPath(V);
  432. (void) printf("Path count = %g\n", pathcount);
  433. }
  434. /* V(x,z) * R(z,y) -> P(x,y) */
  435. Cudd_Ref(P = Cudd_addTriangle(dd,V,R,z,vars));
  436. Cudd_RecursiveDeref(dd,V);
  437. /* P(x,y) => M(z,y) */
  438. Cudd_Ref(M = Cudd_addSwapVariables(dd,P,x,z,vars));
  439. Cudd_RecursiveDeref(dd,P);
  440. if (pr>0) {(void) printf("M"); Cudd_PrintDebug(dd,M,2*vars,pr);}
  441. /* min(z,y) */
  442. Cudd_Ref(min = Cudd_addApply(dd,Cudd_addMinimum,R,M));
  443. Cudd_RecursiveDeref(dd,M);
  444. if (R == min) {
  445. Cudd_RecursiveDeref(dd,min);
  446. if (pr>0) {printf("Done after %d iterations\n",vars-n+1); }
  447. break;
  448. }
  449. /* diff(z,y) */
  450. if (st) {
  451. Cudd_Ref(diff = Cudd_addApply(dd,Cudd_addDiff,min,R));
  452. } else {
  453. Cudd_Ref(diff = min);
  454. }
  455. Cudd_RecursiveDeref(dd,R);
  456. R = min; /* keep a copy of matrix at current iter. */
  457. if (pr > 0) {
  458. double pathcount;
  459. (void) printf("R"); Cudd_PrintDebug(dd,R,2*vars,pr);
  460. pathcount = Cudd_CountPath(R);
  461. (void) printf("Path count = %g\n", pathcount);
  462. }
  463. if (n == 0) {
  464. (void) printf("Negative cycle!\n");
  465. break;
  466. }
  467. }
  468. Cudd_RecursiveDeref(dd,I);
  469. Cudd_Deref(R);
  470. return(R);
  471. } /* end of ntrSquare */