Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac 10232: catch some errors in GLPK backend
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Apr 3, 2016
1 parent 7ffcdd1 commit 6503bc5
Showing 1 changed file with 83 additions and 25 deletions.
108 changes: 83 additions & 25 deletions src/sage/numerical/backends/glpk_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ cdef class GLPKBackend(GenericBackend):
Traceback (most recent call last):
...
ValueError: ...
sage: p.add_variable(name='x',obj=1.0)
sage: p.add_variable(name='x', obj=1.0)
3
sage: p.col_name(3)
'x'
Expand Down Expand Up @@ -1395,33 +1395,62 @@ cdef class GLPKBackend(GenericBackend):
sage: P.set_max(x, 0)
sage: P.get_max(x)
0.0
Check that :trac:`10232` is fixed::
sage: p = get_solver(solver="GLPK")
sage: p.variable_upper_bound(2)
Traceback (most recent call last):
...
GLPKError: ...
sage: p.variable_upper_bound(3, 5)
Traceback (most recent call last):
...
GLPKError: ...
sage: p.add_variable()
0
sage: p.variable_upper_bound(0, 'hey!')
Traceback (most recent call last):
...
TypeError: a float is required
"""
cdef double x
cdef double min
cdef double dvalue

if value is False:
sig_on()
x = glp_get_col_ub(self.lp, index +1)
sig_off()
if x == DBL_MAX:
return None
else:
return x
else:
sig_on()
min = glp_get_col_lb(self.lp, index + 1)
sig_off()

if value is None and min == -DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_FR, 0, 0)

elif value is None:
glp_set_col_bnds(self.lp, index + 1, GLP_LO, min, 0)

elif min == -DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_UP, 0, value)
if value is None:
sig_on()
if min == -DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_FR, 0, 0)
else:
glp_set_col_bnds(self.lp, index + 1, GLP_LO, min, 0)
sig_off()
else:
dvalue = <double?> value

elif min == value:
glp_set_col_bnds(self.lp, index + 1, GLP_FX, value, value)
sig_on()
if min == -DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_UP, 0, dvalue)

else:
glp_set_col_bnds(self.lp, index + 1, GLP_DB, min, value)
elif min == dvalue:
glp_set_col_bnds(self.lp, index + 1, GLP_FX, dvalue, dvalue)
else:
glp_set_col_bnds(self.lp, index + 1, GLP_DB, min, dvalue)
sig_off()

cpdef variable_lower_bound(self, int index, value = False):
"""
Expand Down Expand Up @@ -1457,33 +1486,62 @@ cdef class GLPKBackend(GenericBackend):
sage: P.set_min(x, 0)
sage: P.get_min(x)
0.0
Check that :trac:`10232` is fixed::
sage: p = get_solver(solver="GLPK")
sage: p.variable_lower_bound(2)
Traceback (most recent call last):
...
GLPKError: ...
sage: p.variable_lower_bound(3, 5)
Traceback (most recent call last):
...
GLPKError: ...
sage: p.add_variable()
0
sage: p.variable_lower_bound(0, 'hey!')
Traceback (most recent call last):
...
TypeError: a float is required
"""
cdef double x
cdef double max
cdef double dvalue

if value is False:
sig_on()
x = glp_get_col_lb(self.lp, index +1)
sig_off()
if x == -DBL_MAX:
return None
else:
return x
else:
sig_on()
max = glp_get_col_ub(self.lp, index + 1)
sig_off()

if value is None and max == DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_FR, 0.0, 0.0)

elif value is None:
glp_set_col_bnds(self.lp, index + 1, GLP_UP, 0.0, max)

elif max == DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_LO, value, 0.0)

elif max == value:
glp_set_col_bnds(self.lp, index + 1, GLP_FX, value, value)
if value is None:
sig_on()
if max == DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_FR, 0.0, 0.0)
else:
glp_set_col_bnds(self.lp, index + 1, GLP_UP, 0.0, max)
sig_off()

else:
glp_set_col_bnds(self.lp, index + 1, GLP_DB, value, max)
dvalue = <double?> value

sig_on()
if max == DBL_MAX:
glp_set_col_bnds(self.lp, index + 1, GLP_LO, value, 0.0)
elif max == value:
glp_set_col_bnds(self.lp, index + 1, GLP_FX, value, value)
else:
glp_set_col_bnds(self.lp, index + 1, GLP_DB, value, max)
sig_off()

cpdef write_lp(self, char * filename):
"""
Expand Down

0 comments on commit 6503bc5

Please sign in to comment.