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

(6.27)\[\begin{split}\begin{array}{lccl} \mbox{maximize} & x_0 + 0.64 x_1 & & \\ \mbox{subject to} & 50 x_0 + 31 x_1 & \leq & 250, \\ & 3 x_0 - 2 x_1 & \geq & -4, \\ & x_0, x_1 \geq 0 & & \mbox{and integer} \end{array}\end{split}\]

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 putvartype or one of its bulk analogues:

    # Define variables to be integers
    putvartypelist(task,[ 1, 2 ],
                   [ MSK_VAR_TYPE_INT, MSK_VAR_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
    putdouparam(task,MSK_DPAR_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 MSK_SOL_ITG. No dual solution is defined for integer optimization problems.

Listing 6.13 Source code implementing problem (6.27). Click here to download.
using Mosek
using Printf, SparseArrays

# Define a stream printer to grab output from MOSEK
printstream(msg::String) = print(msg)


bkc = [ MSK_BK_UP, MSK_BK_LO  ]
blc = [      -Inf,      -4.0  ]
buc = [     250.0,       Inf  ]

bkx = [ MSK_BK_LO, MSK_BK_LO  ]
blx = [       0.0,       0.0  ]
bux = [       Inf,       Inf  ]

c   = [       1.0,      0.64 ]

A    = sparse( [ 1, 1, 2, 2],
               [ 1, 2, 1, 2],
               [ 50.0, 31.0,
                 3.0, -2.0] )

numvar = length(bkx)
numcon = length(bkc)


# Create a task
maketask() do task
    # Use remote server: putoptserverhost(task,"http://solve.mosek.com:30080")
    # Attach a printer to the task
    putstreamfunc(task,MSK_STREAM_LOG,printstream)

    # Append 'numcon' empty constraints.
    # The constraints will initially have no bounds.
    appendcons(task,numcon)

    #Append 'numvar' variables.
    # The variables will initially be fixed at zero (x=0).
    appendvars(task,numvar)

    # Set the linear term c_j in the objective.
    putclist(task,[1:numvar;],c)

    # Set the bounds on variables
    # blx[j] <= x_j <= bux[j]
    putvarboundslice(task,1,numvar+1,bkx,blx,bux)

    # Input columns of A
    putacolslice(task,1,numvar+1, A.colptr[1:numvar],A.colptr[2:numvar+1],A.rowval,A.nzval)

    putconboundslice(task,1,numcon+1,bkc,blc,buc)

    # Input the objective sense (minimize/maximize)
    putobjsense(task,MSK_OBJECTIVE_SENSE_MAXIMIZE)

    # Define variables to be integers
    putvartypelist(task,[ 1, 2 ],
                   [ MSK_VAR_TYPE_INT, MSK_VAR_TYPE_INT ])

    # Set max solution time
    putdouparam(task,MSK_DPAR_MIO_MAX_TIME, 60.0)

    # Optimize the task
    optimize(task)

    writedata(task,"milo1.ptf")

    # Print a summary containing information
    # about the solution for debugging purposes
    solutionsummary(task,MSK_STREAM_MSG)

    prosta = getprosta(task,MSK_SOL_ITG)
    solsta = getsolsta(task,MSK_SOL_ITG)

    if solsta == MSK_SOL_STA_INTEGER_OPTIMAL
        # Output a solution
        xx = getxx(task,MSK_SOL_ITG)
        @printf("Optimal solution: %s\n", xx')
    elseif solsta == MSK_SOL_STA_UNKNOWN
        println("Unknown solution status")
    else
        println("Other solution status")
    end

end

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 MSK_IINF_MIO_INITIAL_FEASIBLE_SOLUTION and MSK_DINF_MIO_INITIAL_FEASIBLE_SOLUTION_OBJ, and via the Initial 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 MSK_IPAR_MIO_CONSTRUCT_SOL is switched on. The outcome of this process can be inspected via information items MSK_IINF_MIO_CONSTRUCT_SOLUTION and MSK_DINF_MIO_CONSTRUCT_SOLUTION_OBJ, and via the Construct solution objective entry in the log.

In the following example we focus on inputting a partial integer solution.

(6.28)\[\begin{split}\begin{array} {ll} \mbox{maximize} & 7 x_0 + 10 x_1 + x_2 + 5 x_3 \\ \mbox{subject to} & x_0 + x_1 + x_2 + x_3 \leq 2.5\\ & x_0,x_1,x_2 \in \integral \\ & x_0,x_1,x_2,x_3 \geq 0 \end{array}\end{split}\]

Solution values can be set using putxx, putxxslice or similar .

Listing 6.14 Implementation of problem (6.28) specifying an initial solution. Click here to download.
        # Assign values to integer variables
        # We only set that slice of xx
        putxxslice(task,MSK_SOL_ITG, 1, 4, [1.0, 1.0, 0.0])

        # Request constructing the solution from integer variable values
        putintparam(task,MSK_IPAR_MIO_CONSTRUCT_SOL, MSK_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:

Listing 6.15 Retrieving information about usage of initial solution Click here to download.
            constr = getintinf(task,MSK_IINF_MIO_CONSTRUCT_SOLUTION)
            constrVal = getdouinf(task,MSK_DINF_MIO_CONSTRUCT_SOLUTION_OBJ)
            println("Construct solution utilization: $constr")
            println("Construct solution objective: $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:

(6.29)\[\begin{split}\begin{array}{ll} \mbox{minimize} & x^2+y^2 \\ \mbox{subject to} & x \geq e^y+3.8, \\ & x, y \ \mbox{integer}. \end{array}\end{split}\]

The canonical conic formulation of (6.29) suitable for Optimizer API for Julia is

(6.30)\[\begin{split}\begin{array}{llr} \mbox{minimize} & t & \\ \mbox{subject to} & (t,x,y)\in\Q^3 & (t\geq\sqrt{x^2+y^2}) \\ & (x-3.8, 1, y) \in\EXP & (x-3.8\geq e^y) \\ & x, y \ \mbox{integer}, & \\ & t\in\real. \end{array}\end{split}\]
Listing 6.16 Implementation of problem (6.30). Click here to download.
maketask() do task
    # Use remote server: putoptserverhost(task,"http://solve.mosek.com:30080")
    # Directs the log task stream to the user specified
    # method task_msg_obj.stream
    appendvars(task,3);   # x, y, t
    x=1
    y=2
    t=3
    putvarboundsliceconst(task,1, 4, MSK_BK_FR, -Inf, Inf)

    # Integrality constraints for x, y
    putvartypelist(task,
                   [x,y],
                   [MSK_VAR_TYPE_INT,MSK_VAR_TYPE_INT])

    # Set up the affine expressions
    # x, x-3.8, y, t, 1.0
    appendafes(task,5)
    putafefentrylist(task,
                     [1,2,3,4],
                     [x,x,y,t],
                     [1,1,1,1])
    putafegslice(task,1, 6, Float64[0, -3.8, 0, 0, 1.0])

    # Add constraint (x-3.8, 1, y) \in \EXP
    appendacc(task,appendprimalexpconedomain(task), Int64[2, 5, 3], nothing)

    # Add constraint (t, x, y) \in \QUAD
    appendacc(task,appendquadraticconedomain(task,3), Int64[4, 1, 3], nothing)

    # Objective
    putobjsense(task,MSK_OBJECTIVE_SENSE_MINIMIZE)
    putcj(task,t, 1.0)

    # Optimize the task
    optimize(task)
    solutionsummary(task,MSK_STREAM_MSG)

    xx = getxxslice(task,MSK_SOL_ITG, 1, 3)
    println("x = $(xx[1]) y = $(xx[2])")

    
end

Error and solution status handling were omitted for readability.