cuddAnneal.c 24 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddAnneal.c]

  PackageName [cudd]

  Synopsis    [Reordering of DDs based on simulated annealing]

  Description [Internal procedures included in this file:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
                <ul>
                <li> cuddAnnealing()
                </ul>
               Static procedures included in this file:
                <ul>
                <li> stopping_criterion()
                <li> random_generator()
                <li> ddExchange()
                <li> ddJumpingAux()
                <li> ddJumpingUp()
                <li> ddJumpingDown()
                <li> siftBackwardProb()
                <li> copyOrder()
                <li> restoreOrder()
                </ul>
                ]
Alan Mishchenko committed
26 27 28 29 30

  SeeAlso     []

  Author      [Jae-Young Jang, Jorgen Sivesind]

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
62 63 64

******************************************************************************/

65
#include "misc/util/util_hack.h"
Alan Mishchenko committed
66 67
#include "cuddInt.h"

68 69 70
ABC_NAMESPACE_IMPL_START


71

Alan Mishchenko committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

/* Annealing parameters */
#define BETA 0.6 
#define ALPHA 0.90
#define EXC_PROB 0.4 
#define JUMP_UP_PROB 0.36
#define MAXGEN_RATIO 15.0
#define STOP_TEMP 1.0

/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
99
static char rcsid[] DD_UNUSED = "$Id: cuddAnneal.c,v 1.14 2004/08/13 18:04:46 fabio Exp $";
Alan Mishchenko committed
100 101 102
#endif

#ifdef DD_STATS
103 104 105 106
extern  int     ddTotalNumberSwapping;
extern  int     ddTotalNISwaps;
static  int     tosses;
static  int     acceptances;
Alan Mishchenko committed
107 108 109 110 111 112 113 114 115 116 117 118 119
#endif

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/


/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

