score.cc 18.3 KB
Newer Older
Mac Newbold's avatar
Mac Newbold committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * ASSUMPTIONS:
 *  1. Any switch can get to any other switch either directly
 *     or via at most one other switch (star formation).
 */

// Note on variable names: LEDA has generic 'edge' and 'node'.  When
// these are translated to 'tb_*' structures the variables end in
// r.  I.e. dst -> dstr.  dst is a node, and dstr is a tb_pnode or similar.

// Not sure we need all these LEDA includes.
#include <LEDA/graph_alg.h>
13
#include <LEDA/ugraph.h>
Mac Newbold's avatar
Mac Newbold committed
14 15 16 17
#include <LEDA/graphwin.h>
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
18
#include <LEDA/sortseq.h>
Mac Newbold's avatar
Mac Newbold committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

#include "common.h"
#include "score.h"
#include "virtual.h"
#include "physical.h"

#include "assert.h"

34 35 36
typedef node_array<edge> switch_pred_array;
extern node_array<switch_pred_array> switch_preds;

Mac Newbold's avatar
Mac Newbold committed
37 38 39 40 41 42 43 44 45 46
float score;			// The score of the current mapping
int violated;			// How many times the restrictions
				// have been violated.
node pnodes[MAX_PNODES];	// int->node map
				// pnodes[0] == NULL

violated_info vinfo;		// specific info on violations

extern tb_vgraph G;		// virtual graph
extern tb_pgraph PG;		// physical grpaph
47
extern tb_sgraph SG;		// switch fabric
Mac Newbold's avatar
Mac Newbold committed
48 49 50 51 52 53

edge direct_link(node a,node b);
void score_link(edge e,edge v,bool interswitch);
void unscore_link(edge e,edge v,bool interswitch);
edge find_link_to_switch(node n);
int find_intraswitch_path(node src,node dst,edge *first,edge *second);
54
int find_interswitch_path(node src,node dst,int bandwidth,list<edge> &L);
Mac Newbold's avatar
Mac Newbold committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

#ifdef SCORE_DEBUG_MORE
#define SADD(amount) fprintf(stderr,"SADD: %s = %.2f\n",#amount,amount);score+=amount
#define SSUB(amount) fprintf(stderr,"SSUB: %s = %.2f\n",#amount,amount);score-=amount
#else
#define SADD(amount) score += amount
#define SSUB(amount) score -= amount
#endif

/*
 * score()
 * Returns the score.
 */
float get_score() {return score;}

/*
 * init_score()
 * This initialized the scoring system.  It also clears all
 * assignments.
 */
void init_score()
{
#ifdef SCORE_DEBUG
  fprintf(stderr,"SCORE: Initializing\n");
#endif
  score=0;
  violated=0;
  vinfo.unassigned = vinfo.pnode_load = 0;
  vinfo.no_connection = vinfo.link_users = vinfo.bandwidth = 0;

  node n;
  edge e;
  forall_nodes(n,G) {
    tb_vnode &vn=G[n];
    vn.posistion=0;
    vn.no_connections=0;
    SADD(SCORE_UNASSIGNED);
    vinfo.unassigned++;
    violated++;
  }
  forall_edges(e,G) {
    tb_vlink &ve=G[e];
    ve.type=tb_vlink::LINK_UNKNOWN;
  }
  forall_nodes(n,PG) {
    tb_pnode &pn=PG[n];
    pn.typed=false;
    pn.current_load=0;
    pn.pnodes_used=0;
  }
  forall_edges(e,PG) {
    tb_plink &pe=PG[e];
    pe.bw_used=0;
    pe.users=0;
  }

  assert(pnodes[0] == NULL);
#ifdef SCORE_DEBUG
  fprintf(stderr,"  score = %.2f violated = %d\n",score,violated);
#endif
}

/*
 * void remove_node(node node)
 * This removes a virtual node from the assignments, adjusting
 * the score appropriately.
 */
