Mixed-Integer Linear Programming (MILP) - MATLAB Intlinprog
Mixed-Integer Linear Programming (MILP) - MATLAB Intlinprog
Mixed-Integer Linear Programming (MILP) - MATLAB Intlinprog
Syntax
x = intlinprog(f,intcon,A,b)
x = intlinprog(f,intcon,A,b,Aeq,beq)
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub,x0)
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub,x0,options)
x = intlinprog(problem)
[x,fval,exitflag,output] = intlinprog( ___ )
Description
Mixed-integer linear programming solver.
, , intcon, b , beq , lb , and ub are vectors, and A and Aeq are matrices.
f x
You can specify f, intcon, lb , and ub as vectors or arrays. See Matrix Arguments.
Not e
intlinprog applies only to the solver-based approach. For a discussion of the two optimization approaches, see First Choose
Problem-Based or Solver-Based Approach.
x = intlinprog(f,intcon,A,b) solves min f'*x such that the components of x in intcon are integers, and exam ple
A*x ≤ b.
x = intlinprog(f,intcon,A,b,Aeq,beq) solves the problem above while additionally satisfying the equality
constraints Aeq*x = beq. Set A = [] and b = [] if no inequalities exist.
exam ple
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables,
x, so that the solution is always in the range lb ≤ x ≤ ub. Set Aeq = [] and beq = [] if no equalities exist.
exam ple
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub,x0)optimizes using an initial feasible point x0. Set lb = [] and
ub = [] if no bounds exist.
exam ple
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub,x0,options) minimizes using the optimization options
specified in options. Use optimoptions to set these options. Set x0 = [] if no initial point exists.
exam ple
x = intlinprog(problem) uses a problem structure to encapsulate all solver inputs. You can import a problem
structure from an MPS file using mpsread. You can also create a problem structure from an OptimizationProblem
object by using prob2struct.
exam ple
[x,fval,exitflag,output] = intlinprog( ___ ), for any input arguments described above, returns fval = f'*x,
a value exitflag describing the exit condition, and a structure output containing information about the optimization
process.
x + 2x 2 ≥ −14
1
min 8x 1 + x 2 subject to
−4x − x 2 ≤ −33
x
1
2x 1 + x 2 ≤ 20.
f = [8;1];
intcon = 2;
Convert all inequalities into the form A*x <= b by multiplying “greater than” inequalities by -1.
A = [-1,-2;
-4,-1;
2,1];
b = [14;-33;20];
Call intlinprog.
x = intlinprog(f,intcon,A,b)
Intlinprog stopped at the root node because the objective value is within a gap
tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default
value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
x = 2×1
6.5000
7.0000
x , x2 ≥ 0
1
(
min −3x 1 − 2x 2 − x 3 ) subject to
x + x2 + x3 ≤ 7
x
1
4x 1 + 2x 2 + x 3 = 12.
f = [-3;-2;-1];
intcon = 3;
A = [1,1,1];
b = 7;
Aeq = [4,2,1];
beq = 12;
Call intlinprog.
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
Intlinprog stopped at the root node because the objective value is within a gap
tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default
value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
x = 3×1
0
5.5000
1.0000
Compare the number of steps to solve an integer programming problem both with and
without an initial feasible point. The problem has eight variables, four linear equality
Try it in MATLAB
constraints, and has all variables restricted to be positive.
Aeq = [22 13 26 33 21 3 14 26
39 16 22 28 26 30 23 24
18 14 29 27 30 38 26 26
41 26 28 36 18 38 16 26];
beq = [ 7872
10466
11322
12058];
N = 8;
lb = zeros(N,1);
intcon = 1:N;
f = [2 10 13 17 7 5 7 3];
Solve the problem without using an initial point, and examine the display to see the number of branch-and-bound nodes.
[x1,fval1,exitflag1,output1] = intlinprog(f,intcon,[],[],Aeq,beq,lb);
Intlinprog stopped because the objective value is within a gap tolerance of the
optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon
variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the
default value).
For comparison, find the solution using an initial feasible point.
x0 = [8 62 23 103 53 84 46 34];
[x2,fval2,exitflag2,output2] = intlinprog(f,intcon,[],[],Aeq,beq,lb,[],x0);
Intlinprog stopped because the objective value is within a gap tolerance of the
optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon
variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the
default value).
• Without an initial point, intlinprog took about 30,000 branch-and-bound steps.
• Using an initial point, intlinprog took about 5,000 steps.
Giving an initial point does not always help. For this problem, giving an initial point saves time and computational steps. However, for
some problems, giving an initial point can cause intlinprog to take more steps.
x , x2 ≥ 0
1
(
min −3x 1 − 2x 2 − x 3 ) subject to
x + x2 + x3 ≤ 7
x
1
4x 1 + 2x 2 + x 3 = 12
Specify no display.
options = optimoptions('intlinprog','Display','off');
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub,x0,options)
x = 3×1
0
5.5000
1.0000
This example shows how to set up a problem using the problem-based approach and
then solve it using the solver-based approach. The problem is
Try it in MATLAB
x binary
3
x , x2 ≥ 0
1
(
min −3x 1 − 2x 2 − x 3 ) subject to
x + x2 + x3 ≤ 7
x
1
4x 1 + 2x 2 + x 3 = 12
Create an OptimizationProblem object named prob to represent this problem. To specify a binary variable, create an
optimization variable with integer type, a lower bound of 0, and an upper bound of 1.
x = optimvar('x',2,'LowerBound',0);
xb = optimvar('xb','LowerBound',0,'UpperBound',1,'Type','integer');
prob = optimproblem('Objective',-3*x(1)-2*x(2)-xb);
cons1 = sum(x) + xb <= 7;
cons2 = 4*x(1) + 2*x(2) + xb == 12;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
problem = prob2struct(prob);
[sol,fval,exitflag,output] = intlinprog(problem)
Intlinprog stopped at the root node because the objective value is within a gap
tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default
value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
sol = 3×1
0
5.5000
1.0000
fval = -12
exitflag = 1
output = struct with fields:
relativegap: 0
absolutegap: 0
numfeaspoints: 1
numnodes: 0
constrviolation: 0
message: 'Optimal solution found....'
Both sol(1) and sol(3) are binary-valued. Which value corresponds to the binary optimization variable xb?
prob.Variables
The variable xb appears last in the Variables display, so xb corresponds to sol(3) = 1. See Algorithms.
Call intlinprog with more outputs to see solution details and process.
Try it in MATLAB
The goal is to solve the problem
x binary
3
x , x2 ≥ 0
1
(
min −3x 1 − 2x 2 − x 3 ) subject to
x + x2 + x3 ≤ 7
x
1
4x 1 + 2x 2 + x 3 = 12.
f = [-3;-2;-1];
intcon = 3;
A = [1,1,1];
b = 7;
Aeq = [4,2,1];
beq = 12;
lb = zeros(3,1);
ub = [Inf;Inf;1]; % enforces x(3) is binary
[x,fval,exitflag,output] = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
Intlinprog stopped at the root node because the objective value is within a gap
tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default
value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
x = 3×1
0
5.5000
1.0000
fval = -12
exitflag = 1
output = struct with fields:
relativegap: 0
absolutegap: 0
numfeaspoints: 1
numnodes: 0
constrviolation: 0
message: 'Optimal solution found....'
The output structure shows numnodes is 0. This means intlinprog solved the problem before branching. This is one indication
that the result is reliable. Also, the absolutegap and relativegap fields are 0. This is another indication that the result is reliable.
f — Coefficient vect or
real vector | real array
Coefficient vector, specified as a real vector or real array. The coefficient vector represents the objective function f'*x. The notation
assumes that f is a column vector, but you are free to use a row vector or array. Internally, linprog converts f to the column vector
f(:).
If you specify f = [], intlinprog tries to find a feasible point without trying to minimize an objective function.
Example: f = [4;2;-1.7];
Vector of integer constraints, specified as a vector of positive integers. The values in intcon indicate the components of the
decision variable x that are integer-valued. intcon has values from 1 through numel(f).
intcon can also be an array. Internally, intlinprog converts an array intcon to the vector intcon(:).
Example: intcon = [1,2,7] means x(1), x(2), and x(7) take only integer values.
Linear inequality constraint matrix, specified as a matrix of doubles. A represents the linear coefficients in the constraints A*x ≤ b. A
has size M-by-N, where M is the number of constraints and N = numel(f). To save memory, A can be sparse.
Example: A = [4,3;2,0;4,-1]; means three linear inequalities (three rows) for two decision variables (two columns).
Linear inequality constraint vector, specified as a vector of doubles. b represents the constant vector in the constraints A*x ≤ b. b
has length M, where A is M-by-N.
Example: [4,0]
Linear equality constraint matrix, specified as a matrix of doubles. Aeq represents the linear coefficients in the constraints
Aeq*x = beq. Aeq has size Meq-by-N, where Meq is the number of constraints and N = numel(f). To save memory, Aeq can be
sparse.
Example: A = [4,3;2,0;4,-1]; means three linear inequalities (three rows) for two decision variables (two columns).
Linear equality constraint vector, specified as a vector of doubles. beq represents the constant vector in the constraints
Aeq*x = beq. beq has length Meq, where Aeq is Meq-by-N.
Example: [4,0]
lb — Lower bounds
[] (default) | real vector or array
Lower bounds, specified as a vector or array of doubles. lb represents the lower bounds elementwise in lb ≤ x ≤ ub.
ub — Upper bounds
[] (default) | real vector or array
Upper bounds, specified as a vector or array of doubles. ub represents the upper bounds elementwise in lb ≤ x ≤ ub.
Providing x0 can change the amount of time intlinprog takes to converge. It is difficult to predict how x0 affects the solver. For
suggestions on using appropriate Heuristics with x0, see Tips.
x0 must be feasible with respect to all constraints. If x0 is not feasible, the solver errors. If you do not have a feasible x0, set x0 =
[].
Example: x0 = 100*rand(size(f))
Some options are absent from the optimoptions display. These options appear in italics in the following table. For details, see
View Options.
U – L <= AbsoluteGapTolerance.
ConstraintTolerance Real from 1e-9 through 1e-3 that is the maximum discrepancy 1e-4
that linear constraints can have and still be considered satisfied.
ConstraintTolerance is not a stopping criterion.
Heuristics Algorithm for searching for feasible points (see Heuristics for 'basic'
Finding Feasible Solutions):
• 'basic'
• 'intermediate'
• 'advanced'
• 'rss'
• 'rins'
• 'round'
• 'diving'
• 'rss-diving'
• 'rins-diving'
• 'round-diving'
• 'none'
IntegerTolerance Real from 1e-6 through 1e-3, where the maximum deviation from 1e-5
integer that a component of the solution x can have and still be
considered an integer. IntegerTolerance is not a stopping
criterion.
LPMaxIterations Strictly positive integer, the maximum number of simplex algorithm max(3e4, 10*
iterations per node during the branch-and-bound process. (numberOfEqualities +
numberOfInequalities +
numberOfVariables))
In this expression,
numberOfEqualities
means the number of rows of
Aeq,
numberOfInequalities
means the number of rows of
A, and numberOfVariables
means the number of
elements of f.
LPPreprocess T ype of preprocessing for the solution to the relaxed linear program 'basic'
(see Linear Program Preprocessing):
• 'none' — No preprocessing.
• 'basic' — Use preprocessing.
MaxNodes Strictly positive integer that is the maximum number of nodes 1e7
intlinprog explores in its branch-and-bound process.
MaxTime Positive real that is the maximum time in seconds that intlinprog 7200
runs.
Opt io n Descript io n Def ault
ObjectiveCutOff Real greater than -Inf. During the branch-and-bound calculation, Inf
intlinprog discards any node where the linear programming
solution has an objective value exceeding ObjectiveCutOff.
RelativeGapTolerance Real from 0 through 1. intlinprog stops if the relative difference 1e-4
between the internally calculated upper (U) and lower (L) bounds on
the objective function is less than or equal to
RelativeGapTolerance:
Not e
Although you specify RelativeGapTolerance as a
decimal number, the iterative display and
output.relativegap report the gap as a
percentage, meaning 100 times the measured relative
gap. If the exit message refers to the relative gap, this
value is the measured relative gap, not a percentage.
RootLPMaxIterations Nonnegative integer that is the maximum number of simplex max(3e4, 10*
algorithm iterations to solve the initial linear programming problem. (numberOfEqualities +
numberOfInequalities +
numberOfVariables))
In this expression,
numberOfEqualities
means the number of rows of
Aeq,
numberOfInequalities
means the number of rows of
A, and numberOfVariables
means the number of
elements of f.
Example: options = optimoptions('intlinprog','MaxTime',120)
Structure encapsulating the inputs and options, specified with the following fields.
• f
• intcon
• solver
• options
x — Solut ion
real vector
Solution, returned as a vector that minimizes f'*x subject to all bounds, integer constraints, and linear constraints.
Algorithm stopping condition, returned as an integer identifying the reason the algorithm stopped. The following lists the values of
exitflag and the corresponding reasons intlinprog stopped.
3 T he solution is feasible with respect to the relative ConstraintTolerance tolerance, but is not
feasible with respect to the absolute tolerance.
Exitflags 3 and -9 relate to solutions that have large infeasibilities. These usually arise from linear constraint matrices that have large
condition number, or problems that have large solution components. To correct these issues, try to scale the coefficient matrices,
eliminate redundant linear constraints, or give tighter bounds on the variables.
Solution process summary, returned as a structure containing information about the optimization process.
relativegap Relative percentage difference between upper (U) and lower (L) bounds of the
objective function that intlinprog calculates in its branch-and-bound algorithm.
Not e
Although you specify RelativeGapTolerance as a decimal
number, the iterative display and output.relativegap report the
gap as a percentage, meaning 100 times the measured relative gap.
If the exit message refers to the relative gap, this value is the
measured relative gap, not a percentage.
absolutegap Difference between upper and lower bounds of the objective function that
intlinprog calculates in its branch-and-bound algorithm.
numnodes Number of nodes in branch-and-bound algorithm. If the solution was found during
preprocessing or during the initial cuts, numnodes = 0.
Limitations
• Often, some supposedly integer-valued components of the solution x(intCon) are not precisely integers. intlinprog deems as
integers all solution values within IntegerTolerance of an integer.
To round all supposed integers to be exactly integers, use the round function.
x(intcon) = round(x(intcon));
Caut ion
Rounding solutions can cause the solution to become infeasible. Check feasibility after rounding:
max(A*x - b) % See if entries are not too positive, so have small infeasibility
max(abs(Aeq*x - beq)) % See if entries are near enough to zero
max(x - ub) % Positive entries are violated bounds
max(lb - x) % Positive entries are violated bounds
• intlinprog does not enforce that solution components be integer-valued when their absolute values exceed 2.1e9. When your
solution has such components, intlinprog warns you. If you receive this warning, check the solution to see whether supposedly
integer-valued components of the solution are close to integers.
• intlinprog does not allow components of the problem, such as coefficients in f, A, or ub, to exceed 1e25 in absolute value. If you try
to run intlinprog with such a problem, intlinprog issues an error.
• Currently, you cannot run intlinprog in the Optimization app.
Tips
• To specify binary variables, set the variables to be integers in intcon, and give them lower bounds of 0 and upper bounds of 1.
• Save memory by specifying sparse linear constraint matrices A and Aeq. However, you cannot use sparse matrices for b and beq.
• If you include an x0 argument, intlinprog uses that value in the 'rins' and guided diving heuristics until it finds a better integer-
feasible point. So when you provide x0, you can obtain good results by setting the 'Heuristics' option to 'rins-diving' or
another setting that uses 'rins'.
• To provide logical indices for integer components, meaning a binary vector with 1 indicating an integer, convert to intcon form using
find. For example,
logicalindices = [1,0,0,1,1,0,0];
intcon = find(logicalindices)
intcon =
1 4 5
• intlinprog replaces bintprog. To update old bintprog code to use intlinprog, make the following changes:
˗ Set intcon to 1:numVars, where numVars is the number of variables in your problem.
˗ Set lb to zeros(numVars,1).
˗ Set ub to ones(numVars,1).
˗ Update any relevant options. Use optimoptions to create options for intlinprog.
˗ Change your call to bintprog as follows:
[x,fval,exitflag,output] = bintprog(f,A,b,Aeq,Beq,x0,options)
% Change your call to:
[x,fval,exitflag,output] = intlinprog(f,intcon,A,b,Aeq,Beq,lb,ub,x0,options)
See Also
linprog | mpsread | optimoptions | prob2struct
Topics
Mixed-Integer Linear Programming Basics: Solver-Based
Factory, Warehouse, Sales Allocation Model: Solver-Based
Traveling Salesman Problem: Solver-Based
Solve Sudoku Puzzles Via Integer Programming: Solver-Based
Mixed-Integer Quadratic Programming Portfolio Optimization: Solver-Based
Optimal Dispatch of Power Generators: Solver-Based
Mixed-Integer Linear Programming Algorithms
Tuning Integer Linear Programming
Solver-Based Optimization Problem Setup