Solving transportation problems (LPP) using linprog in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on solving transportation problems (TP) using the linprog function in MATLAB's Optimization Toolbox. The user successfully implemented a MATLAB function to convert a TP into a linear programming problem (LPP) and optimize it, achieving an optimal solution with a cost of 156. This contrasts with the initial basic feasible solution obtained using the North-West corner rule, which yielded a cost of 226. The user confirms that different methods can produce varying results, with MATLAB's approach being more efficient.

PREREQUISITES
  • Understanding of transportation problems (TP) and linear programming problems (LPP)
  • Familiarity with MATLAB and its Optimization Toolbox
  • Knowledge of the linprog function in MATLAB
  • Basic concepts of constraints and objective functions in optimization
NEXT STEPS
  • Explore advanced features of MATLAB's linprog function for optimization
  • Learn about different methods for solving transportation problems, such as Vogel's Approximation and the row-minima method
  • Investigate how to handle unbalanced transportation problems in MATLAB
  • Study the implications of different initial basic feasible solutions in linear programming
USEFUL FOR

Data scientists, operations researchers, and MATLAB users interested in optimizing transportation logistics and understanding linear programming methodologies.

Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,180
Reaction score
2,690
I want to solve transportation problems (TP) in MATLAB. For solving LPPs, MATLAB provides the linprog function in the Optimization Toolbox.

According to my book, the following is a TP:
##\mathbf{D_1}##​
##\mathbf{D_2}##​
##\mathbf{D_3}##​
##\mathbf{D_4}##​
##\mathbf{O_1}##​
##c_{11}##​
##c_{12}##​
##c_{13}##​
##c_{14}##​
##\mathbf{a_1}##​
##\mathbf{O_2}##​
##c_{21}##​
##c_{22}##​
##c_{23}##​
##c_{24}##​
##\mathbf{a_2}##​
##\mathbf{O_3}##​
##c_{31}##​
##c_{32}##​
##c_{33}##​
##c_{34}##​
##\mathbf{a_3}##​
##\mathbf{b_1}##​
##\mathbf{b_2}##​
##\mathbf{b_3}##​
##\mathbf{b_4}##​

Here, ##D_j## are the destinations, ##O_i## are the origins, ##a_i## are the availabilities at the origins ##O_i##, ##b_j## the requirements at destinations ##D_j##, and ##c_{ij}## is the cost of transport of unit commodity from ##O_i## to ##D_j##. We assume that the problem is balanced, i.e. ##\sum\limits_i a_i = \sum\limits_j b_j##.

There are several methods for solving TPs, like North-West corner rule, row-minima method, etc. Here, however, I am interested in converting the above TP to an LPP. The LPP will be:

Minimize the objective function, $$Z = \sum_{i, j} c_{ij} ~ x_{ij}$$ subject to the constraints
  • ##\sum_j x_{ij} = a_i##,
  • ##\sum_i x_{ij} = b_j##,
  • ##x_{ij} \ge 0##.
I have written the following MATLAB function file:
Matlab:
function tp(cost, avb, req)

    if sum(avb) ~= sum(req)
      
        exc = MException('tp:unbalancedProblem', ...
                            'Cannot solve unbalanced problem.');
  
        throw(exc);
    end
  
    x = optimvar('x', size(cost, 1), size(cost, 2), 'LowerBound', 0);
  
    z = sum(x .* cost, 'all');
  
    numCons = size(cost, 1) + size(cost, 2);
  
    cons = optimconstr(numCons, 1);
  
    count = 1;
    for i = 1:1:size(x, 1)
        cons(count) = sum(x(i, :)) == avb(i);
        count = count + 1;
    end
  
    for i = 1:1:size(x, 2)
        cons(count) = sum(x(:, i)) == req(i);
        count = count + 1;
    end
  
    problem = optimproblem('Objective', z, 'ObjectiveSense', 'min');
    problem.Constraints = cons;
  
    show(problem)
  
    problem = prob2struct(problem);
  
    [sol, zval, exitflag, output] = linprog(problem)

