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

Commit

Permalink
Merge branch 't/20303/fixes_for_the_cvxopt_mip_backend' into t/20354/…
Browse files Browse the repository at this point in the history
…pplbackend__add_support_for_integer_variables
  • Loading branch information
Matthias Koeppe committed Apr 5, 2016
2 parents 9f35b65 + e47b608 commit b0179c2
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 15 deletions.
17 changes: 14 additions & 3 deletions src/sage/numerical/backends/coin_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,21 @@ cdef class CoinBackend(GenericBackend):
4
sage: p.ncols() # optional - cbc
5
sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - cbc
sage: p.add_variables(2, lower_bound=-2.0, integer=True, obj=42.0, names=['a','b']) # optional - cbc
6
sage: p.col_name(5) # optional - cbc
TESTS:
Check that arguments are used::
sage: p.col_bounds(5) # tol 1e-8, optional - cbc
(-2.0, None)
sage: p.is_variable_integer(5) # optional - cbc
True
sage: p.col_name(5) # optional - cbc
'a'
sage: p.objective_coefficient(5) # tol 1e-8, optional - cbc
42.0
"""
#cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer))
cdef int vtype = int(binary) + int(continuous) + int(integer)
Expand Down Expand Up @@ -1685,4 +1696,4 @@ cdef class CoinBackend(GenericBackend):
raise MIPSolverException('CBC : Signal sent, getReducedCost() fails')
else:
cost = [c_cost[i] for i in range(n)]
return cost
return cost
15 changes: 14 additions & 1 deletion src/sage/numerical/backends/cplex_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,21 @@ cdef class CPLEXBackend(GenericBackend):
4
sage: p.ncols() # optional - CPLEX
5
sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - CPLEX
sage: p.add_variables(2, lower_bound=-2.0, integer=True, obj=42.0, names=['a','b']) # optional - CPLEX
6
TESTS:
Check that arguments are used::
sage: p.col_bounds(5) # tol 1e-8, optional - CPLEX
(-2.0, None)
sage: p.is_variable_integer(5) # optional - CPLEX
True
sage: p.col_name(5) # optional - CPLEX
'a'
sage: p.objective_coefficient(5) # tol 1e-8, optional - CPLEX
42.0
"""
cdef char * c_name
cdef double c_coeff = obj
Expand Down
33 changes: 26 additions & 7 deletions src/sage/numerical/backends/cvxopt_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ cdef class CVXOPTBackend(GenericBackend):
...
RuntimeError: CVXOPT only supports continuous variables
"""
if obj == None:
if obj is None:
obj = 0.0
if binary or integer:
raise RuntimeError("CVXOPT only supports continuous variables")
Expand All @@ -150,7 +150,7 @@ cdef class CVXOPTBackend(GenericBackend):
return len(self.objective_function) - 1


cpdef int add_variables(self, int n, lower_bound=None, upper_bound=None, binary=False, continuous=True, integer=False, obj=None, names=None) except -1:
cpdef int add_variables(self, int n, lower_bound=0.0, upper_bound=None, binary=False, continuous=True, integer=False, obj=None, names=None) except -1:
"""
Add ``n`` variables.
Expand Down Expand Up @@ -187,11 +187,23 @@ cdef class CVXOPTBackend(GenericBackend):
4
sage: p.ncols()
5
sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b'])
sage: p.add_variables(2, lower_bound=-2.0, obj=42.0, names=['a','b'])
6
TESTS:
Check that arguments are used::
sage: p.col_bounds(5) # tol 1e-8
(-2.0, None)
sage: p.col_name(5)
'a'
sage: p.objective_coefficient(5) # tol 1e-8
42.0
"""
for i in range(n):
self.add_variable()
self.add_variable(lower_bound, upper_bound, binary, continuous, integer, obj,
None if names is None else names[i])
return len(self.objective_function) - 1;


Expand Down Expand Up @@ -417,10 +429,17 @@ cdef class CVXOPTBackend(GenericBackend):
([], [])
sage: p.row_bounds(4)
(None, 2)
TESTS:
It does not add mysterious new variables::
sage: p.ncols()
5
"""
for i in range(number):
self.add_linear_constraint(zip(range(self.ncols()+1),[0]*(self.ncols()+1)),
lower_bound, upper_bound,
self.add_linear_constraint([], lower_bound, upper_bound,
name=None if names is None else names[i])

cpdef int solve(self) except -1:
Expand Down Expand Up @@ -1022,7 +1041,7 @@ cdef class CVXOPTBackend(GenericBackend):
sage: p.solver_parameter("show_progress")
True
"""
if value == None:
if value is None:
return self.param[name]
else:
self.param[name] = value
Expand Down
17 changes: 15 additions & 2 deletions src/sage/numerical/backends/glpk_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,21 @@ cdef class GLPKBackend(GenericBackend):
4
sage: p.ncols()
5
sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b'])
sage: p.add_variables(2, lower_bound=-2.0, integer=True, obj=42.0, names=['a','b'])
6
TESTS:
Check that arguments are used::
sage: p.col_bounds(5) # tol 1e-8
(-2.0, None)
sage: p.is_variable_integer(5)
True
sage: p.col_name(5)
'a'
sage: p.objective_coefficient(5)
42.0
"""
cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer))
if vtype == 0:
Expand Down Expand Up @@ -206,7 +219,7 @@ cdef class GLPKBackend(GenericBackend):
self.objective_coefficient(n_var - i - 1, obj)

