See also
k-Least Absolute Errors in General Graphs
1. Definition
The k-Least Absolute Errors problem on a directed acyclic graph (DAG) is defined as follows. For a walk \(W\) and an edge \((u,v)\), we denote by \(W(u,v)\) the number of times that the walk goes through the edge \((u,v)\). If \(W(u,v)\) does not contain \((u,v)\) , then \(W(u,v) = 0\).
-
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 number \(k\) of walks \(W_1,\dots,W_k\), starting in some node in \(S\) and ending in some node in \(T\), with a weight \(w_i\) associated to each \(P_i\), that minimize the objective function: $$ \sum_{(u,v) \in E} \left|f(u,v) - \sum_{i \in \{1,\dots,k\}}w_i \cdot W_i(u,v)\right|. $$
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:
- This class supports adding subpath constraints, that is, lists of edges that must appear in some solution path. See Subpath constraints for details.
- 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. - The above summation can happen only over a given subset \(E' \subseteq E\) of the edges (set parameter
elements_to_ignore
to be \(E \setminus E'\)), - 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\) is equivalent to adding the edge \((u,v)\) toelements_to_ignore
; the latter option is more efficient, as it results in a smaller model.
Generalized objective function
Formally, the minimized objective function generalized as in 3. and 4. above is: $$ \sum_{(u,v) \in E’} \lambda_{(u,v)} \cdot \left|f(u,v) - \sum_{i \in \{1,\dots,k\} : (u,v) \in P_i }w_i\right|. $$
kLeastAbsErrorsCycles
kLeastAbsErrorsCycles(
G: DiGraph,
flow_attr: str,
k: int = None,
flow_attr_origin: str = "edge",
weight_type: type = float,
subset_constraints: list = [],
subset_constraints_coverage: float = 1.0,
elements_to_ignore: list = [],
error_scaling: dict = {},
additional_starts: list = [],
additional_ends: list = [],
optimization_options: dict = None,
solver_options: dict = {},
trusted_edges_for_safety: list = None,
trusted_edges_for_safety_percentile: float = None,
)
Bases: AbstractWalkModelDiGraph
This class implements the k-LeastAbsoluteErrors problem, namely it looks for a decomposition of a weighted general directed graph, possibly with cycles, into \(k\) weighted walks, minimizing the absolute errors on the edges. The error on an edge is defined as the absolute value of the difference between the weight of the edge and the sum of the weights of the walks that go through it.
Parameters
-
G: nx.DiGraph
The input directed graph, as networkx DiGraph, which can have cycles.
-
flow_attr: str
The attribute name from where to get the flow values on the edges.
-
k: int
The number of walks 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 condensation width of the graph (i.e. the minimum number of \(s\)-\(t\) walks needed to cover all the edges of the graph, except those inedges_to_ignore
). -
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
. -
subset_constraints: list
, optionalList of subset constraints. Default is an empty list. Each subset constraint is a list of edges that must be covered by some solution walk, in any order, according to the
subset_constraints_coverage
parameter (see below). -
subset_constraints_coverage: float
, optionalCoverage fraction of the subset constraints that must be covered by some solution walk.
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 walk). See subset constraints documentation -
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 walks. 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 walk 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 walks going through the edge/node. See ignoring edges documentation -
additional_starts: list
, optionalList of additional start nodes of the walks. Default is an empty list.
-
additional_ends: list
, optionalList of additional end nodes of the walks. Default is an empty list.
-
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. -
trusted_edges_for_safety: list
, optionalList of edges that are trusted to appear in an optimal solution. Default is
None
. If set, the model can apply the safety optimizations for these edges, so it can be significantly faster. See optimizations documentation -
trusted_edges_for_safety_percentile: float
, optionalIf set to a value different than
None
, this will be used to select edges to trust for safety (i.e. they are guaranteed to appear in any optimal solution). Edges whose weight (flow_attr
) is greater than or equal to the percentile value will be trusted for safety. Default isNone
. This is ignored iftrusted_edges_for_safety
is set.
Raises
-
ValueError
- If
weight_type
is notint
orfloat
. - If the edge error scaling factor is not in [0,1].
- If the flow attribute
flow_attr
is not specified in some edge. - If the graph contains edges with negative flow values.
- ValueError: If
flow_attr_origin
is not “node” or “edge”.
- If
Source code in flowpaths/kleastabserrorscycles.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 |
|
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 walks, weights, slacks
and caches the solution.
Returns
-
solution: dict
A dictionary containing the solution walks (key
"walks"
) and their corresponding weights (key"weights"
), and the edge errors (key"edge_errors"
).
Raises
exception
If model is not solved.
Source code in flowpaths/kleastabserrorscycles.py
is_valid_solution
Checks if the solution is valid by comparing the flow from walks 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 walks is equal
(up to
TOLERANCE * num_edge_walks_on_edges[(u, v)]
) to the flow value of the graph edges.