Optimization problems are be constructed by calling the following function.
[objective[, constraints[, name]]]) |
'd'
matrix). The default value is
0.0
.
The second argument is a single constraint, or a list of constraint objects. The default value is an empty list.
The third argument is a string with a name for the problem. The default value is the empty string.
The following attributes and methods are useful for examining and modifying optimization problems.
) |
) |
) |
) |
c) |
c) |
An optimization problem with convex piecewise-linear objective and constraints can be solved by calling the method solve().
[format[, solver]]) |
The first argument is either 'dense'
or 'sparse'
, and
denotes the matrix types used in the matrix representation of the LP.
The default value is 'dense'
.
The second argument is either None
. 'glpk'
or 'mosek'
,
and selects one of three available LP solvers: a default solver
written in Python, the GLPK solver (if installed) or the
MOSEK LP solver (if installed); see section 8.1.
The default value is None
.
The solver reports the outcome of optimization by setting the attribute self.status and by modifying the value attributes of the variables and the constraint multipliers of the problem.
'optimal'
.
The value attributes of the variables in the problem are set
to their computed solutions, and the value attributes of the
multipliers of the constraints of the problem are set to the computed
dual optimal solution.
'primal infeasible'
.
The value attributes of the variables are set to None
.
The value attributes of the
multipliers of the constraints of the problem are set to a certificate
of primal infeasibility.
With the 'glpk'
option, solve()
does not provide certificates of infeasibility.
'dual infeasible'
.
The value attributes of the multipliers of the constraints of
the problem are set to None
.
The value attributes of the
variables are set to a certificate of dual infeasibility.
With the 'glpk'
option, solve() does not provide
certificates of infeasibility.
'unknown'
.
The value attributes of the variables and the constraint
multipliers are set to None
.
As an example we solve the LP
>>> x = variable() >>> y = variable() >>> c1 = ( 2*x+y <= 3 ) >>> c2 = ( x+2*y <= 3 ) >>> c3 = ( x >= 0 ) >>> c4 = ( y >= 0 ) >>> lp1 = op(-4*x-5*y, [c1,c2,c3,c4]) >>> lp1.solve() >>> print lp1.status optimal >>> print lp1.objective.value() -9.0000e+00 >>> print x.value 1.0000e-00 >>> print y.value 1.0000e-00 >>> print c1.multiplier.value 1.0000e-00 >>> print c2.multiplier.value 2.0000e-00 >>> print c3.multiplier.value 8.8912e-09 >>> print c4.multiplier.value 9.8567e-09
We can solve the same LP in matrix form as follows.
>>> x = variable(2) >>> A = matrix([[2.,1.,-1.,0.], [1.,2.,0.,-1.]]) >>> b = matrix([3.,3.,0.,0.]) >>> c = matrix([-4.,-5.]) >>> ineq = ( A*x <= b ) >>> lp2 = op(dot(c,x), ineq) >>> lp2.solve() >>> print lp2.objective.value() -9.0000e+00 >>> print x.value 1.0000e-00 1.0000e-00 >>> print ineq.multiplier.value 1.0000e+00 2.0000e+00 8.8912e-09 9.8567e-09
The op class also includes two methods for writing and reading files in MPS format.
filename) |
'filename'
using
the MPS format. Row and column labels are assigned based on the
variable and constraint names in the LP.
filename) |
'filename'
. The file must be
a fixed-format MPS file. Some features of the MPS format are not
supported: comments beginning with dollar signs,
the row types 'DE', 'DL', 'DG', and 'DN', and the capability of
reading multiple righthand side, bound or range vectors.