Skip to content

Commit 3ef066f

Browse files
committed
Fix how tolerance is used
1 parent e4b9dd3 commit 3ef066f

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

cobra/flux_analysis/find_active_reactions.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def find_active_reactions(model, bigM=10000, zero_cutoff=None,
3030
a large constant for bounding the optimization problem, default 1e4.
3131
zero_cutoff: float, optional
3232
The cutoff to consider for zero flux
33-
(default 1e-5 if relax_bounds is True, else model.tolerance).
33+
Default model.tolerance.
3434
relax_bounds: True or False
3535
Whether to relax the model bounds.
3636
All +ve LB set as 0, all -ve UB set as 0, all -ve LB set as -bigM,
@@ -150,14 +150,14 @@ def find_active_reactions(model, bigM=10000, zero_cutoff=None,
150150
active_rxns += [r.id for r in new_active_rxns]
151151

152152
# loop to find any hidden forward active reactions
153-
constr_min_flux, n_lp_solved = loop_to_find_fwd_active_rxns(
153+
constr_min_flux, n_lp_solved, min_flux = loop_to_find_fwd_active_rxns(
154154
model, z_pos, z_neg, active_rxns, new_active_rxns, rxns_to_check,
155155
eps, max_iterations)
156156

157157
# loop to find any hidden reverse active reactions
158158
loop_to_find_rev_active_rxns(model, active_rxns, rxns_to_check,
159159
constr_min_flux, n_lp_solved,
160-
eps, max_iterations)
160+
eps, min_flux, max_iterations)
161161

162162
return active_rxns
163163

@@ -243,15 +243,11 @@ def build_opt_problem(model, bigM=10000, zero_cutoff=None, relax_bounds=True,
243243
if max_bound < float("inf"):
244244
bigM = max(bigM, max_bound)
245245

246-
# at least 10x >= model.tolerance
247-
eps = model.tolerance * 1e2
246+
# ensure bigM*z << eps at tolerance limit
247+
eps = model.tolerance * bigM * 10
248248
if zero_cutoff is not None:
249249
eps = max(zero_cutoff, eps)
250250

251-
# ensure bigM*z << eps at integrality tolerance limit
252-
if solve == "milp":
253-
eps = max(eps, model.tolerance * bigM * 10)
254-
255251
LOGGER.debug("parameters:\nbigM\t%.f\neps\t%.2e\nfeas_tol\t%.2e",
256252
bigM, eps, model.tolerance)
257253

@@ -410,9 +406,12 @@ def loop_to_find_fwd_active_rxns(model, z_pos, z_neg, active_rxns,
410406

411407
weight_random = {r: np.round(np.random.random(), 6) for r in
412408
rxns_to_check}
409+
min_flux = sum(w for w in weight_random.values()) * model.tolerance * 10
410+
min_flux = max(eps, min_flux)
411+
LOGGER.debug("min_flux: %.4e" % min_flux)
413412
constr_min_flux = model.problem.Constraint(
414-
sum([weight_random[r] * (r.flux_expression) for r in
415-
rxns_to_check]), lb=eps)
413+
sum(w * r.flux_expression for r,w in
414+
weight_random.items()), lb=min_flux)
416415
model.add_cons_vars(constr_min_flux)
417416

418417
n_lp_solved = 3
@@ -442,12 +441,12 @@ def loop_to_find_fwd_active_rxns(model, z_pos, z_neg, active_rxns,
442441
constr_min_flux.set_linear_coefficients(
443442
{r.forward_variable: 0, r.reverse_variable: 0})
444443

445-
return (constr_min_flux, n_lp_solved)
444+
return (constr_min_flux, n_lp_solved, min_flux)
446445

447446

448447
def loop_to_find_rev_active_rxns(model, active_rxns, rxns_to_check,
449448
constr_min_flux, n_lp_solved,
450-
eps, max_iterations):
449+
eps, min_flux, max_iterations):
451450
"""
452451
Subroutine of `find_active_reactions`.
453452
Find flux distributions with >=1 negative flux until infeasibility.
@@ -458,8 +457,8 @@ def loop_to_find_rev_active_rxns(model, active_rxns, rxns_to_check,
458457
" reactions are found:")
459458

460459
# change the constraint into minimum negative flux
461-
constr_min_flux.lb = -eps
462-
constr_min_flux.ub = -eps
460+
constr_min_flux.lb = -min_flux
461+
constr_min_flux.ub = -min_flux
463462
constr_min_flux.lb = None
464463
feas_tol = model.tolerance
465464

0 commit comments

Comments
 (0)