120 121 122 123 124 125 126 127 128
static int stopping_criterion (int c1, int c2, int c3, int c4, double temp);
static double random_generator (void);
static int ddExchange (DdManager *table, int x, int y, double temp);
static int ddJumpingAux (DdManager *table, int x, int x_low, int x_high, double temp);
static Move * ddJumpingUp (DdManager *table, int x, int x_low, int initial_size);
static Move * ddJumpingDown (DdManager *table, int x, int x_high, int initial_size);
static int siftBackwardProb (DdManager *table, Move *moves, int size, double temp);
static void copyOrder (DdManager *table, int *array, int lower, int upper);
static int restoreOrder (DdManager *table, int *array, int lower, int upper);
Alan Mishchenko committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Get new variable-order by simulated annealing algorithm.]

  Description [Get x, y by random selection. Choose either
  exchange or jump randomly. In case of jump, choose between jump_up
  and jump_down randomly. Do exchange or jump and get optimal case.
  Loop until there is no improvement or temperature reaches
  minimum. Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
cuddAnnealing(
  DdManager * table,
  int  lower,
  int  upper)
{
    int         nvars;
    int         size;
    int         x,y;
    int         result;
167 168 169 170 171
    int         c1, c2, c3, c4;
    int         BestCost;
    int         *BestOrder;
    double      NewTemp, temp;
    double      rand1;
Alan Mishchenko committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    int         innerloop, maxGen;
    int         ecount, ucount, dcount;
   
    nvars = upper - lower + 1;

    result = cuddSifting(table,lower,upper);
#ifdef DD_STATS
    (void) fprintf(table->out,"\n");
#endif
    if (result == 0) return(0);

    size = table->keys - table->isolated;

    /* Keep track of the best order. */
    BestCost = size;
Alan Mishchenko committed
187
    BestOrder = ABC_ALLOC(int,nvars);
Alan Mishchenko committed
188
    if (BestOrder == NULL) {
189 190
        table->errorCode = CUDD_MEMORY_OUT;
        return(0);
Alan Mishchenko committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204
    }
    copyOrder(table,BestOrder,lower,upper);

    temp = BETA * size;
    maxGen = (int) (MAXGEN_RATIO * nvars);

    c1 = size + 10;
    c2 = c1 + 10;
    c3 = size;
    c4 = c2 + 10;
    ecount = ucount = dcount = 0;
 
    while (!stopping_criterion(c1, c2, c3, c4, temp)) {
#ifdef DD_STATS
205 206 207
        (void) fprintf(table->out,"temp=%f\tsize=%d\tgen=%d\t",
                       temp,size,maxGen);
        tosses = acceptances = 0;
Alan Mishchenko committed
208
#endif
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        for (innerloop = 0; innerloop < maxGen; innerloop++) {
            /* Choose x, y  randomly. */
            x = (int) Cudd_Random() % nvars;
            do {
                y = (int) Cudd_Random() % nvars;
            } while (x == y);
            x += lower;
            y += lower;
            if (x > y) {
                int tmp = x;
                x = y;
                y = tmp;
            }

            /* Choose move with roulette wheel. */
            rand1 = random_generator();
            if (rand1 < EXC_PROB) {
                result = ddExchange(table,x,y,temp);       /* exchange */
                ecount++;
Alan Mishchenko committed
228
#if 0
229 230 231
                (void) fprintf(table->out,
                               "Exchange of %d and %d: size = %d\n",
                               x,y,table->keys - table->isolated);
Alan Mishchenko committed
232
#endif
233 234 235
            } else if (rand1 < EXC_PROB + JUMP_UP_PROB) {
                result = ddJumpingAux(table,y,x,y,temp); /* jumping_up */
                ucount++;
Alan Mishchenko committed
236
#if 0
237 238 239
                (void) fprintf(table->out,
                               "Jump up of %d to %d: size = %d\n",
                               y,x,table->keys - table->isolated);
Alan Mishchenko committed
240
#endif
241 242 243
            } else {
                result = ddJumpingAux(table,x,x,y,temp); /* jumping_down */
                dcount++;
Alan Mishchenko committed
244
#if 0
245 246 247
                (void) fprintf(table->out,
                               "Jump down of %d to %d: size = %d\n",
                               x,y,table->keys - table->isolated);
Alan Mishchenko committed
248
#endif
249 250 251 252 253 254 255 256 257 258 259 260
            }

            if (!result) {
                ABC_FREE(BestOrder);
                return(0);
            }

            size = table->keys - table->isolated;       /* keep current size */
            if (size < BestCost) {                      /* update best order */
                BestCost = size;
                copyOrder(table,BestOrder,lower,upper);
            }
Alan Mishchenko committed
261
        }
262 263 264 265 266 267 268
        c1 = c2;
        c2 = c3;
        c3 = c4;
        c4 = size;
        NewTemp = ALPHA * temp;
        if (NewTemp >= 1.0) {
            maxGen = (int)(log(NewTemp) / log(temp) * maxGen);
Alan Mishchenko committed
269
        }
270
        temp = NewTemp;                 /* control variable */
Alan Mishchenko committed
271
#ifdef DD_STATS
272 273 274
        (void) fprintf(table->out,"uphill = %d\taccepted = %d\n",
                       tosses,acceptances);
        fflush(table->out);
Alan Mishchenko committed
275 276 277 278
#endif
    }

    result = restoreOrder(table,BestOrder,lower,upper);
Alan Mishchenko committed
279
    ABC_FREE(BestOrder);
Alan Mishchenko committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    if (!result) return(0);
#ifdef DD_STATS
    fprintf(table->out,"#:N_EXCHANGE %8d : total exchanges\n",ecount);
    fprintf(table->out,"#:N_JUMPUP   %8d : total jumps up\n",ucount);
    fprintf(table->out,"#:N_JUMPDOWN %8d : total jumps down",dcount);
#endif
    return(1);

} /* end of cuddAnnealing */


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/

/**Function********************************************************************

  Synopsis    [Checks termination condition.]

  Description [If temperature is STOP_TEMP or there is no improvement
  then terminates. Returns 1 if the termination criterion is met; 0
  otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
stopping_criterion(
  int  c1,
  int  c2,
  int  c3,
  int  c4,
  double  temp)
{
    if (STOP_TEMP < temp) {
317
        return(0);
Alan Mishchenko committed
318
    } else if ((c1 == c2) && (c1 == c3) && (c1 == c4)) {
319
        return(1);
Alan Mishchenko committed
320
    } else {
321
        return(0);
Alan Mishchenko committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    }

} /* end of stopping_criterion */


/**Function********************************************************************

  Synopsis    [Random number generator.]

  Description [Returns a double precision value between 0.0 and 1.0.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static double
339
random_generator(void)
Alan Mishchenko committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
{
    return((double)(Cudd_Random() / 2147483561.0));

} /* end of random_generator */


/**Function********************************************************************

  Synopsis    [This function is for exchanging two variables, x and y.]

  Description [This is the same funcion as ddSwapping except for
  comparison expression.  Use probability function, exp(-size_change/temp).]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
ddExchange(
  DdManager * table,
  int  x,
  int  y,
  double  temp)
{
    Move       *move,*moves;
    int        tmp;
    int        x_ref,y_ref;
    int        x_next,y_next;
    int        size, result;
    int        initial_size, limit_size;

    x_ref = x;
    y_ref = y;

    x_next = cuddNextHigh(table,x);
    y_next = cuddNextLow(table,y);
    moves = NULL;
    initial_size = limit_size = table->keys - table->isolated;

    for (;;) {
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
        if (x_next == y_next) {
            size = cuddSwapInPlace(table,x,x_next);
            if (size == 0) goto ddExchangeOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL) goto ddExchangeOutOfMem;
            move->x = x;
            move->y = x_next;
            move->size = size;
            move->next = moves;
            moves = move;
            size = cuddSwapInPlace(table,y_next,y);
            if (size == 0) goto ddExchangeOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL) goto ddExchangeOutOfMem;
            move->x = y_next;
            move->y = y;
            move->size = size;
            move->next = moves;
            moves = move;
            size = cuddSwapInPlace(table,x,x_next);
            if (size == 0) goto ddExchangeOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL) goto ddExchangeOutOfMem;
            move->x = x;
            move->y = x_next;
            move->size = size;
            move->next = moves;
            moves = move;

            tmp = x;
            x = y;
            y = tmp;
        } else if (x == y_next) {
            size = cuddSwapInPlace(table,x,x_next);
            if (size == 0) goto ddExchangeOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL) goto ddExchangeOutOfMem;
            move->x = x;
            move->y = x_next;
            move->size = size;
            move->next = moves;
            moves = move;
            tmp = x;
            x = y;
            y = tmp;
        } else {
            size = cuddSwapInPlace(table,x,x_next);
            if (size == 0) goto ddExchangeOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL) goto ddExchangeOutOfMem;
            move->x = x;
            move->y = x_next;
            move->size = size;
            move->next = moves;
            moves = move;
            size = cuddSwapInPlace(table,y_next,y);
            if (size == 0) goto ddExchangeOutOfMem;
            move = (Move *)cuddDynamicAllocNode(table);
            if (move == NULL) goto ddExchangeOutOfMem;
            move->x = y_next;
            move->y = y;
            move->size = size;
            move->next = moves;
            moves = move;
            x = x_next;
            y = y_next;
        }
Alan Mishchenko committed
448

449 450 451
        x_next = cuddNextHigh(table,x);
        y_next = cuddNextLow(table,y);
        if (x_next > y_ref) break;
Alan Mishchenko committed
452

453 454 455 456 457
        if ((double) size > DD_MAX_REORDER_GROWTH * (double) limit_size) {
            break;
        } else if (size < limit_size) {
            limit_size = size;
        }
Alan Mishchenko committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    }

    if (y_next>=x_ref) {
        size = cuddSwapInPlace(table,y_next,y);
        if (size == 0) goto ddExchangeOutOfMem;
        move = (Move *)cuddDynamicAllocNode(table);
        if (move == NULL) goto ddExchangeOutOfMem;
        move->x = y_next;
        move->y = y;
        move->size = size;
        move->next = moves;
        moves = move;
    }

    /* move backward and stop at best position or accept uphill move */
    result = siftBackwardProb(table,moves,initial_size,temp);
    if (!result) goto ddExchangeOutOfMem;

    while (moves != NULL) {
477 478 479
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
480 481 482 483 484 485
    }
    return(1);

ddExchangeOutOfMem:
    while (moves != NULL) {
        move = moves->next;
486
        cuddDeallocMove(table, moves);
Alan Mishchenko committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
        moves = move;
    }
    return(0);

} /* end of ddExchange */


