Skip to content

Commit 2db6f2a

Browse files
committed
Merge pull request #100 from aebrahim/cglpk_module_load
cglpk loaded as module, not class
2 parents 7e0ece2 + 33d518c commit 2db6f2a

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

cobra/solvers/__init__.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# attempt to import all working solvers in this directory
22
from __future__ import absolute_import
3-
from warnings import warn
3+
from warnings import warn as _warn
44
from os import name as __name
55

66
solver_dict = {}
@@ -44,10 +44,9 @@ def add_solver(solver_name, use_name=None):
4444
continue
4545
if i.startswith("parameters"):
4646
continue
47-
if i.endswith(".py") or i.endswith(".so"):
48-
possible_solvers.add(i[:-3])
49-
if i.endswith(".pyc") or i.endswith(".pyd"):
50-
possible_solvers.add(i[:-4])
47+
if i.endswith(".py") or i.endswith(".so") or i.endswith(".pyc") \
48+
or i.endswith(".pyd"):
49+
possible_solvers.add(i.split(".")[0])
5150

5251
for solver in possible_solvers:
5352
nicer_name = solver[:-7] if solver.endswith("_solver") else solver
@@ -57,15 +56,12 @@ def add_solver(solver_name, use_name=None):
5756
pass
5857
del solver, nicer_name
5958

60-
try:
61-
from .cglpk import GLP
62-
solver_dict["cglpk"] = GLP
63-
except:
64-
None
65-
6659
del path, listdir
6760
del i, possible_solvers
6861

62+
if len(solver_dict) == 0:
63+
_warn("No LP solvers found")
64+
6965
def get_solver_name(mip=False, qp=False):
7066
"""returns a solver name"""
7167
if len(solver_dict) == 0:
@@ -86,7 +82,7 @@ def get_solver_name(mip=False, qp=False):
8682
return solver_name
8783
for solver_name in solver_dict:
8884
if solver_name not in qp_incapable:
89-
warn("could not verify if %s supports qp" % solver_name)
85+
_warn("could not verify if %s supports qp" % solver_name)
9086
return solver_name
9187
return None # don't want to return glpk
9288
else:

cobra/solvers/cglpk.pyx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,24 @@ cdef class GLP:
325325
glp_copy_prob(other.glp, self.glp, GLP_ON)
326326
return other
327327

328+
# wrappers for all the functions at the module level
329+
create_problem = GLP.create_problem
330+
def set_objective_sense(lp, objective_sense="maximize"):
331+
return lp.set_objective_sense(lp, objective_sense=objective_sense)
332+
cpdef change_variable_bounds(lp, int index, double lower_bound, double upper_bound):
333+
return lp.change_variable_bounds(index, lower_bound, upper_bound)
334+
cpdef change_variable_objective(lp, int index, double value):
335+
return lp.change_variable_objective(index, value)
336+
cpdef change_coefficient(lp, int met_index, int rxn_index, double value):
337+
return lp.change_coefficient(met_index, rxn_index, value)
338+
cpdef set_parameter(lp, parameter_name, value):
339+
return lp.set_parameter(parameter_name, value)
340+
def solve_problem(lp, **kwargs):
341+
return lp.solve_problem(**kwargs)
342+
cpdef get_status(lp):
343+
return lp.get_status()
344+
cpdef get_objective_value(lp):
345+
return lp.get_objective_value()
346+
cpdef format_solution(lp, cobra_model):
347+
return lp.format_solution(cobra_model)
348+
solve = GLP.solve

0 commit comments

Comments
 (0)