You are on page 1of 24

MTH 3107: Linear Programming

J. Kizito

Makerere University

e-mail: john.kizito@mak.ac.ug
www: https://www.socnetsolutions.com/~jona
materials: https://www.socnetsolutions.com/~jona/materials/MTH3107
e-learning environment: http://muele.mak.ac.ug
office: block A, level 3, department of computer science
alt. office: institute of open, distance, and eLearning

Solving LP Problems with MATLAB

Kizito (Makerere University) MTH 3107 Feb, 2022 1 / 13


Overview

1 Syntax

2 Arguments

3 Examples

4 Conclusion

Kizito (Makerere University) MTH 3107 Feb, 2022 2 / 13


Syntax

Syntax
Recall: Matrix Notation

Example 1 General Form


$
maximize 3x1 2x2 '
& Ax ¤ b,
subject to max f T x such that Aeq  x  beq,
4x1 2x2 ¤ 16 '
lb ¤ x ¤ ub.
x
%
x1 2x2 ¤ 8
x1 x2 ¤ 5
x1 ¥ 0; x2 ¥ 0 That is to say...
f = [-3;
 -2] 
In Matrix Notation 4 2
 
 x1  A = 1 2
max 3 2 1 1

x
 2  
4 2   16 b = [16; 8; 5]
s.t 1 2 1 ¤  8 
x lb = zeros(2,1)
x2
1 1 5
Kizito (Makerere University) MTH 3107 Feb, 2022 3 / 13
Syntax

Syntax
Recall: Matrix Notation

Example 1 General Form


$
maximize 3x1 2x2 '
& Ax ¤ b,
subject to max f T x such that Aeq  x  beq,
4x1 2x2 ¤ 16 '
lb ¤ x ¤ ub.
x
%
x1 2x2 ¤ 8
x1 x2 ¤ 5
x1 ¥ 0; x2 ¥ 0 That is to say...
f = [-3;
 -2] 
In Matrix Notation 4 2
 
 x1  A = 1 2
max 3 2 1 1

x
 2  
4 2   16 b = [16; 8; 5]
s.t 1 2 1 ¤  8 
x lb = zeros(2,1)
x2
1 1 5
Kizito (Makerere University) MTH 3107 Feb, 2022 3 / 13
Syntax

Syntax
Recall: Matrix Notation

Example 1 General Form


$
maximize 3x1 2x2 '
& Ax ¤ b,
subject to max f T x such that Aeq  x  beq,
4x1 2x2 ¤ 16 '
lb ¤ x ¤ ub.
x
%
x1 2x2 ¤ 8
x1 x2 ¤ 5
x1 ¥ 0; x2 ¥ 0 That is to say...
f = [-3;
 -2] 
In Matrix Notation 4 2
 
 x1  A = 1 2
max 3 2 1 1

x
 2  
4 2   16 b = [16; 8; 5]
s.t 1 2 1 ¤  8 
x lb = zeros(2,1)
x2
1 1 5
Kizito (Makerere University) MTH 3107 Feb, 2022 3 / 13
Syntax

Syntax
Recall: Matrix Notation

Example 1 General Form


$
maximize 3x1 2x2 '
& Ax ¤ b,
subject to max f T x such that Aeq  x  beq,
4x1 2x2 ¤ 16 '
lb ¤ x ¤ ub.
x
%
x1 2x2 ¤ 8
x1 x2 ¤ 5
x1 ¥ 0; x2 ¥ 0 That is to say...
f = [-3;
 -2] 
In Matrix Notation 4 2
 
 x1  A = 1 2
max 3 2 1 1

x
 2  
4 2   16 b = [16; 8; 5]
s.t 1 2 1 ¤  8 
x lb = zeros(2,1)
x2
1 1 5
Kizito (Makerere University) MTH 3107 Feb, 2022 3 / 13
Arguments

Syntax
Input Arguments