void remove_node(node n)
{
  /* Find pnode assigned to */
  node pnode;

  tb_vnode &vnoder = G[n];
  pnode = pnodes[vnoder.posistion];
  tb_pnode &pnoder = PG[pnode];

#ifdef SCORE_DEBUG
132
  cerr <<  "SCORE: remove_node(" << vnoder.name << ")\n";
Mac Newbold's avatar
Mac Newbold committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  fprintf(stderr,"       no_connections = %d\n",vnoder.no_connections);
#endif

  
  assert(pnode != NULL);

  edge e;
  tb_vlink *vlink;
  node vdst;
  tb_vnode *vdstr;
  node pdst;
  tb_pnode *pdstr;

  // remove the scores associated with each edge
  forall_inout_edges(e,n) {
    vlink=&G[e];
    vdst=G.target(e);
    if (vdst == n)
      vdst = G.source(e);
    vdstr=&G[vdst];
#ifdef SCORE_DEBUG
154
    cerr << "  edge to " << vdstr->name << endl;
Mac Newbold's avatar
Mac Newbold committed
155 156 157 158 159 160 161 162 163 164 165 166 167
#endif
    if (vdstr->posistion == 0) continue;
    if (vlink->type == tb_vlink::LINK_DIRECT) {
      // DIRECT LINK
#ifdef SCORE_DEBUG
      fprintf(stderr,"   direct link\n");
#endif
      unscore_link(vlink->plink,e,false);
    } else if (vlink->type == tb_vlink::LINK_INTERSWITCH) {
      // INTERSWITCH LINK
      pdst=pnodes[vdstr->posistion];
      pdstr=&PG[pdst];
#ifdef SCORE_DEBUG
168
      cerr << "  interswitch link\n";
Mac Newbold's avatar
Mac Newbold committed
169 170
#endif

171 172 173 174 175 176 177
      edge cur;      
      forall(cur,G[e].path) {
	SSUB(SCORE_INTERSWITCH_LINK);
	unscore_link(cur,e,true);
      }
      G[e].path.clear();
      
Mac Newbold's avatar
Mac Newbold committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      unscore_link(vlink->plink_local_one,e,false);
      unscore_link(vlink->plink_local_two,e,false);
    } else if (vlink->type == tb_vlink::LINK_INTRASWITCH) {
      // INTRASWITCH LINK
#ifdef SCORE_DEBUG
      fprintf(stderr,"   intraswitch link\n");
#endif
      SSUB(SCORE_INTRASWITCH_LINK);

      unscore_link(vlink->plink,e,false);
      unscore_link(vlink->plink_two,e,false); 
    } else {
      // No link
      fprintf(stderr,"Internal error - no link\n");
      abort();
    }
  }

  // remove scores associated with the node
  SSUB(SCORE_NO_CONNECTION*vnoder.no_connections);
  violated -= vnoder.no_connections;
  vinfo.no_connection -= vnoder.no_connections;

  // adjust pnode scores
  pnoder.current_load--;
  vnoder.posistion = 0;
  if (pnoder.current_load == 0) {
    // release pnode
#ifdef SCORE_DEBUG
    fprintf(stderr,"  releasing pnode\n");
#endif
    SSUB(SCORE_PNODE);
    node the_switch=pnoder.the_switch;
    if (the_switch) {
      if ((--PG[the_switch].pnodes_used) == 0) {
#ifdef SCORE_DEBUG
214
	cerr << "  releasing switch " << PG[the_switch].name << endl;
Mac Newbold's avatar
Mac Newbold committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#endif
	// release switch
	SSUB(SCORE_SWITCH);
      }
    }
    // revert pnode type
    pnoder.typed=false;
  } else if (pnoder.current_load >= pnoder.max_load) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  reducing penalty, new load = %d (>= %d)\n",pnoder.current_load,pnoder.max_load);
#endif
    SSUB(SCORE_PNODE_PENALTY);
    vinfo.pnode_load--;
    violated--;
  }
  // add score for unassigned node
  SADD(SCORE_UNASSIGNED);
  vinfo.unassigned++;
  violated++;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

  // features/desires
  seq_item desire;
  seq_item feature;
  double value;
  for (desire = vnoder.desires.min_item();desire;
       desire = vnoder.desires.succ(desire)) {
    feature = pnoder.features.lookup(vnoder.desires.key(desire));
#ifdef SCORE_DEBUG
    cerr << "  desire = " << vnoder.desires.key(desire) \
	 << " " << vnoder.desires.inf(desire) << "\n";
#endif
    if (!feature) {
      // Unmatched desire.  Remove cost.
#ifdef SCORE_DEBUG
      cerr << "    unmatched\n";
#endif
      value = vnoder.desires.inf(desire);
      SSUB(SCORE_DESIRE*value);
      if (value >= 1) {
	violated--;
	vinfo.desires--;
      }
    }
  }
  for (feature = pnoder.features.min_item();feature;
       feature = pnoder.features.succ(feature)) {
    desire = vnoder.desires.lookup(pnoder.features.key(feature));
#ifdef SCORE_DEBUG
    cerr << "  feature = " << pnoder.features.key(feature) \
	 << " " << pnoder.features.inf(feature) << "\n";
#endif
    if (! desire) {
      // Unused feature.  Remove weight
#ifdef SCORE_DEBUG
      cerr << "    unused\n";
#endif
      value = pnoder.features.inf(feature);
      SSUB(SCORE_FEATURE*value);
    }
  }
  
