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