transport10.py
Go to the documentation of this file.
6
7 from xlrd import open_workbook
8 from gams import *
9 import sys
10
12  return '''
13  Sets
14  i canning plants
15  j markets
16
17  Parameters
18  a(i) capacity of plant i in cases
19  b(j) demand at market j in cases
20  d(i,j) distance in thousands of miles
21  Scalar f freight in dollars per case per thousand miles /90/;
22
23 \$if not set gdxincname \$abort 'no include file name for data file provided'
24 \$gdxin %gdxincname%
25 \$load i j a b d
26 \$gdxin
27
28  Parameter c(i,j) transport cost in thousands of dollars per case ;
29
30  c(i,j) = f * d(i,j) / 1000 ;
31
32  Variables
33  x(i,j) shipment quantities in cases
34  z total transportation costs in thousands of dollars ;
35
36  Positive Variable x ;
37
38  Equations
39  cost define objective function
40  supply(i) observe supply limit at plant i
41  demand(j) satisfy demand at market j ;
42
43  cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
44
45  supply(i) .. sum(j, x(i,j)) =l= a(i) ;
46
47  demand(j) .. sum(i, x(i,j)) =g= b(j) ;
48
49  Model transport /all/ ;
50
51  Solve transport using lp minimizing z ;
52
53  Display x.l, x.m ; '''
54
55
56 if __name__ == "__main__":
57  wb = open_workbook("..\\Data\\transport.xlsx")
58
59  capacity = wb.sheet_by_name("capacity")
60  demand = wb.sheet_by_name("demand")
61  distance = wb.sheet_by_name("distance")
62
63  # number of markets/plants have to be the same in all spreadsheets
64  assert (distance.ncols-1 == demand.ncols) and (distance.nrows-1 == capacity.ncols), \
65  "Size of the spreadsheets doesn't match"
66
67  if len(sys.argv) > 1:
68  ws = GamsWorkspace(system_directory = sys.argv[1])
69  else:
70  ws = GamsWorkspace()
71
73  i = db.add_set("i", 1, "Plants")
74  j = db.add_set("j", 1, "Markets")
75  capacity_param = db.add_parameter_dc("a", [i], "Capacity")
76  demand_param = db.add_parameter_dc("b", [j], "Demand")
77  distance_param = db.add_parameter_dc("d", [i,j], "Distance")
78
79  for cx in range(capacity.ncols):
81  capacity_param.add_record(str(capacity.cell_value(0, cx))).value = capacity.cell_value(1, cx)
82
83  for cx in range(demand.ncols):
85  demand_param.add_record(str(demand.cell_value(0, cx))).value = demand.cell_value(1, cx)
86
87  for cx in range(1, distance.ncols):
88  for rx in range(1, distance.nrows):
89  keys = ( str(distance.cell_value(rx, 0)), str(distance.cell_value(0, cx)) )
91
92  # Create and run the GAMSJob