transport3.py
Go to the documentation of this file.
9from __future__ import print_function
10from gams import *
11import os
12import sys
13
15 return '''
16 Sets
17 i canning plants / seattle, san-diego /
18 j markets / new-york, chicago, topeka / ;
19
20 Parameters
21
22 a(i) capacity of plant i in cases
23 / seattle 350
24 san-diego 600 /
25
26 b(j) demand at market j in cases
27 / new-york 325
28 chicago 300
29 topeka 275 / ;
30
31 Table d(i,j) distance in thousands of miles
32 new-york chicago topeka
33 seattle 2.5 1.7 1.8
34 san-diego 2.5 1.8 1.4 ;
35
36 Scalar f freight in dollars per case per thousand miles /90/ ; '''
37
39 return '''
40 Sets
41 i canning plants
42 j markets
43
44 Parameters
45 a(i) capacity of plant i in cases
46 b(j) demand at market j in cases
47 d(i,j) distance in thousands of miles
48 Scalar f freight in dollars per case per thousand miles;
49
50$if not set gdxincname $abort 'no include file name for data file provided'
51$gdxin %gdxincname%
52$load i j a b d f
53$gdxin
54
55 Parameter c(i,j) transport cost in thousands of dollars per case ;
56
57 c(i,j) = f * d(i,j) / 1000 ;
58
59 Variables
60 x(i,j) shipment quantities in cases
61 z total transportation costs in thousands of dollars ;
62
63 Positive Variable x ;
64
65 Equations
66 cost define objective function
67 supply(i) observe supply limit at plant i
68 demand(j) satisfy demand at market j ;
69
70 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
71
72 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
73
74 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
75
76 Model transport /all/ ;
77
78 Solve transport using lp minimizing z ;
79
80 Display x.l, x.m ; '''
81
82
83if __name__ == "__main__":
84 if len(sys.argv) > 1:
85 ws = GamsWorkspace(system_directory = sys.argv[1])
86 else:
87 ws = GamsWorkspace()
88
89 t3 = ws.add_job_from_string(get_data_text())
90 t3.run()
91 t3.out_db.export(os.path.join(ws.working_directory, "tdata.gdx"))
92 t3 = ws.add_job_from_string(get_model_text())
93
94 opt = ws.add_options()
95 opt.defines["gdxincname"] = "tdata"
96 opt.all_model_types = "xpress"
97 t3.run(opt)
98 for rec in t3.out_db["x"]:
99 print("x(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))
100
101 t3a = ws.add_job_from_string(get_data_text())
102 t3b = ws.add_job_from_string(get_model_text())
103 t3a.run()
104 opt.defines["gdxincname"] = t3a.out_db.name
105 t3b.run(opt, databases=t3a.out_db)
106 for rec in t3b.out_db["x"]:
107 print("x(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))
108
def get_data_text()
Definition: transport3.py:14
def get_model_text()
Definition: transport3.py:38