- #1
- 2,158
- 2,719
I want to solve transportation problems (TP) in MATLAB. For solving LPPs, MATLAB provides the
According to my book, the following is a TP:
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
Let's consider the following worked out problem from my book:
In MATLAB, I execute as follows:
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.
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##.
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
| ##\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.