Skip to content

Commit 190e95d

Browse files
Fix GLPK tolerance issue
This fixes issue #22 by (1) making sure that plot_flux_space uses one solver consistently throughout all optimizations (2) GLPK tol_bnd was reset from 1e-9 to the default (1e-7). Hopefully that change won't have to be reversed, later.
1 parent 2c116bb commit 190e95d

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

straindesign/glpk_interface.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def __init__(self, c, A_ineq, b_ineq, A_eq, b_eq, lb, ub, vtype, indic_constr, M
183183
glp_add_rows(self.glpk, A_ineq.shape[0] + A_eq.shape[0] + A_indic.shape[0])
184184
eq_type = [GLP_UP] * len(b_ineq) + [GLP_FX] * len(b_eq) + [GLP_UP] * len(b_indic)
185185
for i, t, b in zip(range(len(b_ineq + b_eq + b_indic)), eq_type, b_ineq + b_eq + b_indic):
186-
glp_set_row_bnds(self.glpk, i + 1, t, b, b)
186+
glp_set_row_bnds(self.glpk, i + 1, t, float(b), float(b))
187187

188188
A = sparse.vstack((A_ineq, A_eq, A_indic), 'coo')
189189
ia = intArray(A.nnz + 1)
@@ -201,7 +201,7 @@ def __init__(self, c, A_ineq, b_ineq, A_eq, b_eq, lb, ub, vtype, indic_constr, M
201201
self.lp_params = glp_smcp()
202202
glp_init_smcp(self.lp_params)
203203
self.max_tlim = self.lp_params.tm_lim
204-
self.lp_params.tol_bnd = 1e-9
204+
# self.lp_params.tol_bnd = 1e-9
205205
self.lp_params.msg_lev = 0
206206
# MILP parameters
207207
if self.ismilp:
@@ -368,13 +368,13 @@ def set_ub(self, ub):
368368
type = [glp_get_col_type(self.glpk, i + 1) for i in setvars]
369369
for i, l, u, t in zip(setvars, lb, ub, type):
370370
if t in [GLP_FR, GLP_LO] and isinf(u):
371-
glp_set_col_bnds(self.glpk, i + 1, t, l, u)
371+
glp_set_col_bnds(self.glpk, i + 1, t, float(l), float(u))
372372
elif t == GLP_UP and isinf(u):
373-
glp_set_col_bnds(self.glpk, i + 1, GLP_FR, l, u)
373+
glp_set_col_bnds(self.glpk, i + 1, GLP_FR, float(l), float(u))
374374
elif t in [GLP_LO, GLP_DB, GLP_FX] and not isinf(u) and l < u:
375-
glp_set_col_bnds(self.glpk, i + 1, GLP_DB, l, u)
375+
glp_set_col_bnds(self.glpk, i + 1, GLP_DB, float(l), float(u))
376376
elif t in [GLP_LO, GLP_DB, GLP_FX] and not isinf(u) and l == u:
377-
glp_set_col_bnds(self.glpk, i + 1, GLP_FX, l, u)
377+
glp_set_col_bnds(self.glpk, i + 1, GLP_FX, float(l), float(u))
378378

379379
def set_time_limit(self, t):
380380
"""Set the computation time limit (in seconds)"""
@@ -413,9 +413,9 @@ def add_ineq_constraints(self, A_ineq, b_ineq):
413413
val[i + 1] = float(v)
414414
glp_set_mat_row(self.glpk, numrows + j + 1, numvars, col, val)
415415
if isinf(b_ineq[j]):
416-
glp_set_row_bnds(self.glpk, numrows + j + 1, GLP_FR, -inf, b_ineq[j])
416+
glp_set_row_bnds(self.glpk, numrows + j + 1, GLP_FR, -inf, float(b_ineq[j]))
417417
else:
418-
glp_set_row_bnds(self.glpk, numrows + j + 1, GLP_UP, -inf, b_ineq[j])
418+
glp_set_row_bnds(self.glpk, numrows + j + 1, GLP_UP, -inf, float(b_ineq[j]))
419419

420420
def add_eq_constraints(self, A_eq, b_eq):
421421
"""Add equality constraints to the model
@@ -442,7 +442,7 @@ def add_eq_constraints(self, A_eq, b_eq):
442442
col[i + 1] = i + 1
443443
val[i + 1] = float(v)
444444
glp_set_mat_row(self.glpk, numrows + j + 1, numvars, col, val)
445-
glp_set_row_bnds(self.glpk, numrows + j + 1, GLP_FX, b_eq[j], b_eq[j])
445+
glp_set_row_bnds(self.glpk, numrows + j + 1, GLP_FX, float(b_eq[j]), float(b_eq[j]))
446446

447447
def set_ineq_constraint(self, idx, a_ineq, b_ineq):
448448
"""Replace a specific inequality constraint
@@ -467,9 +467,9 @@ def set_ineq_constraint(self, idx, a_ineq, b_ineq):
467467
val[i + 1] = float(v)
468468
glp_set_mat_row(self.glpk, idx + 1, numvars, col, val)
469469
if isinf(b_ineq):
470-
glp_set_row_bnds(self.glpk, idx + 1, GLP_FR, -inf, b_ineq)
470+
glp_set_row_bnds(self.glpk, idx + 1, GLP_FR, -inf, float(b_ineq))
471471
else:
472-
glp_set_row_bnds(self.glpk, idx + 1, GLP_UP, -inf, b_ineq)
472+
glp_set_row_bnds(self.glpk, idx + 1, GLP_UP, -inf, float(b_ineq))
473473

474474
def getSolution(self, status) -> list:
475475
"""Retrieve solution from GLPK backend"""

straindesign/lptools.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,11 @@ def plot_flux_space(model, axes, **kwargs) -> Tuple[list, list, list]:
855855
elif ax_type[0] == 'yield':
856856
constr += [[{**axes[0][0], **{k: -v * x for k, v in axes[0][1].items()}}, '=', 0]]
857857
if ax_type[1] == 'rate':
858-
sol_vmin = fba(model, constraints=constr, obj=axes[1][0], obj_sense='minimize')
859-
sol_vmax = fba(model, constraints=constr, obj=axes[1][0], obj_sense='maximize')
858+
sol_vmin = fba(model, constraints=constr, obj=axes[1][0], solver=solver, obj_sense='minimize')
859+
sol_vmax = fba(model, constraints=constr, obj=axes[1][0], solver=solver, obj_sense='maximize')
860860
elif ax_type[1] == 'yield':
861-
sol_vmin = yopt(model, constraints=constr, obj_num=axes[1][0], obj_den=axes[1][1], obj_sense='minimize')
862-
sol_vmax = yopt(model, constraints=constr, obj_num=axes[1][0], obj_den=axes[1][1], obj_sense='maximize')
861+
sol_vmin = yopt(model, constraints=constr, obj_num=axes[1][0], obj_den=axes[1][1], solver=solver, obj_sense='minimize')
862+
sol_vmax = yopt(model, constraints=constr, obj_num=axes[1][0], obj_den=axes[1][1], solver=solver, obj_sense='maximize')
863863
lb[i] = ceil_dec(sol_vmin.objective_value, 9)
864864
ub[i] = floor_dec(sol_vmax.objective_value, 9)
865865

0 commit comments

Comments
 (0)