# Mixed-integer nonlinear programming

Knitro provides tools for solving optimization models (both linear and nonlinear) with binary or integer variables. The Knitro mixed integer programming (MIP) code offers three algorithms for mixed-integer nonlinear programming (MINLP). The first is a nonlinear branch and bound method, the second implements the hybrid Quesada-Grossman method for convex MINLP, and the third implements a mixed-integer Sequential Quadratic Programming (MISQP) method that is able to handle non-relaxable integer variables.

The Knitro MINLP code is designed for convex mixed integer programming and is only a heuristic for nonconvex problems. The MINLP code also handles mixed integer linear programs (MILP) of moderate size.

## Overview

The table below presents a brief overview of the main features included in the three MINLP algorithms. For a more detailed description, check Algorithms/Methods.

Features | Branch-and-Bound | Quesada-Grossmann | Mixed Integer Sequential Quadratic Programming |
---|---|---|---|

Non-convex MINLP | ++ | + | ++ |

Convex MNILP | ++ | ||

Expensive evaluations | ++ | ||

Warm-start | + | + | ++ |

MIP heuristics | Rounding / Feasibility pump / MPEC | Rounding / Feasibility pump / MPEC | - |

MIP cutting planes | Knapsack | Knapsack | - |

LP solver | - | IP/Direct or IP/CG or SLQP | - |

- Non-convex MINLP: performance on non-convex MINLP
- Convex MINLP: performance on convex MINLP
- Expensive evaluations: performance on problems with expensive function evalutations
- Warm-start: ability to warm-start
- MIP heuristics: heuristic search approach available to find an initial integer feasible point
- MIP cutting planes: cutting plane methods available
- LP solver: solver available for the resolution of the linear subproblems

## AMPL example

Using MINLP features in AMPL is very simple: one only has to
declare variables as integer in the AMPL model. In our toy example,
from Getting started with AMPL let us modify the declaration of variable *x* as follows:

```
var x{j in 1..3} >= 0 integer;
```

and then run the example again. The Knitro log now mentions 3 integer variables, and displays additional statistics related to the MIP solve.

```
=======================================
Commercial License
Artelys Knitro 11.0.0
=======================================
Knitro changing mip_method from AUTO to 1.
Knitro changing mip_rootalg from AUTO to 1.
Knitro changing mip_lpalg from AUTO to 3.
Knitro changing mip_branchrule from AUTO to 2.
Knitro changing mip_selectrule from AUTO to 2.
Knitro changing mip_rounding from AUTO to 3.
Knitro changing mip_heuristic from AUTO to 1.
Knitro changing mip_pseudoinit from AUTO to 1.
Problem Characteristics
-----------------------
Objective goal: Minimize
Number of variables: 3
bounded below: 3
bounded above: 0
bounded below and above: 0
fixed: 0
free: 0
Number of binary variables: 0
Number of integer variables: 3
Number of constraints: 2
linear equalities: 1
quadratic equalities: 0
gen. nonlinear equalities: 0
linear one-sided inequalities: 0
quadratic one-sided inequalities: 1
gen. nonlinear one-sided inequalities: 0
linear two-sided inequalities: 0
quadratic two-sided inequalities: 0
gen. nonlinear two-sided inequalities: 0
Number of nonzeros in Jacobian: 6
Number of nonzeros in Hessian: 5
Knitro detected 0 GUB constraints
Knitro derived 0 knapsack covers after examining 0 constraints
Knitro solving root node relaxation
Node Left Iinf Objective Best relaxatn Best incumbent
------ ------ ------ -------------- -------------- --------------
* 1 0 0 9.360000e+02 9.360000e+02 9.360000e+02
EXIT: Optimal solution found.
Final Statistics for MIP
------------------------
Final objective value = 9.36000000000000e+02
Final integrality gap (abs / rel) = 0.00e+00 / 0.00e+00 ( 0.00%)
# of nodes processed = 1
# of subproblems solved = 2
Total program time (secs) = 0.00829 ( 0.007 CPU time)
Time spent in evaluations (secs) = 0.00018
===========================================================================
Knitro 11.0.0: Locally optimal or satisfactory solution.
objective 936; integrality gap 0
1 nodes; 2 subproblem solves
```

Note that this example is not particularly interesting since the two solutions we know for the continuous version of this problem are already integer “by chance”. As a consequence, the MINLP solve returns the same solution as the continuous solve. However, if for instance you change the first constraint to:

```
s.t. c1: 8*x[1] + 14*x[2] + 7*x[3] - 50 = 0;
```

