| ... | ... |
@@ -298,399 +298,400 @@ |
| 298 | 298 |
|
| 299 | 299 |
public: |
| 300 | 300 |
|
| 301 | 301 |
/// \name Named Template Parameters |
| 302 | 302 |
/// @{
|
| 303 | 303 |
|
| 304 | 304 |
template <typename T> |
| 305 | 305 |
struct SetLargeCostTraits : public Traits {
|
| 306 | 306 |
typedef T LargeCost; |
| 307 | 307 |
}; |
| 308 | 308 |
|
| 309 | 309 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 310 | 310 |
/// \c LargeCost type. |
| 311 | 311 |
/// |
| 312 | 312 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
| 313 | 313 |
/// type, which is used for internal computations in the algorithm. |
| 314 | 314 |
/// \c Cost must be convertible to \c LargeCost. |
| 315 | 315 |
template <typename T> |
| 316 | 316 |
struct SetLargeCost |
| 317 | 317 |
: public CostScaling<GR, V, C, SetLargeCostTraits<T> > {
|
| 318 | 318 |
typedef CostScaling<GR, V, C, SetLargeCostTraits<T> > Create; |
| 319 | 319 |
}; |
| 320 | 320 |
|
| 321 | 321 |
/// @} |
| 322 | 322 |
|
| 323 | 323 |
protected: |
| 324 | 324 |
|
| 325 | 325 |
CostScaling() {}
|
| 326 | 326 |
|
| 327 | 327 |
public: |
| 328 | 328 |
|
| 329 | 329 |
/// \brief Constructor. |
| 330 | 330 |
/// |
| 331 | 331 |
/// The constructor of the class. |
| 332 | 332 |
/// |
| 333 | 333 |
/// \param graph The digraph the algorithm runs on. |
| 334 | 334 |
CostScaling(const GR& graph) : |
| 335 | 335 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 336 | 336 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 337 | 337 |
std::numeric_limits<Value>::infinity() : |
| 338 | 338 |
std::numeric_limits<Value>::max()) |
| 339 | 339 |
{
|
| 340 | 340 |
// Check the number types |
| 341 | 341 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 342 | 342 |
"The flow type of CostScaling must be signed"); |
| 343 | 343 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 344 | 344 |
"The cost type of CostScaling must be signed"); |
| 345 | 345 |
|
| 346 | 346 |
// Reset data structures |
| 347 | 347 |
reset(); |
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
/// \name Parameters |
| 351 | 351 |
/// The parameters of the algorithm can be specified using these |
| 352 | 352 |
/// functions. |
| 353 | 353 |
|
| 354 | 354 |
/// @{
|
| 355 | 355 |
|
| 356 | 356 |
/// \brief Set the lower bounds on the arcs. |
| 357 | 357 |
/// |
| 358 | 358 |
/// This function sets the lower bounds on the arcs. |
| 359 | 359 |
/// If it is not used before calling \ref run(), the lower bounds |
| 360 | 360 |
/// will be set to zero on all arcs. |
| 361 | 361 |
/// |
| 362 | 362 |
/// \param map An arc map storing the lower bounds. |
| 363 | 363 |
/// Its \c Value type must be convertible to the \c Value type |
| 364 | 364 |
/// of the algorithm. |
| 365 | 365 |
/// |
| 366 | 366 |
/// \return <tt>(*this)</tt> |
| 367 | 367 |
template <typename LowerMap> |
| 368 | 368 |
CostScaling& lowerMap(const LowerMap& map) {
|
| 369 | 369 |
_have_lower = true; |
| 370 | 370 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 371 | 371 |
_lower[_arc_idf[a]] = map[a]; |
| 372 | 372 |
_lower[_arc_idb[a]] = map[a]; |
| 373 | 373 |
} |
| 374 | 374 |
return *this; |
| 375 | 375 |
} |
| 376 | 376 |
|
| 377 | 377 |
/// \brief Set the upper bounds (capacities) on the arcs. |
| 378 | 378 |
/// |
| 379 | 379 |
/// This function sets the upper bounds (capacities) on the arcs. |
| 380 | 380 |
/// If it is not used before calling \ref run(), the upper bounds |
| 381 | 381 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
| 382 | 382 |
/// unbounded from above). |
| 383 | 383 |
/// |
| 384 | 384 |
/// \param map An arc map storing the upper bounds. |
| 385 | 385 |
/// Its \c Value type must be convertible to the \c Value type |
| 386 | 386 |
/// of the algorithm. |
| 387 | 387 |
/// |
| 388 | 388 |
/// \return <tt>(*this)</tt> |
| 389 | 389 |
template<typename UpperMap> |
| 390 | 390 |
CostScaling& upperMap(const UpperMap& map) {
|
| 391 | 391 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 392 | 392 |
_upper[_arc_idf[a]] = map[a]; |
| 393 | 393 |
} |
| 394 | 394 |
return *this; |
| 395 | 395 |
} |
| 396 | 396 |
|
| 397 | 397 |
/// \brief Set the costs of the arcs. |
| 398 | 398 |
/// |
| 399 | 399 |
/// This function sets the costs of the arcs. |
| 400 | 400 |
/// If it is not used before calling \ref run(), the costs |
| 401 | 401 |
/// will be set to \c 1 on all arcs. |
| 402 | 402 |
/// |
| 403 | 403 |
/// \param map An arc map storing the costs. |
| 404 | 404 |
/// Its \c Value type must be convertible to the \c Cost type |
| 405 | 405 |
/// of the algorithm. |
| 406 | 406 |
/// |
| 407 | 407 |
/// \return <tt>(*this)</tt> |
| 408 | 408 |
template<typename CostMap> |
| 409 | 409 |
CostScaling& costMap(const CostMap& map) {
|
| 410 | 410 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 411 | 411 |
_scost[_arc_idf[a]] = map[a]; |
| 412 | 412 |
_scost[_arc_idb[a]] = -map[a]; |
| 413 | 413 |
} |
| 414 | 414 |
return *this; |
| 415 | 415 |
} |
| 416 | 416 |
|
| 417 | 417 |
/// \brief Set the supply values of the nodes. |
| 418 | 418 |
/// |
| 419 | 419 |
/// This function sets the supply values of the nodes. |
| 420 | 420 |
/// If neither this function nor \ref stSupply() is used before |
| 421 | 421 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 422 | 422 |
/// |
| 423 | 423 |
/// \param map A node map storing the supply values. |
| 424 | 424 |
/// Its \c Value type must be convertible to the \c Value type |
| 425 | 425 |
/// of the algorithm. |
| 426 | 426 |
/// |
| 427 | 427 |
/// \return <tt>(*this)</tt> |
| 428 | 428 |
template<typename SupplyMap> |
| 429 | 429 |
CostScaling& supplyMap(const SupplyMap& map) {
|
| 430 | 430 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 431 | 431 |
_supply[_node_id[n]] = map[n]; |
| 432 | 432 |
} |
| 433 | 433 |
return *this; |
| 434 | 434 |
} |
| 435 | 435 |
|
| 436 | 436 |
/// \brief Set single source and target nodes and a supply value. |
| 437 | 437 |
/// |
| 438 | 438 |
/// This function sets a single source node and a single target node |
| 439 | 439 |
/// and the required flow value. |
| 440 | 440 |
/// If neither this function nor \ref supplyMap() is used before |
| 441 | 441 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 442 | 442 |
/// |
| 443 | 443 |
/// Using this function has the same effect as using \ref supplyMap() |
| 444 | 444 |
/// with a map in which \c k is assigned to \c s, \c -k is |
| 445 | 445 |
/// assigned to \c t and all other nodes have zero supply value. |
| 446 | 446 |
/// |
| 447 | 447 |
/// \param s The source node. |
| 448 | 448 |
/// \param t The target node. |
| 449 | 449 |
/// \param k The required amount of flow from node \c s to node \c t |
| 450 | 450 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 451 | 451 |
/// |
| 452 | 452 |
/// \return <tt>(*this)</tt> |
| 453 | 453 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 454 | 454 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 455 | 455 |
_supply[i] = 0; |
| 456 | 456 |
} |
| 457 | 457 |
_supply[_node_id[s]] = k; |
| 458 | 458 |
_supply[_node_id[t]] = -k; |
| 459 | 459 |
return *this; |
| 460 | 460 |
} |
| 461 | 461 |
|
| 462 | 462 |
/// @} |
| 463 | 463 |
|
| 464 | 464 |
/// \name Execution control |
| 465 | 465 |
/// The algorithm can be executed using \ref run(). |
| 466 | 466 |
|
| 467 | 467 |
/// @{
|
| 468 | 468 |
|
| 469 | 469 |
/// \brief Run the algorithm. |
| 470 | 470 |
/// |
| 471 | 471 |
/// This function runs the algorithm. |
| 472 | 472 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 473 | 473 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 474 | 474 |
/// For example, |
| 475 | 475 |
/// \code |
| 476 | 476 |
/// CostScaling<ListDigraph> cs(graph); |
| 477 | 477 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 478 | 478 |
/// .supplyMap(sup).run(); |
| 479 | 479 |
/// \endcode |
| 480 | 480 |
/// |
| 481 | 481 |
/// This function can be called more than once. All the given parameters |
| 482 | 482 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 483 | 483 |
/// is used, thus only the modified parameters have to be set again. |
| 484 | 484 |
/// If the underlying digraph was also modified after the construction |
| 485 | 485 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 486 | 486 |
/// function must be called. |
| 487 | 487 |
/// |
| 488 | 488 |
/// \param method The internal method that will be used in the |
| 489 | 489 |
/// algorithm. For more information, see \ref Method. |
| 490 |
/// \param factor The cost scaling factor. It must be |
|
| 490 |
/// \param factor The cost scaling factor. It must be at least two. |
|
| 491 | 491 |
/// |
| 492 | 492 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 493 | 493 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 494 | 494 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 495 | 495 |
/// optimal flow and node potentials (primal and dual solutions), |
| 496 | 496 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
| 497 | 497 |
/// and infinite upper bound. It means that the objective function |
| 498 | 498 |
/// is unbounded on that arc, however, note that it could actually be |
| 499 | 499 |
/// bounded over the feasible flows, but this algroithm cannot handle |
| 500 | 500 |
/// these cases. |
| 501 | 501 |
/// |
| 502 | 502 |
/// \see ProblemType, Method |
| 503 | 503 |
/// \see resetParams(), reset() |
| 504 |
ProblemType run(Method method = PARTIAL_AUGMENT, int factor = |
|
| 504 |
ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 16) {
|
|
| 505 |
LEMON_ASSERT(factor >= 2, "The scaling factor must be at least 2"); |
|
| 505 | 506 |
_alpha = factor; |
| 506 | 507 |
ProblemType pt = init(); |
| 507 | 508 |
if (pt != OPTIMAL) return pt; |
| 508 | 509 |
start(method); |
| 509 | 510 |
return OPTIMAL; |
| 510 | 511 |
} |
| 511 | 512 |
|
| 512 | 513 |
/// \brief Reset all the parameters that have been given before. |
| 513 | 514 |
/// |
| 514 | 515 |
/// This function resets all the paramaters that have been given |
| 515 | 516 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 516 | 517 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 517 | 518 |
/// |
| 518 | 519 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
| 519 | 520 |
/// parameters are kept for the next \ref run() call, unless |
| 520 | 521 |
/// \ref resetParams() or \ref reset() is used. |
| 521 | 522 |
/// If the underlying digraph was also modified after the construction |
| 522 | 523 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 523 | 524 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 524 | 525 |
/// |
| 525 | 526 |
/// For example, |
| 526 | 527 |
/// \code |
| 527 | 528 |
/// CostScaling<ListDigraph> cs(graph); |
| 528 | 529 |
/// |
| 529 | 530 |
/// // First run |
| 530 | 531 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 531 | 532 |
/// .supplyMap(sup).run(); |
| 532 | 533 |
/// |
| 533 | 534 |
/// // Run again with modified cost map (resetParams() is not called, |
| 534 | 535 |
/// // so only the cost map have to be set again) |
| 535 | 536 |
/// cost[e] += 100; |
| 536 | 537 |
/// cs.costMap(cost).run(); |
| 537 | 538 |
/// |
| 538 | 539 |
/// // Run again from scratch using resetParams() |
| 539 | 540 |
/// // (the lower bounds will be set to zero on all arcs) |
| 540 | 541 |
/// cs.resetParams(); |
| 541 | 542 |
/// cs.upperMap(capacity).costMap(cost) |
| 542 | 543 |
/// .supplyMap(sup).run(); |
| 543 | 544 |
/// \endcode |
| 544 | 545 |
/// |
| 545 | 546 |
/// \return <tt>(*this)</tt> |
| 546 | 547 |
/// |
| 547 | 548 |
/// \see reset(), run() |
| 548 | 549 |
CostScaling& resetParams() {
|
| 549 | 550 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 550 | 551 |
_supply[i] = 0; |
| 551 | 552 |
} |
| 552 | 553 |
int limit = _first_out[_root]; |
| 553 | 554 |
for (int j = 0; j != limit; ++j) {
|
| 554 | 555 |
_lower[j] = 0; |
| 555 | 556 |
_upper[j] = INF; |
| 556 | 557 |
_scost[j] = _forward[j] ? 1 : -1; |
| 557 | 558 |
} |
| 558 | 559 |
for (int j = limit; j != _res_arc_num; ++j) {
|
| 559 | 560 |
_lower[j] = 0; |
| 560 | 561 |
_upper[j] = INF; |
| 561 | 562 |
_scost[j] = 0; |
| 562 | 563 |
_scost[_reverse[j]] = 0; |
| 563 | 564 |
} |
| 564 | 565 |
_have_lower = false; |
| 565 | 566 |
return *this; |
| 566 | 567 |
} |
| 567 | 568 |
|
| 568 | 569 |
/// \brief Reset the internal data structures and all the parameters |
| 569 | 570 |
/// that have been given before. |
| 570 | 571 |
/// |
| 571 | 572 |
/// This function resets the internal data structures and all the |
| 572 | 573 |
/// paramaters that have been given before using functions \ref lowerMap(), |
| 573 | 574 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 574 | 575 |
/// |
| 575 | 576 |
/// It is useful for multiple \ref run() calls. By default, all the given |
| 576 | 577 |
/// parameters are kept for the next \ref run() call, unless |
| 577 | 578 |
/// \ref resetParams() or \ref reset() is used. |
| 578 | 579 |
/// If the underlying digraph was also modified after the construction |
| 579 | 580 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 580 | 581 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 581 | 582 |
/// |
| 582 | 583 |
/// See \ref resetParams() for examples. |
| 583 | 584 |
/// |
| 584 | 585 |
/// \return <tt>(*this)</tt> |
| 585 | 586 |
/// |
| 586 | 587 |
/// \see resetParams(), run() |
| 587 | 588 |
CostScaling& reset() {
|
| 588 | 589 |
// Resize vectors |
| 589 | 590 |
_node_num = countNodes(_graph); |
| 590 | 591 |
_arc_num = countArcs(_graph); |
| 591 | 592 |
_res_node_num = _node_num + 1; |
| 592 | 593 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 593 | 594 |
_root = _node_num; |
| 594 | 595 |
|
| 595 | 596 |
_first_out.resize(_res_node_num + 1); |
| 596 | 597 |
_forward.resize(_res_arc_num); |
| 597 | 598 |
_source.resize(_res_arc_num); |
| 598 | 599 |
_target.resize(_res_arc_num); |
| 599 | 600 |
_reverse.resize(_res_arc_num); |
| 600 | 601 |
|
| 601 | 602 |
_lower.resize(_res_arc_num); |
| 602 | 603 |
_upper.resize(_res_arc_num); |
| 603 | 604 |
_scost.resize(_res_arc_num); |
| 604 | 605 |
_supply.resize(_res_node_num); |
| 605 | 606 |
|
| 606 | 607 |
_res_cap.resize(_res_arc_num); |
| 607 | 608 |
_cost.resize(_res_arc_num); |
| 608 | 609 |
_pi.resize(_res_node_num); |
| 609 | 610 |
_excess.resize(_res_node_num); |
| 610 | 611 |
_next_out.resize(_res_node_num); |
| 611 | 612 |
|
| 612 | 613 |
// Copy the graph |
| 613 | 614 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num; |
| 614 | 615 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 615 | 616 |
_node_id[n] = i; |
| 616 | 617 |
} |
| 617 | 618 |
i = 0; |
| 618 | 619 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 619 | 620 |
_first_out[i] = j; |
| 620 | 621 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 621 | 622 |
_arc_idf[a] = j; |
| 622 | 623 |
_forward[j] = true; |
| 623 | 624 |
_source[j] = i; |
| 624 | 625 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 625 | 626 |
} |
| 626 | 627 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 627 | 628 |
_arc_idb[a] = j; |
| 628 | 629 |
_forward[j] = false; |
| 629 | 630 |
_source[j] = i; |
| 630 | 631 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 631 | 632 |
} |
| 632 | 633 |
_forward[j] = false; |
| 633 | 634 |
_source[j] = i; |
| 634 | 635 |
_target[j] = _root; |
| 635 | 636 |
_reverse[j] = k; |
| 636 | 637 |
_forward[k] = true; |
| 637 | 638 |
_source[k] = _root; |
| 638 | 639 |
_target[k] = i; |
| 639 | 640 |
_reverse[k] = j; |
| 640 | 641 |
++j; ++k; |
| 641 | 642 |
} |
| 642 | 643 |
_first_out[i] = j; |
| 643 | 644 |
_first_out[_res_node_num] = k; |
| 644 | 645 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 645 | 646 |
int fi = _arc_idf[a]; |
| 646 | 647 |
int bi = _arc_idb[a]; |
| 647 | 648 |
_reverse[fi] = bi; |
| 648 | 649 |
_reverse[bi] = fi; |
| 649 | 650 |
} |
| 650 | 651 |
|
| 651 | 652 |
// Reset parameters |
| 652 | 653 |
resetParams(); |
| 653 | 654 |
return *this; |
| 654 | 655 |
} |
| 655 | 656 |
|
| 656 | 657 |
/// @} |
| 657 | 658 |
|
| 658 | 659 |
/// \name Query Functions |
| 659 | 660 |
/// The results of the algorithm can be obtained using these |
| 660 | 661 |
/// functions.\n |
| 661 | 662 |
/// The \ref run() function must be called before using them. |
| 662 | 663 |
|
| 663 | 664 |
/// @{
|
| 664 | 665 |
|
| 665 | 666 |
/// \brief Return the total cost of the found flow. |
| 666 | 667 |
/// |
| 667 | 668 |
/// This function returns the total cost of the found flow. |
| 668 | 669 |
/// Its complexity is O(e). |
| 669 | 670 |
/// |
| 670 | 671 |
/// \note The return type of the function can be specified as a |
| 671 | 672 |
/// template parameter. For example, |
| 672 | 673 |
/// \code |
| 673 | 674 |
/// cs.totalCost<double>(); |
| 674 | 675 |
/// \endcode |
| 675 | 676 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 676 | 677 |
/// type of the algorithm, which is the default return type of the |
| 677 | 678 |
/// function. |
| 678 | 679 |
/// |
| 679 | 680 |
/// \pre \ref run() must be called before using this function. |
| 680 | 681 |
template <typename Number> |
| 681 | 682 |
Number totalCost() const {
|
| 682 | 683 |
Number c = 0; |
| 683 | 684 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 684 | 685 |
int i = _arc_idb[a]; |
| 685 | 686 |
c += static_cast<Number>(_res_cap[i]) * |
| 686 | 687 |
(-static_cast<Number>(_scost[i])); |
| 687 | 688 |
} |
| 688 | 689 |
return c; |
| 689 | 690 |
} |
| 690 | 691 |
|
| 691 | 692 |
#ifndef DOXYGEN |
| 692 | 693 |
Cost totalCost() const {
|
| 693 | 694 |
return totalCost<Cost>(); |
| 694 | 695 |
} |
| 695 | 696 |
#endif |
| 696 | 697 |
|
0 comments (0 inline)