anneal.cc 40.5 KB
Newer Older
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2003-2009 University of Utah and the Flux Group.
4 5 6
 * All rights reserved.
 */

7 8
static const char rcsid[] = "$Id: anneal.cc,v 1.46 2009-05-20 18:06:07 tarunp Exp $";

9 10
#include "anneal.h"

11 12 13 14 15 16
#include "virtual.h"
#include "maps.h"
#include "common.h"
#include "score.h"
#include "solution.h"
#include "vclass.h"
17
#include "neighborhood.h"
18

19 20 21 22
/*
 * Internal variables
 */
// These variables store the best solution.
23
solution best_solution;
24 25 26 27 28 29 30 31 32 33 34

// Map of virtual node name to its vertex descriptor.
name_vvertex_map vname2vertex;

// This is a vector of all the nodes in the top file.  It's used
// to randomly choose nodes.
vvertex_vector virtual_nodes;

// Map of physical node name to its vertex descriptor.
name_pvertex_map pname2vertex;
  
Robert Ricci's avatar
Robert Ricci committed
35
// Map of virtual node name to the physical node name it's fixed to.
36 37 38 39
// The domain is the set of all fixed virtual nodes and the range is
// the set of all fixed physical nodes.
name_name_map fixed_nodes;

Robert Ricci's avatar
Robert Ricci committed
40 41 42 43 44
// Map of virtual node name to the physical node name that we should
// start the virtual node on. However, unlike fixed nodes, assign is
// allowed to move these.
name_name_map node_hints;

45 46 47 48 49
// From assign.cc
#ifdef GNUPLOT_OUTPUT
extern FILE *scoresout, *tempout, *deltaout;
#endif

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/*
 * Parameters used to control annealing
 */
int init_temp = 10;
int temp_prob = 130;
#ifdef LOW_TEMP_STOP
float temp_stop = .005;
#else
float temp_stop = 2;
#endif
int CYCLES = 20;

// The following are basically arbitrary constants
// Initial acceptance ratio for melting
float X0 = .95;
#ifdef LOCAL_DERIVATIVE
float epsilon = 0.0001;
#else
float epsilon = 0.01;
#endif
float delta = 2;

// Number of runs to spend melting
int melt_trans = 1000;
int min_neighborhood_size = 1000;

float temp_rate = 0.9;


79 80
// Determines whether to accept a change of score difference 'change' at
// temperature 'temperature'.
81
inline bool accept(double change, double temperature) {
82 83 84 85 86 87 88 89
  double p;
  int r;

  if (change == 0) {
    p = 1000 * temperature / temp_prob;
  } else {
    p = expf(change/temperature) * 1000;
  }
90
  r = RANDOM() % 1000;
91 92 93 94 95 96
  if (r < p) {
    return 1;
  }
  return 0;
}

97 98

#ifdef SMART_UNMAP
99
/*
100 101
 * XXX - I pulled this code out of the anneal loop, and it needs to be fixed
 * up (and get some arguments and a return type) before it will compile
102
 */