Mac Newbold's avatar
Mac Newbold committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#ifdef SCORE_DEBUG
  fprintf(stderr,"  new score = %.2f  new violated = %d\n",score,violated);
#endif
}

/*
 * int add_node(node node,int ploc)
 * Add a mapping of node to ploc and adjust score appropriately.
 * Returns 1 in the case of an incompatible mapping.  This should
 * never happen as the same checks should be in place in a higher
 * level.  (Optimization?)
 */
int add_node(node n,int ploc)
{
  tb_vnode &vnoder=G[n];
  node pnode = pnodes[ploc];
  tb_pnode &pnoder=PG[pnode];

#ifdef SCORE_DEBUG
295
  cerr << "SCORE: add_node(" << vnoder.name << "," << pnoder.name << "[" << ploc << "])\n";
Mac Newbold's avatar
Mac Newbold committed
296 297
  fprintf(stderr,"  vnode type = ");
  cerr << vnoder.type << " pnode switch = ";
298 299 300 301 302
  if (pnoder.the_switch) {
    cerr << PG[pnoder.the_switch].name;
  } else {
    cerr << "No switch";
  }
Mac Newbold's avatar
Mac Newbold committed
303 304 305 306 307 308 309 310
  cerr << endl;
#endif
  
  // set up pnode
  // figure out type
  if (!pnoder.typed) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  virgin pnode\n");
311
    cerr << "    vtype = " << vnoder.type << "\n";
Mac Newbold's avatar
Mac Newbold committed
312 313 314 315 316
#endif
    
    // Remove check assuming at higher level?
    // Remove higher level checks?
    pnoder.max_load=0;
317 318
    if (pnoder.types.lookup(vnoder.type) != nil)
      pnoder.max_load = pnoder.types.access(vnoder.type);
Mac Newbold's avatar
Mac Newbold committed
319 320 321 322 323 324 325 326
    
    if (pnoder.max_load == 0) {
      // didn't find a type
#ifdef SCORE_DEBUG
      fprintf(stderr,"  no matching type\n");
#endif
      return 1;
    }
327 328 329 330

    pnoder.current_type=vnoder.type;
    pnoder.typed=true;

Mac Newbold's avatar
Mac Newbold committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
#ifdef SCORE_DEBUG
    fprintf(stderr,"  matching type found (");
    cerr << pnoder.current_type << ", max = " << pnoder.max_load;
    cerr << ")" << endl;
#endif
  } else {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  pnode already has type\n");
#endif
    if (pnoder.current_type != vnoder.type) {
#ifdef SCORE_DEBUG      
      fprintf(stderr,"  incompatible types\n");
#endif
      return 1;
    } else {
#ifdef SCORE_DEBUG
      fprintf(stderr,"  comaptible types\n");
#endif
      if (pnoder.current_load == pnoder.max_load) {
350 351 352
	/* XXX - We could ignore this check and let the code
	   at the end of the routine penalize for going over
	   load.  Failing here seems to work better though. */
Mac Newbold's avatar
Mac Newbold committed
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
#ifdef SCORE_DEBUG
	fprintf(stderr,"  node is full\n");
#endif
	return 1;
      }
      
    }
  }

  // set up links
  vnoder.no_connections=0;
  edge e;
  node dst;
  tb_vlink *er;
  tb_plink *pl;
  edge pedge;
  forall_inout_edges(e,n) {
    dst=G.source(e);
    er=&G[e];
    if (dst == n) {
      dst=G.target(e);
    }
    tb_vnode &dstr=G[dst];
    assert(dst != n);
    assert(&dstr != &vnoder);
#ifdef SCORE_DEBUG
379
    cerr << "  edge to " << dstr.name << endl;
Mac Newbold's avatar
Mac Newbold committed
380 381 382 383 384 385 386 387
#endif

    if (dstr.posistion != 0) {
      // dstr is assigned
      node dpnode=pnodes[dstr.posistion];
      tb_pnode &dpnoder=PG[dpnode];

#ifdef SCORE_DEBUG
388
      cerr << "   goes to " << dpnoder.name << endl;
Mac Newbold's avatar
Mac Newbold committed
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
#endif

      if ((pedge=direct_link(dpnode,pnode)) != NULL) {
#ifdef SCORE_DEBUG
	fprintf(stderr,"   found direct link = %p\n",pedge);
#endif
	pl = &PG[pedge];

	// direct
	er->type = tb_vlink::LINK_DIRECT;
	er->plink = pedge;

	score_link(pedge,e,false);
      } else if (pnoder.the_switch &&
		 (pnoder.the_switch == dpnoder.the_switch)) {
	// intraswitch
	assert(pnoder.the_switch == dpnoder.the_switch);
	edge first,second;
	if (find_intraswitch_path(pnode,dpnode,&first,&second) == 1) {
	  fprintf(stderr,"Internal error: Could not find intraswitch link!\n");
	  abort();
	}

	assert(first != NULL);
	assert(second != NULL);
	
#ifdef SCORE_DEBUG
	fprintf(stderr,"   found intraswitch link (%p,%p)\n",first,second);
#endif
	er->type = tb_vlink::LINK_INTRASWITCH;
	er->plink = first;
	er->plink_two = second;
	SADD(SCORE_INTRASWITCH_LINK);

	// check users and bandwidth
	score_link(first,e,false);
	score_link(second,e,false);
      } else {
	// try to find interswitch
#ifdef SCORE_DEBUG
429 430 431
	cerr << "   looking for interswitch link " <<
	  PG[pnoder.the_switch].name << " " <<
	  PG[dpnoder.the_switch].name << endl;
Mac Newbold's avatar
Mac Newbold committed
432 433
#endif
	if (find_interswitch_path(pnoder.the_switch,dpnoder.the_switch,
434
				  er->bandwidth,er->path) == 0) {
Mac Newbold's avatar
Mac Newbold committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448
#ifdef SCORE_DEBUG
	  fprintf(stderr,"   could not find path - no connection\n");
#endif
	  // XXX - this is a hack for the moment
	  pnoder.typed = false;
	  return 1;
	  
	  // couldn't find path.
	  vnoder.no_connections++;
	  SADD(SCORE_NO_CONNECTION);
	  vinfo.no_connection++;
	  violated++;
 	} else {
#ifdef SCORE_DEBUG
449
	  fprintf(stderr,"   found interswitch link\n");
Mac Newbold's avatar
Mac Newbold committed
450 451 452
#endif
	  er->type=tb_vlink::LINK_INTERSWITCH;

453 454 455 456 457 458 459 460
	  edge cur;
	  forall(cur,er->path) {
	    SADD(SCORE_INTERSWITCH_LINK);
#ifdef SCORE_DEBUG
	    cerr << "     " << PG[cur].name << endl;
#endif
	    score_link(cur,e,true);
	  }
Mac Newbold's avatar
Mac Newbold committed
461 462 463 464 465 466 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 497 498 499 500 501 502 503 504 505 506 507

	  er->plink_local_one = find_link_to_switch(pnode);
	  assert(er->plink_local_one != NULL);
	  er->plink_local_two = find_link_to_switch(dpnode);
	  assert(er->plink_local_two != NULL);

	  score_link(er->plink_local_one,e,false);
	  score_link(er->plink_local_two,e,false);
	}
      }
    }
  }
    
  // finish setting up pnode
  pnoder.current_load++;
  vnoder.posistion = ploc;
  if (pnoder.current_load > pnoder.max_load) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  load to high - penalty (%d)\n",pnoder.current_load);
