6.8 Integer Optimization¶
An optimization problem where one or more of the variables are constrained to integer values is called a (mixed) integer optimization problem. MOSEK supports integer variables in combination with linear, quadratic and quadratically constrtained and conic problems (except semidefinite). See the previous tutorials for an introduction to how to model these types of problems.
6.8.1 Example MILO1¶
We use the example
to demonstrate how to set up and solve a problem with integer variables. It has the structure of a linear optimization problem (see Sec. 6.1 (Linear Optimization)) except for integrality constraints on the variables. Therefore, only the specification of the integer constraints requires something new compared to the linear optimization problem discussed previously.
First, the integrality constraints are imposed using the function Task.putvartype
or one of its bulk analogues:
task.putvartypelist([0, 1],
[mosek.variabletype.type_int,
mosek.variabletype.type_int])
Next, the example demonstrates how to set various useful parameters of the mixed-integer optimizer. See Sec. 13.4 (The Optimizer for Mixed-Integer Problems) for details.
# Set max solution time
task.putdouparam(mosek.dparam.mio_max_time, 60.0);
The complete source for the example is listed Listing 6.13. Please note that when we fetch the solution then the integer solution is requested by using soltype.itg
. No dual solution is defined for integer optimization problems.
6.8.2 Specifying an initial solution¶
It is a common strategy to provide a starting feasible point (if one is known in advance) to the mixed-integer solver. This can in many cases reduce solution time.
There are two modes for MOSEK to utilize an initial solution.
A complete solution. MOSEK will first try to check if the current value of the primal variable solution is a feasible point. The solution can either come from a previous solver call or can be entered by the user, however the full solution with values for all variables (both integer and continuous) must be provided. This check is always performed and does not require any extra action from the user. The outcome of this process can be inspected via information items
iinfitem.mio_initial_feasible_solution
anddinfitem.mio_initial_feasible_solution_obj
, and via theInitial feasible solution objective
entry in the log.A partial integer solution. MOSEK can also try to construct a feasible solution by fixing integer variables to the values provided by the user (rounding if necessary) and optimizing over the remaining continuous variables. In this setup the user must provide initial values for all integer variables. This action is only performed if the parameter
iparam.mio_construct_sol
is switched on. The outcome of this process can be inspected via information itemsiinfitem.mio_construct_solution
anddinfitem.mio_construct_solution_obj
, and via theConstruct solution objective
entry in the log.
In the following example we focus on inputting a partial integer solution.
Solution values can be set using Task.putxx
, Task.putxxslice
or similar .
# Assign values to integer variables.
# (We only set a slice of xx)
task.putxxslice(mosek.soltype.itg, 0, 3, [1.0, 1.0, 0.0])
# Request constructing the solution from integer variable values
task.putintparam(mosek.iparam.mio_construct_sol, mosek.onoffkey.on)
The log output from the optimizer will in this case indicate that the inputted values were used to construct an initial feasible solution:
Construct solution objective : 1.950000000000e+01
The same information can be obtained from the API:
constr = task.getintinf(mosek.iinfitem.mio_construct_solution)
constrVal = task.getdouinf(mosek.dinfitem.mio_construct_solution_obj)
print("Construct solution utilization: {0}\nConstruct solution objective: {1:.3f}\n".format(constr, constrVal))
6.8.3 Example MICO1¶
Integer variables can also be used arbitrarily in conic problems (except semidefinite). We refer to the previous tutorials for how to set up a conic optimization problem. Here we present sample code that sets up a simple optimization problem:
The canonical conic formulation of (6.29) suitable for Optimizer API for Python is
with mosek.Task() as task:
task.set_Stream(mosek.streamtype.log, streamprinter)
task.appendvars(3) # x, y, t
x, y, t = 0, 1, 2
task.putvarboundsliceconst(0, 3, mosek.boundkey.fr, -0.0, 0.0)
# Integrality constraints
task.putvartypelist([x,y], [mosek.variabletype.type_int]*2)
# Set up the affine expression
# x, x-3.8, y, t, 1.0
task.appendafes(5)
task.putafefentrylist([0, 1, 2, 3],
[x,x,y,t],
[1,1,1,1])
task.putafegslice(0, 5, [0, -3.8, 0, 0, 1.0])
# Add constraint (x-3.8, 1, y) \in \EXP
task.appendacc(task.appendprimalexpconedomain(), [1, 4, 2], None)
# Add constraint (t, x, y) \in \QUAD
task.appendacc(task.appendquadraticconedomain(3), [3, 0, 2], None)
# Objective
task.putobjsense(mosek.objsense.minimize)
task.putcj(t, 1)
# Optimize the task
task.optimize()
task.solutionsummary(mosek.streamtype.msg)
xx = task.getxxslice(mosek.soltype.itg, 0, 2)
print(xx)
Error and solution status handling were omitted for readability.