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.

757 lines
19 KiB

3 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Reordering of DDs based on simulated annealing
  5. @author Jae-Young Jang, Jorgen Sivesind
  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. /* Annealing parameters */
  40. #define BETA 0.6
  41. #define ALPHA 0.90
  42. #define EXC_PROB 0.4
  43. #define JUMP_UP_PROB 0.36
  44. #define MAXGEN_RATIO 15.0
  45. #define STOP_TEMP 1.0
  46. /*---------------------------------------------------------------------------*/
  47. /* Stucture declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Type declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Variable declarations */
  54. /*---------------------------------------------------------------------------*/
  55. /*---------------------------------------------------------------------------*/
  56. /* Macro declarations */
  57. /*---------------------------------------------------------------------------*/
  58. /** \cond */
  59. /*---------------------------------------------------------------------------*/
  60. /* Static function prototypes */
  61. /*---------------------------------------------------------------------------*/
  62. static int stopping_criterion (int c1, int c2, int c3, int c4, double temp);
  63. static double random_generator (DdManager *dd);
  64. static int ddExchange (DdManager *table, int x, int y, double temp);
  65. static int ddJumpingAux (DdManager *table, int x, int x_low, int x_high, double temp);
  66. static Move * ddJumpingUp (DdManager *table, int x, int x_low, int initial_size);
  67. static Move * ddJumpingDown (DdManager *table, int x, int x_high, int initial_size);
  68. static int siftBackwardProb (DdManager *table, Move *moves, int size, double temp);
  69. static void copyOrder (DdManager *table, int *array, int lower, int upper);
  70. static int restoreOrder (DdManager *table, int *array, int lower, int upper);
  71. /** \endcond */
  72. /*---------------------------------------------------------------------------*/
  73. /* Definition of exported functions */
  74. /*---------------------------------------------------------------------------*/
  75. /*---------------------------------------------------------------------------*/
  76. /* Definition of internal functions */
  77. /*---------------------------------------------------------------------------*/
  78. /**
  79. @brief Get new variable-order by simulated annealing algorithm.
  80. @details Get x, y by random selection. Choose either
  81. exchange or jump randomly. In case of jump, choose between jump_up
  82. and jump_down randomly. Do exchange or jump and get optimal case.
  83. Loop until there is no improvement or temperature reaches
  84. minimum.
  85. @return 1 in case of success; 0 otherwise.
  86. @sideeffect None
  87. */
  88. int
  89. cuddAnnealing(
  90. DdManager * table,
  91. int lower,
  92. int upper)
  93. {
  94. int nvars;
  95. int size;
  96. int x,y;
  97. int result;
  98. int c1, c2, c3, c4;
  99. int BestCost;
  100. int *BestOrder;
  101. double NewTemp, temp;
  102. double rand1;
  103. int innerloop, maxGen;
  104. int ecount, ucount, dcount;
  105. nvars = upper - lower + 1;
  106. result = cuddSifting(table,lower,upper);
  107. #ifdef DD_STATS
  108. (void) fprintf(table->out,"\n");
  109. #endif
  110. if (result == 0) return(0);
  111. size = (int) (table->keys - table->isolated);
  112. /* Keep track of the best order. */
  113. BestCost = size;
  114. BestOrder = ALLOC(int,nvars);
  115. if (BestOrder == NULL) {
  116. table->errorCode = CUDD_MEMORY_OUT;
  117. return(0);
  118. }
  119. copyOrder(table,BestOrder,lower,upper);
  120. temp = BETA * size;
  121. maxGen = (int) (MAXGEN_RATIO * nvars);
  122. c1 = size + 10;
  123. c2 = c1 + 10;
  124. c3 = size;
  125. c4 = c2 + 10;
  126. ecount = ucount = dcount = 0;
  127. while (!stopping_criterion(c1, c2, c3, c4, temp)) {
  128. #ifdef DD_STATS
  129. (void) fprintf(table->out,"temp=%f\tsize=%d\tgen=%d\t",
  130. temp,size,maxGen);
  131. table->tosses = table->acceptances = 0;
  132. #endif
  133. for (innerloop = 0; innerloop < maxGen; innerloop++) {
  134. /* Choose x, y randomly. */
  135. x = (int) Cudd_Random(table) % nvars;
  136. do {
  137. y = (int) Cudd_Random(table) % nvars;
  138. } while (x == y);
  139. x += lower;
  140. y += lower;
  141. if (x > y) {
  142. int tmp = x;
  143. x = y;
  144. y = tmp;
  145. }
  146. /* Choose move with roulette wheel. */
  147. rand1 = random_generator(table);
  148. if (rand1 < EXC_PROB) {
  149. result = ddExchange(table,x,y,temp); /* exchange */
  150. ecount++;
  151. #if 0
  152. (void) fprintf(table->out,
  153. "Exchange of %d and %d: size = %d\n",
  154. x,y,table->keys - table->isolated);
  155. #endif
  156. } else if (rand1 < EXC_PROB + JUMP_UP_PROB) {
  157. result = ddJumpingAux(table,y,x,y,temp); /* jumping_up */
  158. ucount++;
  159. #if 0
  160. (void) fprintf(table->out,
  161. "Jump up of %d to %d: size = %d\n",
  162. y,x,table->keys - table->isolated);
  163. #endif
  164. } else {
  165. result = ddJumpingAux(table,x,x,y,temp); /* jumping_down */
  166. dcount++;
  167. #if 0
  168. (void) fprintf(table->out,
  169. "Jump down of %d to %d: size = %d\n",
  170. x,y,table->keys - table->isolated);
  171. #endif
  172. }
  173. if (!result) {
  174. FREE(BestOrder);
  175. return(0);
  176. }
  177. size = (int) (table->keys - table->isolated); /* keep current size */
  178. if (size < BestCost) { /* update best order */
  179. BestCost = size;
  180. copyOrder(table,BestOrder,lower,upper);
  181. }
  182. }
  183. c1 = c2;
  184. c2 = c3;
  185. c3 = c4;
  186. c4 = size;
  187. NewTemp = ALPHA * temp;
  188. if (NewTemp >= 1.0) {
  189. maxGen = (int)(log(NewTemp) / log(temp) * maxGen);
  190. }
  191. temp = NewTemp; /* control variable */
  192. #ifdef DD_STATS
  193. (void) fprintf(table->out,"uphill = %d\taccepted = %d\n",
  194. table->tosses,table->acceptances);
  195. fflush(table->out);
  196. #endif
  197. }
  198. result = restoreOrder(table,BestOrder,lower,upper);
  199. FREE(BestOrder);
  200. if (!result) return(0);
  201. #ifdef DD_STATS
  202. fprintf(table->out,"#:N_EXCHANGE %8d : total exchanges\n",ecount);
  203. fprintf(table->out,"#:N_JUMPUP %8d : total jumps up\n",ucount);
  204. fprintf(table->out,"#:N_JUMPDOWN %8d : total jumps down",dcount);
  205. #endif
  206. return(1);
  207. } /* end of cuddAnnealing */
  208. /*---------------------------------------------------------------------------*/
  209. /* Definition of static functions */
  210. /*---------------------------------------------------------------------------*/
  211. /**
  212. @brief Checks termination condition.
  213. @details If temperature is STOP_TEMP or there is no improvement
  214. then terminates.
  215. @return 1 if the termination criterion is met; 0 otherwise.
  216. @sideeffect None
  217. */
  218. static int
  219. stopping_criterion(
  220. int c1,
  221. int c2,
  222. int c3,
  223. int c4,
  224. double temp)
  225. {
  226. if (STOP_TEMP < temp) {
  227. return(0);
  228. } else if ((c1 == c2) && (c1 == c3) && (c1 == c4)) {
  229. return(1);
  230. } else {
  231. return(0);
  232. }
  233. } /* end of stopping_criterion */
  234. /**
  235. @brief Random number generator.
  236. @return a double precision value between 0.0 and 1.0.
  237. @sideeffect None
  238. */
  239. static double
  240. random_generator(DdManager * dd)
  241. {
  242. return((double)(Cudd_Random(dd) / 2147483561.0));
  243. } /* end of random_generator */
  244. /**
  245. @brief Exchanges two variables, x and y.
  246. @details This is the same funcion as ddSwapping except for the
  247. comparison expression. Use probability function, exp(-size_change/temp).
  248. @sideeffect None
  249. */
  250. static int
  251. ddExchange(
  252. DdManager * table,
  253. int x,
  254. int y,
  255. double temp)
  256. {
  257. Move *move,*moves;
  258. int tmp;
  259. int x_ref,y_ref;
  260. int x_next,y_next;
  261. int size, result;
  262. int initial_size, limit_size;
  263. x_ref = x;
  264. y_ref = y;
  265. x_next = cuddNextHigh(table,x);
  266. y_next = cuddNextLow(table,y);
  267. moves = NULL;
  268. initial_size = limit_size = (int) (table->keys - table->isolated);
  269. for (;;) {
  270. if (x_next == y_next) {
  271. size = cuddSwapInPlace(table,x,x_next);
  272. if (size == 0) goto ddExchangeOutOfMem;
  273. move = (Move *)cuddDynamicAllocNode(table);
  274. if (move == NULL) goto ddExchangeOutOfMem;
  275. move->x = x;
  276. move->y = x_next;
  277. move->size = size;
  278. move->next = moves;
  279. moves = move;
  280. size = cuddSwapInPlace(table,y_next,y);
  281. if (size == 0) goto ddExchangeOutOfMem;
  282. move = (Move *)cuddDynamicAllocNode(table);
  283. if (move == NULL) goto ddExchangeOutOfMem;
  284. move->x = y_next;
  285. move->y = y;
  286. move->size = size;
  287. move->next = moves;
  288. moves = move;
  289. size = cuddSwapInPlace(table,x,x_next);
  290. if (size == 0) goto ddExchangeOutOfMem;
  291. move = (Move *)cuddDynamicAllocNode(table);
  292. if (move == NULL) goto ddExchangeOutOfMem;
  293. move->x = x;
  294. move->y = x_next;
  295. move->size = size;
  296. move->next = moves;
  297. moves = move;
  298. tmp = x;
  299. x = y;
  300. y = tmp;
  301. } else if (x == y_next) {
  302. size = cuddSwapInPlace(table,x,x_next);
  303. if (size == 0) goto ddExchangeOutOfMem;
  304. move = (Move *)cuddDynamicAllocNode(table);
  305. if (move == NULL) goto ddExchangeOutOfMem;
  306. move->x = x;
  307. move->y = x_next;
  308. move->size = size;
  309. move->next = moves;
  310. moves = move;
  311. tmp = x;
  312. x = y;
  313. y = tmp;
  314. } else {
  315. size = cuddSwapInPlace(table,x,x_next);
  316. if (size == 0) goto ddExchangeOutOfMem;
  317. move = (Move *)cuddDynamicAllocNode(table);
  318. if (move == NULL) goto ddExchangeOutOfMem;
  319. move->x = x;
  320. move->y = x_next;
  321. move->size = size;
  322. move->next = moves;
  323. moves = move;
  324. size = cuddSwapInPlace(table,y_next,y);
  325. if (size == 0) goto ddExchangeOutOfMem;
  326. move = (Move *)cuddDynamicAllocNode(table);
  327. if (move == NULL) goto ddExchangeOutOfMem;
  328. move->x = y_next;
  329. move->y = y;
  330. move->size = size;
  331. move->next = moves;
  332. moves = move;
  333. x = x_next;
  334. y = y_next;
  335. }
  336. x_next = cuddNextHigh(table,x);
  337. y_next = cuddNextLow(table,y);
  338. if (x_next > y_ref) break;
  339. if ((double) size > DD_MAX_REORDER_GROWTH * (double) limit_size) {
  340. break;
  341. } else if (size < limit_size) {
  342. limit_size = size;
  343. }
  344. }
  345. if (y_next>=x_ref) {
  346. size = cuddSwapInPlace(table,y_next,y);
  347. if (size == 0) goto ddExchangeOutOfMem;
  348. move = (Move *)cuddDynamicAllocNode(table);
  349. if (move == NULL) goto ddExchangeOutOfMem;
  350. move->x = y_next;
  351. move->y = y;
  352. move->size = size;
  353. move->next = moves;
  354. moves = move;
  355. }
  356. /* move backward and stop at best position or accept uphill move */
  357. result = siftBackwardProb(table,moves,initial_size,temp);
  358. if (!result) goto ddExchangeOutOfMem;
  359. while (moves != NULL) {
  360. move = moves->next;
  361. cuddDeallocMove(table, moves);
  362. moves = move;
  363. }
  364. return(1);
  365. ddExchangeOutOfMem:
  366. while (moves != NULL) {
  367. move = moves->next;
  368. cuddDeallocMove(table, moves);
  369. moves = move;
  370. }
  371. return(0);
  372. } /* end of ddExchange */
  373. /**
  374. @brief Moves a variable to a specified position.
  375. @details If x==x_low, it executes jumping_down. If x==x_high, it
  376. executes jumping_up. This funcion is similar to ddSiftingAux.
  377. @return 1 in case of success; 0 otherwise.
  378. @sideeffect None
  379. */
  380. static int
  381. ddJumpingAux(
  382. DdManager * table,
  383. int x,
  384. int x_low,
  385. int x_high,
  386. double temp)
  387. {
  388. Move *move;
  389. Move *moves; /* list of moves */
  390. int initial_size;
  391. int result;
  392. initial_size = (int) (table->keys - table->isolated);
  393. #ifdef DD_DEBUG
  394. assert(table->subtables[x].keys > 0);
  395. #endif
  396. moves = NULL;
  397. if (cuddNextLow(table,x) < x_low) {
  398. if (cuddNextHigh(table,x) > x_high) return(1);
  399. moves = ddJumpingDown(table,x,x_high,initial_size);
  400. /* after that point x --> x_high unless early termination */
  401. if (moves == NULL) goto ddJumpingAuxOutOfMem;
  402. /* move backward and stop at best position or accept uphill move */
  403. result = siftBackwardProb(table,moves,initial_size,temp);
  404. if (!result) goto ddJumpingAuxOutOfMem;
  405. } else if (cuddNextHigh(table,x) > x_high) {
  406. moves = ddJumpingUp(table,x,x_low,initial_size);
  407. /* after that point x --> x_low unless early termination */
  408. if (moves == NULL) goto ddJumpingAuxOutOfMem;
  409. /* move backward and stop at best position or accept uphill move */
  410. result = siftBackwardProb(table,moves,initial_size,temp);
  411. if (!result) goto ddJumpingAuxOutOfMem;
  412. } else {
  413. (void) fprintf(table->err,"Unexpected condition in ddJumping\n");
  414. goto ddJumpingAuxOutOfMem;
  415. }
  416. while (moves != NULL) {
  417. move = moves->next;
  418. cuddDeallocMove(table, moves);
  419. moves = move;
  420. }
  421. return(1);
  422. ddJumpingAuxOutOfMem:
  423. while (moves != NULL) {
  424. move = moves->next;
  425. cuddDeallocMove(table, moves);
  426. moves = move;
  427. }
  428. return(0);
  429. } /* end of ddJumpingAux */
  430. /**
  431. @brief This function is for jumping up.
  432. @details This is a simplified version of ddSiftingUp. It does not
  433. use lower bounding.
  434. @return the set of moves in case of success; NULL if memory is full.
  435. @sideeffect None
  436. */
  437. static Move *
  438. ddJumpingUp(
  439. DdManager * table,
  440. int x,
  441. int x_low,
  442. int initial_size)
  443. {
  444. Move *moves;
  445. Move *move;
  446. int y;
  447. int size;
  448. int limit_size = initial_size;
  449. moves = NULL;
  450. y = cuddNextLow(table,x);
  451. while (y >= x_low) {
  452. size = cuddSwapInPlace(table,y,x);
  453. if (size == 0) goto ddJumpingUpOutOfMem;
  454. move = (Move *)cuddDynamicAllocNode(table);
  455. if (move == NULL) goto ddJumpingUpOutOfMem;
  456. move->x = y;
  457. move->y = x;
  458. move->size = size;
  459. move->next = moves;
  460. moves = move;
  461. if ((double) size > table->maxGrowth * (double) limit_size) {
  462. break;
  463. } else if (size < limit_size) {
  464. limit_size = size;
  465. }
  466. x = y;
  467. y = cuddNextLow(table,x);
  468. }
  469. return(moves);
  470. ddJumpingUpOutOfMem:
  471. while (moves != NULL) {
  472. move = moves->next;
  473. cuddDeallocMove(table, moves);
  474. moves = move;
  475. }
  476. return(NULL);
  477. } /* end of ddJumpingUp */
  478. /**
  479. @brief This function is for jumping down.
  480. @details This is a simplified version of ddSiftingDown. It does not
  481. use lower bounding.
  482. @return the set of moves in case of success; NULL if memory is full.
  483. @sideeffect None
  484. */
  485. static Move *
  486. ddJumpingDown(
  487. DdManager * table,
  488. int x,
  489. int x_high,
  490. int initial_size)
  491. {
  492. Move *moves;
  493. Move *move;
  494. int y;
  495. int size;
  496. int limit_size = initial_size;
  497. moves = NULL;
  498. y = cuddNextHigh(table,x);
  499. while (y <= x_high) {
  500. size = cuddSwapInPlace(table,x,y);
  501. if (size == 0) goto ddJumpingDownOutOfMem;
  502. move = (Move *)cuddDynamicAllocNode(table);
  503. if (move == NULL) goto ddJumpingDownOutOfMem;
  504. move->x = x;
  505. move->y = y;
  506. move->size = size;
  507. move->next = moves;
  508. moves = move;
  509. if ((double) size > table->maxGrowth * (double) limit_size) {
  510. break;
  511. } else if (size < limit_size) {
  512. limit_size = size;
  513. }
  514. x = y;
  515. y = cuddNextHigh(table,x);
  516. }
  517. return(moves);
  518. ddJumpingDownOutOfMem:
  519. while (moves != NULL) {
  520. move = moves->next;
  521. cuddDeallocMove(table, moves);
  522. moves = move;
  523. }
  524. return(NULL);
  525. } /* end of ddJumpingDown */
  526. /**
  527. @brief Returns the %DD to the best position encountered during
  528. sifting if there was improvement.
  529. @details Otherwise, "tosses a coin" to decide whether to keep
  530. the current configuration or return the %DD to the original
  531. one.
  532. @return 1 in case of success; 0 otherwise.
  533. @sideeffect None
  534. */
  535. static int
  536. siftBackwardProb(
  537. DdManager * table,
  538. Move * moves,
  539. int size,
  540. double temp)
  541. {
  542. Move *move;
  543. int res;
  544. int best_size = size;
  545. double coin, threshold;
  546. /* Look for best size during the last sifting */
  547. for (move = moves; move != NULL; move = move->next) {
  548. if (move->size < best_size) {
  549. best_size = move->size;
  550. }
  551. }
  552. /* If best_size equals size, the last sifting did not produce any
  553. ** improvement. We now toss a coin to decide whether to retain
  554. ** this change or not.
  555. */
  556. if (best_size == size) {
  557. coin = random_generator(table);
  558. #ifdef DD_STATS
  559. table->tosses++;
  560. #endif
  561. threshold = exp(-((double)(table->keys - table->isolated -
  562. (unsigned int) size))/temp);
  563. if (coin < threshold) {
  564. #ifdef DD_STATS
  565. table->acceptances++;
  566. #endif
  567. return(1);
  568. }
  569. }
  570. /* Either there was improvement, or we have decided not to
  571. ** accept the uphill move. Go to best position.
  572. */
  573. res = (int) (table->keys - table->isolated);
  574. for (move = moves; move != NULL; move = move->next) {
  575. if (res == best_size) return(1);
  576. res = cuddSwapInPlace(table,(int)move->x,(int)move->y);
  577. if (!res) return(0);
  578. }
  579. return(1);
  580. } /* end of sift_backward_prob */
  581. /**
  582. @brief Copies the current variable order to array.
  583. @details At the same time inverts the permutation.
  584. @sideeffect None
  585. */
  586. static void
  587. copyOrder(
  588. DdManager * table,
  589. int * array,
  590. int lower,
  591. int upper)
  592. {
  593. int i;
  594. int nvars;
  595. nvars = upper - lower + 1;
  596. for (i = 0; i < nvars; i++) {
  597. array[i] = table->invperm[i+lower];
  598. }
  599. } /* end of copyOrder */
  600. /**
  601. @brief Restores the variable order in array by a series of sifts up.
  602. @return 1 in case of success; 0 otherwise.
  603. @sideeffect None
  604. */
  605. static int
  606. restoreOrder(
  607. DdManager * table,
  608. int * array,
  609. int lower,
  610. int upper)
  611. {
  612. int i, x, y, size;
  613. int nvars = upper - lower + 1;
  614. for (i = 0; i < nvars; i++) {
  615. x = table->perm[array[i]];
  616. #ifdef DD_DEBUG
  617. assert(x >= lower && x <= upper);
  618. #endif
  619. y = cuddNextLow(table,x);
  620. while (y >= i + lower) {
  621. size = cuddSwapInPlace(table,y,x);
  622. if (size == 0) return(0);
  623. x = y;
  624. y = cuddNextLow(table,x);
  625. }
  626. }
  627. return(1);
  628. } /* end of restoreOrder */