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