if names is not None:
glp_set_col_name(self.lp, n_var, names[number - i - 1])
glp_set_col_name(self.lp, n_var - i, names[number - i - 1])

return n_var - 1

Expand Down
15 changes: 14 additions & 1 deletion src/sage/numerical/backends/gurobi_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,21 @@ cdef class GurobiBackend(GenericBackend):
4
sage: p.ncols() # optional - Gurobi
5
sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - Gurobi
sage: p.add_variables(2, lower_bound=-2.0, integer=True, obj=42.0, names=['a','b']) # optional - Gurobi
6
TESTS:
Check that arguments are used::
sage: p.col_bounds(5) # tol 1e-8, optional - Gurobi
(-2.0, None)
sage: p.is_variable_integer(5) # optional - Gurobi
True
sage: p.col_name(5) # optional - Gurobi
'a'
sage: p.objective_coefficient(5) # tol 1e-8, optional - Gurobi
42.0
"""
cdef int i
cdef int value
Expand Down
21 changes: 20 additions & 1 deletion src/sage/numerical/backends/ppl_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ cdef class PPLBackend(GenericBackend):
'x'
sage: p.objective_coefficient(2)
2/3
sage: p.add_variable(integer=True)
Traceback (most recent call last):
...
NotImplementedError: The PPL backend in Sage only supports continuous variables
"""
if binary or integer:
raise NotImplementedError("The PPL backend in Sage only supports continuous variables")
for i in range(len(self.Matrix)):
self.Matrix[i].append(0)
self.col_lower_bound.append(lower_bound)
Expand Down Expand Up @@ -226,9 +232,22 @@ cdef class PPLBackend(GenericBackend):
4
sage: p.ncols()
5
sage: p.add_variables(2, lower_bound=-2.0, names=['a','b'])
sage: p.add_variables(2, lower_bound=-2.0, obj=42.0, names=['a','b'])
6
TESTS:
Check that arguments are used::
sage: p.col_bounds(5) # tol 1e-8
(-2.0, None)
sage: p.col_name(5)
'a'
sage: p.objective_coefficient(5) # tol 1e-8
42.0
"""
if binary or integer:
raise NotImplementedError("The PPL backend in Sage only supports continuous variables")
for k in range(n):
for i in range(len(self.Matrix)):
self.Matrix[i].append(0)
Expand Down

0 comments on commit b0179c2

Please sign in to comment.