#endif
    SADD(SCORE_PNODE_PENALTY);
    vinfo.pnode_load++;
    violated++;
  } else {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  load is fine\n");
#endif
  }
  if (pnoder.current_load == 1) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  new pnode\n");
#endif
    SADD(SCORE_PNODE);
    if (pnoder.the_switch &&
	(++PG[pnoder.the_switch].pnodes_used) == 1) {
#ifdef SCORE_DEBUG
      fprintf(stderr,"  new switch\n");
#endif
      SADD(SCORE_SWITCH);
    }
  }

  // node no longer unassigned
  SSUB(SCORE_UNASSIGNED);
  vinfo.unassigned--;
  violated--;

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
  // features/desires
  seq_item desire;
  seq_item feature;
  double value;
  for (desire = vnoder.desires.min_item();desire;
       desire = vnoder.desires.succ(desire)) {
    feature = pnoder.features.lookup(vnoder.desires.key(desire));
#ifdef SCORE_DEBUG
    cerr << "  desire = " << vnoder.desires.key(desire) \
	 << " " << vnoder.desires.inf(desire) << "\n";
#endif
    if (!feature) {
      // Unmatched desire.  Add cost.
#ifdef SCORE_DEBUG
      cerr << "    unmatched\n";
#endif
      value = vnoder.desires.inf(desire);
      SADD(SCORE_DESIRE*value);
      if (value >= 1) {
	violated++;
	vinfo.desires++;
      }
    }
  }
  for (feature = pnoder.features.min_item();feature;
       feature = pnoder.features.succ(feature)) {
    desire = vnoder.desires.lookup(pnoder.features.key(feature));
#ifdef SCORE_DEBUG
    cerr << "  feature = " << pnoder.features.key(feature) \
	 << " " << pnoder.features.inf(feature) << "\n";
#endif
    if (! desire) {
      // Unused feature.  Add weight
#ifdef SCORE_DEBUG
      cerr << "    unused\n";
#endif
      value = pnoder.features.inf(feature);
      SADD(SCORE_FEATURE*value);
    }
  }
    