rx, fval, exitflag , output, lambdas  linprog pf , A, b, Aeq, beq, lb, up, x0, options q
f Linear objective function vector
A or Aineq: Matrix for linear inequality constraints
b or bineq: Vector for linear inequality constraints
Aeq Matrix for linear equality constraints
beq Vector for linear equality constraints
lb Vector of lower bounds
ub Vector of upper bounds
x0 Initial point for x, active set algorithm only
solver ’linprog’
options Optimization options used by linprog. Some options apply
to all algorithms, and others are only relevant when using the
large-scale algorithm

Kizito (Makerere University) MTH 3107 Feb, 2022 4 / 13


Examples

Example

Example 2 Now in MATLAB


minimize f px q  5x1  4x2  6x3 , ¡ f = [-5; -4; -6]
subject to ¡ A = [1 -1 1; 3 2 4; 3 2 0]
x1  x2 x3 ¤ 20 ¡ b = [20; 42; 30]
3x1 2x2 4x3 ¤ 42 ¡ lb = zeros(3,1)
3x1 2x2 ¤ 30 ¡ [x,fval,exitflag,output,lambda]
0 ¤ x1 , 0 ¤ x2 , 0 ¤ x3 = linprog(f,A,b,[],[],lb)

In Matrix Notation Solution


   
 x1 
0
min 5 4 6 x2  x  15, fval  78, exitflag 1
x 3
   3  
1 1 1 x1 20
s.t 3 2 4 x2  ¤ 42
3 2 0 x3 30
Kizito (Makerere University) MTH 3107 Feb, 2022 5 / 13
Examples

Example

Example 2 Now in MATLAB


minimize f px q  5x1  4x2  6x3 , ¡ f = [-5; -4; -6]
subject to ¡ A = [1 -1 1; 3 2 4; 3 2 0]
x1  x2 x3 ¤ 20 ¡ b = [20; 42; 30]
3x1 2x2 4x3 ¤ 42 ¡ lb = zeros(3,1)
3x1 2x2 ¤ 30 ¡ [x,fval,exitflag,output,lambda]
0 ¤ x1 , 0 ¤ x2 , 0 ¤ x3 = linprog(f,A,b,[],[],lb)

In Matrix Notation Solution


   
 x1 
0
min 5 4 6 x2  x  15, fval  78, exitflag 1
x 3
   3  
1 1 1 x1 20
s.t 3 2 4 x2  ¤ 42
3 2 0 x3 30
Kizito (Makerere University) MTH 3107 Feb, 2022 5 / 13
Examples

Example

Example 2 Now in MATLAB


minimize f px q  5x1  4x2  6x3 , ¡ f = [-5; -4; -6]
subject to ¡ A = [1 -1 1; 3 2 4; 3 2 0]
x1  x2 x3 ¤ 20 ¡ b = [20; 42; 30]
3x1 2x2 4x3 ¤ 42 ¡ lb = zeros(3,1)
3x1 2x2 ¤ 30 ¡ [x,fval,exitflag,output,lambda]
0 ¤ x1 , 0 ¤ x2 , 0 ¤ x3 = linprog(f,A,b,[],[],lb)

In Matrix Notation Solution


   
 x1 
0
min 5 4 6 x2  x  15, fval  78, exitflag 1
x 3
   3  
1 1 1 x1 20
s.t 3 2 4 x2  ¤ 42
3 2 0 x3 30
Kizito (Makerere University) MTH 3107 Feb, 2022 5 / 13
Examples

Example

Example 2 Now in MATLAB


minimize f px q  5x1  4x2  6x3 , ¡ f = [-5; -4; -6]
subject to ¡ A = [1 -1 1; 3 2 4; 3 2 0]
x1  x2 x3 ¤ 20 ¡ b = [20; 42; 30]
3x1 2x2 4x3 ¤ 42 ¡ lb = zeros(3,1)
3x1 2x2 ¤ 30 ¡ [x,fval,exitflag,output,lambda]
0 ¤ x1 , 0 ¤ x2 , 0 ¤ x3 = linprog(f,A,b,[],[],lb)

