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