/**Function********************************************************************

  Synopsis    [Moves a variable to a specified position.]

  Description [If x==x_low, it executes jumping_down. If x==x_high, it
  executes jumping_up. This funcion is similar to ddSiftingAux. Returns
  1 in case of success; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
ddJumpingAux(
  DdManager * table,
  int  x,
  int  x_low,
  int  x_high,
  double  temp)
{
    Move       *move;
    Move       *moves;        /* list of moves */
    int        initial_size;
    int        result;

    initial_size = table->keys - table->isolated;

#ifdef DD_DEBUG
    assert(table->subtables[x].keys > 0);
#endif

    moves = NULL;

    if (cuddNextLow(table,x) < x_low) {
529 530 531 532 533 534 535
        if (cuddNextHigh(table,x) > x_high) return(1);
        moves = ddJumpingDown(table,x,x_high,initial_size);
        /* after that point x --> x_high unless early termination */
        if (moves == NULL) goto ddJumpingAuxOutOfMem;
        /* move backward and stop at best position or accept uphill move */
        result = siftBackwardProb(table,moves,initial_size,temp);
        if (!result) goto ddJumpingAuxOutOfMem;
Alan Mishchenko committed
536
    } else if (cuddNextHigh(table,x) > x_high) {
537 538 539 540 541 542
        moves = ddJumpingUp(table,x,x_low,initial_size);
        /* after that point x --> x_low unless early termination */
        if (moves == NULL) goto ddJumpingAuxOutOfMem;
        /* move backward and stop at best position or accept uphill move */
        result = siftBackwardProb(table,moves,initial_size,temp);
        if (!result) goto ddJumpingAuxOutOfMem;
Alan Mishchenko committed
543
    } else {
544 545
        (void) fprintf(table->err,"Unexpected condition in ddJumping\n");
        goto ddJumpingAuxOutOfMem;
Alan Mishchenko committed
546 547
    }
    while (moves != NULL) {
548 549 550
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
551 552 553 554 555
    }
    return(1);