In Matrix Notation Solution


   
 x1 
0
min 5 4 6 x2  x  15, fval  78, exitflag 1
x 3
   3  
1 1 1 x1 20
s.t 3 2 4 x2  ¤ 42
3 2 0 x3 30
Kizito (Makerere University) MTH 3107 Feb, 2022 5 / 13
Examples

Output Arguments – rx, fval, exitflag , output, lambdas


exitflag Integer identifying the reason the algorithm terminated. The following lists the values
of exitflag and the corresponding reasons the algorithm terminated
1 Function converged to a solution x
0 Number of iterations exceeded options.MaxIter
-2 No feasible point was found
-3 Problem is unbounded
-4 NaN value was encountered during execution of the algorithms
-5 Both primal and dual problems are infeasible
-7 Search direction became too small. No further progress could be made
lambda Structure containing the Lagrange multipliers at the solution x (separated by con-
straint type). The fields of the structure are:
lower Lower bounds lb
upper Upper bounds ub
ineqlin Linear inequalities
eqlin Linear equalities
output Structure containing information about the optimization. The fields of the structure
are:
iterations Number of iterations
algorithm Optimization algorithm used
cgiterations 0 (large-scale algorithm only, included for backward compatibility)
message Exit message
constrviolation Maximum of constraint functions
firstorderopt First-order optimality measure
Kizito (Makerere University) MTH 3107 Feb, 2022 6 / 13
Examples

Example

Example 1 Solution
 
maximize 3x1 2x2
subject to x  3
2
, fval  13, exitflag 1
4x1 2x2 ¤ 16
x1 2x2 ¤ 8
x1 x2 ¤ 5
Note that we obtain the same solution
x1 ¥ 0; x2 ¥ 0
to this problem as we did when using
the Simplex Method
linprog solves linear programming
In MATLAB minimization problems
¡ f = [-3 -2] For maximization problems, we
¡ A = [4 2; 1 2; 1 1] multiply the objective function by 1.
¡ b = [16; 8; 5] Consequently, the same will apply to
¡ lb = zeros(2,1) fval
¡ [x,fval,exitflag] = i.e., fval  13 for the maximization
linprog(f,A,b,[],[],lb) problem above
Kizito (Makerere University) MTH 3107 Feb, 2022 7 / 13
Examples

Example

Example 1 Solution
 
maximize 3x1 2x2
subject to x  3
2
, fval  13, exitflag 1
4x1 2x2 ¤ 16
x1 2x2 ¤ 8
x1 x2 ¤ 5
Note that we obtain the same solution
x1 ¥ 0; x2 ¥ 0
to this problem as we did when using
the Simplex Method
linprog solves linear programming
In MATLAB minimization problems
¡ f = [-3 -2] For maximization problems, we
¡ A = [4 2; 1 2; 1 1] multiply the objective function by 1.
¡ b = [16; 8; 5] Consequently, the same will apply to
¡ lb = zeros(2,1) fval
¡ [x,fval,exitflag] = i.e., fval  13 for the maximization
linprog(f,A,b,[],[],lb) problem above
Kizito (Makerere University) MTH 3107 Feb, 2022 7 / 13
Examples

Example

Example 1 Solution
 
maximize 3x1 2x2
subject to x  3
2
, fval  13, exitflag 1
4x1 2x2 ¤ 16
x1 2x2 ¤ 8
x1 x2 ¤ 5
Note that we obtain the same solution
x1 ¥ 0; x2 ¥ 0
to this problem as we did when using
the Simplex Method
linprog solves linear programming
In MATLAB minimization problems
¡ f = [-3 -2] For maximization problems, we
¡ A = [4 2; 1 2; 1 1] multiply the objective function by 1.
¡ b = [16; 8; 5] Consequently, the same will apply to
¡ lb = zeros(2,1) fval
¡ [x,fval,exitflag] = i.e., fval  13 for the maximization
linprog(f,A,b,[],[],lb) problem above
Kizito (Makerere University) MTH 3107 Feb, 2022 7 / 13
Examples

