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

Discussion Overview

The discussion revolves around solving transportation problems (TP) using the linprog function in MATLAB, specifically focusing on the conversion of TPs into linear programming problems (LPPs) and the comparison of results obtained through different methods.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes the formulation of a transportation problem and its conversion into a linear programming problem using MATLAB's linprog function.
  • Another participant asserts that different methods for solving transportation problems, such as the North-West corner rule and row-minima method, can yield different solutions, suggesting that the solutions are not unique.
  • A third participant expresses difficulty in executing the MATLAB program and requests clarification on how to proceed.
  • A later reply supports the correctness of the original program, indicating that the result obtained is an optimal solution, while the North-West method provides only an initial basic feasible solution.
  • One participant mentions modifying the function to accommodate unbalanced problems and considers sharing it on MATLAB File Exchange.

Areas of Agreement / Disagreement

Participants generally agree that the MATLAB program produces an optimal solution, while there is a disagreement regarding the interpretation of results from different methods for solving transportation problems.

Contextual Notes

The discussion highlights the potential for multiple solutions in transportation problems depending on the method used, and the implications of using different algorithms for optimization.

Who May Find This Useful

Individuals interested in optimization techniques, MATLAB programming, and transportation problem-solving methods may find this discussion relevant.

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