50 /// \tparam SupplyMap The type of the supply map. |
50 /// \tparam SupplyMap The type of the supply map. |
51 /// |
51 /// |
52 /// \warning |
52 /// \warning |
53 /// - Edge capacities and costs should be \e non-negative \e integers. |
53 /// - Edge capacities and costs should be \e non-negative \e integers. |
54 /// - Supply values should be \e signed \e integers. |
54 /// - Supply values should be \e signed \e integers. |
55 /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value. |
55 /// - The value types of the maps should be convertible to each other. |
56 /// - \c CapacityMap::Value and \c SupplyMap::Value must be |
56 /// - \c CostMap::Value must be signed type. |
57 /// convertible to each other. |
|
58 /// - All value types must be convertible to \c CostMap::Value, which |
|
59 /// must be signed type. |
|
60 /// |
57 /// |
61 /// \note \ref NetworkSimplex provides six different pivot rule |
58 /// \note \ref NetworkSimplex provides six different pivot rule |
62 /// implementations that significantly affect the efficiency of the |
59 /// implementations that significantly affect the efficiency of the |
63 /// algorithm. |
60 /// algorithm. |
64 /// By default a combined pivot rule is used, which is the fastest |
61 /// By default a combined pivot rule is used, which is the fastest |
467 |
464 |
468 // The reduced cost map |
465 // The reduced cost map |
469 ReducedCostMap _red_cost; |
466 ReducedCostMap _red_cost; |
470 |
467 |
471 // Members for handling the original graph |
468 // Members for handling the original graph |
472 FlowMap _flow_result; |
469 FlowMap *_flow_result; |
473 PotentialMap _potential_result; |
470 PotentialMap *_potential_result; |
|
471 bool _local_flow; |
|
472 bool _local_potential; |
474 NodeRefMap _node_ref; |
473 NodeRefMap _node_ref; |
475 EdgeRefMap _edge_ref; |
474 EdgeRefMap _edge_ref; |
476 |
475 |
477 // The entering edge of the current pivot iteration. |
476 // The entering edge of the current pivot iteration. |
478 Edge _in_edge; |
477 Edge _in_edge; |
485 // pivot iteration. |
484 // pivot iteration. |
486 Capacity delta; |
485 Capacity delta; |
487 |
486 |
488 public : |
487 public : |
489 |
488 |
490 /// \brief General constructor of the class (with lower bounds). |
489 /// \brief General constructor (with lower bounds). |
491 /// |
490 /// |
492 /// General constructor of the class (with lower bounds). |
491 /// General constructor (with lower bounds). |
493 /// |
492 /// |
494 /// \param graph The directed graph the algorithm runs on. |
493 /// \param graph The directed graph the algorithm runs on. |
495 /// \param lower The lower bounds of the edges. |
494 /// \param lower The lower bounds of the edges. |
496 /// \param capacity The capacities (upper bounds) of the edges. |
495 /// \param capacity The capacities (upper bounds) of the edges. |
497 /// \param cost The cost (length) values of the edges. |
496 /// \param cost The cost (length) values of the edges. |
504 _graph(), _graph_ref(graph), _lower(&lower), _capacity(_graph), |
503 _graph(), _graph_ref(graph), _lower(&lower), _capacity(_graph), |
505 _cost(_graph), _supply(_graph), _flow(_graph), |
504 _cost(_graph), _supply(_graph), _flow(_graph), |
506 _potential(_graph), _depth(_graph), _parent(_graph), |
505 _potential(_graph), _depth(_graph), _parent(_graph), |
507 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
506 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
508 _state(_graph), _red_cost(_graph, _cost, _potential), |
507 _state(_graph), _red_cost(_graph, _cost, _potential), |
509 _flow_result(graph), _potential_result(graph), |
508 _flow_result(0), _potential_result(0), |
|
509 _local_flow(false), _local_potential(false), |
510 _node_ref(graph), _edge_ref(graph) |
510 _node_ref(graph), _edge_ref(graph) |
511 { |
511 { |
512 // Checking the sum of supply values |
512 // Checking the sum of supply values |
513 Supply sum = 0; |
513 Supply sum = 0; |
514 for (typename Graph::NodeIt n(_graph_ref); n != INVALID; ++n) |
514 for (typename Graph::NodeIt n(_graph_ref); n != INVALID; ++n) |
536 s -= lower[e]; |
536 s -= lower[e]; |
537 _supply[_node_ref[n]] = s; |
537 _supply[_node_ref[n]] = s; |
538 } |
538 } |
539 } |
539 } |
540 |
540 |
541 /// \brief General constructor of the class (without lower bounds). |
541 /// \brief General constructor (without lower bounds). |
542 /// |
542 /// |
543 /// General constructor of the class (without lower bounds). |
543 /// General constructor (without lower bounds). |
544 /// |
544 /// |
545 /// \param graph The directed graph the algorithm runs on. |
545 /// \param graph The directed graph the algorithm runs on. |
546 /// \param capacity The capacities (upper bounds) of the edges. |
546 /// \param capacity The capacities (upper bounds) of the edges. |
547 /// \param cost The cost (length) values of the edges. |
547 /// \param cost The cost (length) values of the edges. |
548 /// \param supply The supply values of the nodes (signed). |
548 /// \param supply The supply values of the nodes (signed). |
553 _graph(), _graph_ref(graph), _lower(NULL), _capacity(_graph), |
553 _graph(), _graph_ref(graph), _lower(NULL), _capacity(_graph), |
554 _cost(_graph), _supply(_graph), _flow(_graph), |
554 _cost(_graph), _supply(_graph), _flow(_graph), |
555 _potential(_graph), _depth(_graph), _parent(_graph), |
555 _potential(_graph), _depth(_graph), _parent(_graph), |
556 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
556 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
557 _state(_graph), _red_cost(_graph, _cost, _potential), |
557 _state(_graph), _red_cost(_graph, _cost, _potential), |
558 _flow_result(graph), _potential_result(graph), |
558 _flow_result(0), _potential_result(0), |
|
559 _local_flow(false), _local_potential(false), |
559 _node_ref(graph), _edge_ref(graph) |
560 _node_ref(graph), _edge_ref(graph) |
560 { |
561 { |
561 // Checking the sum of supply values |
562 // Checking the sum of supply values |
562 Supply sum = 0; |
563 Supply sum = 0; |
563 for (typename Graph::NodeIt n(_graph_ref); n != INVALID; ++n) |
564 for (typename Graph::NodeIt n(_graph_ref); n != INVALID; ++n) |
572 .nodeRef(_node_ref) |
573 .nodeRef(_node_ref) |
573 .edgeRef(_edge_ref) |
574 .edgeRef(_edge_ref) |
574 .run(); |
575 .run(); |
575 } |
576 } |
576 |
577 |
577 /// \brief Simple constructor of the class (with lower bounds). |
578 /// \brief Simple constructor (with lower bounds). |
578 /// |
579 /// |
579 /// Simple constructor of the class (with lower bounds). |
580 /// Simple constructor (with lower bounds). |
580 /// |
581 /// |
581 /// \param graph The directed graph the algorithm runs on. |
582 /// \param graph The directed graph the algorithm runs on. |
582 /// \param lower The lower bounds of the edges. |
583 /// \param lower The lower bounds of the edges. |
583 /// \param capacity The capacities (upper bounds) of the edges. |
584 /// \param capacity The capacities (upper bounds) of the edges. |
584 /// \param cost The cost (length) values of the edges. |
585 /// \param cost The cost (length) values of the edges. |
596 _graph(), _graph_ref(graph), _lower(&lower), _capacity(_graph), |
597 _graph(), _graph_ref(graph), _lower(&lower), _capacity(_graph), |
597 _cost(_graph), _supply(_graph), _flow(_graph), |
598 _cost(_graph), _supply(_graph), _flow(_graph), |
598 _potential(_graph), _depth(_graph), _parent(_graph), |
599 _potential(_graph), _depth(_graph), _parent(_graph), |
599 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
600 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
600 _state(_graph), _red_cost(_graph, _cost, _potential), |
601 _state(_graph), _red_cost(_graph, _cost, _potential), |
601 _flow_result(graph), _potential_result(graph), |
602 _flow_result(0), _potential_result(0), |
|
603 _local_flow(false), _local_potential(false), |
602 _node_ref(graph), _edge_ref(graph) |
604 _node_ref(graph), _edge_ref(graph) |
603 { |
605 { |
604 // Copying _graph_ref to graph |
606 // Copying _graph_ref to graph |
605 copyGraph(_graph, _graph_ref) |
607 copyGraph(_graph, _graph_ref) |
606 .edgeMap(_cost, cost) |
608 .edgeMap(_cost, cost) |
623 _supply[_node_ref[n]] = sum; |
625 _supply[_node_ref[n]] = sum; |
624 } |
626 } |
625 _valid_supply = true; |
627 _valid_supply = true; |
626 } |
628 } |
627 |
629 |
628 /// \brief Simple constructor of the class (without lower bounds). |
630 /// \brief Simple constructor (without lower bounds). |
629 /// |
631 /// |
630 /// Simple constructor of the class (without lower bounds). |
632 /// Simple constructor (without lower bounds). |
631 /// |
633 /// |
632 /// \param graph The directed graph the algorithm runs on. |
634 /// \param graph The directed graph the algorithm runs on. |
633 /// \param capacity The capacities (upper bounds) of the edges. |
635 /// \param capacity The capacities (upper bounds) of the edges. |
634 /// \param cost The cost (length) values of the edges. |
636 /// \param cost The cost (length) values of the edges. |
635 /// \param s The source node. |
637 /// \param s The source node. |
645 _graph(), _graph_ref(graph), _lower(NULL), _capacity(_graph), |
647 _graph(), _graph_ref(graph), _lower(NULL), _capacity(_graph), |
646 _cost(_graph), _supply(_graph, 0), _flow(_graph), |
648 _cost(_graph), _supply(_graph, 0), _flow(_graph), |
647 _potential(_graph), _depth(_graph), _parent(_graph), |
649 _potential(_graph), _depth(_graph), _parent(_graph), |
648 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
650 _pred_edge(_graph), _thread(_graph), _forward(_graph), |
649 _state(_graph), _red_cost(_graph, _cost, _potential), |
651 _state(_graph), _red_cost(_graph, _cost, _potential), |
650 _flow_result(graph), _potential_result(graph), |
652 _flow_result(0), _potential_result(0), |
|
653 _local_flow(false), _local_potential(false), |
651 _node_ref(graph), _edge_ref(graph) |
654 _node_ref(graph), _edge_ref(graph) |
652 { |
655 { |
653 // Copying _graph_ref to graph |
656 // Copying _graph_ref to graph |
654 copyGraph(_graph, _graph_ref) |
657 copyGraph(_graph, _graph_ref) |
655 .edgeMap(_capacity, capacity) |
658 .edgeMap(_capacity, capacity) |
660 _supply[_node_ref[s]] = flow_value; |
663 _supply[_node_ref[s]] = flow_value; |
661 _supply[_node_ref[t]] = -flow_value; |
664 _supply[_node_ref[t]] = -flow_value; |
662 _valid_supply = true; |
665 _valid_supply = true; |
663 } |
666 } |
664 |
667 |
|
668 /// Destructor. |
|
669 ~NetworkSimplex() { |
|
670 if (_local_flow) delete _flow_result; |
|
671 if (_local_potential) delete _potential_result; |
|
672 } |
|
673 |
|
674 /// \brief Sets the flow map. |
|
675 /// |
|
676 /// Sets the flow map. |
|
677 /// |
|
678 /// \return \c (*this) |
|
679 NetworkSimplex& flowMap(FlowMap &map) { |
|
680 if (_local_flow) { |
|
681 delete _flow_result; |
|
682 _local_flow = false; |
|
683 } |
|
684 _flow_result = ↦ |
|
685 return *this; |
|
686 } |
|
687 |
|
688 /// \brief Sets the potential map. |
|
689 /// |
|
690 /// Sets the potential map. |
|
691 /// |
|
692 /// \return \c (*this) |
|
693 NetworkSimplex& potentialMap(PotentialMap &map) { |
|
694 if (_local_potential) { |
|
695 delete _potential_result; |
|
696 _local_potential = false; |
|
697 } |
|
698 _potential_result = ↦ |
|
699 return *this; |
|
700 } |
|
701 |
|
702 /// \name Execution control |
|
703 /// The only way to execute the algorithm is to call the run() |
|
704 /// function. |
|
705 |
|
706 /// @{ |
|
707 |
665 /// \brief Runs the algorithm. |
708 /// \brief Runs the algorithm. |
666 /// |
709 /// |
667 /// Runs the algorithm. |
710 /// Runs the algorithm. |
668 /// |
711 /// |
669 /// \param pivot_rule The pivot rule that is used during the |
712 /// \param pivot_rule The pivot rule that is used during the |
701 /// \return \c true if a feasible flow can be found. |
744 /// \return \c true if a feasible flow can be found. |
702 bool run(PivotRuleEnum pivot_rule = COMBINED_PIVOT) { |
745 bool run(PivotRuleEnum pivot_rule = COMBINED_PIVOT) { |
703 return init() && start(pivot_rule); |
746 return init() && start(pivot_rule); |
704 } |
747 } |
705 |
748 |
|
749 /// @} |
|
750 |
|
751 /// \name Query Functions |
|
752 /// The result of the algorithm can be obtained using these |
|
753 /// functions. |
|
754 /// \n run() must be called before using them. |
|
755 |
|
756 /// @{ |
|
757 |
706 /// \brief Returns a const reference to the edge map storing the |
758 /// \brief Returns a const reference to the edge map storing the |
707 /// found flow. |
759 /// found flow. |
708 /// |
760 /// |
709 /// Returns a const reference to the edge map storing the found flow. |
761 /// Returns a const reference to the edge map storing the found flow. |
710 /// |
762 /// |
711 /// \pre \ref run() must be called before using this function. |
763 /// \pre \ref run() must be called before using this function. |
712 const FlowMap& flowMap() const { |
764 const FlowMap& flowMap() const { |
713 return _flow_result; |
765 return *_flow_result; |
714 } |
766 } |
715 |
767 |
716 /// \brief Returns a const reference to the node map storing the |
768 /// \brief Returns a const reference to the node map storing the |
717 /// found potentials (the dual solution). |
769 /// found potentials (the dual solution). |
718 /// |
770 /// |
719 /// Returns a const reference to the node map storing the found |
771 /// Returns a const reference to the node map storing the found |
720 /// potentials (the dual solution). |
772 /// potentials (the dual solution). |
721 /// |
773 /// |
722 /// \pre \ref run() must be called before using this function. |
774 /// \pre \ref run() must be called before using this function. |
723 const PotentialMap& potentialMap() const { |
775 const PotentialMap& potentialMap() const { |
724 return _potential_result; |
776 return *_potential_result; |
|
777 } |
|
778 |
|
779 /// \brief Returns the flow on the edge. |
|
780 /// |
|
781 /// Returns the flow on the edge. |
|
782 /// |
|
783 /// \pre \ref run() must be called before using this function. |
|
784 Capacity flow(const typename Graph::Edge& edge) const { |
|
785 return (*_flow_result)[edge]; |
|
786 } |
|
787 |
|
788 /// \brief Returns the potential of the node. |
|
789 /// |
|
790 /// Returns the potential of the node. |
|
791 /// |
|
792 /// \pre \ref run() must be called before using this function. |
|
793 Cost potential(const typename Graph::Node& node) const { |
|
794 return (*_potential_result)[node]; |
725 } |
795 } |
726 |
796 |
727 /// \brief Returns the total cost of the found flow. |
797 /// \brief Returns the total cost of the found flow. |
728 /// |
798 /// |
729 /// Returns the total cost of the found flow. The complexity of the |
799 /// Returns the total cost of the found flow. The complexity of the |
731 /// |
801 /// |
732 /// \pre \ref run() must be called before using this function. |
802 /// \pre \ref run() must be called before using this function. |
733 Cost totalCost() const { |
803 Cost totalCost() const { |
734 Cost c = 0; |
804 Cost c = 0; |
735 for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e) |
805 for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e) |
736 c += _flow_result[e] * _cost[_edge_ref[e]]; |
806 c += (*_flow_result)[e] * _cost[_edge_ref[e]]; |
737 return c; |
807 return c; |
738 } |
808 } |
|
809 |
|
810 /// @} |
739 |
811 |
740 private: |
812 private: |
741 |
813 |
742 /// \brief Extends the underlaying graph and initializes all the |
814 /// \brief Extends the underlaying graph and initializes all the |
743 /// node and edge maps. |
815 /// node and edge maps. |
744 bool init() { |
816 bool init() { |
745 if (!_valid_supply) return false; |
817 if (!_valid_supply) return false; |
|
818 |
|
819 // Initializing result maps |
|
820 if (!_flow_result) { |
|
821 _flow_result = new FlowMap(_graph_ref); |
|
822 _local_flow = true; |
|
823 } |
|
824 if (!_potential_result) { |
|
825 _potential_result = new PotentialMap(_graph_ref); |
|
826 _local_potential = true; |
|
827 } |
746 |
828 |
747 // Initializing state and flow maps |
829 // Initializing state and flow maps |
748 for (EdgeIt e(_graph); e != INVALID; ++e) { |
830 for (EdgeIt e(_graph); e != INVALID; ++e) { |
749 _flow[e] = 0; |
831 _flow[e] = 0; |
750 _state[e] = STATE_LOWER; |
832 _state[e] = STATE_LOWER; |
1013 if (_flow[e] > 0) return false; |
1095 if (_flow[e] > 0) return false; |
1014 |
1096 |
1015 // Copying flow values to _flow_result |
1097 // Copying flow values to _flow_result |
1016 if (_lower) { |
1098 if (_lower) { |
1017 for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e) |
1099 for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e) |
1018 _flow_result[e] = (*_lower)[e] + _flow[_edge_ref[e]]; |
1100 (*_flow_result)[e] = (*_lower)[e] + _flow[_edge_ref[e]]; |
1019 } else { |
1101 } else { |
1020 for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e) |
1102 for (typename Graph::EdgeIt e(_graph_ref); e != INVALID; ++e) |
1021 _flow_result[e] = _flow[_edge_ref[e]]; |
1103 (*_flow_result)[e] = _flow[_edge_ref[e]]; |
1022 } |
1104 } |
1023 // Copying potential values to _potential_result |
1105 // Copying potential values to _potential_result |
1024 for (typename Graph::NodeIt n(_graph_ref); n != INVALID; ++n) |
1106 for (typename Graph::NodeIt n(_graph_ref); n != INVALID; ++n) |
1025 _potential_result[n] = _potential[_node_ref[n]]; |
1107 (*_potential_result)[n] = _potential[_node_ref[n]]; |
1026 |
1108 |
1027 return true; |
1109 return true; |
1028 } |
1110 } |
1029 |
1111 |
1030 }; //class NetworkSimplex |
1112 }; //class NetworkSimplex |