ddJumpingAuxOutOfMem:
    while (moves != NULL) {
556 557 558
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    }
    return(0);

} /* end of ddJumpingAux */


/**Function********************************************************************

  Synopsis    [This function is for jumping up.]

  Description [This is a simplified version of ddSiftingUp. It does not
  use lower bounding. Returns the set of moves in case of success; NULL
  if memory is full.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static Move *
ddJumpingUp(
  DdManager * table,
  int  x,
  int  x_low,
  int  initial_size)
{
    Move       *moves;
    Move       *move;
    int        y;
    int        size;
    int        limit_size = initial_size;

    moves = NULL;
    y = cuddNextLow(table,x);
    while (y >= x_low) {
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        size = cuddSwapInPlace(table,y,x);
        if (size == 0) goto ddJumpingUpOutOfMem;
        move = (Move *)cuddDynamicAllocNode(table);
        if (move == NULL) goto ddJumpingUpOutOfMem;
        move->x = y;
        move->y = x;
        move->size = size;
        move->next = moves;
        moves = move;
        if ((double) size > table->maxGrowth * (double) limit_size) {
            break;
        } else if (size < limit_size) {
            limit_size = size;
        }
        x = y;
        y = cuddNextLow(table,x);
Alan Mishchenko committed
610 611 612 613 614
    }
    return(moves);

ddJumpingUpOutOfMem:
    while (moves != NULL) {
615 616 617
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    }
    return(NULL);

} /* end of ddJumpingUp */


