alpar@1: # A TRANSPORTATION PROBLEM alpar@1: # alpar@1: # This problem finds a least cost shipping schedule that meets alpar@1: # requirements at markets and supplies at factories. alpar@1: # alpar@1: # References: alpar@1: # Dantzig G B, "Linear Programming and Extensions." alpar@1: # Princeton University Press, Princeton, New Jersey, 1963, alpar@1: # Chapter 3-3. alpar@1: alpar@1: set I; alpar@1: /* canning plants */ alpar@1: alpar@1: param a{i in I}; alpar@1: /* capacity of plant i in cases */ alpar@1: alpar@1: table plants IN "MySQL" alpar@1: 'Database=glpk;UID=glpk;PWD=gnu' alpar@1: 'SELECT PLANT, CAPA AS CAPACITY FROM transp_capa' : alpar@1: I <- [ PLANT ], a ~ CAPACITY; alpar@1: alpar@1: set J; alpar@1: /* markets */ alpar@1: alpar@1: param b{j in J}; alpar@1: /* demand at market j in cases */ alpar@1: alpar@1: table markets IN "MySQL" alpar@1: 'Database=glpk;UID=glpk;PWD=gnu' alpar@1: 'transp_demand' : alpar@1: J <- [ MARKET ], b ~ DEMAND; alpar@1: alpar@1: param d{i in I, j in J}; alpar@1: /* distance in thousands of miles */ alpar@1: alpar@1: table dist IN "MySQL" alpar@1: 'Database=glpk;UID=glpk;PWD=gnu' alpar@1: 'transp_dist' : alpar@1: [ LOC1, LOC2 ], d ~ DIST; alpar@1: alpar@1: param f; alpar@1: /* freight in dollars per case per thousand miles */ alpar@1: alpar@1: param c{i in I, j in J} := f * d[i,j] / 1000; alpar@1: /* transport cost in thousands of dollars per case */ alpar@1: alpar@1: var x{i in I, j in J} >= 0; alpar@1: /* shipment quantities in cases */ alpar@1: alpar@1: minimize cost: sum{i in I, j in J} c[i,j] * x[i,j]; alpar@1: /* total transportation costs in thousands of dollars */ alpar@1: alpar@1: s.t. supply{i in I}: sum{j in J} x[i,j] <= a[i]; alpar@1: /* observe supply limit at plant i */ alpar@1: alpar@1: s.t. demand{j in J}: sum{i in I} x[i,j] >= b[j]; alpar@1: /* satisfy demand at market j */ alpar@1: alpar@1: solve; alpar@1: alpar@1: table result{i in I, j in J: x[i,j]} OUT "MySQL" alpar@1: 'Database=glpk;UID=glpk;PWD=gnu' alpar@1: 'DELETE FROM transp_result;' alpar@1: 'INSERT INTO transp_result VALUES (?,?,?)' : alpar@1: i ~ LOC1, j ~ LOC2, x[i,j] ~ QUANTITY; alpar@1: alpar@1: data; alpar@1: alpar@1: param f := 90; alpar@1: alpar@1: end;