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.

814 lines
22 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddAnneal.c]
  3. PackageName [cudd]
  4. Synopsis [Reordering of DDs based on simulated annealing]
  5. Description [Internal procedures included in this file:
  6. <ul>
  7. <li> cuddAnnealing()
  8. </ul>
  9. Static procedures included in this file:
  10. <ul>
  11. <li> stopping_criterion()
  12. <li> random_generator()
  13. <li> ddExchange()
  14. <li> ddJumpingAux()
  15. <li> ddJumpingUp()
  16. <li> ddJumpingDown()
  17. <li> siftBackwardProb()
  18. <li> copyOrder()
  19. <li> restoreOrder()
  20. </ul>
  21. ]
  22. SeeAlso []
  23. Author [Jae-Young Jang, Jorgen Sivesind]
  24. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  25. All rights reserved.
  26. Redistribution and use in source and binary forms, with or without
  27. modification, are permitted provided that the following conditions
  28. are met:
  29. Redistributions of source code must retain the above copyright
  30. notice, this list of conditions and the following disclaimer.
  31. Redistributions in binary form must reproduce the above copyright
  32. notice, this list of conditions and the following disclaimer in the
  33. documentation and/or other materials provided with the distribution.
  34. Neither the name of the University of Colorado nor the names of its
  35. contributors may be used to endorse or promote products derived from
  36. this software without specific prior written permission.
  37. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  38. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  40. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  41. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  42. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  43. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  47. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48. POSSIBILITY OF SUCH DAMAGE.]
  49. ******************************************************************************/
  50. #include "util.h"
  51. #include "cuddInt.h"
  52. /*---------------------------------------------------------------------------*/
  53. /* Constant declarations */
  54. /*---------------------------------------------------------------------------*/
  55. /* Annealing parameters */
  56. #define BETA 0.6
  57. #define ALPHA 0.90
  58. #define EXC_PROB 0.4
  59. #define JUMP_UP_PROB 0.36
  60. #define MAXGEN_RATIO 15.0
  61. #define STOP_TEMP 1.0
  62. /*---------------------------------------------------------------------------*/
  63. /* Stucture declarations */
  64. /*---------------------------------------------------------------------------*/
  65. /*---------------------------------------------------------------------------*/
  66. /* Type declarations */
  67. /*---------------------------------------------------------------------------*/
  68. /*---------------------------------------------------------------------------*/
  69. /* Variable declarations */
  70. /*---------------------------------------------------------------------------*/
  71. #ifndef lint
  72. static char rcsid[] DD_UNUSED = "$Id: cuddAnneal.c,v 1.15 2012/02/05 01:07:18 fabio Exp $";
  73. #endif
  74. #ifdef DD_STATS
  75. extern int ddTotalNumberSwapping;
  76. extern int ddTotalNISwaps;
  77. static int tosses;
  78. static int acceptances;
  79. #endif
  80. /*---------------------------------------------------------------------------*/
  81. /* Macro declarations */
  82. /*---------------------------------------------------------------------------*/
  83. /**AutomaticStart*************************************************************/
  84. /*---------------------------------------------------------------------------*/
  85. /* Static function prototypes */
  86. /*---------------------------------------------------------------------------*/
  87. static int stopping_criterion (int c1, int c2, int c3, int c4, double temp);
  88. static double random_generator (void);
  89. static int ddExchange (DdManager *table, int x, int y, double temp);
  90. static int ddJumpingAux (DdManager *table, int x, int x_low, int x_high, double temp);
  91. static Move * ddJumpingUp (DdManager *table, int x, int x_low, int initial_size);
  92. static Move * ddJumpingDown (DdManager *table, int x, int x_high, int initial_size);
  93. static int siftBackwardProb (DdManager *table, Move *moves, int size, double temp);
  94. static void copyOrder (DdManager *table, int *array, int lower, int upper);
  95. static int restoreOrder (DdManager *table, int *array, int lower, int upper);
  96. /**AutomaticEnd***************************************************************/
  97. /*---------------------------------------------------------------------------*/
  98. /* Definition of exported functions */
  99. /*---------------------------------------------------------------------------*/
  100. /*---------------------------------------------------------------------------*/
  101. /* Definition of internal functions */
  102. /*---------------------------------------------------------------------------*/
  103. /**Function********************************************************************
  104. Synopsis [Get new variable-order by simulated annealing algorithm.]
  105. Description [Get x, y by random selection. Choose either
  106. exchange or jump randomly. In case of jump, choose between jump_up
  107. and jump_down randomly. Do exchange or jump and get optimal case.
  108. Loop until there is no improvement or temperature reaches
  109. minimum. Returns 1 in case of success; 0 otherwise.]
  110. SideEffects [None]
  111. SeeAlso []
  112. ******************************************************************************/
  113. int
  114. cuddAnnealing(
  115. DdManager * table,
  116. int lower,
  117. int upper)
  118. {
  119. int nvars;
  120. int size;
  121. int x,y;
  122. int result;
  123. int c1, c2, c3, c4;
  124. int BestCost;
  125. int *BestOrder;
  126. double NewTemp, temp;
  127. double rand1;
  128. int innerloop, maxGen;
  129. int ecount, ucount, dcount;
  130. nvars = upper - lower + 1;
  131. result = cuddSifting(table,lower,upper);
  132. #ifdef DD_STATS
  133. (void) fprintf(table->out,"\n");
  134. #endif
  135. if (result == 0) return(0);
  136. size = table->keys - table->isolated;
  137. /* Keep track of the best order. */
  138. BestCost = size;
  139. BestOrder = ALLOC(int,nvars);
  140. if (BestOrder == NULL) {
  141. table->errorCode = CUDD_MEMORY_OUT;
  142. return(0);
  143. }
  144. copyOrder(table,BestOrder,lower,upper);
  145. temp = BETA * size;
  146. maxGen = (int) (MAXGEN_RATIO * nvars);
  147. c1 = size + 10;
  148. c2 = c1 + 10;
  149. c3 = size;
  150. c4 = c2 + 10;
  151. ecount = ucount = dcount = 0;
  152. while (!stopping_criterion(c1, c2, c3, c4, temp)) {
  153. #ifdef DD_STATS
  154. (void) fprintf(table->out,"temp=%f\tsize=%d\tgen=%d\t",
  155. temp,size,maxGen);
  156. tosses = acceptances = 0;
  157. #endif
  158. for (innerloop = 0; innerloop < maxGen; innerloop++) {
  159. /* Choose x, y randomly. */
  160. x = (int) Cudd_Random() % nvars;
  161. do {
  162. y = (int) Cudd_Random() % nvars;
  163. } while (x == y);
  164. x += lower;
  165. y += lower;
  166. if (x > y) {
  167. int tmp = x;
  168. x = y;
  169. y = tmp;
  170. }
  171. /* Choose move with roulette wheel. */
  172. rand1 = random_generator();
  173. if (rand1 < EXC_PROB) {
  174. result = ddExchange(table,x,y,temp); /* exchange */
  175. ecount++;
  176. #if 0
  177. (void) fprintf(table->out,
  178. "Exchange of %d and %d: size = %d\n",
  179. x,y,table->keys - table->isolated);
  180. #endif
  181. } else if (rand1 < EXC_PROB + JUMP_UP_PROB) {
  182. result = ddJumpingAux(table,y,x,y,temp); /* jumping_up */
  183. ucount++;
  184. #if 0
  185. (void) fprintf(table->out,
  186. "Jump up of %d to %d: size = %d\n",
  187. y,x,table->keys - table->isolated);
  188. #endif
  189. } else {
  190. result = ddJumpingAux(table,x,x,y,temp); /* jumping_down */
  191. dcount++;
  192. #if 0
  193. (void) fprintf(table->out,
  194. "Jump down of %d to %d: size = %d\n",
  195. x,y,table->keys - table->isolated);
  196. #endif
  197. }
  198. if (!result) {
  199. FREE(BestOrder);
  200. return(0);
  201. }
  202. size = table->keys - table->isolated; /* keep current size */
  203. if (size < BestCost) { /* update best order */
  204. BestCost = size;
  205. copyOrder(table,BestOrder,lower,upper);
  206. }
  207. }
  208. c1 = c2;
  209. c2 = c3;
  210. c3 = c4;
  211. c4 = size;
  212. NewTemp = ALPHA * temp;
  213. if (NewTemp >= 1.0) {
  214. maxGen = (int)(log(NewTemp) / log(temp) * maxGen);
  215. }
  216. temp = NewTemp; /* control variable */
  217. #ifdef DD_STATS
  218. (void) fprintf(table->out,"uphill = %d\taccepted = %d\n",
  219. tosses,acceptances);
  220. fflush(table->out);
  221. #endif
  222. }
  223. result = restoreOrder(table,BestOrder,lower,upper);
  224. FREE(BestOrder);
  225. if (!result) return(0);
  226. #ifdef DD_STATS
  227. fprintf(table->out,"#:N_EXCHANGE %8d : total exchanges\n",ecount);
  228. fprintf(table->out,"#:N_JUMPUP %8d : total jumps up\n",ucount);
  229. fprintf(table->out,"#:N_JUMPDOWN %8d : total jumps down",dcount);
  230. #endif
  231. return(1);
  232. } /* end of cuddAnnealing */
  233. /*---------------------------------------------------------------------------*/
  234. /* Definition of static functions */
  235. /*---------------------------------------------------------------------------*/
  236. /**Function********************************************************************
  237. Synopsis [Checks termination condition.]
  238. Description [If temperature is STOP_TEMP or there is no improvement
  239. then terminates. Returns 1 if the termination criterion is met; 0
  240. otherwise.]
  241. SideEffects [None]
  242. SeeAlso []
  243. ******************************************************************************/
  244. static int
  245. stopping_criterion(
  246. int c1,
  247. int c2,
  248. int c3,
  249. int c4,
  250. double temp)
  251. {
  252. if (STOP_TEMP < temp) {
  253. return(0);
  254. } else if ((c1 == c2) && (c1 == c3) && (c1 == c4)) {
  255. return(1);
  256. } else {
  257. return(0);
  258. }
  259. } /* end of stopping_criterion */
  260. /**Function********************************************************************
  261. Synopsis [Random number generator.]
  262. Description [Returns a double precision value between 0.0 and 1.0.]
  263. SideEffects [None]
  264. SeeAlso []
  265. ******************************************************************************/
  266. static double
  267. random_generator(void)
  268. {
  269. return((double)(Cudd_Random() / 2147483561.0));
  270. } /* end of random_generator */
  271. /**Function********************************************************************
  272. Synopsis [This function is for exchanging two variables, x and y.]
  273. Description [This is the same funcion as ddSwapping except for
  274. comparison expression. Use probability function, exp(-size_change/temp).]
  275. SideEffects [None]
  276. SeeAlso []
  277. ******************************************************************************/
  278. static int
  279. ddExchange(
  280. DdManager * table,
  281. int x,
  282. int y,
  283. double temp)
  284. {
  285. Move *move,*moves;
  286. int tmp;
  287. int x_ref,y_ref;
  288. int x_next,y_next;
  289. int size, result;
  290. int initial_size, limit_size;
  291. x_ref = x;
  292. y_ref = y;
  293. x_next = cuddNextHigh(table,x);
  294. y_next = cuddNextLow(table,y);
  295. moves = NULL;
  296. initial_size = limit_size = table->keys - table->isolated;
  297. for (;;) {
  298. if (x_next == y_next) {
  299. size = cuddSwapInPlace(table,x,x_next);
  300. if (size == 0) goto ddExchangeOutOfMem;
  301. move = (Move *)cuddDynamicAllocNode(table);
  302. if (move == NULL) goto ddExchangeOutOfMem;
  303. move->x = x;
  304. move->y = x_next;
  305. move->size = size;
  306. move->next = moves;
  307. moves = move;
  308. size = cuddSwapInPlace(table,y_next,y);
  309. if (size == 0) goto ddExchangeOutOfMem;
  310. move = (Move *)cuddDynamicAllocNode(table);
  311. if (move == NULL) goto ddExchangeOutOfMem;
  312. move->x = y_next;
  313. move->y = y;
  314. move->size = size;
  315. move->next = moves;
  316. moves = move;
  317. size = cuddSwapInPlace(table,x,x_next);
  318. if (size == 0) goto ddExchangeOutOfMem;
  319. move = (Move *)cuddDynamicAllocNode(table);
  320. if (move == NULL) goto ddExchangeOutOfMem;
  321. move->x = x;
  322. move->y = x_next;
  323. move->size = size;
  324. move->next = moves;
  325. moves = move;
  326. tmp = x;
  327. x = y;
  328. y = tmp;
  329. } else if (x == y_next) {
  330. size = cuddSwapInPlace(table,x,x_next);
  331. if (size == 0) goto ddExchangeOutOfMem;
  332. move = (Move *)cuddDynamicAllocNode(table);
  333. if (move == NULL) goto ddExchangeOutOfMem;
  334. move->x = x;
  335. move->y = x_next;
  336. move->size = size;
  337. move->next = moves;
  338. moves = move;
  339. tmp = x;
  340. x = y;
  341. y = tmp;
  342. } else {
  343. size = cuddSwapInPlace(table,x,x_next);
  344. if (size == 0) goto ddExchangeOutOfMem;
  345. move = (Move *)cuddDynamicAllocNode(table);
  346. if (move == NULL) goto ddExchangeOutOfMem;
  347. move->x = x;
  348. move->y = x_next;
  349. move->size = size;
  350. move->next = moves;
  351. moves = move;
  352. size = cuddSwapInPlace(table,y_next,y);
  353. if (size == 0) goto ddExchangeOutOfMem;
  354. move = (Move *)cuddDynamicAllocNode(table);
  355. if (move == NULL) goto ddExchangeOutOfMem;
  356. move->x = y_next;
  357. move->y = y;
  358. move->size = size;
  359. move->next = moves;
  360. moves = move;
  361. x = x_next;
  362. y = y_next;
  363. }
  364. x_next = cuddNextHigh(table,x);
  365. y_next = cuddNextLow(table,y);
  366. if (x_next > y_ref) break;
  367. if ((double) size > DD_MAX_REORDER_GROWTH * (double) limit_size) {
  368. break;
  369. } else if (size < limit_size) {
  370. limit_size = size;
  371. }
  372. }
  373. if (y_next>=x_ref) {
  374. size = cuddSwapInPlace(table,y_next,y);
  375. if (size == 0) goto ddExchangeOutOfMem;
  376. move = (Move *)cuddDynamicAllocNode(table);
  377. if (move == NULL) goto ddExchangeOutOfMem;
  378. move->x = y_next;
  379. move->y = y;
  380. move->size = size;
  381. move->next = moves;
  382. moves = move;
  383. }
  384. /* move backward and stop at best position or accept uphill move */
  385. result = siftBackwardProb(table,moves,initial_size,temp);
  386. if (!result) goto ddExchangeOutOfMem;
  387. while (moves != NULL) {
  388. move = moves->next;
  389. cuddDeallocMove(table, moves);
  390. moves = move;
  391. }
  392. return(1);
  393. ddExchangeOutOfMem:
  394. while (moves != NULL) {
  395. move = moves->next;
  396. cuddDeallocMove(table, moves);
  397. moves = move;
  398. }
  399. return(0);
  400. } /* end of ddExchange */
  401. /**Function********************************************************************
  402. Synopsis [Moves a variable to a specified position.]
  403. Description [If x==x_low, it executes jumping_down. If x==x_high, it
  404. executes jumping_up. This funcion is similar to ddSiftingAux. Returns
  405. 1 in case of success; 0 otherwise.]
  406. SideEffects [None]
  407. SeeAlso []
  408. ******************************************************************************/
  409. static int
  410. ddJumpingAux(
  411. DdManager * table,
  412. int x,
  413. int x_low,
  414. int x_high,
  415. double temp)
  416. {
  417. Move *move;
  418. Move *moves; /* list of moves */
  419. int initial_size;
  420. int result;
  421. initial_size = table->keys - table->isolated;
  422. #ifdef DD_DEBUG
  423. assert(table->subtables[x].keys > 0);
  424. #endif
  425. moves = NULL;
  426. if (cuddNextLow(table,x) < x_low) {
  427. if (cuddNextHigh(table,x) > x_high) return(1);
  428. moves = ddJumpingDown(table,x,x_high,initial_size);
  429. /* after that point x --> x_high unless early termination */
  430. if (moves == NULL) goto ddJumpingAuxOutOfMem;
  431. /* move backward and stop at best position or accept uphill move */
  432. result = siftBackwardProb(table,moves,initial_size,temp);
  433. if (!result) goto ddJumpingAuxOutOfMem;
  434. } else if (cuddNextHigh(table,x) > x_high) {
  435. moves = ddJumpingUp(table,x,x_low,initial_size);
  436. /* after that point x --> x_low unless early termination */
  437. if (moves == NULL) goto ddJumpingAuxOutOfMem;
  438. /* move backward and stop at best position or accept uphill move */
  439. result = siftBackwardProb(table,moves,initial_size,temp);
  440. if (!result) goto ddJumpingAuxOutOfMem;
  441. } else {
  442. (void) fprintf(table->err,"Unexpected condition in ddJumping\n");
  443. goto ddJumpingAuxOutOfMem;
  444. }
  445. while (moves != NULL) {
  446. move = moves->next;
  447. cuddDeallocMove(table, moves);
  448. moves = move;
  449. }
  450. return(1);
  451. ddJumpingAuxOutOfMem:
  452. while (moves != NULL) {
  453. move = moves->next;
  454. cuddDeallocMove(table, moves);
  455. moves = move;
  456. }
  457. return(0);
  458. } /* end of ddJumpingAux */
  459. /**Function********************************************************************
  460. Synopsis [This function is for jumping up.]
  461. Description [This is a simplified version of ddSiftingUp. It does not
  462. use lower bounding. Returns the set of moves in case of success; NULL
  463. if memory is full.]
  464. SideEffects [None]
  465. SeeAlso []
  466. ******************************************************************************/
  467. static Move *
  468. ddJumpingUp(
  469. DdManager * table,
  470. int x,
  471. int x_low,
  472. int initial_size)
  473. {
  474. Move *moves;
  475. Move *move;
  476. int y;
  477. int size;
  478. int limit_size = initial_size;
  479. moves = NULL;
  480. y = cuddNextLow(table,x);
  481. while (y >= x_low) {
  482. size = cuddSwapInPlace(table,y,x);
  483. if (size == 0) goto ddJumpingUpOutOfMem;
  484. move = (Move *)cuddDynamicAllocNode(table);
  485. if (move == NULL) goto ddJumpingUpOutOfMem;
  486. move->x = y;
  487. move->y = x;
  488. move->size = size;
  489. move->next = moves;
  490. moves = move;
  491. if ((double) size > table->maxGrowth * (double) limit_size) {
  492. break;
  493. } else if (size < limit_size) {
  494. limit_size = size;
  495. }
  496. x = y;
  497. y = cuddNextLow(table,x);
  498. }
  499. return(moves);
  500. ddJumpingUpOutOfMem:
  501. while (moves != NULL) {
  502. move = moves->next;
  503. cuddDeallocMove(table, moves);
  504. moves = move;
  505. }
  506. return(NULL);
  507. } /* end of ddJumpingUp */
  508. /**Function********************************************************************
  509. Synopsis [This function is for jumping down.]
  510. Description [This is a simplified version of ddSiftingDown. It does not
  511. use lower bounding. Returns the set of moves in case of success; NULL
  512. if memory is full.]
  513. SideEffects [None]
  514. SeeAlso []
  515. ******************************************************************************/
  516. static Move *
  517. ddJumpingDown(
  518. DdManager * table,
  519. int x,
  520. int x_high,
  521. int initial_size)
  522. {
  523. Move *moves;
  524. Move *move;
  525. int y;
  526. int size;
  527. int limit_size = initial_size;
  528. moves = NULL;
  529. y = cuddNextHigh(table,x);
  530. while (y <= x_high) {
  531. size = cuddSwapInPlace(table,x,y);
  532. if (size == 0) goto ddJumpingDownOutOfMem;
  533. move = (Move *)cuddDynamicAllocNode(table);
  534. if (move == NULL) goto ddJumpingDownOutOfMem;
  535. move->x = x;
  536. move->y = y;
  537. move->size = size;
  538. move->next = moves;
  539. moves = move;
  540. if ((double) size > table->maxGrowth * (double) limit_size) {
  541. break;
  542. } else if (size < limit_size) {
  543. limit_size = size;
  544. }
  545. x = y;
  546. y = cuddNextHigh(table,x);
  547. }
  548. return(moves);
  549. ddJumpingDownOutOfMem:
  550. while (moves != NULL) {
  551. move = moves->next;
  552. cuddDeallocMove(table, moves);
  553. moves = move;
  554. }
  555. return(NULL);
  556. } /* end of ddJumpingDown */
  557. /**Function********************************************************************
  558. Synopsis [Returns the DD to the best position encountered during
  559. sifting if there was improvement.]
  560. Description [Otherwise, "tosses a coin" to decide whether to keep
  561. the current configuration or return the DD to the original
  562. one. Returns 1 in case of success; 0 otherwise.]
  563. SideEffects [None]
  564. SeeAlso []
  565. ******************************************************************************/
  566. static int
  567. siftBackwardProb(
  568. DdManager * table,
  569. Move * moves,
  570. int size,
  571. double temp)
  572. {
  573. Move *move;
  574. int res;
  575. int best_size = size;
  576. double coin, threshold;
  577. /* Look for best size during the last sifting */
  578. for (move = moves; move != NULL; move = move->next) {
  579. if (move->size < best_size) {
  580. best_size = move->size;
  581. }
  582. }
  583. /* If best_size equals size, the last sifting did not produce any
  584. ** improvement. We now toss a coin to decide whether to retain
  585. ** this change or not.
  586. */
  587. if (best_size == size) {
  588. coin = random_generator();
  589. #ifdef DD_STATS
  590. tosses++;
  591. #endif
  592. threshold = exp(-((double)(table->keys - table->isolated - size))/temp);
  593. if (coin < threshold) {
  594. #ifdef DD_STATS
  595. acceptances++;
  596. #endif
  597. return(1);
  598. }
  599. }
  600. /* Either there was improvement, or we have decided not to
  601. ** accept the uphill move. Go to best position.
  602. */
  603. res = table->keys - table->isolated;
  604. for (move = moves; move != NULL; move = move->next) {
  605. if (res == best_size) return(1);
  606. res = cuddSwapInPlace(table,(int)move->x,(int)move->y);
  607. if (!res) return(0);
  608. }
  609. return(1);
  610. } /* end of sift_backward_prob */
  611. /**Function********************************************************************
  612. Synopsis [Copies the current variable order to array.]
  613. Description [Copies the current variable order to array.
  614. At the same time inverts the permutation.]
  615. SideEffects [None]
  616. SeeAlso []
  617. ******************************************************************************/
  618. static void
  619. copyOrder(
  620. DdManager * table,
  621. int * array,
  622. int lower,
  623. int upper)
  624. {
  625. int i;
  626. int nvars;
  627. nvars = upper - lower + 1;
  628. for (i = 0; i < nvars; i++) {
  629. array[i] = table->invperm[i+lower];
  630. }
  631. } /* end of copyOrder */
  632. /**Function********************************************************************
  633. Synopsis [Restores the variable order in array by a series of sifts up.]
  634. Description [Restores the variable order in array by a series of sifts up.
  635. Returns 1 in case of success; 0 otherwise.]
  636. SideEffects [None]
  637. SeeAlso []
  638. ******************************************************************************/
  639. static int
  640. restoreOrder(
  641. DdManager * table,
  642. int * array,
  643. int lower,
  644. int upper)
  645. {
  646. int i, x, y, size;
  647. int nvars = upper - lower + 1;
  648. for (i = 0; i < nvars; i++) {
  649. x = table->perm[array[i]];
  650. #ifdef DD_DEBUG
  651. assert(x >= lower && x <= upper);
  652. #endif
  653. y = cuddNextLow(table,x);
  654. while (y >= i + lower) {
  655. size = cuddSwapInPlace(table,y,x);
  656. if (size == 0) return(0);
  657. x = y;
  658. y = cuddNextLow(table,x);
  659. }
  660. }
  661. return(1);
  662. } /* end of restoreOrder */