Optimization problems are be constructed by calling the following function.
op([objective[, constraints[, name]]])
The first argument specifies the objective function to be minimized. It can be an affine or convex piecewise-linear function with length 1, a variable with length 1, or a scalar constant (integer, float or 1 by 1 dense ’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.
objective
The objective or cost function. One can write to this attribute to change the objective of an existing problem.
variables()
Returns a list of the variables of the problem.
constraints()
Returns a list of the constraints.
inequalities()
Returns a list of the inequality constraints.
equalities()
Returns a list of the equality constraints.
delconstraint(c)
Deletes constraint c from the problem.
addconstraint(c)
Adds constraint c to the problem.
An optimization problem with convex piecewise-linear objective and constraints can be solved by calling the method solve().
solve([format[, solver]])
This function converts the optimization problem to a linear program in matrix form and then solves it using the solver described in section 7.3.
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: the default solver written in Python, the GLPK solver (if installed) or the MOSEK LP solver (if installed); see section 7.3. 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.
We refer to section 7.3 for details on the algorithms and the different solver options.
As an example we solve the LP
We can solve the same LP in matrix form as follows.
The op class also includes two methods for writing and reading files in MPS format.
tofile(filename)
If the problem is an LP, writes it to the file ’filename’ using the MPS format. Row and column labels are assigned based on the variable and constraint names in the LP.
fromfile(filename)
Reads the LP from the file ’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.