end
Let's consider the following worked out problem from my book:
##\mathbf{D_1}##​
##\mathbf{D_2}##​
##\mathbf{D_3}##​
##\mathbf{D_4}##​
##\mathbf{a_i}~\downarrow##​
##\mathbf{O_1}##​
4​
6​
9​
5​
16​
##\mathbf{O_2}##​
2​
6​
4​
1​
12​
##\mathbf{O_3}##​
5​
7​
2​
9​
15​
##\mathbf{b_j}~\rightarrow##​
12​
14​
9​
8​

In MATLAB, I execute as follows:
Matlab:
>> cost = [4 6 9 5; 2 6 4 1; 5 7 2 9];
>> avb = [16 12 15];
>> req = [12 14 9 8];
>>
>> tp(cost, avb, req)

  OptimizationProblem :

    Solve for:
       x

    minimize :
       4*x(1, 1) + 2*x(2, 1) + 5*x(3, 1) + 6*x(1, 2) + 6*x(2, 2) + 7*x(3, 2) + 9*x(1, 3) + 4*x(2, 3)
     + 2*x(3, 3) + 5*x(1, 4) + x(2, 4) + 9*x(3, 4)    subject to :
       x(1, 1) + x(1, 2) + x(1, 3) + x(1, 4) == 16
       x(2, 1) + x(2, 2) + x(2, 3) + x(2, 4) == 12
       x(3, 1) + x(3, 2) + x(3, 3) + x(3, 4) == 15
       x(1, 1) + x(2, 1) + x(3, 1) == 12
       x(1, 2) + x(2, 2) + x(3, 2) == 14
       x(1, 3) + x(2, 3) + x(3, 3) == 9
       x(1, 4) + x(2, 4) + x(3, 4) == 8

    variable bounds:
       0 <= x(1, 1)
       0 <= x(2, 1)
       0 <= x(3, 1)
       0 <= x(1, 2)
       0 <= x(2, 2)
       0 <= x(3, 2)
       0 <= x(1, 3)
       0 <= x(2, 3)
       0 <= x(3, 3)
       0 <= x(1, 4)
       0 <= x(2, 4)
       0 <= x(3, 4)Optimal solution found.sol =

     8
     4
     0
     8
     0
     6
     0
     0
     9
     0
     8
     0zval =

   156exitflag =

     1output =

  struct with fields:

         iterations: 6
    constrviolation: 0
            message: 'Optimal solution found.'
          algorithm: 'dual-simplex'
      firstorderopt: 0

In the book, they have solved using north-west corner rule. The answer given is ##z = 226##.

Is my MATLAB program wrong, or am I just getting a more optimized answer than the book?

N.B.: This is a programming question and not a homework question.
 
Physics news on Phys.org
I believe my program is correct. The different methods that I mentioned above, like row minima method, NW corner rule, Vogel's Approximation, etc., when applied to the same problem, can give in different solutions. So, the solutions are not unique in most cases. MATLAB's algorithm for solving the LPP is certainly far better than any of those methods.

I have also modified the function to make it work for unbalanced problems too. Maybe I will upload the file to MATLAB File Exchange to make it available for others.
 
  • Like
Likes   Reactions: MathematicalPhysicist
Hello Wrichik Basu!
I tried this linear transportation problem in Matlab and it did not work. Please, kindly explain to me the way forward.
Regards
Jude +2348064095411
 
Your program is correct based on the answer it produced. The NW method answer you were referring to is the initial basic feasible solution while yours produced the optimal solution with its corresponding optimal value. I have tried the problem using Mathematica and it gave me the results with yours (optimal solution and value). Please, kindly clarify me on how you ran the MATLAB program to get the result. I am interested to learn it. Thanks
 

Similar threads

Replies
0
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
2
Views
3K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K