k-Minimum Path Error
See also
In the k-Minimum Path Error problem tries to model problems where the weight along a path is not constant. As such, edges that appear in more solution paths will be allowed to have a higher error (i.e. difference between their input weight/flow value and the sum of the weights of the paths that use them). More formally, paths now receive also a slack, which intuitively models how much the weight along a path can vary. Ideally, we can decompose the weighted graphs with \(k\) paths that overall have small slack values.
1. Definition
The k-Minimum Path Error problem on a directed acyclic graph (DAG) is defined as follows:
-
INPUT:
- A directed graph \(G = (V,E)\), and a weight function on \(G\), namely weights \(f(u,v)\) for every edge \((u,v)\) of \(G\). The weights are arbitrary non-negative numbers and do not need to satisfy flow conservation.
- \(k \in \mathbb{Z}\)
-
OUTPUT: A list of \(k\) of source-to-sink paths, \(P_1,\dots,P_k\), with a weight \(w_i\), and a slack \(\rho_i\) associated to each \(P_i\), that satisfy the constraint $$ \left|f(u,v) - \sum_{i \in \{1,\dots,k\} : (u,v) \in P_i }w_i\right| \leq \sum_{i \in \{1,\dots,k\} : (u,v) \in P_i }\rho_i, ~\forall (u,v) \in E, $$ and minimize the objective function $$ \rho_1 + \cdots + \rho_k. $$
Note
- This class support also graphs with flow values on nodes. Set the parameter
flow_attr_origin = "node"
. For details on how these are handled internally, see Handling graphs with flows / weights on nodes. - The graph may have more than one source or sink nodes, in which case the solution paths are just required to start in any source node, and end in any sink node.
2. Generalizations
This class implements a more general version, as follows:
- The paths can start/end not only in source/sink nodes, but also in given sets of start/end nodes (set parameters
additional_starts
andadditional_ends
). See also Additional start/end nodes. - This class supports adding subpath constraints, that is, lists of edges that must appear in some solution path. See Subpath constraints for details.
- The above constraint can happen only over a given subset \(E' \subseteq E\) of the edges (set parameter
elements_to_ignore
to be \(E \setminus E'\)). See also ignoring edges documentation. - The error (i.e. the above absolute of the difference) of every edge can contribute differently to the objective function, according to a scale factor \(\in [0,1]\). Set these via a dictionary that you pass to
error_scaling
, which stores the scale factor \(\lambda_{(u,v)} \in [0,1]\) of each edge \((u,v)\) in the dictionary. Setting \(\lambda_{(u,v)} = 0\) will add the edge \((u,v)\) toelements_to_ignore
, because the constraint for \((u,v)\) becomes always true. See also ignoring edges documentation. - Another way to relax the constraint is to allow also some looseness in the slack value, based on the length of the solution path. Intuitively, suppose that longer paths have even higher variance in their weight across the edges of the path, while shorter paths less. Formally, suppose that we have a function \(\alpha : \mathbb{N} \rightarrow \mathbb{R}^+\) that for every solution path length \(\ell\), it returns a multiplicative factor \(\alpha(\ell)\). Then, we can multiply each path slack \(\rho_i\) by \(\alpha(|P_i|)\) in the constraint of the problem (where \(|P_i|\) denotes the length of solution path \(P_i\)). In the above example, we could set \(\alpha(\ell) > 1\) for “large” lengths \(\ell\). Note that in this model we keep the same objective function (i.e. sum of slacks), and thus this multiplier has no effect on the objective value. You can pass the function \(\alpha\) to the class as a piecewise encoding, via parameters
path_length_ranges
andpath_length_factors
, see kMinPathError().
Generalized constraint
Formally, the constraint generalized as in 3., 4. and 5. above is: $$ \lambda_{(u,v)} \cdot \left|f(u,v) - \sum_{i \in \{1,\dots,k\} : (u,v) \in P_i }w_i\right| \leq \sum_{i \in \{1,\dots,k\} : (u,v) \in P_i }\rho_i \cdot \alpha(|P_i|), ~\forall (u,v) \in E’. $$
A lowerbound on \(k\)
The value of \(k\) must be at least the edge width of graph, meaning the minimum number of paths to cover all the edges in \(E'\), except those edges \((u,v)\) for which \(\lambda_{u,v} = 0\). This value always gives a feasible model.
If you do not know this lower bound, you can pass k = None
and the model will automatically set k
to this lowerbound value.
3. References
- Fernando H. C. Dias, Alexandru I. Tomescu Accurate Flow Decomposition via Robust Integer Linear Programming IEEE/ACM Transactions on Computational Biology and Bioinformatics 21(6), 1955-1964, 2024 (preprint)
kMinPathError
kMinPathError(
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 = [],
error_scaling: dict = {},
path_length_ranges: list = [],
path_length_factors: list = [],
additional_starts: list = [],
additional_ends: list = [],
solution_weights_superset: list = None,
optimization_options: dict = None,
solver_options: dict = None,
)
Bases: AbstractPathModelDAG
This class implements the k-MinPathError model from Dias, Tomescu, Accurate Flow Decomposition via Robust Integer Linear Programming, IEEE/ACM TCBB 2024 (see preprint)
Given an edge-weighted DAG, this model looks for k paths, with associated weights and slacks, such that for every edge (u,v), the sum of the weights of the paths going through (u,v) minus the flow value of (u,v) is at most the sum of the slacks of the paths going through (u,v). The objective is to minimize the sum of the slacks.
The paths start in any source node of the graph and end in any sink node of the graph. You can allow for additional
start or end nodes by specifying them in the additional_starts
and additional_ends
parameters.
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.
Unknown \(k\)
If you do not have a good guess for \(k\), you can pass
k=None
and the model will set \(k\) to the edge width of the graph. -
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: int | float
, optionalThe type of the weights and slacks (
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 (or nodes, ifflow_attr_origin
is"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_length
is then the fraction of the total length of the constraint (specified vialength_attr
, see below) 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_origin
is"node"
). Defaults toNone
.- If set, then the subpath lengths (above) are in terms of the edge/node lengths specified in the
length_attr
field 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_origin
is"node"
) to ignore when adding constrains on flow explanation by the weighted paths. Default is an empty list. See ignoring edges documentation -
error_scaling: dict
, optionalDictionary
edge: factor
(ornode: factor
, ifflow_attr_origin
is"node"
)) storing the error scale factor (in [0,1]) of every edge, which scale the allowed difference between edge/node weight and path weights. Default is an empty dict. If an edge/node has a missing error scale factor, it is assumed to be 1. The factors are used to scale the difference between the flow value of the edge/node and the sum of the weights of the paths going through the edge/node. See ignoring edges documentation -
path_length_ranges: list
, optionalList of ranges for the solution path lengths. Default is an empty list. If this list is not empty, the solution path slacks are scaled by the corresponding factor in
path_length_factors
depending on the length of the solution path.Example
If you pass
For example, if a path has length in the range [0, 15], its slack will be multiplied by 1.6 when comparing the flow value of the edge to the sum of path slacks, but this multiplier will have no effect on the objective function. That is, in the objective function we still minimize the sum of path slacks, not the sum of scaled path slacks. -
path_length_factors: list
, optionalList of slack scale factors, based on the path lengths. Default is an empty list. If this list is not empty, the path slacks are scaled by the corresponding factor in
path_length_factors
depending on the length of the path. See the above example. -
additional_starts: list
, optionalList of additional start nodes of the paths. Default is an empty list. See additional start/end nodes documentation.
-
additional_ends: list
, optionalList of additional end nodes of the paths. Default is an empty list. See additional start/end nodes 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 the superset appears at most once in the solution weight. -
optimization_options: dict
, optionalDictionary with the optimization options. Default is
None
. See optimization options documentation. -
solver_options: dict
, optionalDictionary with the solver options. Default is
{}
. See solver options documentation.
Raises
-
ValueError
- If
weight_type
is not int or float. - If some edge does not have the flow attribute specified as
flow_attr
. - If
path_length_factors
is not empty andweight_type
is float. - If the number of path length ranges is not equal to the number of error scale factors.
- If the edge error scaling factor is not between 0 and 1.
- If the graph contains edges with negative (<0) flow values.
- ValueError: If
flow_attr_origin
is not “node” or “edge”.
- If
Source code in flowpaths/kminpatherror.py
10 11 12 13 14 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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
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, weights, slacks
and caches the solution.
Warning
Make sure you called .solve()
before calling this method.
Returns
-
solution: dict
A dictionary containing the solution paths (key
"paths"
) and their corresponding weights (key"weights"
) and slacks (key"slacks"
). Ifpath_length_factors
is not empty, it also contains the scaled slacks (key"scaled_slacks"
).
Raises
exception
If model is not solved.
Source code in flowpaths/kminpatherror.py
is_valid_solution
Checks if the solution is valid by checking of the weighted paths and their slacks satisfy the constraints of the problem.
Warning
Make sure you called .solve()
before calling this method.
Raises
ValueError
: If the solution is not available.
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.
Source code in flowpaths/kminpatherror.py
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
|