Example

Example 1 Solution
 
maximize 3x1 2x2
subject to x  3
2
, fval  13, exitflag 1
4x1 2x2 ¤ 16
x1 2x2 ¤ 8
x1 x2 ¤ 5
Note that we obtain the same solution
x1 ¥ 0; x2 ¥ 0
to this problem as we did when using
the Simplex Method
linprog solves linear programming
In MATLAB minimization problems
¡ f = [-3 -2] For maximization problems, we
¡ A = [4 2; 1 2; 1 1] multiply the objective function by 1.
¡ b = [16; 8; 5] Consequently, the same will apply to
¡ lb = zeros(2,1) fval
¡ [x,fval,exitflag] = i.e., fval  13 for the maximization
linprog(f,A,b,[],[],lb) problem above
Kizito (Makerere University) MTH 3107 Feb, 2022 7 / 13
Examples

Formulation Examples
Example 3

Problem
You need to buy some filing cabinets. You know that Cabinet X costs $10 per unit,
requires six square feet of floor space, and holds eight cubic feet of files. Cabinet Y
costs $20 per unit, requires eight square feet of floor space, and holds twelve cubic feet
of files. You have been given $140 for this purchase, though you don’t have to spend
that much. The office has room for no more than 72 square feet of cabinets. How many
of which model should you buy, in order to maximize storage volume?

Formulation
1 Let x be the number of model X cabinets purchased,
y the number of model Y cabinets purchased
2 Cost: 10x 20y ¤ 140
Space: 6x 8y ¤ 72
3 Volume: 8x 12y
4 Write as a dictionary

Kizito (Makerere University) MTH 3107 Feb, 2022 8 / 13


Examples

Formulation Examples
Example 3

Problem
You need to buy some filing cabinets. You know that Cabinet X costs $10 per unit,
requires six square feet of floor space, and holds eight cubic feet of files. Cabinet Y
costs $20 per unit, requires eight square feet of floor space, and holds twelve cubic feet
of files. You have been given $140 for this purchase, though you don’t have to spend
that much. The office has room for no more than 72 square feet of cabinets. How many
of which model should you buy, in order to maximize storage volume?

Formulation
1 Let x be the number of model X cabinets purchased,
y the number of model Y cabinets purchased
2 Cost: 10x 20y ¤ 140
Space: 6x 8y ¤ 72
3 Volume: 8x 12y
4 Write as a dictionary

Kizito (Makerere University) MTH 3107 Feb, 2022 8 / 13


Examples

Formulation Examples
Example 4

Problem
A school is preparing a trip for 400 students. The company which is providing the
transportation has 10 buses of 50 seats each and 8 buses of 40 seats, but only has 9
drivers available. The rental cost for a large bus is $800 and $600 for the small bus.
Calculate how many buses of each type should be used for the trip for the least possible
cost

Formulation
1 Let x be the number of small buses, y the number of big buses
2 minimize 600x 800y
subject to
40x 50y ¥ 400
x1 2x2 ¤ 8
x y ¤9
x ¥ 0; y ¥ 0

Kizito (Makerere University) MTH 3107 Feb, 2022 9 / 13


Examples

Formulation Examples
Example 4

Problem
A school is preparing a trip for 400 students. The company which is providing the
transportation has 10 buses of 50 seats each and 8 buses of 40 seats, but only has 9
drivers available. The rental cost for a large bus is $800 and $600 for the small bus.
Calculate how many buses of each type should be used for the trip for the least possible
cost

Formulation
1 Let x be the number of small buses, y the number of big buses
2 minimize 600x 800y
subject to
40x 50y ¥ 400
x1 2x2 ¤ 8
x y ¤9
x ¥ 0; y ¥ 0