Mac Newbold's avatar
Mac Newbold committed
549 550 551 552 553 554 555 556 557 558 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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
#ifdef SCORE_DEBUG
  fprintf(stderr,"  posistion = %d\n",vnoder.posistion);
  fprintf(stderr,"  new score = %.2f  new violated = %d\n",score,violated);
#endif
  return 0;
}

// returns "best" direct link between a and b.
// best = less users
//        break ties with minimum bw_used
edge direct_link(node a,node b)
{
  node dst;
  edge e;
  edge best = NULL;
  tb_plink *pl;
  tb_plink *bestpl = NULL;
  forall_inout_edges(e,a) {
    dst=PG.target(e);
    if (dst == a)
      dst=PG.source(e);
    if (dst == b) {
      pl = &PG[e];
      if (! bestpl ||
	  (pl->users < bestpl->users ||
	   (pl->users == bestpl->users &&
	    pl->bw_used < bestpl->bw_used))) {
	best = e;
	bestpl = pl;
      }
    }
  }
  return best;
}

edge find_link_to_switch(node n)
{
  tb_pnode &nr = PG[n];

  edge e;
  node edst;
  float best_bw=1000.0;
  int best_users = 1000;
  float bw;
  edge best=NULL;
  forall_inout_edges(e,n) {
    edst = PG.target(e);
    if (edst == n)
      edst = PG.source(e);
    if (edst == nr.the_switch) {
      tb_plink &er = PG[e];
      bw = er.bw_used / er.bandwidth;
      if ((er.users < best_users) ||
	  ((er.users == best_users) && (bw < best_bw))) {
	best = e;
	best_bw = bw;
	best_users = er.users;
      }
    }
  }
  
  return best;
}

// this looks for the best links between the src and the switch
// and the dst and the switch.
int find_intraswitch_path(node src,node dst,edge *first,edge *second)
{
  tb_pnode &srcr=PG[src];
  tb_pnode &dstr=PG[dst];

  assert(srcr.the_switch == dstr.the_switch);
  assert(srcr.the_switch != NULL);
  assert(src != dst);

  *first = find_link_to_switch(src);
  *second = find_link_to_switch(dst);

  if ((*first != NULL) &&
      (*second != NULL)) return 0;
  return 1;
}

