|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Network Simplex algorithm for minimum cost flow. More...
#include <tpl_netcost.H>
Public Types | |
| using | Node = typename Net::Node |
| using | Arc = typename Net::Arc |
| using | Flow_Type = typename Net::Flow_Type |
| using | Node_Info = Simplex_Node_Info< Flow_Type > |
| using | Arc_Info = Simplex_Arc_Info< Flow_Type > |
Public Member Functions | |
| Network_Simplex (Net &network) | |
| Construct Network Simplex solver. | |
| void | set_lower_bound (Arc *a, Flow_Type lower_bound) |
| Set lower bound for an arc. | |
| Flow_Type | get_lower_bound (Arc *a) const |
| Get lower bound for an arc. | |
| bool | is_at_lower_bound (Arc *a) const |
| Check if arc flow is at lower bound, 0. | |
| bool | is_at_upper_bound (Arc *a) const |
| Check if arc flow is at upper bound (capacity). | |
| Flow_Type | residual_capacity (Arc *a) const |
| Get residual capacity (room to increase flow). | |
| Flow_Type | residual_lower (Arc *a) const |
| Get residual lower (room to decrease flow). | |
| size_t | run (Flow_Type unused=0) |
| Execute Network Simplex algorithm. | |
| size_t | get_num_pivots () const |
| Return number of pivots performed in last run. | |
| const NetworkSimplexStats & | get_stats () const noexcept |
| Return execution statistics from last run. | |
| void | print_stats () const |
| Print execution statistics to stdout. | |
| size_t | force_partial_arcs_into_tree () |
| Phase I: Force all partial-flow arcs into the spanning tree. | |
| bool | is_valid_basic_solution () const |
| Check if current solution is a valid basic feasible solution. | |
| size_t | count_non_tree_partial_arcs () const |
| Count arcs with partial flow not in tree. | |
| bool | verify_tree_reduced_costs () const |
| Verify that tree arcs have zero reduced cost. | |
| bool | verify_tree_integrity () const |
| Verify tree integrity - all parent_arcs should be Tree arcs. | |
| void | print_diagnostics () const |
| Print diagnostic information about current state. | |
Private Member Functions | |
| void | set_tolerances () |
| Compute the tolerances from the capacities, flows and costs. | |
| void | ensure_ran (const char *member) const |
| Throw domain_error, naming member, if run() has not made the structures of the arcs that it reads. | |
| bool | feasible_flow () const |
| Whether the flow of the network is feasible: finite and within the bounds of each arc, and kept by each node but the source and the sink, within the tolerance of check_node(). | |
| size_t | expected_tree_arcs () const |
| The arcs of the trees of a basic solution, one by node but the root of the tree of each component of the network; zero before run(). | |
| Node_Info & | ninfo (Node *p) |
| Get node info reference. | |
| const Node_Info & | ninfo (Node *p) const |
| Get node info const reference. | |
| Arc_Info & | ainfo (Arc *a) |
| Get arc info reference. | |
| const Arc_Info & | ainfo (Arc *a) const |
| Get arc info const reference. | |
| Node * | parent (Node *p) const |
| Get parent node. | |
| Arc * | parent_arc (Node *p) const |
| Get parent arc. | |
| long | depth (Node *p) const |
| Get depth. | |
| Flow_Type | potential (Node *p) const |
| Get potential. | |
| Flow_Type | reduced_cost (Arc *a) const |
| Compute reduced cost of an arc. | |
| void | init_structures () |
| Initialize data structures. | |
| bool | is_partial_flow (Arc *a) const |
| Check if arc has partial flow (strictly between bounds). | |
| void | build_spanning_tree () |
| Build initial spanning tree from source. | |
| Arc * | find_entering_arc () |
| Find entering arc using most negative reduced cost. | |
| Node * | find_lca (Node *u, Node *v) |
| Find the lowest common ancestor of two nodes. | |
| bool | augment_and_find_leaving (Arc *entering, Flow_Type &delta, Arc *&leaving, bool &leaving_goes_lower) |
| Augment flow around the cycle and find leaving arc. | |
| void | pivot_tree (Arc *entering, Arc *leaving, bool leaving_goes_lower) |
| Update tree structure after pivot. | |
Static Private Member Functions | |
| static void | move_flow (Arc *a, const Flow_Type d, const bool up) noexcept |
| Add d to the flow of a, if up, or take it away. | |
| static bool | without_capacity (const Arc *a) noexcept |
| Whether an arc has no capacity: its capacity is infinite or max(). | |
Private Attributes | |
| Net & | net |
| DynArray< Node_Info > | node_info |
| DynArray< Arc_Info > | arc_info |
| DynMapTree< Node *, size_t > | node_to_idx |
| DynMapTree< Arc *, size_t > | arc_to_idx |
| Node * | root = nullptr |
| size_t | num_pivots = 0 |
| long | lca_mark = 0 |
| NetworkSimplexStats | stats |
| Flow_Type | flow_tol = Flow_Type{0} |
| Flow_Type | cost_tol = Flow_Type{0} |
| bool | ran = false |
| bool | unbounded = false |
Static Private Attributes | |
| static constexpr Flow_Type | Inf = std::numeric_limits<Flow_Type>::max() |
Network Simplex algorithm for minimum cost flow.
This class implements the Network Simplex algorithm, a specialized version of the Simplex method for minimum cost network flow problems. It maintains a spanning tree of basic arcs and iteratively improves the solution through pivoting operations.
The algorithm uses a two-phase approach:
After Ford-Fulkerson, some arcs may have partial flow (0 < flow < cap). For a valid basic solution, these arcs MUST be in the spanning tree (non-basic arcs must be at their bounds). Phase I forces all partial-flow arcs into the tree through pivots.
Standard Network Simplex that iteratively:
This implementation follows the presentation in Sedgewick's "Algorithms" with some adaptations for the Aleph-w graph representation and the addition of Phase I to handle the basic feasibility requirement.
The algorithm assumes the network already has a feasible flow (typically from a max-flow algorithm like Ford-Fulkerson). It then optimizes the cost while maintaining the same flow value. The lower bound of every arc is 0: it supports no other.
| Net | Network type (must be Net_Cost_Graph or compatible). |
Definition at line 1289 of file tpl_netcost.H.
| using Aleph::Network_Simplex< Net >::Arc = typename Net::Arc |
Definition at line 1293 of file tpl_netcost.H.
| using Aleph::Network_Simplex< Net >::Arc_Info = Simplex_Arc_Info<Flow_Type> |
Definition at line 1296 of file tpl_netcost.H.
| using Aleph::Network_Simplex< Net >::Flow_Type = typename Net::Flow_Type |
Definition at line 1294 of file tpl_netcost.H.
| using Aleph::Network_Simplex< Net >::Node = typename Net::Node |
Definition at line 1292 of file tpl_netcost.H.
| using Aleph::Network_Simplex< Net >::Node_Info = Simplex_Node_Info<Flow_Type> |
Definition at line 1295 of file tpl_netcost.H.
|
inlineexplicit |
Construct Network Simplex solver.
| [in,out] | network | The network to optimize. |
Definition at line 2072 of file tpl_netcost.H.
|
inlineprivate |
Get arc info reference.
Definition at line 1431 of file tpl_netcost.H.
|
inlineprivate |
Get arc info const reference.
Definition at line 1434 of file tpl_netcost.H.
|
inlineprivate |
Augment flow around the cycle and find leaving arc.
When entering arc is added, it creates a cycle. This function:
Flow pushed around a cycle, directed or not, or around a loop, keeps the value of the flow, and the entering arc makes the cost lower.
| [in] | entering | The entering arc. |
| [out] | delta | Flow change amount. |
| [out] | leaving | The leaving arc (becomes non-basic). |
| [out] | leaving_goes_lower | True if leaving arc goes to lower bound. |
Definition at line 1756 of file tpl_netcost.H.
References Aleph::and.
|
inlineprivate |
Build initial spanning tree from source.
Creates a spanning tree rooted at the source node and computes initial potentials. Prioritizes arcs with partial flow. The nodes that it does not reach, as those of a flow apart from the source, get trees of their own, with the potentials of each from 0.
Non-tree arcs are classified as Lower or Upper based on their current flow relative to capacity.
Definition at line 1506 of file tpl_netcost.H.
References Aleph::and, Aleph::DynList< T >::append(), Aleph::DynListQueue< T >::get(), Aleph::DynListQueue< T >::is_empty(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), Aleph::DynListQueue< T >::put(), r, Aleph::reduced_cost(), and root().
|
inline |
Count arcs with partial flow not in tree.
Definition at line 2432 of file tpl_netcost.H.
References Aleph::and, and Aleph::count().
|
inlineprivate |
Get depth.
Definition at line 1443 of file tpl_netcost.H.
|
inlineprivate |
Throw domain_error, naming member, if run() has not made the structures of the arcs that it reads.
Definition at line 1373 of file tpl_netcost.H.
References ah_domain_error_if.
|
inlineprivate |
The arcs of the trees of a basic solution, one by node but the root of the tree of each component of the network; zero before run().
vsize() - 1 counted those of a single tree.
Definition at line 1412 of file tpl_netcost.H.
|
inlineprivate |
Whether the flow of the network is feasible: finite and within the bounds of each arc, and kept by each node but the source and the sink, within the tolerance of check_node().
check_network() also asks that each node be connected, as the source of a network whose source is its sink, isolated, is not.
Definition at line 1384 of file tpl_netcost.H.
References Aleph::Net_Graph< NodeT, ArcT >::node_tolerance(), and out.
|
inlineprivate |
Find entering arc using most negative reduced cost.
Scans all non-tree arcs and selects the one with the largest violation of the optimality conditions:
This implementation checks BOTH directions for each arc:
This is essential for finding all negative cycles, including those that require reversing flow through arcs with flow > 0.
Definition at line 1662 of file tpl_netcost.H.
References Aleph::and, and Aleph::reduced_cost().
|
inlineprivate |
Find the lowest common ancestor of two nodes.
Uses marking to efficiently find the LCA in O(d) where d is depth.
| [in] | u | First node. |
| [in] | v | Second node. |
Definition at line 1719 of file tpl_netcost.H.
References Aleph::and.
|
inline |
Phase I: Force all partial-flow arcs into the spanning tree.
For a valid basic feasible solution in Network Simplex, all arcs with flow strictly between bounds (0 < flow < cap) MUST be in the spanning tree. Non-basic arcs must be at their bounds.
This method identifies arcs with partial flow that are not in the tree and forces pivots to include them. Each pivot replaces one tree arc with the entering arc, maintaining exactly N-1 tree arcs.
Definition at line 2332 of file tpl_netcost.H.
References Aleph::and.
|
inline |
Get lower bound for an arc.
| [in] | a | Arc to query. |
Definition at line 2101 of file tpl_netcost.H.
|
inline |
Return number of pivots performed in last run.
Definition at line 2298 of file tpl_netcost.H.
|
inlinenoexcept |
Return execution statistics from last run.
Definition at line 2301 of file tpl_netcost.H.
|
inlineprivate |
Initialize data structures.
Definition at line 1460 of file tpl_netcost.H.
References Aleph::Simplex_Arc_Info< Ftype >::state.
|
inline |
Check if arc flow is at lower bound, 0.
With a floating flow, within the tolerance of the flows of the last run(); exactly before it.
| [in] | a | Arc to check. |
Definition at line 2114 of file tpl_netcost.H.
|
inline |
Check if arc flow is at upper bound (capacity).
With a floating flow, within the tolerance of the flows of the last run(); exactly before it.
| [in] | a | Arc to check. |
Definition at line 2127 of file tpl_netcost.H.
|
inlineprivate |
Check if arc has partial flow (strictly between bounds).
Definition at line 1491 of file tpl_netcost.H.
References Aleph::and.
|
inline |
Check if current solution is a valid basic feasible solution.
A valid BFS requires:
Definition at line 2415 of file tpl_netcost.H.
|
inlinestaticprivatenoexcept |
Add d to the flow of a, if up, or take it away.
A floating flow stays within [0, cap]: rounding left it an ulp beyond, and then every later cycle through the arc had a negative ratio, so that its pivot failed and run() ended as if it were optimal.
Definition at line 1327 of file tpl_netcost.H.
|
inlineprivate |
Get node info reference.
Definition at line 1425 of file tpl_netcost.H.
|
inlineprivate |
Get node info const reference.
Definition at line 1428 of file tpl_netcost.H.
|
inlineprivate |
Get parent node.
Definition at line 1437 of file tpl_netcost.H.
|
inlineprivate |
Get parent arc.
Definition at line 1440 of file tpl_netcost.H.
|
inlineprivate |
Update tree structure after pivot.
Replaces the leaving arc with the entering arc and updates parent pointers, depths, and potentials.
| [in] | entering | The entering arc. |
| [in] | leaving | The leaving arc. |
| [in] | leaving_goes_lower | True if leaving goes to lower bound. |
Definition at line 1909 of file tpl_netcost.H.
References Aleph::and, Aleph::DynListQueue< T >::get(), LocateFunctions< Container, Type >::get_it(), Aleph::DynList< T >::insert(), Aleph::DynListQueue< T >::is_empty(), Aleph::next(), and Aleph::DynListQueue< T >::put().
|
inlineprivate |
Get potential.
Definition at line 1446 of file tpl_netcost.H.
|
inline |
Print diagnostic information about current state.
Definition at line 2492 of file tpl_netcost.H.
References Aleph::and, and Aleph::reduced_cost().
|
inline |
Print execution statistics to stdout.
Definition at line 2304 of file tpl_netcost.H.
|
inlineprivate |
Compute reduced cost of an arc.
Reduced cost = c_ij - pi_i + pi_j For optimality: reduced cost >= 0 for arcs at lower bound (flow = 0) reduced cost <= 0 for arcs at upper bound (flow = cap)
Definition at line 1452 of file tpl_netcost.H.
|
inline |
Get residual capacity (room to increase flow).
| [in] | a | Arc to query. |
Definition at line 2137 of file tpl_netcost.H.
|
inline |
Get residual lower (room to decrease flow).
| [in] | a | Arc to query. |
a->flow. Definition at line 2151 of file tpl_netcost.H.
|
inline |
Execute Network Simplex algorithm.
Computes minimum cost flow. It optimizes the cost of the flow that the network has, as one of Ford-Fulkerson, keeping its value.
With a floating flow, its comparisons are within tolerances relative to the data: 1000 epsilons of the largest capacity or flow, and of the largest cost; and the residues of rounding that the pivots leave, as the arc that leaves the tree takes its bound exactly, go back at the end, as those of the maximum flows (see net_detail::return_dust()), also before the exception of an unbounded program: every node keeps its flow within the tolerance of check_node().
| [in] | unused | Kept for API compatibility. |
| std::domain_error | if the network is not of a single source and a single sink, if its flow is not feasible (beyond the capacity of an arc, negative, infinite, or not kept by a node but the source and the sink), or if the program is unbounded: a cycle of negative cost through arcs without capacity; the network keeps then a feasible flow of the same value, that of the pivots made |
| std::runtime_error | if the pivots reach their bound before the optimum; the network keeps a feasible flow of the same value |
Definition at line 2185 of file tpl_netcost.H.
References ah_domain_error, ah_domain_error_if, ah_runtime_error_if, Aleph::and, Aleph::build_spanning_tree(), and Aleph::net_detail::return_dust().
Referenced by Aleph::max_flow_min_cost_by_network_simplex().
|
inline |
Set lower bound for an arc.
Network_Simplex supports no lower bound but 0, that of every arc. It stored another one, which no part of the algorithm read, and which the next run() set to 0.
| [in] | a | Arc to set lower bound for. |
| [in] | lower_bound | Minimum flow value; only 0. |
| std::domain_error | if lower_bound is not 0. |
Definition at line 2087 of file tpl_netcost.H.
References ah_domain_error_if, and Aleph::lower_bound().
|
inlineprivate |
Compute the tolerances from the capacities, flows and costs.
Definition at line 1350 of file tpl_netcost.H.
|
inline |
Verify tree integrity - all parent_arcs should be Tree arcs.
Definition at line 2472 of file tpl_netcost.H.
References root().
|
inline |
Verify that tree arcs have zero reduced cost.
In a valid basic solution, all tree arcs must have reduced cost = 0. This is a consistency check for the potential values.
Definition at line 2452 of file tpl_netcost.H.
References Aleph::reduced_cost().
|
inlinestaticprivatenoexcept |
Whether an arc has no capacity: its capacity is infinite or max().
Definition at line 1344 of file tpl_netcost.H.
References Aleph::net_detail::without_capacity().
|
private |
Definition at line 1301 of file tpl_netcost.H.
|
private |
Definition at line 1303 of file tpl_netcost.H.
|
private |
Definition at line 1318 of file tpl_netcost.H.
|
private |
Definition at line 1317 of file tpl_netcost.H.
|
staticconstexprprivate |
Definition at line 1309 of file tpl_netcost.H.
|
private |
Definition at line 1306 of file tpl_netcost.H.
|
private |
Definition at line 1299 of file tpl_netcost.H.
|
private |
Definition at line 1300 of file tpl_netcost.H.
|
private |
Definition at line 1302 of file tpl_netcost.H.
|
private |
Definition at line 1305 of file tpl_netcost.H.
|
private |
Definition at line 1320 of file tpl_netcost.H.
|
private |
Definition at line 1304 of file tpl_netcost.H.
|
mutableprivate |
Definition at line 1307 of file tpl_netcost.H.
|
private |
Definition at line 1321 of file tpl_netcost.H.