k-Flow Decomposition
This class implements a solver for the problem of decomposing a flow into a given number \(k\) of paths (\(k\)-flow decomposition). This problem is a generalization of Minimum Flow Decomposition, in the sense that we are also given the number of paths that we need to decompose the flow in.
The class MinFlowDecomp uses this class internally to find the minimum value of \(k\) for which a \(k\)-flow decomposition exists.
Warning
Suppose that the number of paths of a minimum flow decomposition is \(k^*\). If we ask for a decomposition with \(k > k^*\) paths, this class will always return a decomposition with \(k\) paths, but some paths might have weight 0.
kFlowDecomp
kFlowDecomp(G: DiGraph, flow_attr: str, k: int, weight_type: type = float, subpath_constraints: list = [], subpath_constraints_coverage: float = 1.0, subpath_constraints_coverage_length: float = None, edge_length_attr: str = None, optimization_options: dict = None, solver_options: dict = None)
Bases: AbstractPathModelDAG
Class to decompose a flow into a given number of weighted paths.
Initialize the Flow Decompostion model for a given number of paths num_paths
.
Parameters
-
G : nx.DiGraph
The input directed acyclic graph, as networkx DiGraph.
-
flow_attr : str
The attribute name from where to get the flow values on the edges.
-
k: int
The number of paths to decompose in.
-
weight_type : type
, optionalThe type of weights (
int
orfloat
). Default isfloat
. -
subpath_constraints : list
, optionalList of subpath constraints. Default is an empty list. Each subpath constraint is a list of edges that must be covered by some solution path, according to the
subpath_constraints_coverage
orsubpath_constraints_coverage_length
parameters (see below). -
subpath_constraints_coverage : float
, optionalCoverage fraction of the subpath constraints that must be covered by some solution paths.
Defaults to
1.0
(meaning that 100% of the edges of the constraint need to be covered by some solution path). See subpath constraints documentation -
subpath_constraints_coverage_length : float
, optionalCoverage length of the subpath constraints. Default is
None
. If set, this overridessubpath_constraints_coverage
, and the coverage constraint is expressed in terms of the subpath constraint length.subpath_constraints_coverage_length
is then the fraction of the total length of the constraint (specified viaedge_length_attr
) needs to appear in some solution path. See subpath constraints documentation -
edge_length_attr : str
, optionalAttribute name for edge lengths. Default is
None
. -
optimization_options : dict
, optionalDictionary with the optimization options. Default is
None
. See optimization options documentation. This class also supports the optimization"optimize_with_greedy": True
(this is the default value). This will use a greedy algorithm to solve the problem, and if the number of paths returned by it equals a lowerbound on the solution size, then we know the greedy solution is optimum, and it will use that. The lowerbound used currently is the edge-width of the graph, meaning the minimum number of paths needed to cover all edges. This is a correct lowerbound because any flow decomposition must cover all edges, as they have non-zero flow. -
solver_options : dict
, optionalDictionary with the solver options. Default is
None
. See solver options documentation.
Raises
- ValueError: If
weight_type
is not int or float. - ValueError: If some edge does not have the flow attribute specified as
flow_attr
. - ValueError: If the graph does not satisfy flow conservation on nodes different from source or sink.
- ValueError: If the graph contains edges with negative (<0) flow values.
Source code in flowpaths/kflowdecomp.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
get_solution
Retrieves the solution for the flow decomposition problem.
If the solution has already been computed and cached as self.solution
, it returns the cached solution.
Otherwise, it checks if the problem has been solved, computes the solution paths and weights,
and caches the solution.
Returns
-
solution: dict
A dictionary containing the solution paths (key
"paths"
) and their corresponding weights (key"weights"
).
Raises
exception
If model is not solved.
Source code in flowpaths/kflowdecomp.py
is_valid_solution
Checks if the solution is valid by comparing the flow from paths with the flow attribute in the graph edges.
Raises
- ValueError: If the solution is not available (i.e., self.solution is None).
Returns
- bool: True if the solution is valid, False otherwise.
Notes
- get_solution() must be called before this method.
- The solution is considered valid if the flow from paths is equal
(up to
TOLERANCE * num_paths_on_edges[(u, v)]
) to the flow value of the graph edges.