10from gams
import GamsWorkspace
11import gams.numpy
as gnp
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';
24Scalar f
'freight in dollars per case per thousand miles';
26$
if not set gdxincname $abort
'no include file name for data file provided'
31Parameter c(i,j)
'transport cost in thousands of dollars per case';
32c(i,j) = f*
d(i,j)/1000;
35 x(i,j)
'shipment quantities in cases'
36 z
'total transportation costs in thousands of dollars';
41 cost
'define objective function'
42 supply(i)
'observe supply limit at plant i'
43 demand(j)
'satisfy demand at market j';
45cost.. z =e= sum((i,j), c(i,j)*x(i,j));
47supply(i).. sum(j, x(i,j)) =l=
a(i);
49demand(j).. sum(i, x(i,j)) =g=
b(j);
53solve transport using lp minimizing z;
58if __name__ == "__main__":
59 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
60 ws = GamsWorkspace(system_directory=sys_dir)
62 plants = np.array([[
"Seattle",
""], [
"San-Diego",
""]])
63 markets = np.array([[
"New-York",
""], [
"Chicago",
""], [
"Topeka",
""]])
64 capacity = np.array([[
"Seattle", 350.0], [
"San-Diego", 600.0]], dtype=object)
66 [[
"New-York", 325.0], [
"Chicago", 300.0], [
"Topeka", 275.0]], dtype=object
70 [
"Seattle",
"New-York", 2.5],
71 [
"Seattle",
"Chicago", 1.7],
72 [
"Seattle",
"Topeka", 1.8],
73 [
"San-Diego",
"New-York", 2.5],
74 [
"San-Diego",
"Chicago", 1.8],
75 [
"San-Diego",
"Topeka", 1.4],
80 db = ws.add_database()
81 gams2np = gnp.Gams2Numpy(ws.system_directory)
83 i = db.add_set(
"i", 1,
"canning plants")
84 gams2np.gmdFillSymbolStr(db, i, plants)
86 j = db.add_set(
"j", 1,
"markets")
87 gams2np.gmdFillSymbolStr(db, j, markets)
89 a = db.add_parameter_dc(
"a", [i],
"capacity of plant i in cases")
90 gams2np.gmdFillSymbolStr(db, a, capacity)
92 b = db.add_parameter_dc(
"b", [j],
"demand at market j in cases")
93 gams2np.gmdFillSymbolStr(db, b, demand)
95 d = db.add_parameter_dc(
"d", [i, j],
"distance in thousands of miles")
96 gams2np.gmdFillSymbolStr(db, d, distance)
98 f = db.add_parameter(
"f", 0,
"freight in dollars per case per thousand miles")
99 gams2np.gmdFillSymbolStr(db, f, np.array([[90]]))
101 job = ws.add_job_from_string(GAMS_MODEL)
102 opt = ws.add_options()
104 opt.defines[
"gdxincname"] = db.name
105 opt.all_model_types =
"xpress"
107 job.run(opt, databases=db)
108 for rec
in gams2np.gmdReadSymbolStr(job.out_db,
"x"):
109 print(f
"x({rec[0]},{rec[1]}): level={rec[2]}, marginal={rec[3]}")