| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#pragma once |
| 34 |
#undef DEBUG_LVL |
| 35 |
#define DEBUG_LVL 0 |
| 36 |
|
| 37 |
#if DEBUG_LVL>0 |
| 38 |
#include <iomanip> |
| 39 |
#endif |
| 40 |
|
| 41 |
#undef EPSILON |
| 42 |
#undef _EPSILON |
| 43 |
#undef MAX_DEBUG_ITER |
| 44 |
#define EPSILON std::numeric_limits<Cost>::epsilon() |
| 45 |
#define _EPSILON 1e-14 |
| 46 |
#define MAX_DEBUG_ITER 100000 |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#define HASHMAP |
| 55 |
|
| 56 |
#define SPARSE_FLOW |
| 57 |
|
| 58 |
#include <vector> |
| 59 |
#include <limits> |
| 60 |
#include <algorithm> |
| 61 |
#include <iostream> |
| 62 |
#ifdef HASHMAP |
| 63 |
#include <unordered_map> |
| 64 |
#else |
| 65 |
#include <map> |
| 66 |
#endif |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#ifdef _OPENMP |
| 71 |
#include <omp.h> |
| 72 |
#endif |
| 73 |
#include <cmath> |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#include "full_bipartitegraph_omp.h" |
| 78 |
|
| 79 |
#undef INVALIDNODE |
| 80 |
#undef INVALID |
| 81 |
#define INVALIDNODE -1 |
| 82 |
#define INVALID (-1) |
| 83 |
|
| 84 |
namespace lemon_omp { |
| 85 |
|
| 86 |
int64_t max_threads = -1; |
| 87 |
|
| 88 |
template <typename T> |
| 89 |
class ProxyObject; |
| 90 |
|
| 91 |
template<typename T> |
| 92 |
class SparseValueVector |
| 93 |
{ |
| 94 |
public: |
| 95 |
SparseValueVector(size_t n = 0) |
| 96 |
{ |
| 97 |
} |
| 98 |
void resize(size_t n = 0) {}; |
| 99 |
T operator[](const size_t id) const |
| 100 |
{ |
| 101 |
#ifdef HASHMAP |
| 102 |
typename std::unordered_map<size_t, T>::const_iterator it = data.find(id); |
| 103 |
#else |
| 104 |
typename std::map<size_t, T>::const_iterator it = data.find(id); |
| 105 |
#endif |
| 106 |
if (it == data.end()) |
| 107 |
return 0; |
| 108 |
else |
| 109 |
return it->second; |
| 110 |
} |
| 111 |
|
| 112 |
ProxyObject<T> operator[](const size_t id) |
| 113 |
{ |
| 114 |
return ProxyObject<T>(this, id); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
#ifdef HASHMAP |
| 119 |
std::unordered_map<size_t, T> data; |
| 120 |
#else |
| 121 |
std::map<size_t, T> data; |
| 122 |
#endif |
| 123 |
|
| 124 |
}; |
| 125 |
|
| 126 |
template <typename T> |
| 127 |
class ProxyObject { |
| 128 |
public: |
| 129 |
ProxyObject(SparseValueVector<T> *v, size_t idx) { _v = v; _idx = idx; }; |
| 130 |
ProxyObject<T> & operator=(const T &v) { |
| 131 |
|
| 132 |
|
| 133 |
if (v != 0) |
| 134 |
_v->data[_idx] = v; |
| 135 |
return *this; |
| 136 |
} |
| 137 |
|
| 138 |
operator T() { |
| 139 |
|
| 140 |
|
| 141 |
#ifdef HASHMAP |
| 142 |
typename std::unordered_map<size_t, T>::iterator it = _v->data.find(_idx); |
| 143 |
#else |
| 144 |
typename std::map<size_t, T>::iterator it = _v->data.find(_idx); |
| 145 |
#endif |
| 146 |
if (it == _v->data.end()) |
| 147 |
return 0; |
| 148 |
else |
| 149 |
return it->second; |
| 150 |
} |
| 151 |
|
| 152 |
void operator+=(T val) |
| 153 |
{ |
| 154 |
if (val == 0) return; |
| 155 |
#ifdef HASHMAP |
| 156 |
typename std::unordered_map<size_t, T>::iterator it = _v->data.find(_idx); |
| 157 |
#else |
| 158 |
typename std::map<size_t, T>::iterator it = _v->data.find(_idx); |
| 159 |
#endif |
| 160 |
if (it == _v->data.end()) |
| 161 |
_v->data[_idx] = val; |
| 162 |
else |
| 163 |
{ |
| 164 |
T sum = it->second + val; |
| 165 |
if (sum == 0) |
| 166 |
_v->data.erase(it); |
| 167 |
else |
| 168 |
it->second = sum; |
| 169 |
} |
| 170 |
} |
| 171 |
void operator-=(T val) |
| 172 |
{ |
| 173 |
if (val == 0) return; |
| 174 |
#ifdef HASHMAP |
| 175 |
typename std::unordered_map<size_t, T>::iterator it = _v->data.find(_idx); |
| 176 |
#else |
| 177 |
typename std::map<size_t, T>::iterator it = _v->data.find(_idx); |
| 178 |
#endif |
| 179 |
if (it == _v->data.end()) |
| 180 |
_v->data[_idx] = -val; |
| 181 |
else |
| 182 |
{ |
| 183 |
T sum = it->second - val; |
| 184 |
if (sum == 0) |
| 185 |
_v->data.erase(it); |
| 186 |
else |
| 187 |
it->second = sum; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
SparseValueVector<T> *_v; |
| 192 |
size_t _idx; |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
template <typename GR, typename V = int, typename C = V, typename ArcsType = int64_t> |
| 234 |
class NetworkSimplexSimple |
| 235 |
{ |
| 236 |
public: |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
NetworkSimplexSimple(const GR& graph, bool arc_mixing, int nbnodes, ArcsType nb_arcs, uint64_t maxiters = 0, int numThreads=-1) : |
| 248 |
_graph(graph), |
| 249 |
_arc_mixing(arc_mixing), _init_nb_nodes(nbnodes), _init_nb_arcs(nb_arcs), |
| 250 |
MAX(std::numeric_limits<Value>::max()), |
| 251 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 252 |
std::numeric_limits<Value>::infinity() : MAX) |
| 253 |
{ |
| 254 |
|
| 255 |
reset(); |
| 256 |
max_iter = maxiters; |
| 257 |
#ifdef _OPENMP |
| 258 |
if (max_threads < 0) { |
| 259 |
max_threads = omp_get_max_threads(); |
| 260 |
} |
| 261 |
if (numThreads > 0 && numThreads<=max_threads){ |
| 262 |
num_threads = numThreads; |
| 263 |
} else if (numThreads == -1 || numThreads>max_threads) { |
| 264 |
num_threads = max_threads; |
| 265 |
} else { |
| 266 |
num_threads = 1; |
| 267 |
} |
| 268 |
omp_set_num_threads(num_threads); |
| 269 |
#else |
| 270 |
num_threads = 1; |
| 271 |
#endif |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
typedef V Value; |
| 276 |
|
| 277 |
typedef C Cost; |
| 278 |
|
| 279 |
public: |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
enum ProblemType { |
| 285 |
|
| 286 |
INFEASIBLE, |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
OPTIMAL, |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
UNBOUNDED, |
| 295 |
|
| 296 |
MAX_ITER_REACHED |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
enum SupplyType { |
| 309 |
|
| 310 |
|
| 311 |
GEQ, |
| 312 |
|
| 313 |
|
| 314 |
LEQ |
| 315 |
}; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private: |
| 320 |
uint64_t max_iter; |
| 321 |
int num_threads; |
| 322 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 323 |
|
| 324 |
typedef std::vector<int> IntVector; |
| 325 |
typedef std::vector<ArcsType> ArcVector; |
| 326 |
typedef std::vector<Value> ValueVector; |
| 327 |
typedef std::vector<Cost> CostVector; |
| 328 |
|
| 329 |
typedef std::vector<char> BoolVector; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
enum ArcState { |
| 334 |
STATE_UPPER = -1, |
| 335 |
STATE_TREE = 0, |
| 336 |
STATE_LOWER = 1 |
| 337 |
}; |
| 338 |
|
| 339 |
typedef std::vector<signed char> StateVector; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
private: |
| 344 |
|
| 345 |
|
| 346 |
const GR &_graph; |
| 347 |
int _node_num; |
| 348 |
ArcsType _arc_num; |
| 349 |
ArcsType _all_arc_num; |
| 350 |
ArcsType _search_arc_num; |
| 351 |
|
| 352 |
|
| 353 |
SupplyType _stype; |
| 354 |
Value _sum_supply; |
| 355 |
|
| 356 |
inline int _node_id(int n) const { return _node_num - n - 1; }; |
| 357 |
|
| 358 |
|
| 359 |
IntVector _source; |
| 360 |
IntVector _target; |
| 361 |
bool _arc_mixing; |
| 362 |
|
| 363 |
|
| 364 |
CostVector _cost; |
| 365 |
ValueVector _supply; |
| 366 |
#ifdef SPARSE_FLOW |
| 367 |
SparseValueVector<Value> _flow; |
| 368 |
#else |
| 369 |
ValueVector _flow; |
| 370 |
#endif |
| 371 |
|
| 372 |
CostVector _pi; |
| 373 |
|
| 374 |
|
| 375 |
IntVector _parent; |
| 376 |
ArcVector _pred; |
| 377 |
IntVector _thread; |
| 378 |
IntVector _rev_thread; |
| 379 |
IntVector _succ_num; |
| 380 |
IntVector _last_succ; |
| 381 |
IntVector _dirty_revs; |
| 382 |
BoolVector _forward; |
| 383 |
StateVector _state; |
| 384 |
ArcsType _root; |
| 385 |
|
| 386 |
|
| 387 |
ArcsType in_arc, join, u_in, v_in, u_out, v_out; |
| 388 |
ArcsType first, second, right, last; |
| 389 |
ArcsType stem, par_stem, new_stem; |
| 390 |
Value delta; |
| 391 |
|
| 392 |
const Value MAX; |
| 393 |
|
| 394 |
ArcsType mixingCoeff; |
| 395 |
|
| 396 |
public: |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
const Value INF; |
| 404 |
|
| 405 |
private: |
| 406 |
|
| 407 |
|
| 408 |
inline ArcsType sequence(ArcsType k) const { |
| 409 |
ArcsType smallv = (k > num_total_big_subsequence_numbers) & 1; |
| 410 |
|
| 411 |
k -= num_total_big_subsequence_numbers * smallv; |
| 412 |
ArcsType subsequence_length2 = subsequence_length - smallv; |
| 413 |
ArcsType subsequence_num = (k / subsequence_length2) + num_big_subsequences * smallv; |
| 414 |
ArcsType subsequence_offset = (k % subsequence_length2) * mixingCoeff; |
| 415 |
|
| 416 |
return subsequence_offset + subsequence_num; |
| 417 |
} |
| 418 |
ArcsType subsequence_length; |
| 419 |
ArcsType num_big_subsequences; |
| 420 |
ArcsType num_total_big_subsequence_numbers; |
| 421 |
|
| 422 |
inline ArcsType getArcID(const Arc &arc) const |
| 423 |
{ |
| 424 |
|
| 425 |
ArcsType n = _arc_num - GR::id(arc) - 1; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
if (_arc_mixing) |
| 430 |
return sequence(n); |
| 431 |
else |
| 432 |
return n; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
inline ArcsType getSource(const ArcsType arc) const |
| 437 |
{ |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
ArcsType n = _arc_num - arc - 1; |
| 442 |
if (_arc_mixing) |
| 443 |
n = mixingCoeff*(n%mixingCoeff) + n / mixingCoeff; |
| 444 |
|
| 445 |
ArcsType b; |
| 446 |
if (n >= 0) |
| 447 |
b = _node_id(_graph.source(GR::arcFromId(n))); |
| 448 |
else |
| 449 |
{ |
| 450 |
n = arc + 1 - _arc_num; |
| 451 |
if (n <= _node_num) |
| 452 |
b = _node_num; |
| 453 |
else |
| 454 |
if (n >= _graph._n1) |
| 455 |
b = _graph._n1; |
| 456 |
else |
| 457 |
b = _graph._n1 - n; |
| 458 |
} |
| 459 |
|
| 460 |
return b; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
class BlockSearchPivotRule |
| 467 |
{ |
| 468 |
private: |
| 469 |
|
| 470 |
|
| 471 |
const IntVector &_source; |
| 472 |
const IntVector &_target; |
| 473 |
const CostVector &_cost; |
| 474 |
const StateVector &_state; |
| 475 |
const CostVector &_pi; |
| 476 |
ArcsType &_in_arc; |
| 477 |
ArcsType _search_arc_num; |
| 478 |
|
| 479 |
|
| 480 |
ArcsType _block_size; |
| 481 |
ArcsType _next_arc; |
| 482 |
NetworkSimplexSimple &_ns; |
| 483 |
|
| 484 |
public: |
| 485 |
|
| 486 |
|
| 487 |
BlockSearchPivotRule(NetworkSimplexSimple &ns) : |
| 488 |
_source(ns._source), _target(ns._target), |
| 489 |
_cost(ns._cost), _state(ns._state), _pi(ns._pi), |
| 490 |
_in_arc(ns.in_arc), _search_arc_num(ns._search_arc_num), |
| 491 |
_next_arc(0), _ns(ns) |
| 492 |
{ |
| 493 |
|
| 494 |
const double BLOCK_SIZE_FACTOR = 1; |
| 495 |
const ArcsType MIN_BLOCK_SIZE = 10; |
| 496 |
|
| 497 |
_block_size = std::max(ArcsType(BLOCK_SIZE_FACTOR * std::sqrt(double(_search_arc_num))), MIN_BLOCK_SIZE); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
bool findEnteringArc() { |
| 502 |
Cost min_val = 0; |
| 503 |
|
| 504 |
ArcsType N = _ns.num_threads; |
| 505 |
|
| 506 |
std::vector<Cost> minArray(N, 0); |
| 507 |
std::vector<ArcsType> arcId(N); |
| 508 |
ArcsType bs = (ArcsType)ceil(_block_size / (double)N); |
| 509 |
|
| 510 |
for (ArcsType i = 0; i < _search_arc_num; i += _block_size) { |
| 511 |
|
| 512 |
ArcsType e; |
| 513 |
int j; |
| 514 |
#pragma omp parallel |
| 515 |
{ |
| 516 |
#ifdef _OPENMP |
| 517 |
int t = omp_get_thread_num(); |
| 518 |
#else |
| 519 |
int t = 0; |
| 520 |
#endif |
| 521 |
|
| 522 |
#pragma omp for schedule(static, bs) lastprivate(e) |
| 523 |
for (j = 0; j < std::min(i + _block_size, _search_arc_num) - i; j++) { |
| 524 |
e = (_next_arc + i + j); if (e >= _search_arc_num) e -= _search_arc_num; |
| 525 |
Cost c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
| 526 |
if (c < minArray[t]) { |
| 527 |
minArray[t] = c; |
| 528 |
arcId[t] = e; |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
for (int j = 0; j < N; j++) { |
| 533 |
if (minArray[j] < min_val) { |
| 534 |
min_val = minArray[j]; |
| 535 |
_in_arc = arcId[j]; |
| 536 |
} |
| 537 |
} |
| 538 |
Cost a = std::abs(_pi[_source[_in_arc]]) > std::abs(_pi[_target[_in_arc]]) ? std::abs(_pi[_source[_in_arc]]) : std::abs(_pi[_target[_in_arc]]); |
| 539 |
a = a > std::abs(_cost[_in_arc]) ? a : std::abs(_cost[_in_arc]); |
| 540 |
if (min_val < -EPSILON*a) { |
| 541 |
_next_arc = e; |
| 542 |
return true; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
Cost a = fabs(_pi[_source[_in_arc]]) > fabs(_pi[_target[_in_arc]]) ? fabs(_pi[_source[_in_arc]]) : fabs(_pi[_target[_in_arc]]); |
| 547 |
a = a > fabs(_cost[_in_arc]) ? a : fabs(_cost[_in_arc]); |
| 548 |
if (min_val >= -EPSILON*a) return false; |
| 549 |
|
| 550 |
return true; |
| 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 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
}; |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
public: |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
int _init_nb_nodes; |
| 661 |
ArcsType _init_nb_arcs; |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
template<typename CostMap> |
| 682 |
NetworkSimplexSimple& costMap(const CostMap& map) { |
| 683 |
Arc a; _graph.first(a); |
| 684 |
for (; a != INVALID; _graph.next(a)) { |
| 685 |
_cost[getArcID(a)] = map[a]; |
| 686 |
} |
| 687 |
return *this; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
template<typename Value> |
| 701 |
NetworkSimplexSimple& setCost(const Arc& arc, const Value cost) { |
| 702 |
_cost[getArcID(arc)] = cost; |
| 703 |
return *this; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
template<typename SupplyMap> |
| 719 |
NetworkSimplexSimple& supplyMap(const SupplyMap& map) { |
| 720 |
Node n; _graph.first(n); |
| 721 |
for (; n != INVALIDNODE; _graph.next(n)) { |
| 722 |
_supply[_node_id(n)] = map[n]; |
| 723 |
} |
| 724 |
return *this; |
| 725 |
} |
| 726 |
template<typename SupplyMap> |
| 727 |
NetworkSimplexSimple& supplyMap(const SupplyMap* map1, int n1, const SupplyMap* map2, int n2) { |
| 728 |
Node n; _graph.first(n); |
| 729 |
for (; n != INVALIDNODE; _graph.next(n)) { |
| 730 |
if (n<n1) |
| 731 |
_supply[_node_id(n)] = map1[n]; |
| 732 |
else |
| 733 |
_supply[_node_id(n)] = map2[n - n1]; |
| 734 |
} |
| 735 |
return *this; |
| 736 |
} |
| 737 |
template<typename SupplyMap> |
| 738 |
NetworkSimplexSimple& supplyMapAll(SupplyMap val1, int n1, SupplyMap val2, int n2) { |
| 739 |
Node n; _graph.first(n); |
| 740 |
for (; n != INVALIDNODE; _graph.next(n)) { |
| 741 |
if (n<n1) |
| 742 |
_supply[_node_id(n)] = val1; |
| 743 |
else |
| 744 |
_supply[_node_id(n)] = val2; |
| 745 |
} |
| 746 |
return *this; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
NetworkSimplexSimple& stSupply(const Node& s, const Node& t, Value k) { |
| 767 |
for (int i = 0; i != _node_num; ++i) { |
| 768 |
_supply[i] = 0; |
| 769 |
} |
| 770 |
_supply[_node_id(s)] = k; |
| 771 |
_supply[_node_id(t)] = -k; |
| 772 |
return *this; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
NetworkSimplexSimple& supplyType(SupplyType supply_type) { |
| 785 |
_stype = supply_type; |
| 786 |
return *this; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
ProblemType run() { |
| 830 |
#if DEBUG_LVL>0 |
| 831 |
std::cout << "OPTIMAL = " << OPTIMAL << "\nINFEASIBLE = " << INFEASIBLE << "\nUNBOUNDED = " << UNBOUNDED << "\nMAX_ITER_REACHED" << MAX_ITER_REACHED << "\n" ; |
| 832 |
#endif |
| 833 |
if (!init()) return INFEASIBLE; |
| 834 |
#if DEBUG_LVL>0 |
| 835 |
std::cout << "Init done, starting iterations\n"; |
| 836 |
#endif |
| 837 |
|
| 838 |
return start(); |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
NetworkSimplexSimple& resetParams() { |
| 878 |
for (int i = 0; i != _node_num; ++i) { |
| 879 |
_supply[i] = 0; |
| 880 |
} |
| 881 |
for (ArcsType i = 0; i != _arc_num; ++i) { |
| 882 |
_cost[i] = 1; |
| 883 |
} |
| 884 |
_stype = GEQ; |
| 885 |
return *this; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
NetworkSimplexSimple& reset() { |
| 910 |
|
| 911 |
_node_num = _init_nb_nodes; |
| 912 |
_arc_num = _init_nb_arcs; |
| 913 |
int all_node_num = _node_num + 1; |
| 914 |
ArcsType max_arc_num = _arc_num + 2 * _node_num; |
| 915 |
|
| 916 |
_source.resize(max_arc_num); |
| 917 |
_target.resize(max_arc_num); |
| 918 |
|
| 919 |
_cost.resize(max_arc_num); |
| 920 |
_supply.resize(all_node_num); |
| 921 |
_flow.resize(max_arc_num); |
| 922 |
_pi.resize(all_node_num); |
| 923 |
|
| 924 |
_parent.resize(all_node_num); |
| 925 |
_pred.resize(all_node_num); |
| 926 |
_forward.resize(all_node_num); |
| 927 |
_thread.resize(all_node_num); |
| 928 |
_rev_thread.resize(all_node_num); |
| 929 |
_succ_num.resize(all_node_num); |
| 930 |
_last_succ.resize(all_node_num); |
| 931 |
_state.resize(max_arc_num); |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
if (_arc_mixing && _node_num > 1) { |
| 936 |
|
| 937 |
|
| 938 |
const ArcsType k = std::max(ArcsType(_arc_num / _node_num), ArcsType(3)); |
| 939 |
mixingCoeff = k; |
| 940 |
subsequence_length = _arc_num / mixingCoeff + 1; |
| 941 |
num_big_subsequences = _arc_num % mixingCoeff; |
| 942 |
num_total_big_subsequence_numbers = subsequence_length * num_big_subsequences; |
| 943 |
|
| 944 |
#pragma omp parallel for schedule(static) |
| 945 |
for (Arc a = 0; a <= _graph.maxArcId(); a++) { |
| 946 |
ArcsType i = sequence(_graph.maxArcId()-a); |
| 947 |
_source[i] = _node_id(_graph.source(a)); |
| 948 |
_target[i] = _node_id(_graph.target(a)); |
| 949 |
} |
| 950 |
} else { |
| 951 |
|
| 952 |
ArcsType i = 0; |
| 953 |
Arc a; _graph.first(a); |
| 954 |
for (; a != INVALID; _graph.next(a), ++i) { |
| 955 |
_source[i] = _node_id(_graph.source(a)); |
| 956 |
_target[i] = _node_id(_graph.target(a)); |
| 957 |
|
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
resetParams(); |
| 963 |
return *this; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
template <typename Number> |
| 1001 |
Number totalCost() const { |
| 1002 |
Number c = 0; |
| 1003 |
|
| 1004 |
#ifdef SPARSE_FLOW |
| 1005 |
#ifdef HASHMAP |
| 1006 |
typename std::unordered_map<size_t, Value>::const_iterator it; |
| 1007 |
#else |
| 1008 |
typename std::map<size_t, Value>::const_iterator it; |
| 1009 |
#endif |
| 1010 |
for (it = _flow.data.begin(); it!=_flow.data.end(); ++it) |
| 1011 |
c += Number(it->second) * Number(_cost[it->first]); |
| 1012 |
return c; |
| 1013 |
#else |
| 1014 |
for (ArcsType i = 0; i<_flow.size(); i++) |
| 1015 |
c += _flow[i] * Number(_cost[i]); |
| 1016 |
return c; |
| 1017 |
#endif |
| 1018 |
} |
| 1019 |
|
| 1020 |
#ifndef DOXYGEN |
| 1021 |
Cost totalCost() const { |
| 1022 |
return totalCost<Cost>(); |
| 1023 |
} |
| 1024 |
#endif |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
Value flow(const Arc& a) const { |
| 1032 |
return _flow[getArcID(a)]; |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
template <typename FlowMap> |
| 1043 |
void flowMap(FlowMap &map) const { |
| 1044 |
Arc a; _graph.first(a); |
| 1045 |
for (; a != INVALID; _graph.next(a)) { |
| 1046 |
map.set(a, _flow[getArcID(a)]); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
Cost potential(const Node& n) const { |
| 1057 |
return _pi[_node_id(n)]; |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
template <typename PotentialMap> |
| 1069 |
void potentialMap(PotentialMap &map) const { |
| 1070 |
Node n; _graph.first(n); |
| 1071 |
for (; n != INVALID; _graph.next(n)) { |
| 1072 |
map.set(n, _pi[_node_id(n)]); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
private: |
| 1079 |
|
| 1080 |
|
| 1081 |
bool init() { |
| 1082 |
if (_node_num == 0) return false; |
| 1083 |
|
| 1084 |
|
| 1085 |
_sum_supply = 0; |
| 1086 |
for (int i = 0; i != _node_num; ++i) { |
| 1087 |
_sum_supply += _supply[i]; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
Cost ART_COST; |
| 1095 |
if (std::numeric_limits<Cost>::is_exact) { |
| 1096 |
ART_COST = std::numeric_limits<Cost>::max() / 2 + 1; |
| 1097 |
} else { |
| 1098 |
ART_COST = 0; |
| 1099 |
for (ArcsType i = 0; i != _arc_num; ++i) { |
| 1100 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
| 1101 |
} |
| 1102 |
ART_COST = (ART_COST + 1) * _node_num; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
for (ArcsType i = 0; i != _arc_num; ++i) { |
| 1107 |
#ifndef SPARSE_FLOW |
| 1108 |
_flow[i] = 0; |
| 1109 |
#endif |
| 1110 |
_state[i] = STATE_LOWER; |
| 1111 |
} |
| 1112 |
#ifdef SPARSE_FLOW |
| 1113 |
_flow = SparseValueVector<Value>(); |
| 1114 |
#endif |
| 1115 |
|
| 1116 |
|
| 1117 |
_root = _node_num; |
| 1118 |
_parent[_root] = -1; |
| 1119 |
_pred[_root] = -1; |
| 1120 |
_thread[_root] = 0; |
| 1121 |
_rev_thread[0] = _root; |
| 1122 |
_succ_num[_root] = _node_num + 1; |
| 1123 |
_last_succ[_root] = _root - 1; |
| 1124 |
_supply[_root] = -_sum_supply; |
| 1125 |
_pi[_root] = 0; |
| 1126 |
|
| 1127 |
|
| 1128 |
if (_sum_supply == 0) { |
| 1129 |
|
| 1130 |
_search_arc_num = _arc_num; |
| 1131 |
_all_arc_num = _arc_num + _node_num; |
| 1132 |
for (ArcsType u = 0, e = _arc_num; u != _node_num; ++u, ++e) { |
| 1133 |
_parent[u] = _root; |
| 1134 |
_pred[u] = e; |
| 1135 |
_thread[u] = u + 1; |
| 1136 |
_rev_thread[u + 1] = u; |
| 1137 |
_succ_num[u] = 1; |
| 1138 |
_last_succ[u] = u; |
| 1139 |
_state[e] = STATE_TREE; |
| 1140 |
if (_supply[u] >= 0) { |
| 1141 |
_forward[u] = true; |
| 1142 |
_pi[u] = 0; |
| 1143 |
_source[e] = u; |
| 1144 |
_target[e] = _root; |
| 1145 |
_flow[e] = _supply[u]; |
| 1146 |
_cost[e] = 0; |
| 1147 |
} else { |
| 1148 |
_forward[u] = false; |
| 1149 |
_pi[u] = ART_COST; |
| 1150 |
_source[e] = _root; |
| 1151 |
_target[e] = u; |
| 1152 |
_flow[e] = -_supply[u]; |
| 1153 |
_cost[e] = ART_COST; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} else if (_sum_supply > 0) { |
| 1157 |
|
| 1158 |
_search_arc_num = _arc_num + _node_num; |
| 1159 |
ArcsType f = _arc_num + _node_num; |
| 1160 |
for (ArcsType u = 0, e = _arc_num; u != _node_num; ++u, ++e) { |
| 1161 |
_parent[u] = _root; |
| 1162 |
_thread[u] = u + 1; |
| 1163 |
_rev_thread[u + 1] = u; |
| 1164 |
_succ_num[u] = 1; |
| 1165 |
_last_succ[u] = u; |
| 1166 |
if (_supply[u] >= 0) { |
| 1167 |
_forward[u] = true; |
| 1168 |
_pi[u] = 0; |
| 1169 |
_pred[u] = e; |
| 1170 |
_source[e] = u; |
| 1171 |
_target[e] = _root; |
| 1172 |
_flow[e] = _supply[u]; |
| 1173 |
_cost[e] = 0; |
| 1174 |
_state[e] = STATE_TREE; |
| 1175 |
} else { |
| 1176 |
_forward[u] = false; |
| 1177 |
_pi[u] = ART_COST; |
| 1178 |
_pred[u] = f; |
| 1179 |
_source[f] = _root; |
| 1180 |
_target[f] = u; |
| 1181 |
_flow[f] = -_supply[u]; |
| 1182 |
_cost[f] = ART_COST; |
| 1183 |
_state[f] = STATE_TREE; |
| 1184 |
_source[e] = u; |
| 1185 |
_target[e] = _root; |
| 1186 |
|
| 1187 |
_cost[e] = 0; |
| 1188 |
_state[e] = STATE_LOWER; |
| 1189 |
++f; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
_all_arc_num = f; |
| 1193 |
} else { |
| 1194 |
|
| 1195 |
_search_arc_num = _arc_num + _node_num; |
| 1196 |
ArcsType f = _arc_num + _node_num; |
| 1197 |
for (ArcsType u = 0, e = _arc_num; u != _node_num; ++u, ++e) { |
| 1198 |
_parent[u] = _root; |
| 1199 |
_thread[u] = u + 1; |
| 1200 |
_rev_thread[u + 1] = u; |
| 1201 |
_succ_num[u] = 1; |
| 1202 |
_last_succ[u] = u; |
| 1203 |
if (_supply[u] <= 0) { |
| 1204 |
_forward[u] = false; |
| 1205 |
_pi[u] = 0; |
| 1206 |
_pred[u] = e; |
| 1207 |
_source[e] = _root; |
| 1208 |
_target[e] = u; |
| 1209 |
_flow[e] = -_supply[u]; |
| 1210 |
_cost[e] = 0; |
| 1211 |
_state[e] = STATE_TREE; |
| 1212 |
} else { |
| 1213 |
_forward[u] = true; |
| 1214 |
_pi[u] = -ART_COST; |
| 1215 |
_pred[u] = f; |
| 1216 |
_source[f] = u; |
| 1217 |
_target[f] = _root; |
| 1218 |
_flow[f] = _supply[u]; |
| 1219 |
_state[f] = STATE_TREE; |
| 1220 |
_cost[f] = ART_COST; |
| 1221 |
_source[e] = _root; |
| 1222 |
_target[e] = u; |
| 1223 |
|
| 1224 |
_cost[e] = 0; |
| 1225 |
_state[e] = STATE_LOWER; |
| 1226 |
++f; |
| 1227 |
} |
| 1228 |
} |
| 1229 |
_all_arc_num = f; |
| 1230 |
} |
| 1231 |
|
| 1232 |
return true; |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
void findJoinNode() { |
| 1237 |
int u = _source[in_arc]; |
| 1238 |
int v = _target[in_arc]; |
| 1239 |
while (u != v) { |
| 1240 |
if (_succ_num[u] < _succ_num[v]) { |
| 1241 |
u = _parent[u]; |
| 1242 |
} else { |
| 1243 |
v = _parent[v]; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
join = u; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
bool findLeavingArc() { |
| 1252 |
|
| 1253 |
|
| 1254 |
if (_state[in_arc] == STATE_LOWER) { |
| 1255 |
first = _source[in_arc]; |
| 1256 |
second = _target[in_arc]; |
| 1257 |
} else { |
| 1258 |
first = _target[in_arc]; |
| 1259 |
second = _source[in_arc]; |
| 1260 |
} |
| 1261 |
delta = INF; |
| 1262 |
char result = 0; |
| 1263 |
Value d; |
| 1264 |
ArcsType e; |
| 1265 |
|
| 1266 |
|
| 1267 |
for (int u = first; u != join; u = _parent[u]) { |
| 1268 |
e = _pred[u]; |
| 1269 |
d = _forward[u] ? _flow[e] : INF; |
| 1270 |
if (d < delta) { |
| 1271 |
delta = d; |
| 1272 |
u_out = u; |
| 1273 |
result = 1; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
for (int u = second; u != join; u = _parent[u]) { |
| 1278 |
e = _pred[u]; |
| 1279 |
d = _forward[u] ? INF : _flow[e]; |
| 1280 |
if (d <= delta) { |
| 1281 |
delta = d; |
| 1282 |
u_out = u; |
| 1283 |
result = 2; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
if (result == 1) { |
| 1288 |
u_in = first; |
| 1289 |
v_in = second; |
| 1290 |
} else { |
| 1291 |
u_in = second; |
| 1292 |
v_in = first; |
| 1293 |
} |
| 1294 |
return result != 0; |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
void changeFlow(bool change) { |
| 1299 |
|
| 1300 |
if (delta > 0) { |
| 1301 |
Value val = _state[in_arc] * delta; |
| 1302 |
_flow[in_arc] += val; |
| 1303 |
for (int u = _source[in_arc]; u != join; u = _parent[u]) { |
| 1304 |
_flow[_pred[u]] += _forward[u] ? -val : val; |
| 1305 |
} |
| 1306 |
for (int u = _target[in_arc]; u != join; u = _parent[u]) { |
| 1307 |
_flow[_pred[u]] += _forward[u] ? val : -val; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
if (change) { |
| 1312 |
_state[in_arc] = STATE_TREE; |
| 1313 |
_state[_pred[u_out]] = |
| 1314 |
(_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; |
| 1315 |
} else { |
| 1316 |
_state[in_arc] = -_state[in_arc]; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
|
| 1321 |
void updateTreeStructure() { |
| 1322 |
int old_rev_thread = _rev_thread[u_out]; |
| 1323 |
int old_succ_num = _succ_num[u_out]; |
| 1324 |
int old_last_succ = _last_succ[u_out]; |
| 1325 |
v_out = _parent[u_out]; |
| 1326 |
|
| 1327 |
|
| 1328 |
if (u_in == u_out) { |
| 1329 |
|
| 1330 |
_parent[u_in] = v_in; |
| 1331 |
_pred[u_in] = in_arc; |
| 1332 |
_forward[u_in] = (u_in == _source[in_arc]); |
| 1333 |
|
| 1334 |
|
| 1335 |
if (_thread[v_in] != u_out) { |
| 1336 |
ArcsType after = _thread[old_last_succ]; |
| 1337 |
_thread[old_rev_thread] = after; |
| 1338 |
_rev_thread[after] = old_rev_thread; |
| 1339 |
after = _thread[v_in]; |
| 1340 |
_thread[v_in] = u_out; |
| 1341 |
_rev_thread[u_out] = v_in; |
| 1342 |
_thread[old_last_succ] = after; |
| 1343 |
_rev_thread[after] = old_last_succ; |
| 1344 |
} |
| 1345 |
} else { |
| 1346 |
|
| 1347 |
|
| 1348 |
int thread_continue = old_rev_thread == v_in ? |
| 1349 |
_thread[old_last_succ] : _thread[v_in]; |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
int stem = u_in; |
| 1354 |
int par_stem = v_in; |
| 1355 |
int next_stem; |
| 1356 |
int last = _last_succ[u_in]; |
| 1357 |
int before, after = _thread[last]; |
| 1358 |
_thread[v_in] = u_in; |
| 1359 |
_dirty_revs.clear(); |
| 1360 |
_dirty_revs.push_back(v_in); |
| 1361 |
while (stem != u_out) { |
| 1362 |
|
| 1363 |
next_stem = _parent[stem]; |
| 1364 |
_thread[last] = next_stem; |
| 1365 |
_dirty_revs.push_back(last); |
| 1366 |
|
| 1367 |
|
| 1368 |
before = _rev_thread[stem]; |
| 1369 |
_thread[before] = after; |
| 1370 |
_rev_thread[after] = before; |
| 1371 |
|
| 1372 |
|
| 1373 |
_parent[stem] = par_stem; |
| 1374 |
par_stem = stem; |
| 1375 |
stem = next_stem; |
| 1376 |
|
| 1377 |
|
| 1378 |
last = _last_succ[stem] == _last_succ[par_stem] ? |
| 1379 |
_rev_thread[par_stem] : _last_succ[stem]; |
| 1380 |
after = _thread[last]; |
| 1381 |
} |
| 1382 |
_parent[u_out] = par_stem; |
| 1383 |
_thread[last] = thread_continue; |
| 1384 |
_rev_thread[thread_continue] = last; |
| 1385 |
_last_succ[u_out] = last; |
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
if (old_rev_thread != v_in) { |
| 1390 |
_thread[old_rev_thread] = after; |
| 1391 |
_rev_thread[after] = old_rev_thread; |
| 1392 |
} |
| 1393 |
|
| 1394 |
|
| 1395 |
for (int i = 0; i != int(_dirty_revs.size()); ++i) { |
| 1396 |
int u = _dirty_revs[i]; |
| 1397 |
_rev_thread[_thread[u]] = u; |
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
int tmp_sc = 0, tmp_ls = _last_succ[u_out]; |
| 1403 |
for (int u = u_out, p = _parent[u]; u != u_in; u = p, p = _parent[u]) { |
| 1404 |
_pred[u] = _pred[p]; |
| 1405 |
_forward[u] = !_forward[p]; |
| 1406 |
tmp_sc += _succ_num[u] - _succ_num[p]; |
| 1407 |
_succ_num[u] = tmp_sc; |
| 1408 |
_last_succ[p] = tmp_ls; |
| 1409 |
} |
| 1410 |
_pred[u_in] = in_arc; |
| 1411 |
_forward[u_in] = (u_in == _source[in_arc]); |
| 1412 |
_succ_num[u_in] = old_succ_num; |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
int up_limit_out = _last_succ[join] == v_in ? join : -1; |
| 1417 |
int last_succ_out = _last_succ[u_out]; |
| 1418 |
for (int u = v_in; u != -1 && _last_succ[u] == v_in; u = _parent[u]) { |
| 1419 |
_last_succ[u] = last_succ_out; |
| 1420 |
} |
| 1421 |
|
| 1422 |
|
| 1423 |
if (join != old_rev_thread && v_in != old_rev_thread) { |
| 1424 |
for (int u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
| 1425 |
u = _parent[u]) { |
| 1426 |
_last_succ[u] = old_rev_thread; |
| 1427 |
} |
| 1428 |
} else if (last_succ_out != old_last_succ) { |
| 1429 |
for (int u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
| 1430 |
u = _parent[u]) { |
| 1431 |
_last_succ[u] = last_succ_out; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
|
| 1436 |
for (int u = v_in; u != join; u = _parent[u]) { |
| 1437 |
_succ_num[u] += old_succ_num; |
| 1438 |
} |
| 1439 |
|
| 1440 |
for (int u = v_out; u != join; u = _parent[u]) { |
| 1441 |
_succ_num[u] -= old_succ_num; |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
void updatePotential() { |
| 1446 |
Cost sigma = _pi[v_in] - _pi[u_in] - |
| 1447 |
((_forward[u_in])?_cost[in_arc]:(-_cost[in_arc])); |
| 1448 |
int end = _thread[_last_succ[u_in]]; |
| 1449 |
for (int u = u_in; u != end; u = _thread[u]) { |
| 1450 |
_pi[u] += sigma; |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
bool initialPivots() { |
| 1457 |
Value curr, total = 0; |
| 1458 |
std::vector<Node> supply_nodes, demand_nodes; |
| 1459 |
Node u; _graph.first(u); |
| 1460 |
for (; u != INVALIDNODE; _graph.next(u)) { |
| 1461 |
curr = _supply[_node_id(u)]; |
| 1462 |
if (curr > 0) { |
| 1463 |
total += curr; |
| 1464 |
supply_nodes.push_back(u); |
| 1465 |
} else if (curr < 0) { |
| 1466 |
demand_nodes.push_back(u); |
| 1467 |
} |
| 1468 |
} |
| 1469 |
if (_sum_supply > 0) total -= _sum_supply; |
| 1470 |
if (total <= 0) return true; |
| 1471 |
|
| 1472 |
ArcVector arc_vector; |
| 1473 |
if (_sum_supply >= 0) { |
| 1474 |
if (supply_nodes.size() == 1 && demand_nodes.size() == 1) { |
| 1475 |
|
| 1476 |
|
| 1477 |
BoolVector reached(_node_num, false); |
| 1478 |
Node s = supply_nodes[0], t = demand_nodes[0]; |
| 1479 |
std::vector<Node> stack; |
| 1480 |
reached[t] = true; |
| 1481 |
stack.push_back(t); |
| 1482 |
while (!stack.empty()) { |
| 1483 |
Node u, v = stack.back(); |
| 1484 |
stack.pop_back(); |
| 1485 |
if (v == s) break; |
| 1486 |
Arc a; _graph.firstIn(a, v); |
| 1487 |
for (; a != INVALID; _graph.nextIn(a)) { |
| 1488 |
if (reached[u = _graph.source(a)]) continue; |
| 1489 |
ArcsType j = getArcID(a); |
| 1490 |
arc_vector.push_back(j); |
| 1491 |
reached[u] = true; |
| 1492 |
stack.push_back(u); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
} else { |
| 1496 |
arc_vector.resize(demand_nodes.size()); |
| 1497 |
|
| 1498 |
#pragma omp parallel for |
| 1499 |
for (int i = 0; i < demand_nodes.size(); ++i) { |
| 1500 |
Node v = demand_nodes[i]; |
| 1501 |
Cost min_cost = std::numeric_limits<Cost>::max(); |
| 1502 |
Arc min_arc = INVALID; |
| 1503 |
Arc a; _graph.firstIn(a, v); |
| 1504 |
for (; a != INVALID; _graph.nextIn(a)) { |
| 1505 |
Cost c = _cost[getArcID(a)]; |
| 1506 |
if (c < min_cost) { |
| 1507 |
min_cost = c; |
| 1508 |
min_arc = a; |
| 1509 |
} |
| 1510 |
} |
| 1511 |
arc_vector[i] = getArcID(min_arc); |
| 1512 |
} |
| 1513 |
arc_vector.erase(std::remove(arc_vector.begin(), arc_vector.end(), INVALID), arc_vector.end()); |
| 1514 |
} |
| 1515 |
} else { |
| 1516 |
arc_vector.resize(supply_nodes.size()); |
| 1517 |
|
| 1518 |
#pragma omp parallel for |
| 1519 |
for (int i = 0; i < int(supply_nodes.size()); ++i) { |
| 1520 |
Node u = supply_nodes[i]; |
| 1521 |
Cost min_cost = std::numeric_limits<Cost>::max(); |
| 1522 |
Arc min_arc = INVALID; |
| 1523 |
Arc a; _graph.firstOut(a, u); |
| 1524 |
for (; a != INVALID; _graph.nextOut(a)) { |
| 1525 |
Cost c = _cost[getArcID(a)]; |
| 1526 |
if (c < min_cost) { |
| 1527 |
min_cost = c; |
| 1528 |
min_arc = a; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
arc_vector[i] = getArcID(min_arc); |
| 1532 |
} |
| 1533 |
arc_vector.erase(std::remove(arc_vector.begin(), arc_vector.end(), INVALID), arc_vector.end()); |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
for (ArcsType i = 0; i != ArcsType(arc_vector.size()); ++i) { |
| 1538 |
in_arc = arc_vector[i]; |
| 1539 |
if (_state[in_arc] * (_cost[in_arc] + _pi[_source[in_arc]] - |
| 1540 |
_pi[_target[in_arc]]) >= 0) continue; |
| 1541 |
findJoinNode(); |
| 1542 |
bool change = findLeavingArc(); |
| 1543 |
if (delta >= MAX) return false; |
| 1544 |
changeFlow(change); |
| 1545 |
if (change) { |
| 1546 |
updateTreeStructure(); |
| 1547 |
updatePotential(); |
| 1548 |
} |
| 1549 |
} |
| 1550 |
return true; |
| 1551 |
} |
| 1552 |
|
| 1553 |
|
| 1554 |
ProblemType start() { |
| 1555 |
return start<BlockSearchPivotRule>(); |
| 1556 |
} |
| 1557 |
|
| 1558 |
template <typename PivotRuleImpl> |
| 1559 |
ProblemType start() { |
| 1560 |
PivotRuleImpl pivot(*this); |
| 1561 |
ProblemType retVal = OPTIMAL; |
| 1562 |
|
| 1563 |
|
| 1564 |
if (!initialPivots()) return UNBOUNDED; |
| 1565 |
|
| 1566 |
uint64_t iter_number = 0; |
| 1567 |
|
| 1568 |
while (pivot.findEnteringArc()) { |
| 1569 |
if ((++iter_number <= max_iter&&max_iter > 0) || max_iter<=0) { |
| 1570 |
#if DEBUG_LVL>0 |
| 1571 |
if(iter_number>MAX_DEBUG_ITER) |
| 1572 |
break; |
| 1573 |
if(iter_number%1000==0||iter_number%1000==1){ |
| 1574 |
Cost curCost=totalCost(); |
| 1575 |
Value sumFlow=0; |
| 1576 |
Cost a; |
| 1577 |
a= (fabs(_pi[_source[in_arc]])>=fabs(_pi[_target[in_arc]])) ? fabs(_pi[_source[in_arc]]) : fabs(_pi[_target[in_arc]]); |
| 1578 |
a=a>=fabs(_cost[in_arc])?a:fabs(_cost[in_arc]); |
| 1579 |
for (int i=0; i<_flow.size(); i++) { |
| 1580 |
sumFlow+=_state[i]*_flow[i]; |
| 1581 |
} |
| 1582 |
std::cout << "Sum of the flow " << std::setprecision(20) << sumFlow << "\n" << iter_number << " iterations, current cost=" << curCost << "\nReduced cost=" << _state[in_arc] * (_cost[in_arc] + _pi[_source[in_arc]] -_pi[_target[in_arc]]) << "\nPrecision = "<< -EPSILON*(a) << "\n"; |
| 1583 |
std::cout << "Arc in = (" << _node_id(_source[in_arc]) << ", " << _node_id(_target[in_arc]) <<")\n"; |
| 1584 |
std::cout << "Supplies = (" << _supply[_source[in_arc]] << ", " << _supply[_target[in_arc]] << ")\n"; |
| 1585 |
std::cout << _cost[in_arc] << "\n"; |
| 1586 |
std::cout << _pi[_source[in_arc]] << "\n"; |
| 1587 |
std::cout << _pi[_target[in_arc]] << "\n"; |
| 1588 |
std::cout << a << "\n"; |
| 1589 |
} |
| 1590 |
#endif |
| 1591 |
|
| 1592 |
findJoinNode(); |
| 1593 |
bool change = findLeavingArc(); |
| 1594 |
if (delta >= MAX) return UNBOUNDED; |
| 1595 |
changeFlow(change); |
| 1596 |
if (change) { |
| 1597 |
updateTreeStructure(); |
| 1598 |
updatePotential(); |
| 1599 |
} |
| 1600 |
|
| 1601 |
#if DEBUG_LVL>0 |
| 1602 |
else{ |
| 1603 |
std::cout << "No change\n"; |
| 1604 |
} |
| 1605 |
#endif |
| 1606 |
|
| 1607 |
#if DEBUG_LVL>1 |
| 1608 |
std::cout << "Arc in = (" << _source[in_arc] << ", " << _target[in_arc] << ")\n"; |
| 1609 |
#endif |
| 1610 |
|
| 1611 |
|
| 1612 |
} else { |
| 1613 |
|
| 1614 |
retVal = MAX_ITER_REACHED; |
| 1615 |
break; |
| 1616 |
} |
| 1617 |
|
| 1618 |
} |
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
#if DEBUG_LVL>0 |
| 1623 |
Cost curCost=totalCost(); |
| 1624 |
Value sumFlow=0; |
| 1625 |
Cost a; |
| 1626 |
a= (fabs(_pi[_source[in_arc]])>=fabs(_pi[_target[in_arc]])) ? fabs(_pi[_source[in_arc]]) : fabs(_pi[_target[in_arc]]); |
| 1627 |
a=a>=fabs(_cost[in_arc])?a:fabs(_cost[in_arc]); |
| 1628 |
for (int i=0; i<_flow.size(); i++) { |
| 1629 |
sumFlow+=_state[i]*_flow[i]; |
| 1630 |
} |
| 1631 |
|
| 1632 |
std::cout << "Sum of the flow " << std::setprecision(20) << sumFlow << "\n" << niter << " iterations, current cost=" << curCost << "\nReduced cost=" << _state[in_arc] * (_cost[in_arc] + _pi[_source[in_arc]] -_pi[_target[in_arc]]) << "\nPrecision = "<< -EPSILON*(a) << "\n"; |
| 1633 |
|
| 1634 |
std::cout << "Arc in = (" << _node_id(_source[in_arc]) << ", " << _node_id(_target[in_arc]) <<")\n"; |
| 1635 |
std::cout << "Supplies = (" << _supply[_source[in_arc]] << ", " << _supply[_target[in_arc]] << ")\n"; |
| 1636 |
|
| 1637 |
#endif |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
#if DEBUG_LVL>1 |
| 1642 |
sumFlow=0; |
| 1643 |
for (int i=0; i<_flow.size(); i++) { |
| 1644 |
sumFlow+=_state[i]*_flow[i]; |
| 1645 |
if (_state[i]==STATE_TREE) { |
| 1646 |
std::cout << "Non zero value at (" << _node_num+1-_source[i] << ", " << _node_num+1-_target[i] << ")\n"; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
std::cout << "Sum of the flow " << sumFlow << "\n"<< niter <<" iterations, current cost=" << totalCost() << "\n"; |
| 1650 |
#endif |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
if(retVal == OPTIMAL){ |
| 1656 |
for (ArcsType e = _search_arc_num; e != _all_arc_num; ++e) { |
| 1657 |
if (_flow[e] != 0){ |
| 1658 |
if (fabs(_flow[e]) > _EPSILON) |
| 1659 |
return INFEASIBLE; |
| 1660 |
else |
| 1661 |
_flow[e]=0; |
| 1662 |
} |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
if (_sum_supply == 0) { |
| 1669 |
if (_stype == GEQ) { |
| 1670 |
Cost max_pot = -std::numeric_limits<Cost>::max(); |
| 1671 |
for (ArcsType i = 0; i != _node_num; ++i) { |
| 1672 |
if (_pi[i] > max_pot) max_pot = _pi[i]; |
| 1673 |
} |
| 1674 |
if (max_pot > 0) { |
| 1675 |
for (ArcsType i = 0; i != _node_num; ++i) |
| 1676 |
_pi[i] -= max_pot; |
| 1677 |
} |
| 1678 |
} else { |
| 1679 |
Cost min_pot = std::numeric_limits<Cost>::max(); |
| 1680 |
for (ArcsType i = 0; i != _node_num; ++i) { |
| 1681 |
if (_pi[i] < min_pot) min_pot = _pi[i]; |
| 1682 |
} |
| 1683 |
if (min_pot < 0) { |
| 1684 |
for (ArcsType i = 0; i != _node_num; ++i) |
| 1685 |
_pi[i] -= min_pot; |
| 1686 |
} |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
return retVal; |
| 1691 |
} |
| 1692 |
|
| 1693 |
}; |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
} |