Solving transportation problems (LPP) using linprog in MATLAB

  • MATLAB
  • Thread starter Wrichik Basu
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses solving transportation problems using MATLAB's linprog function in the Optimization Toolbox. The conversation also mentions converting the transportation problem to a linear programming problem and provides a MATLAB function for solving it. The conversation also mentions different methods for solving transportation problems and the potential for different solutions using these methods. The conversation ends with a discussion about the correctness of the MATLAB program and the possibility of uploading it to MATLAB File Exchange.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,139
2,715
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
  • #2
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 MathematicalPhysicist
  • #3
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
 
  • #4
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
 

FAQ: Solving transportation problems (LPP) using linprog in MATLAB

What is linprog and how does it relate to solving transportation problems?

Linprog stands for linear programming and is a mathematical technique used to optimize a linear objective function subject to linear constraints. In transportation problems, linprog can be used to find the most efficient transportation plan that minimizes costs while meeting demand and supply requirements.

What are the steps involved in solving transportation problems using linprog in MATLAB?

The steps involved are:

  • Formulating the problem as a linear program
  • Defining the objective function and constraints
  • Setting up the problem in MATLAB using the linprog function
  • Running the linprog function to solve the problem and obtain the optimal solution
  • Interpreting and analyzing the results

What are the advantages of using linprog in MATLAB for solving transportation problems?

Some advantages include:

  • Efficiency and speed in solving complex transportation problems
  • Ability to handle large datasets and multiple constraints
  • Flexibility in modifying the objective function and constraints
  • Accurate and reliable results
  • Integration with other MATLAB functions and tools for further analysis and visualization

What are some common challenges when using linprog in MATLAB for solving transportation problems?

Some challenges include:

  • Formulating the problem as a linear program can be complex and time-consuming
  • Choosing appropriate objective function and constraints can be difficult
  • The solution may not always be intuitive or easy to interpret
  • Optimal solutions may not always be realistic in real-world scenarios
  • There may be limitations in the number of variables and constraints that can be handled by linprog

Can linprog in MATLAB be used for other types of optimization problems besides transportation problems?

Yes, linprog can be used for a variety of optimization problems such as production planning, resource allocation, and portfolio optimization. However, it is most commonly used for problems with linear objective functions and constraints.

Similar threads

Back
Top