k-Flow Decomposition
See also
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,
flow_attr_origin: str = "edge",
weight_type: type = float,
subpath_constraints: list = [],
subpath_constraints_coverage: float = 1.0,
subpath_constraints_coverage_length: float = None,
length_attr: str = None,
elements_to_ignore: list = [],
solution_weights_superset: list = None,
optimization_options: dict = {},
solver_options: dict = {},
)
Bases: AbstractPathModelDAG
Initialize the Flow Decomposition model for a given number of paths k.
Parameters
-
G : nx.DiGraphThe input directed acyclic graph, as networkx DiGraph.
-
flow_attr : strThe attribute name from where to get the flow values on the edges.
-
k: intThe number of paths to decompose in.
-
flow_attr_origin : str, optionalThe origin of the flow attribute. Default is
"edge". Options:"edge": the flow attribute is assumed to be on the edges of the graph."node": the flow attribute is assumed to be on the nodes of the graph. See the documentation on how node-weighted graphs are handled.
-
weight_type : type, optionalThe type of weights (
intorfloat). 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_coverageorsubpath_constraints_coverage_lengthparameters (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 (or nodes, ifflow_attr_originis"node") 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_lengthis then the fraction of the total length of the constraint (specified vialength_attr) needs to appear in some solution path. See subpath constraints documentation -
length_attr : str, optionalThe attribute name from where to get the edge lengths (or node length, if
flow_attr_originis"node"). Defaults toNone.- If set, then the subpath lengths (above) are in terms of the edge/node lengths specified in the
length_attrfield of each edge/node. - If set, and an edge/node has a missing edge length, then it gets length 1.
- If set, then the subpath lengths (above) are in terms of the edge/node lengths specified in the
-
elements_to_ignore : list, optionalList of edges (or nodes, if
flow_attr_originis"node") to ignore when adding constrains on flow explanation by the weighted paths. Default is an empty list. See ignoring edges documentation -
solution_weights_superset: list, optionalList of allowed weights for the paths. Default is
None. If set, the model will use the solution path weights only from this set, with the property that every weight in this list appears at most once in the solution weight. That is, if you want to have more paths with the same weight, add it more times tosolution_weights_superset. -
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_typeis 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.
- ValueError: If
flow_attr_originis not “node” or “edge”.
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
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.
Parameters
-
remove_empty_paths: bool, optionalIf
True, removes empty paths from the solution. Default isFalse. These can happen only if passed the optimization option"allow_empty_paths" : True.
Returns
-
solution: dictA dictionary containing the solution paths (key
"paths") and their corresponding weights (key"weights").
Raises
exceptionIf 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.