9from gams
import GamsWorkspace
13 i 'canning plants' / seattle, san-diego /
14 j
'markets' / new-york, chicago, topeka /;
17 a(i)
'capacity of plant i in cases'
21 b(j)
'demand at market j in cases'
26Table d(i,j)
'distance in thousands of miles'
27 new-york chicago topeka
29 san-diego 2.5 1.8 1.4;
31Scalar f
'freight in dollars per case per thousand miles' / 90 /;
40 a(i)
'capacity of plant i in cases'
41 b(j)
'demand at market j in cases'
42 d(i,j)
'distance in thousands of miles';
44$
if not set incname $abort
'no include file name for data file provided'
47Parameter c(i,j)
'transport cost in thousands of dollars per case';
48c(i,j) = f*d(i,j)/1000;
51 x(i,j)
'shipment quantities in cases'
52 z
'total transportation costs in thousands of dollars';
57 cost
'define objective function'
58 supply(i)
'observe supply limit at plant i'
59 demand(j)
'satisfy demand at market j';
61cost.. z =e= sum((i,j), c(i,j)*x(i,j));
63supply(i).. sum(j, x(i,j)) =l= a(i);
65demand(j).. sum(i, x(i,j)) =g= b(j);
69solve transport using lp minimizing z;
74if __name__ == "__main__":
75 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
76 ws = GamsWorkspace(system_directory=sys_dir)
78 with open(os.path.join(ws.working_directory,
"tdata.gms"),
"w")
as file:
81 job = ws.add_job_from_string(GAMS_MODEL)
82 opt = ws.add_options()
83 opt.defines[
"incname"] =
"tdata"
85 for rec
in job.out_db[
"x"]:
87 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"