1 © 2014 The MathWorks, Inc.
Mixed-integer linear programming in
MATLAB
Seth DeLand
2
Topics
Refresher on Mixed-integer linear programming (MILP)
Solving an MILP problem in MATLAB
MILP algorithms
Tuning the solver
3
MILP vs. LP
Linear Programming (linprog)
min𝑥
𝑓𝑇𝑥 𝐴𝑥 ≤ 𝑏
𝐴𝑒𝑞𝑥 = 𝑏𝑒𝑞𝑙𝑏 ≤ 𝑥 ≤ 𝑢𝑏
s.t.
Mixed-integer LP (intlinprog)
min𝑥
𝑓𝑇𝑥 𝐴𝑥 ≤ 𝑏
𝐴𝑒𝑞𝑥 = 𝑏𝑒𝑞𝑙𝑏 ≤ 𝑥 ≤ 𝑢𝑏
s.t.
Some (or all) 𝑥 must be integers
[xopt,fval,eflag,output] = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub,opts);
[xopt,fval,eflag,output] = linprog(f,A,b,Aeq,beq,lb,ub,opts);
4
Demo: Cash-flow matching
Idea: Buy bonds to cover pension
fund obligations
Variables: How many of each
bond to buy?
Constraints: Payments from
bonds must be greater than or
equal to pension fund obligations
Objective: Minimize the size of
the investment you make 1 2 3 4 5 6 7 80
20
40
60
80
100
120
Time Period
Ca
sh
Flo
w (
$)
1 2 3 4 5 6 7 80
5
10
15x 10
5
Time Period
Pa
ym
en
ts (
$)
5
𝑛1⋮𝑛5
Extending the problem
−𝑐𝑎𝑠ℎ𝐹𝑙𝑜𝑤𝑠
𝑛1⋮𝑛5
≤ −𝑜𝑏𝑙𝑖𝑔𝑎𝑡𝑖𝑜𝑛𝑠
𝑛1⋮𝑛5𝑦1⋮𝑦5
1
0⋱
0
1
−1000
0⋱
0
−1000
𝑛1⋮𝑛5𝑦1⋮𝑦5
=0⋮0
min𝑥
𝑓𝑇𝑥 𝐴𝑥 ≤ 𝑏
𝐴𝑒𝑞𝑥 = 𝑏𝑒𝑞𝑙𝑏 ≤ 𝑥 ≤ 𝑢𝑏
s.t.
𝑥(intcon) must be integers 𝑥:
𝑛1 = 1000𝑦1
⋮ 𝑛5 = 1000𝑦5
−𝑐𝑎𝑠ℎ𝐹𝑙𝑜𝑤𝑠 𝑧𝑒𝑟𝑜𝑠(8,5)
𝑛1⋮
𝑛5𝑦1⋮
𝑦5
≤ −𝑜𝑏𝑙𝑖𝑔𝑎𝑡𝑖𝑜𝑛𝑠
6
Recap
Better solution from intlinprog than rounding the
solution from linprog
Added binary variables
– Allowed us to model “quantities of 1000”
– Updated sizes of constraint matrices and objective function
7
Demo: Index replication
C(i,j) : how closely is asset i related to asset j?
Correlation matrix
8
Index replication
Goal: pick a subset of assets based on correlations
– If asset A is highly correlated with asset B, then we only need one
of them in our replicating portfolio
Each asset in the replicating portfolio represents one or
more assets in the universe
Optimization problem: select 20 assets for the
replicating portfolio that best represent the universe
9
Approach – binary mask
1 0 0 0 0
0 0 0 1 0
1 0 0 0 0
0 0 0 1 0
0 0 0 1 0
x:
.*
1 0.18 0.71 0.48 0.23
0.18 1 0.33 0.54 0.44
0.71 0.33 1 0.12 0.31
0.48 0.54 0.12 1 0.63
0.23 0.44 0.31 0.63 1
C:
Constraints on x
Only a single 1 in each row
Only 2 columns that contain 1’s
Objective
Maximize sum(C.*x)
10
1 0 0 0 0
0 0 0 0.54 0
0.71 0 0 0 0
0 0 0 1 0
0 0 0 0.63 0
Approach – binary mask
Constraints on x
Only a single 1 in each row
Only 2 columns that contain 1’s
Objective
Maximize sum(C.*x)
1 0 0 0 0
0 0 0 1 0
1 0 0 0 0
0 0 0 1 0
0 0 0 1 0
x:
.*
1 0.18 0.71 0.48 0.23
0.18 1 0.33 0.54 0.44
0.71 0.33 1 0.12 0.31
0.48 0.54 0.12 1 0.63
0.23 0.44 0.31 0.63 1
C: sum(C.*x) = 3.88
11
MILP formulation
Variables:
Constraints:
Objective:
𝑥𝑖,𝑗 0 − 1 is stock i represented by stock j ?
𝑦𝑗 0 − 1 is stock j in the replicating portfolio?
𝑥𝑖,𝑗 = 1
𝑛
𝑖=1
𝑥𝑖,𝑗 − 𝑦𝑗 ≤ 0
𝑛
𝑖=1
max𝑥
𝐶𝑖,𝑗𝑥𝑖,𝑗
𝑛
𝑗=1
𝑛
𝑖=1
𝑦𝑗 = 20
𝑛
𝑗=1
Only
one “1”
per row
Auxiliary
variables
12
MILP algorithm
LP
Preprocessing
Root LP
Cut Generation
Heuristics
Integer
Programming
Preprocessing
Branch and
Bound
Reduce the problem size
Solve the problem without integer constraints
Tighten LP relaxation
Further tighten LP relaxation
Try to find integer feasible solutions
Systematically search for optimal solution
13
Tuning options
LP
Preprocessing
Root LP
Cut Generation
Heuristics
Integer
Programming
Preprocessing
Branch and
Bound
LPPreprocess
RootLPAlgorithm, RootLPMaxIter
IPPreprocess
CutGeneration,
CutGenMaxIter
Heuristics,
HeuristicsMaxNodes
BranchingRule, LPMaxIter, MaxNodes,
MaxNumFeasPoints, NodeSelection
14
Recap
Found 20 assets that best represent universe
Large optimization problem: >100k binary variables
intlinprog algorithm consists of several steps
– Change option values to tune solver
15
Key takeaways
intlinprog function for MILP (R2014a)
MILP gives better solutions than rounding LP solutions
Use auxiliary variables to expand scope of problems
Can solve large problems, but performance is problem
dependent