632 633 634 635 636
// this uses the shortest paths calculated over the switch graph to
// find a path between src and dst.  It passes out list<edge>, a list
// of the edges used. (assumed to be empty to begin with).
// Returns 0 if no path exists and 1 otherwise.
int find_interswitch_path(node src,node dst,int bandwidth,list<edge> &L)
Mac Newbold's avatar
Mac Newbold committed
637
{
638 639 640 641 642 643 644
  // We know the shortest path from src to node already.  It's stored
  // in switch_preds[src] and is a node_array<edge>.  Let P be this
  // array.  We can trace our shortest path by starting at the end and
  // following the pred edges back until we reach src.  We need to be
  // careful though because the switch_preds deals with elements of SG
  // and we have elements of PG.
  if ((src == nil) || (dst == nil)) {return 0;}
Mac Newbold's avatar
Mac Newbold committed
645
  
646 647 648 649 650 651 652
  node sg_src = PG[src].sgraph_switch;
  node sg_dst = PG[dst].sgraph_switch;
  edge sg_ed;
  node sg_cur=sg_dst;
  switch_pred_array &preds = switch_preds[sg_src];
  if (preds[sg_dst] == nil) {
    // unreachable
Mac Newbold's avatar
Mac Newbold committed
653 654
    return 0;
  }
655 656 657 658 659 660 661 662 663 664
  while (sg_cur != sg_src) {
    sg_ed = preds[sg_cur];
    L.push(SG[sg_ed].mate);
    if (SG.source(sg_ed) == sg_cur) {
      sg_cur = SG.target(sg_ed);
    } else {
      sg_cur = SG.source(sg_ed);
    }
  }
  return 1;
Mac Newbold's avatar
Mac Newbold committed
665 666 667 668 669 670 671 672 673 674 675 676 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 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
}

// this does scoring for over users and over bandwidth on edges.
void score_link(edge e,edge v,bool interswitch)
{
  tb_plink &pl = PG[e];
  tb_vlink &er = G[v];

#ifdef SCORE_DEBUG
  fprintf(stderr,"  score_link(%p)\n",e);
#endif

  if (! interswitch) {
    pl.users++;
    if (pl.users == 1) {
#ifdef SCORE_DEBUG
      fprintf(stderr,"    first user\n");
#endif
      SADD(SCORE_DIRECT_LINK);
    } else {
#ifdef SCORE_DEBUG
      fprintf(stderr,"    not first user - penalty\n");
#endif
      SADD(SCORE_DIRECT_LINK_PENALTY);
      vinfo.link_users++;
      violated++;
    }
  }
  
  // bandwidth
  int prev_bw = pl.bw_used;
  pl.bw_used += er.bandwidth;
  if ((pl.bw_used > pl.bandwidth) &&
      (prev_bw <= pl.bandwidth)) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"    went over bandwidth (%d > %d)\n",
	    pl.bw_used,pl.bandwidth);
#endif
    violated++;
    vinfo.bandwidth++;
    SADD(SCORE_OVER_BANDWIDTH);
  }
}

void unscore_link(edge e,edge v,bool interswitch)
{
  tb_plink &pl = PG[e];
  tb_vlink &er = G[v];

#ifdef SCORE_DEBUG
  fprintf(stderr,"  unscore_link(%p)\n",e);
#endif

  if (!interswitch) {
    pl.users--;
    if (pl.users == 0) {
      // link no longer used
#ifdef SCORE_DEBUG
      fprintf(stderr,"   freeing link\n");
#endif
      SSUB(SCORE_DIRECT_LINK);
    } else {
      // getting close to no violations
#ifdef SCORE_DEBUG
      fprintf(stderr,"   reducing users\n");
#endif
      SSUB(SCORE_DIRECT_LINK_PENALTY);
      vinfo.link_users--;
      violated--;
    }
  }
  
  // bandwidth check
  int prev_bw = pl.bw_used;
  pl.bw_used -= er.bandwidth;
  if ((pl.bw_used <= pl.bandwidth) &&
      (prev_bw > pl.bandwidth)) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"   went under bandwidth (%d <= %d)\n",
	    pl.bw_used,pl.bandwidth);
#endif
    violated--;
    vinfo.bandwidth--;
    SSUB(SCORE_OVER_BANDWIDTH);
  }
}