instead of:

```
s.t. c1: 8*x[1] + 14*x[2] + 7*x[3] - 56 = 0;
```

you will observe that the integer solution differs from the continuous one.

## MATLAB example

To use the MINLP features in MATLAB, one must use the function *knitromatlab_mip*,
rather than *knitromatlab*. The function signature is very similar to *knitromatlab*,
but three additional argument arrays are used.
The most elaborate form is:

```
[x,fval,exitflag,output,lambda,grad,hessian] =
knitromatlab_mip(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,...
xType,objFnType,cineqFnType,extendedFeatures,options,KnitroOptions)
```

The array *xType* sets the variable types and must be the same length as *x0* if it is used.
Continuous, integer, and binary variables are set with 0, 1, and 2, respectively. Passing an
empty array, [], is equivalent to an array of all zeros.

The scalar *objFnType* sets the objective function type.
Uncertain, convex, and nonconvex are set with 0, 1, and 2, respectively. Passing an
empty array, [], is equivalent to passing zero.

The array *cineqFnType* sets the inequality constraint function types and its length must be the same as the
number of inequality constraints. Linear constraints are known to be convex, and nonlinear
equality constraints are known to be nonconvex, so they are not included in the array.
Uncertain, convex, and nonconvex inequality constraints are set with 0, 1, and 2, respectively. Passing an
empty array, [], is equivalent to passing an array of all zeros.

Modifying the toy example in MATLAB to use integer variables can be done as follows:

```
xType = [2;2;2];
objFnType = 1;
cineqFnType = 1;
%modify the solver call
x = knitromatlab_mip(obj, x0, A, b, Aeq, beq, lb, ub, ...
nlcon, xType, objFnType, cineqFnType);
```

## C example

As with AMPL, defining a MIP problem only requires declaring integer
variables via the API function `KN_set_var_types()`

(by default, variables are assumed to be continuous).

In order to turn our C toy example into a MINLP problem, it thus suffices to add

```
/* in the declarations */
int i;
/* mark all variables as integer */
for (i=0; i<n; i++) {
error = KN_set_var_type (kc, i, KN_VARTYPE_INTEGER);
}
```

The Knitro log will look similar to what we observed in the AMPL example above. Again, this example is quite unusual in the sense that the continuous solution is already integer, which of course is not the case in general.

## Object-oriented C++ example

A MIP problem is defined and solved via the object-oriented interface by adding additional problem information in the problem class.

In the following, we will define how to turn the toy example into a MINLP problem.
The `ProblemExample`

class has to be extended with new definitions.

In the function `setObjectiveProperties()`

, the function
`KTRProblem::setObjFnType(int fnType)`

is used to define the objective
function type:

```
setObjFnType(KNITRO::KTREnums::FunctionType::Convex);
```

In the function `setConstraintProperties()`

, the constraint function
types are defined with the function `KTRProblem::setConFnTypes(int id, int fnType)`

:

```
setConFnTypes(0, KNITRO::KTREnums::FunctionType::Convex);
setConFnTypes(1, KNITRO::KTREnums::FunctionType::Nonconvex);
```

In the function `setVariableProperties()`

, the variable types are defined
with the function `KTRProblem::setVarFntypes(int fnTypes)`

:

```
setVarFnTypes(KNITRO::KTREnums::VariableType::Integer);
```

Without specifying a variable index, the function sets variable types for all variables to integer.

This example uses the `KTRProblem`

class to simplify implementing `KTRIProblem`

. If using `KTRIProblem`

only, the functions `KTRIProblem::getObjFnType`

, `KTRIProblem::getConFnType`

, and `KTRIProblem::getVarFnType`

should be implemented to return the appropriate values.

The KNITRO log will look similar to what we observed in the AMPL example above. Again, this example is quite unusual in the sense that the continuous solution is already integer, which of course is not the case in general.

## MINLP options

Many user options are provided for the MIP features to tune performance, including
options for branching, node selection, rounding and heuristics for finding integer
feasible points. User options specific to the MIP tools begin with *mip_*.
It is recommended to experiment with several
of these options as they often can make a significant difference in performance.

Name | Meaning |
---|---|

`mip_branchrule` |
MIP branching rule |

`mip_debug` |
MIP debugging level (0=none, 1=all) |

`mip_gub_branch` |
Branch on GUBs (0=no, 1=yes) |

`mip_heuristic` |
MIP heuristic search |

`mip_heuristic_maxit` |
MIP heuristic iteration limit |

`mip_heuristic_terminate` |
MIP heuristic termination condition |