/**Function********************************************************************

  Synopsis    [This function is for jumping down.]

  Description [This is a simplified version of ddSiftingDown. It does not
  use lower bounding. Returns the set of moves in case of success; NULL
  if memory is full.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static Move *
ddJumpingDown(
  DdManager * table,
  int  x,
  int  x_high,
  int  initial_size)
{
    Move       *moves;
    Move       *move;
    int        y;
    int        size;
    int        limit_size = initial_size;

    moves = NULL;
    y = cuddNextHigh(table,x);
    while (y <= x_high) {
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
        size = cuddSwapInPlace(table,x,y);
        if (size == 0) goto ddJumpingDownOutOfMem;
        move = (Move *)cuddDynamicAllocNode(table);
        if (move == NULL) goto ddJumpingDownOutOfMem;
        move->x = x;
        move->y = y;
        move->size = size;
        move->next = moves;
        moves = move;
        if ((double) size > table->maxGrowth * (double) limit_size) {
            break;
        } else if (size < limit_size) {
            limit_size = size;
        }
        x = y;
        y = cuddNextHigh(table,x);
Alan Mishchenko committed
669 670 671 672 673
    }
    return(moves);

ddJumpingDownOutOfMem:
    while (moves != NULL) {
674 675 676
        move = moves->next;
        cuddDeallocMove(table, moves);
        moves = move;
Alan Mishchenko committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    }
    return(NULL);

} /* end of ddJumpingDown */


/**Function********************************************************************

  Synopsis [Returns the DD to the best position encountered during
  sifting if there was improvement.]

  Description [Otherwise, "tosses a coin" to decide whether to keep
  the current configuration or return the DD to the original
  one. Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
siftBackwardProb(
  DdManager * table,
  Move * moves,
  int  size,
  double  temp)
{
    Move   *move;
    int    res;
    int    best_size = size;
    double coin, threshold;

    /* Look for best size during the last sifting */
    for (move = moves; move != NULL; move = move->next) {
711 712 713
        if (move->size < best_size) {
            best_size = move->size;
        }
Alan Mishchenko committed
714 715 716 717 718 719 720
    }
    
    /* If best_size equals size, the last sifting did not produce any
    ** improvement. We now toss a coin to decide whether to retain
    ** this change or not.
    */
    if (best_size == size) {
721
        coin = random_generator();
Alan Mishchenko committed
722
#ifdef DD_STATS
723
        tosses++;
Alan Mishchenko committed
724
#endif
725 726
        threshold = exp(-((double)(table->keys - table->isolated - size))/temp);
        if (coin < threshold) {
Alan Mishchenko committed
727
#ifdef DD_STATS
728
            acceptances++;
Alan Mishchenko committed
729
#endif
730 731
            return(1);
        }
Alan Mishchenko committed
732 733 734 735 736 737 738
    }

    /* Either there was improvement, or we have decided not to
    ** accept the uphill move. Go to best position.
    */
    res = table->keys - table->isolated;
    for (move = moves; move != NULL; move = move->next) {
739 740 741
        if (res == best_size) return(1);
        res = cuddSwapInPlace(table,(int)move->x,(int)move->y);
        if (!res) return(0);
Alan Mishchenko committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    }

    return(1);

} /* end of sift_backward_prob */


/**Function********************************************************************

  Synopsis    [Copies the current variable order to array.]

  Description [Copies the current variable order to array.
  At the same time inverts the permutation.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
copyOrder(
  DdManager * table,
  int * array,
  int  lower,
  int  upper)
{
    int i;
    int nvars;

    nvars = upper - lower + 1;
    for (i = 0; i < nvars; i++) {
773
        array[i] = table->invperm[i+lower];
Alan Mishchenko committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
    }

} /* end of copyOrder */


/**Function********************************************************************

  Synopsis    [Restores the variable order in array by a series of sifts up.]

  Description [Restores the variable order in array by a series of sifts up.
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static int
restoreOrder(
  DdManager * table,
  int * array,
  int  lower,
  int  upper)
{
    int i, x, y, size;
    int nvars = upper - lower + 1;

    for (i = 0; i < nvars; i++) {
802
        x = table->perm[array[i]];
Alan Mishchenko committed
803 804 805 806
#ifdef DD_DEBUG
    assert(x >= lower && x <= upper);
#endif
        y = cuddNextLow(table,x);
807 808 809 810 811 812
        while (y >= i + lower) {
            size = cuddSwapInPlace(table,y,x);
            if (size == 0) return(0);
            x = y;
            y = cuddNextLow(table,x);
        }
Alan Mishchenko committed
813 814 815 816 817 818
    }

    return(1);

} /* end of restoreOrder */

819

820 821
ABC_NAMESPACE_IMPL_END

822