In this post, we explain how to solve linear programming problems in MATLAB. The YouTube video accompanying this post is given below.
Formulation of Linear Programming Problems in MATLAB
To solve linear programming problems in MATLAB you need the Optimization Toolbox.
In MATLAB, linear programming problems are formulated as follows
(1)
where is the vector of coefficients, is the vector of optimization variables, is the matrix of coefficients describing the inequality constraints, is the matrix defining the equality constraints, and , , , and are vectors of coefficients. The relation symbol in (1) is applied element-wise, for example for the vector
(2)
the notation
(3)
means
(4)
The cost function is defined by the scalar variable
(5)
The problem (1) is called linear optimization or linear programming problems, because
1.) Cost function (5) is linear.
2.) The equality and inequality constraints are represented by linear functions.
This problem formulation is very similar to the standard formulation of linear programs. The main difference is that in standard form minimization in (1) is replaced by maximization.
Example of Transforming an Optimization Problem into the MATLAB Form
Here, we explain how to transform an optimization problem into the MATLAB form (1). Consider the following optimization problem
(6)
We can write this problem as follows
(7)
(8)
Implementation in MATLAB
The optimization problem is defined and implemented in the MATLAB script below.
clear
f=[3 -10 5];
A=[5 1 4; -5 3 -3];
b=[100; 50];
Aeq=[1 2 1; -5 7 3];
beq=[10; -5];
lb=[-1000; -500; -1500];
ub=[1000; 500; 1500];
options = optimoptions('linprog','Algorithm','interior-point','Display','iter','MaxIterations',2000,'OptimalityTolerance',1e-9,'ConstraintTolerance',1e-6);
% 'Algorithm' - 'interior-point' or 'dual-simplex' or 'interior-point-legacy'
% 'Display' - 'iter'
% 'MaxIterations'- 2000
% 'OptimalityTolerance'
%
% options = optimoptions('linprog','Algorithm','dual-simplex');
[x,fval,exitflag,output] = linprog(f,A,b,Aeq,beq,lb,ub,options)
We use the MATLAB function linprog() to solve the optimization problem. This function takes as arguments the vector , matrix , vector , matrix , vector , lower bound vector , upper bound vector , and options. This function returns the solution , value of the cost function fval, exitflag (1 if converged), and the output structure that in our case looks like this
iterations: 4
message: 'Minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in feasible directions, to within the selected value of the function tolerance, and constraints are satisfied to within the selected value of the constraint tolerance.'
algorithm: 'interior-point'
constrviolation: 4.6185e-13
firstorderopt: 1.1196e-11
The function optimoptions() is used to define the solver options. First, we tell this function that we define the options for linprog() function. Then we choose a solver, set a display parameter to ‘iter’ (meaning that we display the optimization progress), set the maximum number of iterations, and set the tolerances. The rest of the code is self explanatory.