`mip_implications` |
Add logical implications (0=no, 1=yes) |

`mip_integer_tol` |
Threshold for deciding integrality |

`mip_integral_gap_abs` |
Absolute integrality gap stop tolerance |

`mip_integral_gap_rel` |
Relative integrality gap stop tolerance |

`mip_intvar_strategy` |
Treatment of integer variables |

`mip_knapsack` |
Add knapsack cuts (0=no, 1=ineqs, 2=lifted, 2=ineqs+lifted) |

`mip_lpalg` |
LP subproblem algorithm |

`mip_maxnodes` |
Maximum nodes explored |

`mip_maxsolves` |
Maximum subproblem solves |

`mip_maxtime_cpu` |
Maximum CPU time in seconds for MIP |

`mip_maxtime_real` |
Maximum real in seconds time for MIP |

`mip_method` |
MIP method (0=auto, 1=BB, 2=HQG, 3=MISQP) |

`mip_nodealg` |
Standard node relaxation algorithm |

`mip_outinterval` |
MIP output interval |

`mip_outlevel` |
MIP output level |

`mip_outsub` |
Enable MIP subproblem output |

`mip_pseudoinit` |
Pseudo-cost initialization |

`mip_relaxable` |
Are integer variables relaxable? |

`mip_rootalg` |
Root node relaxation algorithm |

`mip_rounding` |
MIP rounding rule |

`mip_selectdir` |
MIP node selection direction |

`mip_selectrule` |
MIP node selection rule |

`mip_strong_candlim` |
Strong branching candidate limit |

`mip_strong_level` |
Strong branching tree level limit |

`mip_strong_maxit` |
Strong branching iteration limit |

`mip_terminate` |
Termination condition for MIP |

## Algorithms/Methods

The default MINLP method in Knitro is a standard implementation of branch-and-bound for nonlinear optimization. This method involves solving a relaxed, continuous nonlinear optimization subproblem at every node of the branch-and-bounds tree. This method is generally the preferred method. It is primarily designed for convex models, and in this case the integrality gap measure can be trusted. It can also be applied to non-convex models, and often works well on these models. However it may sometimes get stuck at integer feasible points that are not globally optimal solutions when the model in nonconvex. In addition, the integrality gap meaure may not be accurate since this measure is based on the assumption that the nonlinear optimization subproblems are always solved to global optimality (which may not be the case when the model is nonconvex).

The hybrid Quesada-Grossman (HQG) method in Knitro is a variant of branch-and-bound for MINLP. It maintains one branch-and-bound tree but solves linear programming (LP) subproblems at most of the nodes, while only occasionally solving nonlinear optimization subproblems at integer feasible nodes. The solutions of the LP subproblems are used to generate outer approximations/cuts, which are continually added to the master problem. This method should generally only be applied to convex models since the outer approximations are only valid when the model is convex. This method will typically take many more nodes to solve compared with the standard branch-and-bound method, but the node subproblems are often easier to solve since most of them are LPs.

The third method (MISQP) is a largely heuristic method that attempts to extend the SQP method for continuous, nonlinear optimization to the case where there are integer variables. This method is primarily designed for small problems (e.g. less than 100 variables) where function evaluations may involve expensive black-box simulations and derivatives may not be available. In contrast to the other MINLP algorithms in Knitro, this method is able to handle models where the integer variables cannot be relaxed. This means that the simulations or function evaluations can only occur when integer variables are at integer values (e.g. the integer variables may have no meaning at non-integral values). This method will typically converge in far fewer function evaluations compared with the other MINLP methods in Knitro and is primarily intended for small problems where these evaluations are the dominant cost. This method can be applied to either convex or non-convex models, but may converge to non-global integer, feasible points. However, since this algorithm runs similarly to the continuous SQP algorithm, you can apply the parallel multi-start feature (see Section Multistart) to the MISQP method to increase the chances of finding the global solution.

## Branching priorities

It is also possible to specify branching priorities in Knitro. Priorities must be positive numbers (variables with non-positive values are ignored). Variables with higher priority values will be considered for branching before variables with lower priority values. When priorities for a subset of variables are equal, the branching rule is applied as a tiebreaker.

Branching priorities in AMPL

Branching priorities for integer variables can be specified in AMPL using the AMPL suffixes feature (see AMPL suffixes defined for Knitro) as shown below.

```
...
ampl: var x{j in 1..3} >= 0 integer;
...
ampl: suffix priority IN, integer, >=0, <=9999;
ampl: let x[1].priority := 5;
ampl: let x[2].priority := 1;
ampl: let x[3].priority := 10;
```