Kizito (Makerere University) MTH 3107 Feb, 2022 9 / 13


Examples

Formulation Examples
Example 5 – Problem
A cargo plane has three compartments for storing cargo: front, center and rear. These
compartments have the following limits on both weight and space:

Compartment Weight capacity (tons) Space capacity (m3 )


Front 10 6800
Center 16 8700
Rear 8 5300

Furthermore, the weight of the cargo in the respective compartments must be the same
proportion of that compartments weight capacity to maintain the balance of the plane. The
following four cargoes are available for shipment on the next flight:

Cargo Weight (tons) Volume (m3 {ton) Profit (${ton)


C1 18 480 310
C2 15 650 380
C3 23 580 350
C4 12 390 285

Any proportion of these cargoes can be accepted. The objective is to determine how much (if
any) of each cargo C1, C2, C3 and C4 should be accepted and how to distribute each among
the compartments so that the total profit for the flight is maximized.
Kizito (Makerere University) MTH 3107 Feb, 2022 10 / 13
Examples

Formulation Examples
Example 5 – Formulation

1 Let xij : the number of tonnes of cargo i that is put into compartment j where
xij ¥ 0 for i  1, 2, 3, 4; j  1, 2, 3 (Front, Centre, Rear)

2 Available cargo: 3 Weight capacity:


x11 x12 x13 ¤ 18 x11 x21 x31 x41 ¤ 10
x21 x22 x23 ¤ 15 x12 x22 x32 x42 ¤ 16
x31 x32 x33 ¤ 23 x13 x23 x33 x43 ¤8
x41 x42 x43 ¤ 12
4 Volume (space) capacity:
480x11 650x21 580x31 390x41 ¤ 6800
480x12 650x22 580x32 390x42 ¤ 8700
480x13 650x23 580x33 390x43 ¤ 5300
5 Weight of the cargo in the respective compartments:
px11 x21 x31 x41 q{10  px12 x22 x32 x42 q{16  px13 x23 x33 x43 q{8
6 Objective – maximize total profit:
310px11 x12 x13 q 380px21 x22 x23 q 350px31 x32 x33 q 285px41 x42 x43 q
Kizito (Makerere University) MTH 3107 Feb, 2022 11 / 13
Examples

Formulation Examples
Example 5 – The Dictionary

maximize
310px11 x12 x13 q 380px21 x22 x23 q 350px31 x32 x33 q 285px41 x42 x43 q
subject to
x11 x12 x13 ¤ 18
x21 x22 x23 ¤ 15
x31 x32 x33 ¤ 23
x41 x42 x43 ¤ 12
x11 x21 x31 x41 ¤ 10
x12 x22 x32 x42 ¤ 16
x13 x23 x33 x43 ¤ 8
480x11 650x21 580x31 390x41 ¤ 6800
480x12 650x22 580x32 390x42 ¤ 8700
480x13 650x23 580x33 390x43 ¤ 5300
16x11 16x21 16x31 16x41  10x12  10x22  10x32  10x42  0
8x12 8x22 8x32 8x42  16x13  16x23  16x33  16x43  0

Kizito (Makerere University) MTH 3107 Feb, 2022 12 / 13


Conclusion

Example 5
Conclusion

This example demonstrates the advantages of using a software package to


solve the linear program, rather than a judgemental approach, which in
this case include:
1 Actually maximise profit, rather than just believing that our
judgemental solution does so (we may have bad judgement, even if
we are experts)
2 Makes the cargo loading the decision one that we can solve in a
routine operational manner on a computer, rather than having to
exercise judgement each and every time we want to solve it
3 Problems that can be appropriately formulated as linear programs are
almost always better solved by computers than by people
4 Can perform sensitivity analysis very easily using a computer

Kizito (Makerere University) MTH 3107 Feb, 2022 13 / 13

You might also like