alpar@1
|
1 |
# A TRANSPORTATION PROBLEM
|
alpar@1
|
2 |
#
|
alpar@1
|
3 |
# This problem finds a least cost shipping schedule that meets
|
alpar@1
|
4 |
# requirements at markets and supplies at factories.
|
alpar@1
|
5 |
#
|
alpar@1
|
6 |
# References:
|
alpar@1
|
7 |
# Dantzig G B, "Linear Programming and Extensions."
|
alpar@1
|
8 |
# Princeton University Press, Princeton, New Jersey, 1963,
|
alpar@1
|
9 |
# Chapter 3-3.
|
alpar@1
|
10 |
|
alpar@1
|
11 |
set I;
|
alpar@1
|
12 |
/* canning plants */
|
alpar@1
|
13 |
|
alpar@1
|
14 |
param a{i in I};
|
alpar@1
|
15 |
/* capacity of plant i in cases */
|
alpar@1
|
16 |
|
alpar@1
|
17 |
table plants IN "MySQL"
|
alpar@1
|
18 |
'Database=glpk;UID=glpk;PWD=gnu'
|
alpar@1
|
19 |
'SELECT PLANT, CAPA AS CAPACITY FROM transp_capa' :
|
alpar@1
|
20 |
I <- [ PLANT ], a ~ CAPACITY;
|
alpar@1
|
21 |
|
alpar@1
|
22 |
set J;
|
alpar@1
|
23 |
/* markets */
|
alpar@1
|
24 |
|
alpar@1
|
25 |
param b{j in J};
|
alpar@1
|
26 |
/* demand at market j in cases */
|
alpar@1
|
27 |
|
alpar@1
|
28 |
table markets IN "MySQL"
|
alpar@1
|
29 |
'Database=glpk;UID=glpk;PWD=gnu'
|
alpar@1
|
30 |
'transp_demand' :
|
alpar@1
|
31 |
J <- [ MARKET ], b ~ DEMAND;
|
alpar@1
|
32 |
|
alpar@1
|
33 |
param d{i in I, j in J};
|
alpar@1
|
34 |
/* distance in thousands of miles */
|
alpar@1
|
35 |
|
alpar@1
|
36 |
table dist IN "MySQL"
|
alpar@1
|
37 |
'Database=glpk;UID=glpk;PWD=gnu'
|
alpar@1
|
38 |
'transp_dist' :
|
alpar@1
|
39 |
[ LOC1, LOC2 ], d ~ DIST;
|
alpar@1
|
40 |
|
alpar@1
|
41 |
param f;
|
alpar@1
|
42 |
/* freight in dollars per case per thousand miles */
|
alpar@1
|
43 |
|
alpar@1
|
44 |
param c{i in I, j in J} := f * d[i,j] / 1000;
|
alpar@1
|
45 |
/* transport cost in thousands of dollars per case */
|
alpar@1
|
46 |
|
alpar@1
|
47 |
var x{i in I, j in J} >= 0;
|
alpar@1
|
48 |
/* shipment quantities in cases */
|
alpar@1
|
49 |
|
alpar@1
|
50 |
minimize cost: sum{i in I, j in J} c[i,j] * x[i,j];
|
alpar@1
|
51 |
/* total transportation costs in thousands of dollars */
|
alpar@1
|
52 |
|
alpar@1
|
53 |
s.t. supply{i in I}: sum{j in J} x[i,j] <= a[i];
|
alpar@1
|
54 |
/* observe supply limit at plant i */
|
alpar@1
|
55 |
|
alpar@1
|
56 |
s.t. demand{j in J}: sum{i in I} x[i,j] >= b[j];
|
alpar@1
|
57 |
/* satisfy demand at market j */
|
alpar@1
|
58 |
|
alpar@1
|
59 |
solve;
|
alpar@1
|
60 |
|
alpar@1
|
61 |
table result{i in I, j in J: x[i,j]} OUT "MySQL"
|
alpar@1
|
62 |
'Database=glpk;UID=glpk;PWD=gnu'
|
alpar@1
|
63 |
'DELETE FROM transp_result;'
|
alpar@1
|
64 |
'INSERT INTO transp_result VALUES (?,?,?)' :
|
alpar@1
|
65 |
i ~ LOC1, j ~ LOC2, x[i,j] ~ QUANTITY;
|
alpar@1
|
66 |
|
alpar@1
|
67 |
data;
|
alpar@1
|
68 |
|
alpar@1
|
69 |
param f := 90;
|
alpar@1
|
70 |
|
alpar@1
|
71 |
end;
|