Skip to content

Commit

Permalink
add time limit as permissible solver state
Browse files Browse the repository at this point in the history
  • Loading branch information
cdiener authored and Midnighter committed Sep 15, 2018
1 parent 0fc8ea2 commit 3b39e7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 16 additions & 0 deletions cobra/test/test_solver_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

import cobra.util.solver as su
from cobra.exceptions import OptimizationError

stable_optlang = ["glpk", "cplex", "gurobi"]
optlang_solvers = ["optlang-" + s for s in stable_optlang if s in su.solvers]
Expand Down Expand Up @@ -129,3 +130,18 @@ def test_fix_objective_as_constraint_minimize(self, model, solver):
model.slim_optimize()
assert (constr[fx_name].lb, constr[fx_name].ub) == (
None, model.solver.objective.value)


def test_time_limit(large_model):
if su.interface_to_str(large_model.problem) != "glpk":
pytest.skip("requires GLPK")

# have to do it like that since optlang only accepts inputs in seconds
# whereas GLPK accets milliseconds
large_model.solver.configuration._smcp.tm_lim = 1
with pytest.warns(UserWarning):
sol = large_model.optimize()
assert sol.fluxes is not None

with pytest.raises(OptimizationError):
sol = large_model.optimize(raise_error=True)
5 changes: 3 additions & 2 deletions cobra/util/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import optlang
from optlang.symbolics import Basic, Zero
from optlang.interface import (OPTIMAL, NUMERIC, FEASIBLE, INFEASIBLE,
SUBOPTIMAL, ITERATION_LIMIT)
SUBOPTIMAL, ITERATION_LIMIT, TIME_LIMIT)

from cobra.exceptions import OptimizationError, SolverNotFound,\
OPTLANG_TO_EXCEPTIONS_DICT
Expand All @@ -36,7 +36,8 @@
qp_solvers = ["cplex", "gurobi"]

# optlang solution statuses which still allow retrieving primal values
has_primals = [NUMERIC, FEASIBLE, INFEASIBLE, SUBOPTIMAL, ITERATION_LIMIT]
has_primals = [NUMERIC, FEASIBLE, INFEASIBLE, SUBOPTIMAL, ITERATION_LIMIT,
TIME_LIMIT]


def linear_reaction_coefficients(model, reactions=None):
Expand Down

0 comments on commit 3b39e7b

Please sign in to comment.