103 104 105 106
void smart_unmap() {
	// XXX: Should probably randomize this
	// XXX: Add support for not using PER_VNODE_TT
	// XXX: Not very robust
107

108
	freednode = true;
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	tt_entry tt = vnode_type_table[vn->name];
	int size = tt.first;
	pclass_vector *acceptable_types = tt.second;
	// Find a node to kick out
	bool foundnode = false;
	int offi = RANDOM();
	int index;
	for (int i = 0; i < size; i++) {
	  index = (i + offi) % size;
	  if ((*acceptable_types)[index]->used_members.find(vn->type) ==
	      (*acceptable_types)[index]->used_members.end()) {
	    continue;
	  }
	  if ((*acceptable_types)[index]->used_members[vn->type]->size() == 0) {
	    continue;
	  }
	  foundnode = true;
127 128 129
	  break;
	}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	if (foundnode) {
	  assert((*acceptable_types)[index]->used_members[vn->type]->size());
	  tb_pclass::tb_pnodeset::iterator it =
	    (*acceptable_types)[index]->used_members[vn->type]->begin();
	  int j = RANDOM() %
	    (*acceptable_types)[index]->used_members[vn->type]->size();
	  while (j > 0) {
	    it++;
	    j--;
	  }
	  tb_vnode_set::iterator it2 = (*it)->assigned_nodes.begin();
	  int k = RANDOM() % (*it)->assigned_nodes.size();
	  while (k > 0) {
	    it2++;
	    k--;
	  }
	  tb_vnode *kickout = *it2;
	  assert(kickout->assigned);
	  vvertex toremove = vname2vertex[kickout->name];
	  newpnode = *it;
	  remove_node(toremove);
151
	  unassigned_nodes.push_front(toremove);
152 153 154 155
	} else {
	  cerr << "Failed to find a replacement!" << endl;
	}
#endif /* SMART_UNMAP */
156

157
#ifdef SMART_UNMAP
158
/*
159 160
 * Part II of the smart_unmap code - again, needs to be fixed before it
 * will compile.
161
 */
162
void smart_unmap_part2() {
163
#ifdef PER_VNODE_TT
164
	  tt_entry tt = vnode_type_table[vn->name];
165
#else
166
	  tt_entry tt = type_table[vn->type];
167
#endif
168
	  pclass_vector *acceptable_types = tt.second;
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	  while (1) {
	    bool keepgoing = false;
	    if (get(vvertex_pmap,virtual_nodes[toremove])->fixed) {
	      keepgoing = true;
	    } else if (! get(vvertex_pmap,virtual_nodes[toremove])->assigned) {
	      keepgoing = true;
	    } else {
	      pvertex pv = get(vvertex_pmap,virtual_nodes[toremove])->assignment;
	      tb_pnode *pn = get(pvertex_pmap,pv);
	      int j;
	      for (j = 0; j < acceptable_types->size(); j++) {
		if ((*acceptable_types)[j] == pn->my_class) {
		  break;
		}
	      }
	      if (j == acceptable_types->size()) {
		keepgoing = true;
	      }
	    }
189

190 191 192
	    if (!keepgoing) {
	      break;
	    }
193
	}
194 195
#endif	
	
196 197 198
// We put the temperature outside the function so that external stuff, like
// status_report in assign.cc, can see it.
double temp;
199 200

/* When this is finished the state will reflect the best solution found. */
201 202 203
void anneal(bool scoring_selftest, bool check_fixed_nodes,
        double scale_neighborhood, double *initial_temperature,
        double use_connected_pnode_find)
204 205 206
{
  cout << "Annealing." << endl;

207 208 209 210 211 212 213 214 215 216 217 218
  /*
   * The score and number of violations at the start of the inner annealing
   * loop
   */
  double prev_score = 0;
  int prev_violated = 0;

  /*
   * 
   */
  double new_score = 0;
  double scorediff;
219 220 221 222 223 224 225 226
 
  // The number of iterations that took place.
  iters = 0;
  iters_to_best = 0;
  int accepts = 0;
  

  int nnodes = num_vertices(VG);
227
  //int npnodes = num_vertices(PG);
228 229 230 231 232 233 234 235 236 237 238
  int npclasses = pclasses.size();
  
  float cycles = CYCLES*(float)(nnodes + num_edges(VG) + PHYSICAL(npnodes));

  int mintrans = (int)cycles;
  int trans;
  int naccepts = 20*(nnodes + PHYSICAL(npnodes));
  pvertex oldpos;
  bool oldassigned;
  int num_fixed=0;
  double meltedtemp;
239
  temp = init_temp;
240 241
  double deltatemp, deltaavg;

242 243
  // List of unassigned virtual nodes
  slist<vvertex> unassigned_nodes;
244 245

#ifdef VERBOSE
246
  cout << "Initialized to cycles="<<cycles<<" mintrans="
247 248 249 250 251 252 253
       << mintrans<<" naccepts="<<naccepts<< endl;
#endif

  /* Set up the initial counts */
  init_score();

  /* Set up fixed nodes */
254 255 256 257 258
  /* Count of nodes which could not be fixed - we wait until we've tried to fix
   * all nodes before bailing, so that the user gets to see all of the
   * messages.
   */
  int fix_failed = 0;
259 260 261
  for (name_name_map::iterator fixed_it=fixed_nodes.begin();
       fixed_it!=fixed_nodes.end();++fixed_it) {
    if (vname2vertex.find((*fixed_it).first) == vname2vertex.end()) {
262
      cout << "*** Fixed virtual node: " << (*fixed_it).first <<
263
	" does not exist." << endl;
264 265
      fix_failed++;
      continue;
266 267 268
    }
    vvertex vv = vname2vertex[(*fixed_it).first];
    if (pname2vertex.find((*fixed_it).second) == pname2vertex.end()) {
269
      cout << "*** Fixed physical node: " << (*fixed_it).second <<
270
	" not available." << endl;
271 272
      fix_failed++;
      continue;
273 274 275 276 277
    }
    pvertex pv = pname2vertex[(*fixed_it).second];
    tb_vnode *vn = get(vvertex_pmap,vv);
    tb_pnode *pn = get(pvertex_pmap,pv);
    if (vn->vclass != NULL) {
278 279 280 281 282 283 284 285 286 287 288 289
      // Find a type on this physical node that can satisfy something in the
      // virtual class
      if (pn->typed) {
        if (vn->vclass->has_type(pn->current_type)) {
          vn->type = pn->current_type;
        }
      } else {
        for (tb_pnode::types_list::iterator i = pn->type_list.begin();
            i != pn->type_list.end(); i++) {
          // For now, if we find more than one match, we pick the first. It's
          // possible that picking some other type would give us a better
          // score, but let's noty worry about that
290 291
          if (vn->vclass->has_type((*i)->get_ptype()->name())) {
            vn->type = (*i)->get_ptype()->name();
292 293 294 295 296
            break;
          }
        }
      }
      if (vn->type.empty()) {
297 298
        // This is an internal error, so it's okay to handle it in a different
        // way from the others
299
        cout << "*** Unable to find a type for fixed, vtyped, node " << vn->name
300 301 302 303 304 305
          << endl;
        exit(EXIT_FATAL);
      } else {
        cout << "Setting type of vclass node " << vn->name << " to "
          << vn->type << "\n";
      }
306
    }
307 308 309 310 311 312 313 314 315 316 317

    /*
     * Normally, we want to bypass some checks in add_node for fixed nodes -
     * but not always (usually for testing purposes).
     */
    bool skip_checks = true;
    if (check_fixed_nodes) {
        skip_checks = false;
    }

    if (add_node(vv,pv,false,skip_checks,false) == 1) {
318
      cout << "*** Fixed node: Could not map " << vn->name <<
319
	" to " << pn->name << endl;
320 321
      fix_failed++;
      continue;
322 323
    }
    vn->fixed = true;
324 325 326 327 328 329
    /*
    if (vn->vclass != NULL) {
      vn->type = vn->vclass->choose_type();
      cout << "Picked type " << vn->type << " for " << vn->name << endl;
    }
    */
330 331 332
    num_fixed++;
  }

333 334 335 336 337
  if (fix_failed){
    cout << "*** Some fixed nodes failed to map" << endl;
    exit(EXIT_UNRETRYABLE);
  }

338 339 340 341
  // Subtract the number of fixed nodes from nnodes, since they don't really
  // count
  if (num_fixed) {
      cout << "Adjusting dificulty estimate for fixed nodes, " <<
342
	  (nnodes - num_fixed) << " remain.\n";
343 344
  }

345 346 347 348
  /* We'll check against this later to make sure that whe we've unmapped
   * everything, the score is the same */
  double initial_score = get_score();

Robert Ricci's avatar
Robert Ricci committed
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
  /*
   * Handle node hints - we do this _after_ we've figured out the initial
   * score, since, unlike fixed nodes, hints get unmapped before we do the
   * final mapping. Also, we ignore any hints for vnodes which have already
   * been assigned - they must have been fixed, and that over-rides the hint.
   */
  for (name_name_map::iterator hint_it=node_hints.begin();
       hint_it!=node_hints.end();++hint_it) {
    if (vname2vertex.find((*hint_it).first) == vname2vertex.end()) {
      cout << "Warning: Hinted node: " << (*hint_it).first <<
	"does not exist." << endl;
      continue;
    }
    vvertex vv = vname2vertex[(*hint_it).first];
    if (pname2vertex.find((*hint_it).second) == pname2vertex.end()) {
      cout << "Warning: Hinted node: " << (*hint_it).second <<
	" not available." << endl;
      continue;
    }
    pvertex pv = pname2vertex[(*hint_it).second];
    tb_vnode *vn = get(vvertex_pmap,vv);
    tb_pnode *pn = get(pvertex_pmap,pv);
    if (vn->assigned) {
      cout << "Warning: Skipping hint for node " << vn->name << ", which is "
	<< "fixed in place" << endl;
      continue;
    }
376
    if (add_node(vv,pv,false,false,false) == 1) {
Robert Ricci's avatar
Robert Ricci committed
377 378 379 380 381
      cout << "Warning: Hinted node: Could not map " << vn->name <<
	" to " << pn->name << endl;
      continue;
    }
  }
382 383 384 385
       
  /*
   * Find out the starting temperature
   */
386 387
  prev_score = get_score();
  prev_violated = violated;
388 389

#ifdef VERBOSE
390
  cout << "Problem started with score "<<prev_score<<" and "<< violated
391 392 393
       << " violations." << endl;
#endif

394 395
  best_score = prev_score;
  best_violated = prev_violated;
396

397 398 399
  /*
   * Make a list of all nodes that are still unassigned
   */
400 401 402 403 404
  vvertex_iterator vit,veit;
  tie(vit,veit) = vertices(VG);
  for (;vit!=veit;++vit) {
    tb_vnode *vn = get(vvertex_pmap,*vit);
    if (vn->assigned) {
405 406
	best_solution.set_assignment(*vit,vn->assignment);
	best_solution.set_vtype_assignment(*vit,vn->type);
407
    } else {
408
	best_solution.clear_assignment(*vit);
409
	unassigned_nodes.push_front(*vit);
410 411
    }
  }
412 413 414 415 416 417 418 419 420 421 422 423
  
  /*
   * Set any links that have been assigned
   */
  vedge_iterator eit, eeit;
  tie(eit, eeit) = edges(VG);
  for (;eit!=eeit;++eit) {
      tb_vlink *vlink = get(vedge_pmap, *eit);
      if (vlink->link_info.type_used != tb_link_info::LINK_UNMAPPED) {
	  best_solution.set_link_assignment(*eit,vlink->link_info);
      }
  }
424

425 426 427 428 429 430
  /*
   * The neighborhood size is the number of solutions we can reach with one
   * transition operation - it's roughly the number of virtual nodes times the
   * number of pclasses. This is how long we usually stick with a given 
   * temperature.
   */
431
  int neighborsize;
432
  neighborsize = (nnodes - num_fixed) * npclasses;
433 434 435
  if (neighborsize < min_neighborhood_size) {
    neighborsize = min_neighborhood_size;
  }
436 437 438 439 440

  // Allow scaling of the neighborhood size, so we can make assign try harder
  // (or less hard)
  neighborsize = (int)(neighborsize * scale_neighborhood);

441 442 443 444
#ifdef CHILL
  double scores[neighborsize];
#endif

445
  if (num_fixed >= nnodes) {
446 447 448 449 450 451 452 453
    cout << "All nodes are fixed.  No annealing." << endl;
    goto DONE;
  }
  
  vvertex vv;
  tb_vnode *vn;

  // Crap added by ricci
454
#ifdef MELT
455
  bool melting;
456
#endif
457 458 459 460 461
  int nincreases, ndecreases;
  double avgincrease;
  double avgscore;
  double initialavg;
  double stddev;
462 463 464 465 466
  bool finished; 
  bool forcerevert; 
  // Lame, we have to do this on a seperate line, or the compiler gets mad about
  // the goto above crossing initialization. Well, okay, okay, I know the goto
  // itself is lame....
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  finished = forcerevert = false;
  int tsteps;
  int mintsteps;

#define MAX_AVG_HIST 16
  double avghist[MAX_AVG_HIST];
  int hstart, nhist;
  hstart = nhist = 0;
  double lasttemp;
  double smoothedavg, lastsmoothed;
  lastsmoothed = 500000.0f;
  lasttemp = 5000.0f;
  int melttrials;
  melttrials = 0;

  bool finishedonce;
  finishedonce = false;

  tsteps = 0;
  mintsteps = MAX_AVG_HIST;
  tsteps = 0;
  mintsteps = MAX_AVG_HIST;
  tsteps = 0;
  mintsteps = MAX_AVG_HIST;

  // Make sure the last two don't prevent us from running!
  avgscore = initialavg = 1.0;

  stddev = 0;

497 498 499
  /*
   * Initial temperature calcuation/melting
   */
500
#ifdef MELT
Robert Ricci's avatar
Robert Ricci committed
501 502 503 504 505 506 507
  if (initial_temperature == NULL) {
      melting = true;
  } else {
      melting = false;
      temp = *initial_temperature;
      cout << "Starting with initial temperature " << temp << endl;
  }
508 509 510 511 512 513 514
#ifdef TIME_TARGET
  meltstart = used_time();
#endif
#else
  melting = false;
#endif

515
  /*
516 517
   * When melting, this is the number of different solutions we will try during
   * this temperature step
518
   */
519
  melt_trans = neighborsize;
520 521 522 523 524 525 526 527
  
  /*
   * The main annealing loop!
   * Each iteration is a temperature step - how we get out of the loop depends
   * on what the termination condition is. Normally, we have a target temperature
   * at which we stop, but with EPSILON_TERMINATE, we watch the derivative of the
   * average temperature, and break out of the loop when it gets small enough.
   */
528 529 530 531 532
#ifdef EPSILON_TERMINATE
  while(1) {
#else
  while (temp >= temp_stop) {
#endif
533
      
534
#ifdef VERBOSE
535 536
    cout << "Temperature:  " << temp << " Best: " << best_score <<
      " (" << best_violated << ")" << endl;
537
#endif
538

539 540 541
    /*
     * Initialize this temperature step
     */
542 543 544 545
    trans = 0;
    accepts = 0;
    nincreases = ndecreases = 0;
    avgincrease = 0.0;
546
    avgscore = prev_score;
547
#ifdef CHILL
548
    scores[0] = prev_score;
549 550
#endif

551 552
    // Adjust the number of transitions we're going to do based on the number
    // of pclasses that are actually 'in play'
553 554
    int transitions = (int)(neighborsize *
      (count_enabled_pclasses() *1.0 / pclasses.size()));
555 556
    assert(transitions <= neighborsize);

557 558 559 560
    if (melting) {
      cout << "Doing melting run" << endl;
    }

561 562 563 564 565 566
    /*
     * The inner loop - 
     * Each iteration of this inner loop corresponds to one attempt to try a new
     * solution. When we're melting, we have a special number of transitions
     * we're shooting for.
     */
567 568
    while ((melting && (trans < melt_trans))
#ifdef NEIGHBOR_LENGTH
569
	    || (trans < transitions)) {
570 571 572 573
#else
	    || (!melting && (trans < mintrans && accepts < naccepts))) {
#endif

574 575 576
    RDEBUG(cout << "ANNEALING: Loop starts with score " << get_score() <<
            " violations " << violated << endl;)

577 578 579 580 581 582 583 584 585 586
#ifdef STATS
      cout << "STATS temp:" << temp << " score:" << get_score() <<
	" violated:" << violated << " trans:" << trans <<
	" accepts:" << accepts << " current_time:" <<
	used_time() << endl;
#endif 
      pvertex newpos;
      trans++;
      iters++;

587 588 589 590 591 592
      /*
       * Find a virtual node to map -
       * If there are any virtual nodes that are not yet mapped, start with
       *   those
       * If not, find some other random vnode, which we'll unmap then remap
       */
593
      if (! unassigned_nodes.empty()) {
594 595 596 597 598 599 600
        // Pick a random node from the list of unassigned nodes
        int choice = RANDOM() % unassigned_nodes.size();
        slist<vvertex>::iterator uit = unassigned_nodes.begin();
        for (int i = 0; i < choice; i++) { uit++; }
        assert(uit != unassigned_nodes.end());

        vv = *uit;
601
	assert(!get(vvertex_pmap,vv)->assigned);
602 603 604 605
        unassigned_nodes.erase(uit);
        RDEBUG(cout << "Using unassigned node " << choice << ": " <<
                get(vvertex_pmap,vv)->name << " (" <<
                unassigned_nodes.size() << " in queue)" << endl;)
606
      } else {
607
	int start = RANDOM()%nnodes;
608 609 610 611 612 613 614 615 616 617 618
	int choice = start;
	while (get(vvertex_pmap,virtual_nodes[choice])->fixed) {
	  choice = (choice +1) % nnodes;
	  if (choice == start) {
	      choice = -1;
	      break;
	  }
	}
	if (choice >= 0) {
	    vv = virtual_nodes[choice];
	} else {
619
	    cout << "**** Error, unable to find any non-fixed nodes" << endl;
620 621
	    goto DONE;
	}
622
      }      
623 624
      vn = get(vvertex_pmap,vv);
      RDEBUG(cout << "Reassigning " << vn->name << endl;)
625 626 627 628
	  
      /*
       * Keep track of the old assignment for this node
       */
629 630
      oldassigned = vn->assigned;
      oldpos = vn->assignment;
631 632 633 634 635

      if (oldassigned) {
          RDEBUG(cout << "   was assigned to " <<
                  get(pvertex_pmap,oldpos)->name << endl;)
      }
636
      
637 638 639 640 641
      /*
       * Problem: If we free the chosen vnode now, we might just try remapping
       * it to the same pnode. If FREE_IMMEDIATELY is not set, we do the 
       * later, after we've chosen a pnode unmapping
       */
642 643 644 645 646 647 648
#ifdef FREE_IMMEDIATELY
      if (oldassigned) {
	remove_node(vv);
	RDEBUG(cout << "Freeing up " << vn->name << endl;)
      }
#endif
      
649 650 651 652
      /*
       * We have to handle vnodes with vtypes (vclasses) specially - we have
       * to make the vtype pick a type to masquerade as for now.
       */
653 654 655
      if (vn->vclass != NULL) {
	vn->type = vn->vclass->choose_type();
#ifdef SCORE_DEBUG
656
	cerr << "vclass " << vn->vclass->get_name()  << ": choose type for " <<
657
	    vn->name << " = " << vn->type << " dominant = " <<
658
	    vn->vclass->get_dominant() << endl;
659 660
#endif
      }
661 662 663 664 665 666 667
      
      // Did we free a node?
      bool freednode = false;
      
      /* 
       * Find a pnode to map this vnode to
       */
668 669
      tb_pnode *newpnode = NULL;
      if ((use_connected_pnode_find != 0)
670
	  && ((RANDOM() % 1000) < (use_connected_pnode_find * 1000))) {
671
        RDEBUG(cout << "   using find_pnode_connected" << endl;)
672 673
	newpnode = find_pnode_connected(vv,vn);
      }
674 675 676 677 678
      
      /* 
       * If not using the connected find, or it failed to find a node, then
       * fall back on the regular algorithm to find a pnode
       */
679
      if (newpnode == NULL) {
680
        RDEBUG(cout << "   using find_pnode" << endl;)
681 682
	newpnode = find_pnode(vn);
      }
683 684 685 686
      
      /*
       * If we didn't free the vnode up above, do it now
       */
687
#ifndef FREE_IMMEDIATELY
688 689
      if (oldassigned) {
	RDEBUG(cout << "removing: !lan, oldassigned" << endl;)
690
	remove_node(vv);
691
      }
692
#endif
693 694 695 696 697
      
      /*
       * If we didn't find a node to map this vnode to, free up some other
       * vnode so that we can make progress - otherwise, we could get stuck
       */
698
      if (newpnode == NULL) {
699
#ifndef SMART_UNMAP
700
	// Push this node back onto the unassigned map
701
	unassigned_nodes.push_front(vv);
702
	int start = RANDOM()%nnodes;
703
	int toremove = start;
704 705 706 707 708
	while (get(vvertex_pmap,virtual_nodes[toremove])->fixed ||
	       (! get(vvertex_pmap,virtual_nodes[toremove])->assigned)) {
	    toremove = (toremove +1) % nnodes;
	  if (toremove == start) {
	    toremove = -1;
709
            RDEBUG(cout << "Not removing a node" << endl;)
710 711 712 713
	    break;
	  }	
      }	
      if (toremove >= 0) {
714 715
	RDEBUG(cout << "removing: freeing up node " <<
                get(vvertex_pmap,virtual_nodes[toremove])->name << endl;)
716
	remove_node(virtual_nodes[toremove]);
717
	unassigned_nodes.push_front(virtual_nodes[toremove]);
718 719 720 721 722 723 724
      }	
      
      /*
       * Start again with another vnode - which will probably be the same one,
       * since we just marked it as unmapped. But now, there will be at least one
       * free pnode
       */
725
      RDEBUG(cout << "Failed to find a possible mapping; try again..." << endl;)
726 727 728 729 730 731
      continue;	
#else /* SMART_UNMAP */
      // XXX: This code is broken for now, which is okay, because we weren't
      // using it
      smart_unmap();
      smart_unmap_part2();
732
#endif
733 734 735 736 737 738
      }
    
      /*
       * Okay, we've got pnode to map this vnode to - let's do it
       */
      if (newpnode != NULL) {	
739
        RDEBUG(cout << "MOVE: " << vn->name << " to " << newpnode->name << " " << endl;)
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
        newpos = pnode2vertex[newpnode];
        if (scoring_selftest) {
	  // Run a little test here - see if the score we get by adding	
	  // this node, then removing it, is the same one we had before
	  double oldscore = get_score();
	  int oldviolated = violated;
	  double tempscore;
	  int tempviolated;
	  if (!add_node(vv,newpos,false,false,false)) {
	    tempscore = get_score();
	    tempviolated = violated;
	    remove_node(vv);
	  }	
	  if ((oldscore != get_score()) || (oldviolated != violated)) {
	    cerr << "Scoring problem adding a mapping - oldscore was " <<
Robert Ricci's avatar
Robert Ricci committed
755
		oldscore <<  " current score is " << get_score() << " tempscore was "
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
		<< tempscore << endl;
	    cerr << "oldviolated was " << oldviolated << " newviolated is "
		<< violated << " tempviolated was " << tempviolated << endl;
	    cerr << "I was tring to map " << vn->name << " to " <<
		newpnode->name << endl;
	    print_solution(best_solution);
	    cerr << vinfo;
	    abort();
          }
        }
      
        /*
         * Actually try the new mapping - if it fails, the node is still
         * unassigned, and we go back and try with another
         */
        if (add_node(vv,newpos,false,false,false) != 0) {
772
	  unassigned_nodes.push_front(vv);
773
          RDEBUG(cout << "failed" << endl;)
774 775 776
	  continue;
        }
      } else { // pnode != NULL
777
#ifdef SMART_UNMAP
778
        unassigned_nodes.push_front(vv);
779
#endif
780 781 782 783
        if (freednode) {
	  continue;
        }	
      }
784

785 786 787
      /*
       * Okay, now that we've mapped some new node, let's check the scoring
       */
788 789
      new_score = get_score();
      assert(new_score >= 0);
790 791

      // Negative means bad
792
      scorediff = prev_score - new_score;
793 794 795
      // This looks funny, because < 0 means worse, which means an increase in
      // score
      if (scorediff < 0) {
796 797 798
        nincreases++;
        avgincrease = avgincrease * (nincreases -1) / nincreases +
  		    (-scorediff)  / nincreases;
799
      } else {
800 801 802 803 804 805 806
        ndecreases++;
      }	
   
      /*
       * Here are all the various conditions for deciding if we're going to accept
       * this transition
       */
807
      bool accepttrans = false;
808
      if (melting) {
809 810 811
        // When melting, we take everything!
	accepttrans = true;
	RDEBUG(cout << "accept: melting" << endl;)
812
      } else {
813
#ifdef NO_VIOLATIONS
814 815
        // Here, we don't consider violations at all, just whether the regular
        // simulated annealing accept conditions
816
	if (new_score < prev_score) {
817
	    accepttrans = true;
818
	    RDEBUG(cout << "accept: better (" << new_score << "," << prev_score
819 820 821
		   << ")" << endl;)
        } else if (accept(scorediff,temp)) {
  	  accepttrans = true;
822 823
	  RDEBUG(cout << "accept: metropolis (" << new_score << ","
		 << prev_score << "," << expf(scorediff/(temp*sensitivity))
824 825
		 << ")" << endl;)
        }
826
#else
827
#ifdef SPECIAL_VIOLATION_TREATMENT
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
        /*
         * In this ifdef, we always accept new solutions that have fewer
         * violations than the old solution, and when we're trying to
         * determine whether or not to accept a new solution with a higher
         * score, we don't take violations into the account.
         *
         * The problem with this shows up at low temperatures. What can often
         * happen is that we accept a solution with worse violations but a
         * better (or similar) score. Then, if we were to try, say the first
         * solution (or a score-equivalent one) again, we'd accept it again.
         *
         * What this leads to is 'thrashing', where we have a whole lot of
         * variation of scores over time, but are not making any real
         * progress. This prevents the cooling schedule from converging for
         * much, much longer than it should really take.
         */
844 845 846 847
        RDEBUG(cout << "CRITERIA: v=" << violated << " bv=" <<
                prev_violated << " ns=" << new_score << " bs=" <<
                prev_score << " sd=" << scorediff << " t=" << temp << endl;)
        if ((violated == prev_violated) && (new_score < prev_score)) {
848
	  accepttrans = true;
849
	  RDEBUG(cout << "accept: better (" << new_score << "," << prev_score
850
		 << ")" << endl;)
851
	} else if (violated < prev_violated) {
852
	  accepttrans = true;
853 854
	  RDEBUG(cout << "accept: better (violations) (" << new_score << ","
		 << prev_score << "," << violated << "," << prev_violated
855 856 857 858 859
		 << ")" << endl;
	    cout << "Violations: (new) " << violated << endl;
	    cout << vinfo;)
        } else if (accept(scorediff,temp)) {
	  accepttrans = true;
860 861
	  RDEBUG(cout << "accept: metropolis (" << new_score << ","
		 << prev_score << "," << scorediff << "," << temp
862 863
		 << ")" << endl;)
        }
864
#else // no SPECIAL_VIOLATION_TREATMENT
865 866 867 868 869 870 871 872 873 874 875
        /*
         * In this branch of the ifdef, we give violations no special
         * treatment when it comes to accepting new solution - we just add
         * them into the score. This makes assign behave in a more 'classic'
         * simulated annealing manner.
         *
         * One consequence, though, is that we have to be more careful with
         * scores. We do not want to be able to get into a situation where
         * adding a violation results in a _lower_ score than a solution with
         * fewer violations.
         */
876 877
        double adjusted_new_score = new_score + violated * VIOLATION_SCORE;
        double adjusted_old_score = prev_score + prev_violated * VIOLATION_SCORE;
878 879 880 881 882 883

        if (adjusted_new_score < adjusted_old_score) {
          accepttrans = true;
        } else if (accept(adjusted_old_score - adjusted_new_score,temp)) {
	  accepttrans = true;
        }
884 885 886 887

#endif // SPECIAL_VIOLATION_TREATMENT

      }
888
#endif // NO_VIOLATIONS
889

890 891 892
      /* 
       * Okay, we've decided to accep this transition - do some bookkeeping
       */
893
      if (accepttrans) {
894 895 896
	// Accept change
	prev_score = new_score;
	prev_violated = violated;
897

898 899
#ifdef GNUPLOT_OUTPUT
	fprintf(tempout,"%f\n",temp);
900
	fprintf(scoresout,"%f\n",new_score);
901
	fprintf(deltaout,"%f\n",-scorediff);
902
#endif // GNUPLOT_OUTPUT
903

904
	avgscore += new_score;
905 906 907 908
	accepts++;

#ifdef CHILL
	 if (!melting) {
909
	     scores[accepts] = new_score;
910
	 }
911
#endif // CHILL
912

913 914 915 916
        /*
         * Okay, if this is the best score we've gotten so far, let's do some
	 * further bookkeeping - copy it into the structures for our best solution
	 */
917
#ifdef NO_VIOLATIONS
918
	if (new_score < best_score) {
919
#else // NO_VIOLATIONS
920 921 922
	if ((violated < best_violated) ||
	    ((violated == best_violated) &&
	     (new_score < best_score))) {
923 924
#endif // NO_VIOLATIONS
	    
925 926
#ifdef SCORE_DEBUG
	  cerr << "New best solution." << endl;
927
#endif // SCORE_DEBUG
928 929
	  tie(vit,veit) = vertices(VG);
	  for (;vit!=veit;++vit) {
930 931 932 933
	      tb_vnode *vnode = get(vvertex_pmap,*vit);
	      if (vnode->assigned) {
		  best_solution.set_assignment(*vit,vnode->assignment);
		  best_solution.set_vtype_assignment(*vit,vnode->type);
934 935 936
	      } else {
		  best_solution.clear_assignment(*vit);
	      }
937
	  }
938
	  
939 940 941 942
	  vedge_iterator edge_it, edge_it_end;
	  tie(edge_it, edge_it_end) = edges(VG);
	  for (;edge_it!=edge_it_end;++edge_it) {
	      tb_vlink *vlink = get(vedge_pmap, *edge_it);
943
	      if (vlink->link_info.type_used != tb_link_info::LINK_UNMAPPED) {
944
		  best_solution.set_link_assignment(*edge_it,vlink->link_info);
945
	      } else {
946
		  best_solution.clear_link_assignment(*edge_it);
947 948 949
	      }
	  }	
	  
950 951
	  best_score = new_score;
	  best_violated = violated;
952 953 954 955 956
	  iters_to_best = iters;
#ifdef SCORE_DEBUG
	  cerr << "New best recorded" << endl;
#endif
	}
957 958
      } else { // !acceptrans
	// Reject change, go back to the state we were in before
959 960 961
	RDEBUG(cout << "removing: rejected change" << endl;)
	remove_node(vv);
	if (oldassigned) {
962
	  add_node(vv,oldpos,false,false,false);
963 964 965
	} else {
          unassigned_nodes.push_front(vv);
        }
966 967
      }

968 969 970 971 972
      /*
       * If we're melting, we do a little extra bookkeeping to do, becuase the
       * goal of melting is to come up with an initial temperature such that
       * almost every transition will be accepted
       */
973 974 975 976 977 978 979
      if (melting) {
	temp = avgincrease /
	  log(nincreases/ (nincreases * X0 - ndecreases * (1 - X0)));
	if (!(temp > 0.0)) {
	    temp = 0.0;
	}
      }
980 981 982 983
      
      /*
       * With TIME_TERMINATE, we just give up after our time limit
       */
984 985 986 987 988 989 990 991 992
#ifdef TIME_TERMINATE
      if (timelimit && ((used_time() - timestart) > timelimit)) {
	printf("Reached end of run time, finishing\n");
	forcerevert = true;
	finished = true;
	goto NOTQUITEDONE;
      }
#endif

993 994
    } /* End of inner annealing loop */
     
995 996

NOTQUITEDONE:
997 998 999 1000 1001 1002 1003 1004 1005
    RDEBUG(printf("avgscore: %f = %f / %i\n",avgscore / (accepts +1),avgscore,accepts+1);)
	
    /*
     * Most of the code past this point concerns itself with the cooling
     * schedule (what the next temperature step should be
     */
	
    // Keep an average of the score over this temperature step	
    avgscore = avgscore / (accepts +1);
1006

1007 1008 1009
    /*
     * If we were melting, then we we need to pick an initial temperature
     */
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    if (melting) {
      melting = false;
      initialavg = avgscore;
      meltedtemp = temp;
      RDEBUG(cout << "Melting finished with a temperature of " << temp
	<< " avg score was " << initialavg << endl;)
      if (!(meltedtemp > 0.0)) { // This backwards expression to catch NaNs
	cout << "Finished annealing while melting!" << endl;
	finished = true;
	forcerevert = true;
      }
1021 1022 1023 1024 1025
      /*
       * With TIME_TARGET, we look at how long melting took, then use that to
       * estimate how many temperature steps it will take to hit our time
       * target. We adjust our cooling schedule accordingly.
       */
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
#ifdef TIME_TARGET
      if (timetarget) {
	double melttime = used_time() - meltstart;
	double timeleft = timetarget - melttime;
	double stepsleft = timeleft / melttime;
	cout << "Melting took " << melttime << " seconds, will try for "
	  << stepsleft << " temperature steps" << endl;
	temp_rate = pow(temp_stop/temp,1/stepsleft);
	cout << "Timelimit: " << timelimit << " Timeleft: " << timeleft
	  << " temp_rate: " << temp_rate << endl;
      }
#endif
    } else {
1039 1040 1041 1042 1043
      /*
       * The CHILL cooling schedule is the standard one from the Simulated
       * Annealing literature - it lower the temperature based on the standard
       * deviation of the scores of accepted configurations
       */
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
#ifdef CHILL
      if (!melting) {
	  stddev = 0;
	  for (int i = 0; i <= accepts; i++) {
	    stddev += pow(scores[i] - avgscore,2);
	  }
	  stddev /= (accepts +1);
	  stddev = sqrt(stddev);
	  temp = temp / (1 + (temp * log(1 + delta))/(3  * stddev));
      }
#else
1055 1056 1057 1058
      /* 
       * This is assign's original cooling schedule - more predictable, but not
       * at all reactive to the problem at hand
       */
1059 1060 1061 1062 1063
      temp *= temp_rate;
#endif
    }


1064 1065 1066
    /*
     * Debugging
     */
1067 1068