# 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.
using System;

namespace mosek.example
{
public class MsgClass : mosek.Stream
{

public MsgClass ()
{
/* Construct the object */
}

public override void streamCB (string msg)
{
Console.Write ("{0}", msg);
}
}

public class milo1
{
public static void Main ()
{
const int numcon = 2;
const int numvar = 2;

// 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    = { new int[]  {0,   1},  new int[] {0,    1}   };
double[][] aval = { new double[] {50.0, 3.0}, new double[] {31.0, -2.0} };

double[] xx  = new double[numvar];

mosek.Env env = null;

try
{
// Make mosek environment.
env  = new mosek.Env ();
// Directs the log task stream to the user specified
MsgClass task_msg_obj = new MsgClass ();

/* Give MOSEK an estimate of the size of the input data.
This is done to increase the speed of inputting data.
However, it is optional. */
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */

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

/* Optionally add a constant term to the objective. */

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 */

// Print a summary containing information
//   about the solution for debugging purposes

mosek.solsta solsta;
/* Get status information about the solution */
xx);

switch (solsta)
{
case mosek.solsta.optimal:
case mosek.solsta.near_optimal:
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:", xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
case mosek.solsta.near_dual_infeas_cer:
case mosek.solsta.near_prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
mosek.prosta prosta;
switch (prosta)
{
case mosek.prosta.prim_infeas_or_unbounded:
Console.WriteLine("Problem status Infeasible or unbounded");
break;
case mosek.prosta.prim_infeas:
Console.WriteLine("Problem status Infeasible.");
break;
case mosek.prosta.unknown:
Console.WriteLine("Problem status unknown.");
break;
default:
Console.WriteLine("Other problem status.");
break;
}
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
throw;
}
finally
{
if (env  != null)  env.Dispose ();
}
}
}
}


## 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.cs. For more details about this process see Sec. 14 (The Optimizer for Mixed-integer Problems).