Ticket #415: network_simplex.patch
File network_simplex.patch, 6.0 KB (added by , 14 years ago) |
---|
-
.h
old new 243 243 244 244 private: 245 245 246 // Handle multiplication of costs with _state and _pred_dir 247 // elements which are in {-1, 0, 1} and {-1, 1} respectively. This 248 // means the Cost type '*' operator is never used and need not 249 // exist. 250 static inline Cost unitMul(signed char state, Cost cost) { 251 // fastest for integral costs on x86: 252 // return cost * (int)state; 253 254 // fastest for integral costs on machines with predicated execution 255 // and/or high latency integer multiplies (e.g. arm, ia64): 256 if(state == 1) 257 return cost; 258 else if(state == -1) 259 return -cost; 260 else 261 return 0; 262 } 263 246 264 // Implementation of the First Eligible pivot rule 247 265 class FirstEligiblePivotRule 248 266 { … … 274 292 bool findEnteringArc() { 275 293 Cost c; 276 294 for (int e = _next_arc; e != _search_arc_num; ++e) { 277 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);295 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 278 296 if (c < 0) { 279 297 _in_arc = e; 280 298 _next_arc = e + 1; … … 282 300 } 283 301 } 284 302 for (int e = 0; e != _next_arc; ++e) { 285 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);303 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 286 304 if (c < 0) { 287 305 _in_arc = e; 288 306 _next_arc = e + 1; … … 322 340 bool findEnteringArc() { 323 341 Cost c, min = 0; 324 342 for (int e = 0; e != _search_arc_num; ++e) { 325 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);343 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 326 344 if (c < min) { 327 345 min = c; 328 346 _in_arc = e; … … 376 394 int cnt = _block_size; 377 395 int e; 378 396 for (e = _next_arc; e != _search_arc_num; ++e) { 379 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);397 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 380 398 if (c < min) { 381 399 min = c; 382 400 _in_arc = e; … … 387 405 } 388 406 } 389 407 for (e = 0; e != _next_arc; ++e) { 390 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);408 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 391 409 if (c < min) { 392 410 min = c; 393 411 _in_arc = e; … … 462 480 min = 0; 463 481 for (int i = 0; i < _curr_length; ++i) { 464 482 e = _candidates[i]; 465 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);483 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 466 484 if (c < min) { 467 485 min = c; 468 486 _in_arc = e; … … 478 496 min = 0; 479 497 _curr_length = 0; 480 498 for (e = _next_arc; e != _search_arc_num; ++e) { 481 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);499 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 482 500 if (c < 0) { 483 501 _candidates[_curr_length++] = e; 484 502 if (c < min) { … … 489 507 } 490 508 } 491 509 for (e = 0; e != _next_arc; ++e) { 492 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);510 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 493 511 if (c < 0) { 494 512 _candidates[_curr_length++] = e; 495 513 if (c < min) { … … 575 593 Cost c; 576 594 for (int i = 0; i != _curr_length; ++i) { 577 595 e = _candidates[i]; 578 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);596 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 579 597 if (c < 0) { 580 598 _cand_cost[e] = c; 581 599 } else { … … 588 606 int limit = _head_length; 589 607 590 608 for (e = _next_arc; e != _search_arc_num; ++e) { 591 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);609 c = unitMul(_state[e], (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 592 610 if (c < 0) { 593 611 _cand_cost[e] = c; 594 612 _candidates[_curr_length++] = e; … … 600 618 } 601 619 } 602 620 for (e = 0; e != _next_arc; ++e) { 603 _cand_cost[e] = _state[e] *604 (_cost[e] + _pi[_source[e]] - _pi[_target[e]]) ;621 _cand_cost[e] = unitMul(_state[e], 622 (_cost[e] + _pi[_source[e]] - _pi[_target[e]])); 605 623 if (_cand_cost[e] < 0) { 606 624 _candidates[_curr_length++] = e; 607 625 } … … 986 1004 Number c = 0; 987 1005 for (ArcIt a(_graph); a != INVALID; ++a) { 988 1006 int i = _arc_id[a]; 989 c += Number(_flow[i]) * Number(_cost[i]);1007 c += _cost[i] * _flow[i]; 990 1008 } 991 1009 return c; 992 1010 } … … 1449 1467 // Update potentials in the subtree that has been moved 1450 1468 void updatePotential() { 1451 1469 Cost sigma = _pi[v_in] - _pi[u_in] - 1452 _pred_dir[u_in] * _cost[in_arc];1470 unitMul(_pred_dir[u_in], _cost[in_arc]); 1453 1471 int end = _thread[_last_succ[u_in]]; 1454 1472 for (int u = u_in; u != end; u = _thread[u]) { 1455 1473 _pi[u] += sigma; … … 1536 1554 // Perform heuristic initial pivots 1537 1555 for (int i = 0; i != int(arc_vector.size()); ++i) { 1538 1556 in_arc = arc_vector[i]; 1539 if ( _state[in_arc] *(_cost[in_arc] + _pi[_source[in_arc]] -1540 _pi[_target[in_arc]]) >= 0) continue;1557 if (unitMul(_state[in_arc], (_cost[in_arc] + _pi[_source[in_arc]] - 1558 _pi[_target[in_arc]])) >= 0) continue; 1541 1559 findJoinNode(); 1542 1560 bool change = findLeavingArc(); 1543 1561 if (delta >= MAX) return false;