An Optimization Routine for the Number of Paths
Models implemented with the class AbstractPathModelDAG assume a fixed number \(k\) of paths. This class provides an automatic method of iterating in an increasing manner over values of \(k\) to find the best one (i.e., when a stopping criterion has been met).
For example, our custom class for the Minimum Flow Decomposition problem could be emulated like in the code below. However, MinFlowDecomp implements also many other optimizations that make it much faster than this generic routine.
mfd_model = fp.NumPathsOptimization(
model_type = fp.kFlowDecomp,
stop_on_first_feasible=True,
G=graph,
flow_attr="flow",
)
If you want to pass additional parameters to the model, you can just add append them, for example:
mfd_model = fp.NumPathsOptimization(
model_type = fp.kFlowDecomp,
stop_on_first_feasible=True,
G=graph,
flow_attr="flow",
subpath_constraints=[[("a", "c"),("c", "t")]],
subpath_constraints_coverage=0.5,
optimization_options={"optimize_with_greedy": False}
)
This routine is also used by specialized wrappers that optimize \(k\) automatically for the Minimum Discordant Nodes objective:
NumPathsOptimization
NumPathsOptimization(
model_type,
stop_on_first_feasible: bool = None,
stop_on_delta_abs: float = None,
stop_on_delta_rel: float = None,
stop_on_infeasible_on_delta: bool = True,
min_num_paths: int = 1,
max_num_paths: int = 2
** 64,
time_limit: float = float(
"inf"
),
**kwargs
)
Bases: AbstractPathModelDAG, AbstractWalkModelDiGraph
This is a generic class to find the “best” number of path/walks for optimization
problems implemented using AbstractPathModelDAG or
AbstractWalkModelDiGraph, and parameterized by the number of
paths/walks considered.
The class iterates over a range of k values, creating and
solving a model for each path/walk count until one of the stopping conditions is met.
Parameters
-
model_typeThe type of the model used for optimization.
-
stop_on_first_feasible : bool, optionalIf True, the optimization process stops as soon as a feasible solution is found. Default is None.
-
stop_on_delta_abs: float, optionalThe threshold for change (in absolute value) in objective value between iterations to determine stopping the optimization. Default is
None. -
stop_on_delta_rel: float, optionalThe relative threshold for change (in absolute value) in objective value between iterations to determine stopping the optimization. This is computed as the difference in objective value between iterations divided by the objective value of the previous iteration. Default is
None.Pass at least one stopping criterion
At least one of the stopping criterion must be set:
stop_on_first_feasible,stop_on_delta_abs,stop_on_delta_rel. -
stop_on_infeasible_on_delta : bool, optionalIf True (default), and a delta-based stopping mode is enabled (
stop_on_delta_absorstop_on_delta_rel), stop immediately and report infeasible when an iteration becomes infeasible (including the first tried iteration). -
min_num_paths : int, optionalMinimum number of paths/walks to be considered in the optimization. Default is 1. The class will also call
get_lowerbound_k()on themodel_typeand the provided arguments to get a better lower bound. -
max_num_paths : int, optionalMaximum number of paths/walks to be computed. Default is
2**64. -
time_limit : float, optionalTime limit (in seconds) for the optimization process. Default is
float("inf"). -
**kwargsThe keyword arguments to be passed to the model.
Note
Do not pass the parameter
khere, as it will be handled by the internal optimization process.
Raises
-
ValueErrorIf none of the stopping criteria (
stop_on_first_feasible,stop_on_delta_abs, orstop_on_delta_rel) is provided (i.e., all areNone).
Source code in flowpaths/numpathsoptimization.py
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 | |
solve_time_elapsed
property
Returns the elapsed time since the start of the solve process.
Returns
-
floatThe elapsed time in seconds.
get_objective_value
Returns the objective value of the model, if it is solved. Otherwise, raises an exception.
get_solution
Retrieves the solution from the wrapped model.
Parameters
-
remove_empty_paths: bool, optionalIf True, remove empty paths/walks (fewer than two nodes) and filter aligned list fields consistently. Default is False.
Returns
-
solution: dictThe solution obtained from the wrapped model.
Raises
exceptionIf model is not solved.
Source code in flowpaths/numpathsoptimization.py
is_valid_solution
Checks if the solution is valid, by calling the is_valid_solution() method of the model.
solve
Attempts to solve the optimization problem by iterating over a range of k values, creating and
solving a model for each count until one of the stopping conditions is met.
The method iterates from the maximum between the minimum allowed paths and a lower bound (via
get_lowerbound_k() of the model) to the maximum allowed paths. For each iteration:
- Creates a model instance with the current number of paths/walks (
k). - Solves the model, and checks if it has been successfully solved.
-
Applies various stopping criteria including:
stop_on_first_feasible: stops at the first feasible solution.stop_on_delta_abs: stops if the absolute change in the objective value between iterations is less than or equal to thestop_on_delta_absvalue.stop_on_delta_rel: stops if the relative change in the objective value between iterations is less than or equal to thestop_on_delta_relvalue.
-
Stops if the elapsed time exceeds the designated time limit.
Upon termination, the method sets the overall solve status:
- If no feasible solution was found, the status is marked as infeasible.
- If the process did not exceed the time limit but no other stopping condition was met, the status is marked as unbounded.
- If any of the stopping criteria (feasible or delta conditions) were satisfied, the status is set as solved.
If a valid solution is found, it stores the solution, updates solve statistics and the model,
marks the problem as solved, and returns True. Otherwise, it returns False.
Returns
-
boolTrueif an optimal solution is found and the problem is marked as solved,Falseotherwise.
Source code in flowpaths/numpathsoptimization.py
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |