11from gams
import GamsWorkspace
16 i 'canning plants' / seattle, san-diego /
17 j
'markets' / new-york, chicago, topeka /;
20 a(i)
'capacity of plant i in cases'
24 b(j)
'demand at market j in cases'
29Table d(i,j)
'distance in thousands of miles'
30 new-york chicago topeka
32 san-diego 2.5 1.8 1.4;
34Scalar f
'freight in dollars per case per thousand miles' / 90 /;
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';
47Scalar f
'freight in dollars per case per thousand miles';
49$
if not set gdxincname $abort
'no include file name for data file provided'
54Parameter c(i,j)
'transport cost in thousands of dollars per case';
55c(i,j) = f*d(i,j)/1000;
58 x(i,j)
'shipment quantities in cases'
59 z
'total transportation costs in thousands of dollars';
64 cost
'define objective function'
65 supply(i)
'observe supply limit at plant i'
66 demand(j)
'satisfy demand at market j';
68cost.. z =e= sum((i,j), c(i,j)*x(i,j));
70supply(i).. sum(j, x(i,j)) =l= a(i);
72demand(j).. sum(i, x(i,j)) =g= b(j);
76solve transport using lp minimizing z;
81if __name__ == "__main__":
82 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
83 ws = GamsWorkspace(system_directory=sys_dir)
85 job = ws.add_job_from_string(GAMS_DATA)
87 job.out_db.export(os.path.join(ws.working_directory,
"tdata.gdx"))
88 job = ws.add_job_from_string(GAMS_MODEL)
90 opt = ws.add_options()
91 opt.defines[
"gdxincname"] =
"tdata"
92 opt.all_model_types =
"xpress"
94 for rec
in job.out_db[
"x"]:
96 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
99 job_data = ws.add_job_from_string(GAMS_DATA)
100 job_model = ws.add_job_from_string(GAMS_MODEL)
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"]:
106 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"