# 6.5 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 and conic quadratic problems. See the previous tutorials for an introduction to how to model these types of problems.

## 6.5.1 Example MILO1¶

We use the example

(1)$\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 Task.putvartype:

      for (int j = 0; j < numvar; ++j)


Next, the example demonstrates how to set various useful parameters of the mixed-integer optimizer. See Sec. 14 (The Optimizer for Mixed-integer Problems) for details.

      /* Set max solution time */


The complete source for the example is listed Listing 9. Please note that when Task.getsolutionslice is called, the integer solution is requested by using soltype.itg. No dual solution is defined for integer optimization problems.

Listing 9 Source code implementing problem (1). Click here to download.
package com.mosek.example;
import mosek.*;

public class milo1 {
static final int numcon = 2;
static final int numvar = 2;

public static void main (String[] args) {
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;

mosek.boundkey[] bkc
= { mosek.boundkey.up, mosek.boundkey.lo };
double[] blc = { -infinity,         -4.0 };
double[] buc = { 250.0,             infinity };

mosek.boundkey[] bkx
= { mosek.boundkey.lo, mosek.boundkey.lo  };
double[] blx = { 0.0,               0.0 };
double[] bux = { infinity,          infinity };

double[] c   = {1.0, 0.64 };

int[][] asub    = { {0,   1},    {0,    1}   };
double[][] aval = { {50.0, 3.0}, {31.0, -2.0} };

int[] ptrb = { 0, 2 };
int[] ptre = { 2, 4 };

double[] xx  = new double[numvar];

try (Env  env  = new Env();
// Directs the log task stream to the user specified
mosek.streamtype.log,
new mosek.Stream()
{ public void stream(String msg) { System.out.print(msg); }});
new mosek.ItgSolutionCallback() {
public void callback(double[] xx) {
System.out.print("New integer solution: ");
for (double v : xx) System.out.print("" + v + " ");
System.out.println("");
}
});
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */

/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */

for (int j = 0; j < numvar; ++j) {
/* Set the linear term c_j in the objective.*/
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
/* Input column j of A */
asub[j],               /* Row index of non-zeros in column j.*/
aval[j]);              /* Non-zero Values of column j. */
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)

/* Specify integer variables. */
for (int j = 0; j < numvar; ++j)

/* Set max solution time */

/* A maximization problem */
/* Solve the problem */
try {
} catch (mosek.Warning e) {
System.out.println (" Mosek warning:");
System.out.println (e.toString ());
}

// Print a summary containing information
//   about the solution for debugging purposes
xx);
mosek.solsta solsta[] = new mosek.solsta[1];
/* Get status information about the solution */

switch (solsta[0]) {
case integer_optimal:
case near_integer_optimal:
System.out.println("Optimal solution\n");
for (int j = 0; j < numvar; ++j)
System.out.println ("x[" + j + "]:" + xx[j]);
break;
case prim_feas:
System.out.println("Feasible solution\n");
for (int j = 0; j < numvar; ++j)
System.out.println ("x[" + j + "]:" + xx[j]);
break;

case unknown:
mosek.prosta prosta[] = new mosek.prosta[1];
switch (prosta[0]) {
case prim_infeas_or_unbounded:
System.out.println("Problem status Infeasible or unbounded");
break;
case prim_infeas:
System.out.println("Problem status Infeasible.");
break;
case unknown:
System.out.println("Problem status unknown.");
break;
default:
System.out.println("Other problem status.");
break;
}
break;
default:
System.out.println("Other solution status");
break;
}
}
catch (mosek.Exception e) {
System.out.println ("An error or warning was encountered");
System.out.println (e.getMessage ());
throw e;
}
}
}


## 6.5.2 Specifying an initial solution¶

Solution time of can often be reduced by providing an initial solution for the solver. It is not necessary to specify the whole solution. By setting the iparam.mio_construct_sol parameter to onoffkey.on and inputting values for the integer variables only, MOSEK will be forced to compute the remaining continuous variable values. If the specified integer solution is infeasible or incomplete, MOSEK will simply ignore it.

We concentrate on a simple example below.

(2)$\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 Task.putxxslice and related methods.

Listing 10 Implementation of problem (2) specifying an initial solution. Click here to download.
      // Construct an initial feasible solution from the
//     values of the integer valuse specified

The complete code is not very different from the first example and is available for download as mioinitsol.java. For more details about this process see Sec. 14 (The Optimizer for Mixed-integer Problems).