CVXOPT Backend¶
AUTHORS:
Ingolfur Edvardsson (2014-05): initial implementation
- class sage.numerical.backends.cvxopt_backend.CVXOPTBackend[source]¶
Bases:
GenericBackend
MIP Backend that uses the CVXOPT solver.
There is no support for integer variables.
EXAMPLES:
sage: p = MixedIntegerLinearProgram(solver='CVXOPT')
>>> from sage.all import * >>> p = MixedIntegerLinearProgram(solver='CVXOPT')
- add_col(indices, coeffs)[source]¶
Add a column.
INPUT:
indices
– list of integers; this list contains the indices of the constraints in which the variable’s coefficient is nonzerocoeffs
– list of real values; associates a coefficient to the variable in each of the constraints in which it appears. Namely, the i-th entry ofcoeffs
corresponds to the coefficient of the variable in the constraint represented by the i-th entry inindices
.
Note
indices
andcoeffs
are expected to be of the same length.EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.ncols() 0 sage: p.nrows() 0 sage: p.add_linear_constraints(5, 0, None) sage: p.add_col(range(5), range(5)) sage: p.nrows() 5
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.ncols() 0 >>> p.nrows() 0 >>> p.add_linear_constraints(Integer(5), Integer(0), None) >>> p.add_col(range(Integer(5)), range(Integer(5))) >>> p.nrows() 5
- add_linear_constraint(coefficients, lower_bound, upper_bound, name=None)[source]¶
Add a linear constraint.
INPUT:
coefficients
an iterable with(c,v)
pairs wherec
is a variable index (integer) andv
is a value (real value).lower_bound
– a lower bound, either a real value orNone
upper_bound
– an upper bound, either a real value orNone
name
– an optional name for this row (default:None
)
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variables(5) 4 sage: p.add_linear_constraint(zip(range(5), range(5)), 2.0, 2.0) sage: p.row(0) ([1, 2, 3, 4], [1, 2, 3, 4]) sage: p.row_bounds(0) (2.00000000000000, 2.00000000000000) sage: p.add_linear_constraint(zip(range(5), range(5)), 1.0, 1.0, name='foo') sage: p.row_name(-1) 'foo'
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variables(Integer(5)) 4 >>> p.add_linear_constraint(zip(range(Integer(5)), range(Integer(5))), RealNumber('2.0'), RealNumber('2.0')) >>> p.row(Integer(0)) ([1, 2, 3, 4], [1, 2, 3, 4]) >>> p.row_bounds(Integer(0)) (2.00000000000000, 2.00000000000000) >>> p.add_linear_constraint(zip(range(Integer(5)), range(Integer(5))), RealNumber('1.0'), RealNumber('1.0'), name='foo') >>> p.row_name(-Integer(1)) 'foo'
- add_variable(lower_bound=0.0, upper_bound=None, binary=False, continuous=True, integer=False, obj=None, name=None)[source]¶
Add a variable.
This amounts to adding a new column to the matrix. By default, the variable is both positive and real. Variable types are always continuous, and thus the parameters
binary
,integer
, andcontinuous
have no effect.INPUT:
lower_bound
– the lower bound of the variable (default: 0)upper_bound
– the upper bound of the variable (default:None
)binary
–True
if the variable is binary (default:False
)continuous
–True
if the variable is continuous (default:True
)integer
–True
if the variable is integer (default:False
)obj
– (optional) coefficient of this variable in the objective function (default: 0.0)name
– an optional name for the newly added variable (default:None
)
OUTPUT: the index of the newly created variable
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.ncols() 0 sage: p.add_variable() 0 sage: p.ncols() 1 sage: p.add_variable() 1 sage: p.add_variable(lower_bound=-2.0) 2 sage: p.add_variable(continuous=True) 3 sage: p.add_variable(name='x',obj=1.0) 4 sage: p.col_name(3) 'x_3' sage: p.col_name(4) 'x' sage: p.objective_coefficient(4) 1.00000000000000
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.ncols() 0 >>> p.add_variable() 0 >>> p.ncols() 1 >>> p.add_variable() 1 >>> p.add_variable(lower_bound=-RealNumber('2.0')) 2 >>> p.add_variable(continuous=True) 3 >>> p.add_variable(name='x',obj=RealNumber('1.0')) 4 >>> p.col_name(Integer(3)) 'x_3' >>> p.col_name(Integer(4)) 'x' >>> p.objective_coefficient(Integer(4)) 1.00000000000000
- col_bounds(index)[source]¶
Return the bounds of a specific variable.
INPUT:
index
– integer; the variable’s id
OUTPUT:
A pair
(lower_bound, upper_bound)
. Each of them can be set toNone
if the variable is not bounded in the corresponding direction, and is a real value otherwise.EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variable() 0 sage: p.col_bounds(0) (0.0, None) sage: p.variable_upper_bound(0, 5) sage: p.col_bounds(0) (0.0, 5)
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variable() 0 >>> p.col_bounds(Integer(0)) (0.0, None) >>> p.variable_upper_bound(Integer(0), Integer(5)) >>> p.col_bounds(Integer(0)) (0.0, 5)
- col_name(index)[source]¶
Return the
index
-th col name.INPUT:
index
– integer; the col’s idname
– (char *
) its name; when set toNULL
(default), the method returns the current name
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variable(name="I am a variable") 0 sage: p.col_name(0) 'I am a variable'
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variable(name="I am a variable") 0 >>> p.col_name(Integer(0)) 'I am a variable'
- get_objective_value()[source]¶
Return the value of the objective function.
Note
Behaviour is undefined unless
solve
has been called before.EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='cvxopt') sage: p.add_variables(2) 1 sage: p.add_linear_constraint([(0,1), (1,2)], None, 3) sage: p.set_objective([2, 5]) sage: p.solve() 0 sage: N(p.get_objective_value(),4) 7.5 sage: N(p.get_variable_value(0),4) 3.6e-7 sage: N(p.get_variable_value(1),4) 1.5
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='cvxopt') >>> p.add_variables(Integer(2)) 1 >>> p.add_linear_constraint([(Integer(0),Integer(1)), (Integer(1),Integer(2))], None, Integer(3)) >>> p.set_objective([Integer(2), Integer(5)]) >>> p.solve() 0 >>> N(p.get_objective_value(),Integer(4)) 7.5 >>> N(p.get_variable_value(Integer(0)),Integer(4)) 3.6e-7 >>> N(p.get_variable_value(Integer(1)),Integer(4)) 1.5
- get_variable_value(variable)[source]¶
Return the value of a variable given by the solver.
Note
Behaviour is undefined unless
solve
has been called before.EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variables(2) 1 sage: p.add_linear_constraint([(0,1), (1, 2)], None, 3) sage: p.set_objective([2, 5]) sage: p.solve() 0 sage: N(p.get_objective_value(),4) 7.5 sage: N(p.get_variable_value(0),4) 3.6e-7 sage: N(p.get_variable_value(1),4) 1.5
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variables(Integer(2)) 1 >>> p.add_linear_constraint([(Integer(0),Integer(1)), (Integer(1), Integer(2))], None, Integer(3)) >>> p.set_objective([Integer(2), Integer(5)]) >>> p.solve() 0 >>> N(p.get_objective_value(),Integer(4)) 7.5 >>> N(p.get_variable_value(Integer(0)),Integer(4)) 3.6e-7 >>> N(p.get_variable_value(Integer(1)),Integer(4)) 1.5
- is_maximization()[source]¶
Test whether the problem is a maximization
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.is_maximization() True sage: p.set_sense(-1) sage: p.is_maximization() False
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.is_maximization() True >>> p.set_sense(-Integer(1)) >>> p.is_maximization() False
- is_variable_binary(index)[source]¶
Test whether the given variable is of binary type. CVXOPT does not allow integer variables, so this is a bit moot.
INPUT:
index
– integer; the variable’s id
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.ncols() 0 sage: p.add_variable() 0 sage: p.set_variable_type(0,0) Traceback (most recent call last): ... ValueError: ... sage: p.is_variable_binary(0) False
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.ncols() 0 >>> p.add_variable() 0 >>> p.set_variable_type(Integer(0),Integer(0)) Traceback (most recent call last): ... ValueError: ... >>> p.is_variable_binary(Integer(0)) False
- is_variable_continuous(index)[source]¶
Test whether the given variable is of continuous/real type. CVXOPT does not allow integer variables, so this is a bit moot.
INPUT:
index
– integer; the variable’s id
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.ncols() 0 sage: p.add_variable() 0 sage: p.is_variable_continuous(0) True sage: p.set_variable_type(0,1) Traceback (most recent call last): ... ValueError: ... sage: p.is_variable_continuous(0) True
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.ncols() 0 >>> p.add_variable() 0 >>> p.is_variable_continuous(Integer(0)) True >>> p.set_variable_type(Integer(0),Integer(1)) Traceback (most recent call last): ... ValueError: ... >>> p.is_variable_continuous(Integer(0)) True
- is_variable_integer(index)[source]¶
Test whether the given variable is of integer type. CVXOPT does not allow integer variables, so this is a bit moot.
INPUT:
index
– integer; the variable’s id
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.ncols() 0 sage: p.add_variable() 0 sage: p.set_variable_type(0,-1) sage: p.set_variable_type(0,1) Traceback (most recent call last): ... ValueError: ... sage: p.is_variable_integer(0) False
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.ncols() 0 >>> p.add_variable() 0 >>> p.set_variable_type(Integer(0),-Integer(1)) >>> p.set_variable_type(Integer(0),Integer(1)) Traceback (most recent call last): ... ValueError: ... >>> p.is_variable_integer(Integer(0)) False
- ncols()[source]¶
Return the number of columns/variables.
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.ncols() 0 sage: p.add_variables(2) 1 sage: p.ncols() 2
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.ncols() 0 >>> p.add_variables(Integer(2)) 1 >>> p.ncols() 2
- nrows()[source]¶
Return the number of rows/constraints.
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.nrows() 0 sage: p.add_variables(5) 4 sage: p.add_linear_constraints(2, 2.0, None) sage: p.nrows() 2
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.nrows() 0 >>> p.add_variables(Integer(5)) 4 >>> p.add_linear_constraints(Integer(2), RealNumber('2.0'), None) >>> p.nrows() 2
- objective_coefficient(variable, coeff=None)[source]¶
Set or get the coefficient of a variable in the objective function
INPUT:
variable
– integer; the variable’s idcoeff
– double; its coefficient
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variable() 0 sage: p.objective_coefficient(0) 0.0 sage: p.objective_coefficient(0,2) sage: p.objective_coefficient(0) 2.0
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variable() 0 >>> p.objective_coefficient(Integer(0)) 0.0 >>> p.objective_coefficient(Integer(0),Integer(2)) >>> p.objective_coefficient(Integer(0)) 2.0
- problem_name(name=None)[source]¶
Return or define the problem’s name.
INPUT:
name
– string; the problem’s name. When set toNone
(default), the method returns the problem’s name.
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.problem_name() '' sage: p.problem_name("There once was a french fry") sage: print(p.problem_name()) There once was a french fry
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.problem_name() '' >>> p.problem_name("There once was a french fry") >>> print(p.problem_name()) There once was a french fry
- row(i)[source]¶
Return a row.
INPUT:
index
– integer; the constraint’s id
OUTPUT:
A pair
(indices, coeffs)
whereindices
lists the entries whose coefficient is nonzero, and to whichcoeffs
associates their coefficient on the model of theadd_linear_constraint
method.EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variables(5) 4 sage: p.add_linear_constraint(list(zip(range(5), range(5))), 2, 2) sage: p.row(0) ([1, 2, 3, 4], [1, 2, 3, 4]) sage: p.row_bounds(0) (2, 2)
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variables(Integer(5)) 4 >>> p.add_linear_constraint(list(zip(range(Integer(5)), range(Integer(5)))), Integer(2), Integer(2)) >>> p.row(Integer(0)) ([1, 2, 3, 4], [1, 2, 3, 4]) >>> p.row_bounds(Integer(0)) (2, 2)
- row_bounds(index)[source]¶
Return the bounds of a specific constraint.
INPUT:
index
– integer; the constraint’s id
OUTPUT:
A pair
(lower_bound, upper_bound)
. Each of them can be set toNone
if the constraint is not bounded in the corresponding direction, and is a real value otherwise.EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variables(5) 4 sage: p.add_linear_constraint(list(zip(range(5), range(5))), 2, 2) sage: p.row(0) ([1, 2, 3, 4], [1, 2, 3, 4]) sage: p.row_bounds(0) (2, 2)
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variables(Integer(5)) 4 >>> p.add_linear_constraint(list(zip(range(Integer(5)), range(Integer(5)))), Integer(2), Integer(2)) >>> p.row(Integer(0)) ([1, 2, 3, 4], [1, 2, 3, 4]) >>> p.row_bounds(Integer(0)) (2, 2)
- row_name(index)[source]¶
Return the
index
-th row name.INPUT:
index
– integer; the row’s id
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_linear_constraints(1, 2, None, names=["Empty constraint 1"]) sage: p.row_name(0) 'Empty constraint 1'
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_linear_constraints(Integer(1), Integer(2), None, names=["Empty constraint 1"]) >>> p.row_name(Integer(0)) 'Empty constraint 1'
- set_objective(coeff, d=0.0)[source]¶
Set the objective function.
INPUT:
coeff
– list of real values, whose i-th element is the coefficient of the i-th variable in the objective functiond
– double; the constant term in the linear function (set to \(0\) by default)
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variables(5) 4 sage: p.set_objective([1, 1, 2, 1, 3]) sage: [p.objective_coefficient(x) for x in range(5)] [1, 1, 2, 1, 3]
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variables(Integer(5)) 4 >>> p.set_objective([Integer(1), Integer(1), Integer(2), Integer(1), Integer(3)]) >>> [p.objective_coefficient(x) for x in range(Integer(5))] [1, 1, 2, 1, 3]
- set_sense(sense)[source]¶
Set the direction (maximization/minimization).
INPUT:
sense
– integer:\(+1\) => Maximization
\(-1\) => Minimization
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.is_maximization() True sage: p.set_sense(-1) sage: p.is_maximization() False
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.is_maximization() True >>> p.set_sense(-Integer(1)) >>> p.is_maximization() False
- set_variable_type(variable, vtype)[source]¶
Set the type of a variable.
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='cvxopt') sage: p.add_variables(5) 4 sage: p.set_variable_type(3, -1) sage: p.set_variable_type(3, -2) Traceback (most recent call last): ... ValueError: ...
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='cvxopt') >>> p.add_variables(Integer(5)) 4 >>> p.set_variable_type(Integer(3), -Integer(1)) >>> p.set_variable_type(Integer(3), -Integer(2)) Traceback (most recent call last): ... ValueError: ...
- solve()[source]¶
Solve the problem.
Note
This method raises
MIPSolverException
exceptions when the solution cannot be computed for any reason (none exists, or the LP solver was not able to find it, etc…)EXAMPLES:
sage: p = MixedIntegerLinearProgram(solver='cvxopt', maximization=False) sage: x = p.new_variable(nonnegative=True) sage: p.set_objective(-4*x[0] - 5*x[1]) sage: p.add_constraint(2*x[0] + x[1] <= 3) sage: p.add_constraint(2*x[1] + x[0] <= 3) sage: N(p.solve(), digits=2) -9.0 sage: p = MixedIntegerLinearProgram(solver='cvxopt', maximization=False) sage: x = p.new_variable(nonnegative=True) sage: p.set_objective(x[0] + 2*x[1]) sage: p.add_constraint(-5*x[0] + x[1] <= 7) sage: p.add_constraint(-5*x[0] + x[1] >= 7) sage: p.add_constraint(x[0] + x[1] >= 26) sage: p.add_constraint(x[0] >= 3) sage: p.add_constraint(x[1] >= 4) sage: N(p.solve(), digits=4) 48.83 sage: p = MixedIntegerLinearProgram(solver='cvxopt') sage: x = p.new_variable(nonnegative=True) sage: p.set_objective(x[0] + x[1] + 3*x[2]) sage: p.solver_parameter("show_progress",True) sage: p.add_constraint(x[0] + 2*x[1] <= 4) sage: p.add_constraint(5*x[2] - x[1] <= 8) sage: N(p.solve(), digits=2) pcost dcost gap pres dres k/t ... 8.8
>>> from sage.all import * >>> p = MixedIntegerLinearProgram(solver='cvxopt', maximization=False) >>> x = p.new_variable(nonnegative=True) >>> p.set_objective(-Integer(4)*x[Integer(0)] - Integer(5)*x[Integer(1)]) >>> p.add_constraint(Integer(2)*x[Integer(0)] + x[Integer(1)] <= Integer(3)) >>> p.add_constraint(Integer(2)*x[Integer(1)] + x[Integer(0)] <= Integer(3)) >>> N(p.solve(), digits=Integer(2)) -9.0 >>> p = MixedIntegerLinearProgram(solver='cvxopt', maximization=False) >>> x = p.new_variable(nonnegative=True) >>> p.set_objective(x[Integer(0)] + Integer(2)*x[Integer(1)]) >>> p.add_constraint(-Integer(5)*x[Integer(0)] + x[Integer(1)] <= Integer(7)) >>> p.add_constraint(-Integer(5)*x[Integer(0)] + x[Integer(1)] >= Integer(7)) >>> p.add_constraint(x[Integer(0)] + x[Integer(1)] >= Integer(26)) >>> p.add_constraint(x[Integer(0)] >= Integer(3)) >>> p.add_constraint(x[Integer(1)] >= Integer(4)) >>> N(p.solve(), digits=Integer(4)) 48.83 >>> p = MixedIntegerLinearProgram(solver='cvxopt') >>> x = p.new_variable(nonnegative=True) >>> p.set_objective(x[Integer(0)] + x[Integer(1)] + Integer(3)*x[Integer(2)]) >>> p.solver_parameter("show_progress",True) >>> p.add_constraint(x[Integer(0)] + Integer(2)*x[Integer(1)] <= Integer(4)) >>> p.add_constraint(Integer(5)*x[Integer(2)] - x[Integer(1)] <= Integer(8)) >>> N(p.solve(), digits=Integer(2)) pcost dcost gap pres dres k/t ... 8.8
When the optimal solution is not unique, CVXOPT as an interior point solver gives a different type of solution compared to the solvers that use the simplex method.
In the following example, the top face of the cube is optimal, and CVXOPT gives the center point of the top face, whereas the other tested solvers return a vertex:
sage: c = MixedIntegerLinearProgram(solver='cvxopt') sage: p = MixedIntegerLinearProgram(solver='ppl') sage: g = MixedIntegerLinearProgram() sage: xc = c.new_variable(nonnegative=True) sage: xp = p.new_variable(nonnegative=True) sage: xg = g.new_variable(nonnegative=True) sage: c.set_objective(xc[2]) sage: p.set_objective(xp[2]) sage: g.set_objective(xg[2]) sage: c.add_constraint(xc[0] <= 100) sage: c.add_constraint(xc[1] <= 100) sage: c.add_constraint(xc[2] <= 100) sage: p.add_constraint(xp[0] <= 100) sage: p.add_constraint(xp[1] <= 100) sage: p.add_constraint(xp[2] <= 100) sage: g.add_constraint(xg[0] <= 100) sage: g.add_constraint(xg[1] <= 100) sage: g.add_constraint(xg[2] <= 100) sage: N(c.solve(), digits=4) 100.0 sage: N(c.get_values(xc[0]), digits=3) 50.0 sage: N(c.get_values(xc[1]), digits=3) 50.0 sage: N(c.get_values(xc[2]), digits=4) 100.0 sage: N(p.solve(), digits=4) 100.0 sage: N(p.get_values(xp[0]), 2) 0.00 sage: N(p.get_values(xp[1]), 2) 0.00 sage: N(p.get_values(xp[2]), digits=4) 100.0 sage: N(g.solve(), digits=4) 100.0 sage: N(g.get_values(xg[0]), 2) # abstol 1e-15 0.00 sage: N(g.get_values(xg[1]), 2) # abstol 1e-15 0.00 sage: N(g.get_values(xg[2]), digits=4) 100.0
>>> from sage.all import * >>> c = MixedIntegerLinearProgram(solver='cvxopt') >>> p = MixedIntegerLinearProgram(solver='ppl') >>> g = MixedIntegerLinearProgram() >>> xc = c.new_variable(nonnegative=True) >>> xp = p.new_variable(nonnegative=True) >>> xg = g.new_variable(nonnegative=True) >>> c.set_objective(xc[Integer(2)]) >>> p.set_objective(xp[Integer(2)]) >>> g.set_objective(xg[Integer(2)]) >>> c.add_constraint(xc[Integer(0)] <= Integer(100)) >>> c.add_constraint(xc[Integer(1)] <= Integer(100)) >>> c.add_constraint(xc[Integer(2)] <= Integer(100)) >>> p.add_constraint(xp[Integer(0)] <= Integer(100)) >>> p.add_constraint(xp[Integer(1)] <= Integer(100)) >>> p.add_constraint(xp[Integer(2)] <= Integer(100)) >>> g.add_constraint(xg[Integer(0)] <= Integer(100)) >>> g.add_constraint(xg[Integer(1)] <= Integer(100)) >>> g.add_constraint(xg[Integer(2)] <= Integer(100)) >>> N(c.solve(), digits=Integer(4)) 100.0 >>> N(c.get_values(xc[Integer(0)]), digits=Integer(3)) 50.0 >>> N(c.get_values(xc[Integer(1)]), digits=Integer(3)) 50.0 >>> N(c.get_values(xc[Integer(2)]), digits=Integer(4)) 100.0 >>> N(p.solve(), digits=Integer(4)) 100.0 >>> N(p.get_values(xp[Integer(0)]), Integer(2)) 0.00 >>> N(p.get_values(xp[Integer(1)]), Integer(2)) 0.00 >>> N(p.get_values(xp[Integer(2)]), digits=Integer(4)) 100.0 >>> N(g.solve(), digits=Integer(4)) 100.0 >>> N(g.get_values(xg[Integer(0)]), Integer(2)) # abstol 1e-15 0.00 >>> N(g.get_values(xg[Integer(1)]), Integer(2)) # abstol 1e-15 0.00 >>> N(g.get_values(xg[Integer(2)]), digits=Integer(4)) 100.0
- solver_parameter(name, value=None)[source]¶
Return or define a solver parameter.
INPUT:
name
– string; the parametervalue
– the parameter’s value if it is to be defined, orNone
(default) to obtain its current value
Note
The list of available parameters is available at
solver_parameter()
.EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.solver_parameter("show_progress") False sage: p.solver_parameter("show_progress", True) sage: p.solver_parameter("show_progress") True
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.solver_parameter("show_progress") False >>> p.solver_parameter("show_progress", True) >>> p.solver_parameter("show_progress") True
- variable_lower_bound(index, value=False)[source]¶
Return or define the lower bound on a variable.
INPUT:
index
– integer; the variable’s idvalue
– real value, orNone
to mean that the variable has not lower bound. When set toFalse
(default), the method returns the current value.
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variable() 0 sage: p.col_bounds(0) (0.0, None) sage: p.variable_lower_bound(0, 5) sage: p.col_bounds(0) (5, None)
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variable() 0 >>> p.col_bounds(Integer(0)) (0.0, None) >>> p.variable_lower_bound(Integer(0), Integer(5)) >>> p.col_bounds(Integer(0)) (5, None)
- variable_upper_bound(index, value=False)[source]¶
Return or define the upper bound on a variable.
INPUT:
index
– integer; the variable’s idvalue
– real value, orNone
to mean that the variable has not upper bound. When set toFalse
(default), the method returns the current value.
EXAMPLES:
sage: from sage.numerical.backends.generic_backend import get_solver sage: p = get_solver(solver='CVXOPT') sage: p.add_variable() 0 sage: p.col_bounds(0) (0.0, None) sage: p.variable_upper_bound(0, 5) sage: p.col_bounds(0) (0.0, 5)
>>> from sage.all import * >>> from sage.numerical.backends.generic_backend import get_solver >>> p = get_solver(solver='CVXOPT') >>> p.add_variable() 0 >>> p.col_bounds(Integer(0)) (0.0, None) >>> p.variable_upper_bound(Integer(0), Integer(5)) >>> p.col_bounds(Integer(0)) (0.0, 5)