Changes in lemon/cost_scaling.h [839:f3bc4e9b5f3a:840:2914b6f0fde0] in lemon-main
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/cost_scaling.h
r839 r840 105 105 /// \tparam GR The digraph type the algorithm runs on. 106 106 /// \tparam V The number type used for flow amounts, capacity bounds 107 /// and supply values in the algorithm. By default it is \c int.107 /// and supply values in the algorithm. By default, it is \c int. 108 108 /// \tparam C The number type used for costs and potentials in the 109 /// algorithm. By default it is the same as \c V. 109 /// algorithm. By default, it is the same as \c V. 110 /// \tparam TR The traits class that defines various types used by the 111 /// algorithm. By default, it is \ref CostScalingDefaultTraits 112 /// "CostScalingDefaultTraits<GR, V, C>". 113 /// In most cases, this parameter should not be set directly, 114 /// consider to use the named template parameters instead. 110 115 /// 111 116 /// \warning Both number types must be signed and all input data must … … 137 142 /// 138 143 /// The large cost type used for internal computations. 139 /// Using the \ref CostScalingDefaultTraits "default traits class", 140 /// it is \c long \c long if the \c Cost type is integer, 144 /// By default, it is \c long \c long if the \c Cost type is integer, 141 145 /// otherwise it is \c double. 142 146 typedef typename TR::LargeCost LargeCost; … … 341 345 LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, 342 346 "The cost type of CostScaling must be signed"); 343 347 348 // Reset data structures 349 reset(); 350 } 351 352 /// \name Parameters 353 /// The parameters of the algorithm can be specified using these 354 /// functions. 355 356 /// @{ 357 358 /// \brief Set the lower bounds on the arcs. 359 /// 360 /// This function sets the lower bounds on the arcs. 361 /// If it is not used before calling \ref run(), the lower bounds 362 /// will be set to zero on all arcs. 363 /// 364 /// \param map An arc map storing the lower bounds. 365 /// Its \c Value type must be convertible to the \c Value type 366 /// of the algorithm. 367 /// 368 /// \return <tt>(*this)</tt> 369 template <typename LowerMap> 370 CostScaling& lowerMap(const LowerMap& map) { 371 _have_lower = true; 372 for (ArcIt a(_graph); a != INVALID; ++a) { 373 _lower[_arc_idf[a]] = map[a]; 374 _lower[_arc_idb[a]] = map[a]; 375 } 376 return *this; 377 } 378 379 /// \brief Set the upper bounds (capacities) on the arcs. 380 /// 381 /// This function sets the upper bounds (capacities) on the arcs. 382 /// If it is not used before calling \ref run(), the upper bounds 383 /// will be set to \ref INF on all arcs (i.e. the flow value will be 384 /// unbounded from above). 385 /// 386 /// \param map An arc map storing the upper bounds. 387 /// Its \c Value type must be convertible to the \c Value type 388 /// of the algorithm. 389 /// 390 /// \return <tt>(*this)</tt> 391 template<typename UpperMap> 392 CostScaling& upperMap(const UpperMap& map) { 393 for (ArcIt a(_graph); a != INVALID; ++a) { 394 _upper[_arc_idf[a]] = map[a]; 395 } 396 return *this; 397 } 398 399 /// \brief Set the costs of the arcs. 400 /// 401 /// This function sets the costs of the arcs. 402 /// If it is not used before calling \ref run(), the costs 403 /// will be set to \c 1 on all arcs. 404 /// 405 /// \param map An arc map storing the costs. 406 /// Its \c Value type must be convertible to the \c Cost type 407 /// of the algorithm. 408 /// 409 /// \return <tt>(*this)</tt> 410 template<typename CostMap> 411 CostScaling& costMap(const CostMap& map) { 412 for (ArcIt a(_graph); a != INVALID; ++a) { 413 _scost[_arc_idf[a]] = map[a]; 414 _scost[_arc_idb[a]] = -map[a]; 415 } 416 return *this; 417 } 418 419 /// \brief Set the supply values of the nodes. 420 /// 421 /// This function sets the supply values of the nodes. 422 /// If neither this function nor \ref stSupply() is used before 423 /// calling \ref run(), the supply of each node will be set to zero. 424 /// 425 /// \param map A node map storing the supply values. 426 /// Its \c Value type must be convertible to the \c Value type 427 /// of the algorithm. 428 /// 429 /// \return <tt>(*this)</tt> 430 template<typename SupplyMap> 431 CostScaling& supplyMap(const SupplyMap& map) { 432 for (NodeIt n(_graph); n != INVALID; ++n) { 433 _supply[_node_id[n]] = map[n]; 434 } 435 return *this; 436 } 437 438 /// \brief Set single source and target nodes and a supply value. 439 /// 440 /// This function sets a single source node and a single target node 441 /// and the required flow value. 442 /// If neither this function nor \ref supplyMap() is used before 443 /// calling \ref run(), the supply of each node will be set to zero. 444 /// 445 /// Using this function has the same effect as using \ref supplyMap() 446 /// with such a map in which \c k is assigned to \c s, \c -k is 447 /// assigned to \c t and all other nodes have zero supply value. 448 /// 449 /// \param s The source node. 450 /// \param t The target node. 451 /// \param k The required amount of flow from node \c s to node \c t 452 /// (i.e. the supply of \c s and the demand of \c t). 453 /// 454 /// \return <tt>(*this)</tt> 455 CostScaling& stSupply(const Node& s, const Node& t, Value k) { 456 for (int i = 0; i != _res_node_num; ++i) { 457 _supply[i] = 0; 458 } 459 _supply[_node_id[s]] = k; 460 _supply[_node_id[t]] = -k; 461 return *this; 462 } 463 464 /// @} 465 466 /// \name Execution control 467 /// The algorithm can be executed using \ref run(). 468 469 /// @{ 470 471 /// \brief Run the algorithm. 472 /// 473 /// This function runs the algorithm. 474 /// The paramters can be specified using functions \ref lowerMap(), 475 /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). 476 /// For example, 477 /// \code 478 /// CostScaling<ListDigraph> cs(graph); 479 /// cs.lowerMap(lower).upperMap(upper).costMap(cost) 480 /// .supplyMap(sup).run(); 481 /// \endcode 482 /// 483 /// This function can be called more than once. All the given parameters 484 /// are kept for the next call, unless \ref resetParams() or \ref reset() 485 /// is used, thus only the modified parameters have to be set again. 486 /// If the underlying digraph was also modified after the construction 487 /// of the class (or the last \ref reset() call), then the \ref reset() 488 /// function must be called. 489 /// 490 /// \param method The internal method that will be used in the 491 /// algorithm. For more information, see \ref Method. 492 /// \param factor The cost scaling factor. It must be larger than one. 493 /// 494 /// \return \c INFEASIBLE if no feasible flow exists, 495 /// \n \c OPTIMAL if the problem has optimal solution 496 /// (i.e. it is feasible and bounded), and the algorithm has found 497 /// optimal flow and node potentials (primal and dual solutions), 498 /// \n \c UNBOUNDED if the digraph contains an arc of negative cost 499 /// and infinite upper bound. It means that the objective function 500 /// is unbounded on that arc, however, note that it could actually be 501 /// bounded over the feasible flows, but this algroithm cannot handle 502 /// these cases. 503 /// 504 /// \see ProblemType, Method 505 /// \see resetParams(), reset() 506 ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) { 507 _alpha = factor; 508 ProblemType pt = init(); 509 if (pt != OPTIMAL) return pt; 510 start(method); 511 return OPTIMAL; 512 } 513 514 /// \brief Reset all the parameters that have been given before. 515 /// 516 /// This function resets all the paramaters that have been given 517 /// before using functions \ref lowerMap(), \ref upperMap(), 518 /// \ref costMap(), \ref supplyMap(), \ref stSupply(). 519 /// 520 /// It is useful for multiple \ref run() calls. Basically, all the given 521 /// parameters are kept for the next \ref run() call, unless 522 /// \ref resetParams() or \ref reset() is used. 523 /// If the underlying digraph was also modified after the construction 524 /// of the class or the last \ref reset() call, then the \ref reset() 525 /// function must be used, otherwise \ref resetParams() is sufficient. 526 /// 527 /// For example, 528 /// \code 529 /// CostScaling<ListDigraph> cs(graph); 530 /// 531 /// // First run 532 /// cs.lowerMap(lower).upperMap(upper).costMap(cost) 533 /// .supplyMap(sup).run(); 534 /// 535 /// // Run again with modified cost map (resetParams() is not called, 536 /// // so only the cost map have to be set again) 537 /// cost[e] += 100; 538 /// cs.costMap(cost).run(); 539 /// 540 /// // Run again from scratch using resetParams() 541 /// // (the lower bounds will be set to zero on all arcs) 542 /// cs.resetParams(); 543 /// cs.upperMap(capacity).costMap(cost) 544 /// .supplyMap(sup).run(); 545 /// \endcode 546 /// 547 /// \return <tt>(*this)</tt> 548 /// 549 /// \see reset(), run() 550 CostScaling& resetParams() { 551 for (int i = 0; i != _res_node_num; ++i) { 552 _supply[i] = 0; 553 } 554 int limit = _first_out[_root]; 555 for (int j = 0; j != limit; ++j) { 556 _lower[j] = 0; 557 _upper[j] = INF; 558 _scost[j] = _forward[j] ? 1 : -1; 559 } 560 for (int j = limit; j != _res_arc_num; ++j) { 561 _lower[j] = 0; 562 _upper[j] = INF; 563 _scost[j] = 0; 564 _scost[_reverse[j]] = 0; 565 } 566 _have_lower = false; 567 return *this; 568 } 569 570 /// \brief Reset all the parameters that have been given before. 571 /// 572 /// This function resets all the paramaters that have been given 573 /// before using functions \ref lowerMap(), \ref upperMap(), 574 /// \ref costMap(), \ref supplyMap(), \ref stSupply(). 575 /// 576 /// It is useful for multiple run() calls. If this function is not 577 /// used, all the parameters given before are kept for the next 578 /// \ref run() call. 579 /// However, the underlying digraph must not be modified after this 580 /// class have been constructed, since it copies and extends the graph. 581 /// \return <tt>(*this)</tt> 582 CostScaling& reset() { 344 583 // Resize vectors 345 584 _node_num = countNodes(_graph); … … 409 648 410 649 // Reset parameters 411 reset(); 412 } 413 414 /// \name Parameters 415 /// The parameters of the algorithm can be specified using these 416 /// functions. 417 418 /// @{ 419 420 /// \brief Set the lower bounds on the arcs. 421 /// 422 /// This function sets the lower bounds on the arcs. 423 /// If it is not used before calling \ref run(), the lower bounds 424 /// will be set to zero on all arcs. 425 /// 426 /// \param map An arc map storing the lower bounds. 427 /// Its \c Value type must be convertible to the \c Value type 428 /// of the algorithm. 429 /// 430 /// \return <tt>(*this)</tt> 431 template <typename LowerMap> 432 CostScaling& lowerMap(const LowerMap& map) { 433 _have_lower = true; 434 for (ArcIt a(_graph); a != INVALID; ++a) { 435 _lower[_arc_idf[a]] = map[a]; 436 _lower[_arc_idb[a]] = map[a]; 437 } 438 return *this; 439 } 440 441 /// \brief Set the upper bounds (capacities) on the arcs. 442 /// 443 /// This function sets the upper bounds (capacities) on the arcs. 444 /// If it is not used before calling \ref run(), the upper bounds 445 /// will be set to \ref INF on all arcs (i.e. the flow value will be 446 /// unbounded from above). 447 /// 448 /// \param map An arc map storing the upper bounds. 449 /// Its \c Value type must be convertible to the \c Value type 450 /// of the algorithm. 451 /// 452 /// \return <tt>(*this)</tt> 453 template<typename UpperMap> 454 CostScaling& upperMap(const UpperMap& map) { 455 for (ArcIt a(_graph); a != INVALID; ++a) { 456 _upper[_arc_idf[a]] = map[a]; 457 } 458 return *this; 459 } 460 461 /// \brief Set the costs of the arcs. 462 /// 463 /// This function sets the costs of the arcs. 464 /// If it is not used before calling \ref run(), the costs 465 /// will be set to \c 1 on all arcs. 466 /// 467 /// \param map An arc map storing the costs. 468 /// Its \c Value type must be convertible to the \c Cost type 469 /// of the algorithm. 470 /// 471 /// \return <tt>(*this)</tt> 472 template<typename CostMap> 473 CostScaling& costMap(const CostMap& map) { 474 for (ArcIt a(_graph); a != INVALID; ++a) { 475 _scost[_arc_idf[a]] = map[a]; 476 _scost[_arc_idb[a]] = -map[a]; 477 } 478 return *this; 479 } 480 481 /// \brief Set the supply values of the nodes. 482 /// 483 /// This function sets the supply values of the nodes. 484 /// If neither this function nor \ref stSupply() is used before 485 /// calling \ref run(), the supply of each node will be set to zero. 486 /// 487 /// \param map A node map storing the supply values. 488 /// Its \c Value type must be convertible to the \c Value type 489 /// of the algorithm. 490 /// 491 /// \return <tt>(*this)</tt> 492 template<typename SupplyMap> 493 CostScaling& supplyMap(const SupplyMap& map) { 494 for (NodeIt n(_graph); n != INVALID; ++n) { 495 _supply[_node_id[n]] = map[n]; 496 } 497 return *this; 498 } 499 500 /// \brief Set single source and target nodes and a supply value. 501 /// 502 /// This function sets a single source node and a single target node 503 /// and the required flow value. 504 /// If neither this function nor \ref supplyMap() is used before 505 /// calling \ref run(), the supply of each node will be set to zero. 506 /// 507 /// Using this function has the same effect as using \ref supplyMap() 508 /// with such a map in which \c k is assigned to \c s, \c -k is 509 /// assigned to \c t and all other nodes have zero supply value. 510 /// 511 /// \param s The source node. 512 /// \param t The target node. 513 /// \param k The required amount of flow from node \c s to node \c t 514 /// (i.e. the supply of \c s and the demand of \c t). 515 /// 516 /// \return <tt>(*this)</tt> 517 CostScaling& stSupply(const Node& s, const Node& t, Value k) { 518 for (int i = 0; i != _res_node_num; ++i) { 519 _supply[i] = 0; 520 } 521 _supply[_node_id[s]] = k; 522 _supply[_node_id[t]] = -k; 523 return *this; 524 } 525 526 /// @} 527 528 /// \name Execution control 529 /// The algorithm can be executed using \ref run(). 530 531 /// @{ 532 533 /// \brief Run the algorithm. 534 /// 535 /// This function runs the algorithm. 536 /// The paramters can be specified using functions \ref lowerMap(), 537 /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). 538 /// For example, 539 /// \code 540 /// CostScaling<ListDigraph> cs(graph); 541 /// cs.lowerMap(lower).upperMap(upper).costMap(cost) 542 /// .supplyMap(sup).run(); 543 /// \endcode 544 /// 545 /// This function can be called more than once. All the parameters 546 /// that have been given are kept for the next call, unless 547 /// \ref reset() is called, thus only the modified parameters 548 /// have to be set again. See \ref reset() for examples. 549 /// However, the underlying digraph must not be modified after this 550 /// class have been constructed, since it copies and extends the graph. 551 /// 552 /// \param method The internal method that will be used in the 553 /// algorithm. For more information, see \ref Method. 554 /// \param factor The cost scaling factor. It must be larger than one. 555 /// 556 /// \return \c INFEASIBLE if no feasible flow exists, 557 /// \n \c OPTIMAL if the problem has optimal solution 558 /// (i.e. it is feasible and bounded), and the algorithm has found 559 /// optimal flow and node potentials (primal and dual solutions), 560 /// \n \c UNBOUNDED if the digraph contains an arc of negative cost 561 /// and infinite upper bound. It means that the objective function 562 /// is unbounded on that arc, however, note that it could actually be 563 /// bounded over the feasible flows, but this algroithm cannot handle 564 /// these cases. 565 /// 566 /// \see ProblemType, Method 567 ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) { 568 _alpha = factor; 569 ProblemType pt = init(); 570 if (pt != OPTIMAL) return pt; 571 start(method); 572 return OPTIMAL; 573 } 574 575 /// \brief Reset all the parameters that have been given before. 576 /// 577 /// This function resets all the paramaters that have been given 578 /// before using functions \ref lowerMap(), \ref upperMap(), 579 /// \ref costMap(), \ref supplyMap(), \ref stSupply(). 580 /// 581 /// It is useful for multiple run() calls. If this function is not 582 /// used, all the parameters given before are kept for the next 583 /// \ref run() call. 584 /// However, the underlying digraph must not be modified after this 585 /// class have been constructed, since it copies and extends the graph. 586 /// 587 /// For example, 588 /// \code 589 /// CostScaling<ListDigraph> cs(graph); 590 /// 591 /// // First run 592 /// cs.lowerMap(lower).upperMap(upper).costMap(cost) 593 /// .supplyMap(sup).run(); 594 /// 595 /// // Run again with modified cost map (reset() is not called, 596 /// // so only the cost map have to be set again) 597 /// cost[e] += 100; 598 /// cs.costMap(cost).run(); 599 /// 600 /// // Run again from scratch using reset() 601 /// // (the lower bounds will be set to zero on all arcs) 602 /// cs.reset(); 603 /// cs.upperMap(capacity).costMap(cost) 604 /// .supplyMap(sup).run(); 605 /// \endcode 606 /// 607 /// \return <tt>(*this)</tt> 608 CostScaling& reset() { 609 for (int i = 0; i != _res_node_num; ++i) { 610 _supply[i] = 0; 611 } 612 int limit = _first_out[_root]; 613 for (int j = 0; j != limit; ++j) { 614 _lower[j] = 0; 615 _upper[j] = INF; 616 _scost[j] = _forward[j] ? 1 : -1; 617 } 618 for (int j = limit; j != _res_arc_num; ++j) { 619 _lower[j] = 0; 620 _upper[j] = INF; 621 _scost[j] = 0; 622 _scost[_reverse[j]] = 0; 623 } 624 _have_lower = false; 650 resetParams(); 625 651 return *this; 626 652 }
Note: See TracChangeset
for help on using the changeset viewer.