examples/sql/transp_odbc.mod
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
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 "iODBC"
alpar@1
    18
  'DSN=glpk;UID=glpk;PWD=gnu'
alpar@1
    19
  'SELECT PLANT, CAPA AS CAPACITY'
alpar@1
    20
  'FROM transp_capa' :
alpar@1
    21
   I <- [ PLANT ], a ~ CAPACITY;
alpar@1
    22
alpar@1
    23
set J;
alpar@1
    24
/* markets */
alpar@1
    25
alpar@1
    26
param b{j in J};
alpar@1
    27
/* demand at market j in cases */
alpar@1
    28
alpar@1
    29
table markets IN "iODBC"
alpar@1
    30
  'DSN=glpk;UID=glpk;PWD=gnu'
alpar@1
    31
  'transp_demand' :
alpar@1
    32
  J <- [ MARKET ], b ~ DEMAND;
alpar@1
    33
alpar@1
    34
param d{i in I, j in J};
alpar@1
    35
/* distance in thousands of miles */
alpar@1
    36
alpar@1
    37
table dist IN "iODBC"
alpar@1
    38
  'DSN=glpk;UID=glpk;PWD=gnu'
alpar@1
    39
  'transp_dist' :
alpar@1
    40
  [ LOC1, LOC2 ], d ~ DIST;
alpar@1
    41
alpar@1
    42
param f;
alpar@1
    43
/* freight in dollars per case per thousand miles */
alpar@1
    44
alpar@1
    45
param c{i in I, j in J} := f * d[i,j] / 1000;
alpar@1
    46
/* transport cost in thousands of dollars per case */
alpar@1
    47
alpar@1
    48
var x{i in I, j in J} >= 0;
alpar@1
    49
/* shipment quantities in cases */
alpar@1
    50
alpar@1
    51
minimize cost: sum{i in I, j in J} c[i,j] * x[i,j];
alpar@1
    52
/* total transportation costs in thousands of dollars */
alpar@1
    53
alpar@1
    54
s.t. supply{i in I}: sum{j in J} x[i,j] <= a[i];
alpar@1
    55
/* observe supply limit at plant i */
alpar@1
    56
alpar@1
    57
s.t. demand{j in J}: sum{i in I} x[i,j] >= b[j];
alpar@1
    58
/* satisfy demand at market j */
alpar@1
    59
alpar@1
    60
solve;
alpar@1
    61
alpar@1
    62
table result{i in I, j in J: x[i,j]} OUT "iODBC"
alpar@1
    63
  'DSN=glpk;UID=glpk;PWD=gnu'
alpar@1
    64
  'DELETE FROM transp_result;'
alpar@1
    65
  'INSERT INTO transp_result VALUES (?,?,?)' :
alpar@1
    66
  i ~ LOC1, j ~ LOC2, x[i,j] ~ QUANTITY;
alpar@1
    67
alpar@1
    68
data;
alpar@1
    69
alpar@1
    70
param f := 90;
alpar@1
    71
alpar@1
    72
end;