See the AMPL documentation for more information on the “.priority ” suffix.

Branching priorities in the callable library API

Branching priorities for integer variables
can be specified through the callable library interface using
the `KN_set_set_mip_branching_priorities()`

functions shown below.

```
int KNITRO_API KN_set_mip_branching_priorities
( KN_context_ptr kc,
const KNINT nV,
const KNINT * const indexVars,
const int * const xPriorities);
int KNITRO_API KN_set_mip_branching_priorities_all
( KN_context_ptr kc,
const int * const xPriorities);
int KNITRO_API KN_set_mip_branching_priority
( KN_context_ptr kc,
const KNINT indexVar,
const int xPriority);
```

Values for continuous variables are ignored.
Knitro makes a local copy of all inputs, so the application may free memory after the call. This
routine must be called after calling `KN_set_var_types()`

to mark integer variables.

Branching priorities in the object-oriented interface

Branching priorities for integer variables can be specified through the object-oriented interface using the function shown below.

```
void KTRSolver::mipSetBranchingPriorities(const std::vector<int>& xPriorities);
```

The `std::vector<int>`

*xPriorities* has length *“n”*, where *n* is the number of variables.
Values for continuous variables are ignored.
This method must be called after calling the `KTRSolver`

constructor and before calling
`KTRSolver::solve()`

.

## Special Treatment of Integer Variables

You can specify special treatment of integer variables using the
`mip_intvar_strategy`

user option in Knitro. In particularly, you can use this
option to specify that all integer variables are relaxed, or that all binary variables
should be converted to complementarity constraints (see Section Complementarity constraints for a
description of complementarity constraints).

In addition you can specify special treatments of individual integer variables through
the callable library interface function `KN_set_mip_intvar_strategies()`

```
int KNITRO_API KN_set_mip_intvar_strategies
( KN_context_ptr kc,
const KNINT nV,
const KNINT * const indexVars,
const int * const xStrategies);
int KNITRO_API KN_set_mip_intvar_strategies_all
( KN_context_ptr kc,
const int * const xStrategies);
int KNITRO_API KN_set_mip_intvar_strategy
( KN_context_ptr kc,
const KNINT indexVar,
const int xStrategy);
```

Here *indexVars* specifies the index of the integer variable you want to apply the
special treatment to, and *xStrategies* specifies how you want to handle that
particular integer variable (e.g., no special treatment, relax, or convert to a
complementarity constraint).

Special strategies for integer variables
can be specified in the AMPL interface using the *intvarstrategy* AMPL suffix,
and in the MATLAB interface using the *extendedFeatures.xIntStrategy* structure.

## MINLP callbacks

The Knitro MINLP procedure provides a user callback utility that can be used in the callable library API to perform some user task after each node is processed in the branch-and-bound tree. This callback function is set by calling the following function:

```
int KNITRO_API KN_set_mip_node_callback (KN_context_ptr kc,
KN_user_callback * const fnPtr,
void * const userParams);
```

See the Callable library API reference section in the Reference Manual for details on setting this callback function and the prototype for this callback function.

## Determining convexity/concavity

Knowing whether or not a function is convex may be useful in methods for
mixed integer programming as linearizations derived from convex functions
can be used as outer approximations of those constraints.
These outer approximations are useful
in computing lower bounds. The callable library API allows for the
user to specify whether or not the problem
functions (objective and constraints) are convex or concave via the functions:
`KN_set_obj_property()`

and `KN_set_con_properties()`

.

A function *f* is convex if for any two points *x* and *y*, we have

and concave if

By default functions are assumed to be nonconvex (i.e. neither convex, nor concave).
The objective function and any constraint functions that satisfy the
conditions above should be marked as convex or concave.
All linear functions are convex. Any nonlinear
equality constraint is nonconvex. Knitro will use function convexity/concavity
information to determine whether the optimization problem as a whole is convex or not.
If the problem is determined to be convex Knitro may be able to apply specializations
to improve performance. If you know your model is convex, you can also directly
tell Knitro this by setting the `convex`

option.

The MIP solvers in Knitro are designed for convex problems. For nonconvex problems, these solvers are only heuristics and may terminate at non-optimal points. The continuous solvers in Knitro can handle either convex or nonconvex models. However, for nonconvex models, they may converge to local (rather than global) optimal solutions.

## Additional examples

Examples for solving MINLP problems using the MATLAB, C, C++, Java, C#, Python and R interfaces are provided with the distribution in the knitromatlab and examples directories.