11from gams
import GamsWorkspace
12import gams.core.gdx
as gdx
13import gams.numpy
as gnp
22 a(i)
'capacity of plant i in cases'
23 b(j)
'demand at market j in cases'
24 d(i,j)
'distance in thousands of miles';
26Scalar f
'freight in dollars per case per thousand miles';
28$
if not set gdxincname $abort
'no include file name for data file provided'
33Parameter c(i,j)
'transport cost in thousands of dollars per case';
34c(i,j) = f*d(i,j)/1000;
37 x(i,j)
'shipment quantities in cases'
38 z
'total transportation costs in thousands of dollars';
43 cost
'define objective function'
44 supply(i)
'observe supply limit at plant i'
45 demand(j)
'satisfy demand at market j';
47cost.. z =e= sum((i,j), c(i,j)*x(i,j));
49supply(i).. sum(j, x(i,j)) =l= a(i);
51demand(j).. sum(i, x(i,j)) =g= b(j);
55solve transport using lp minimizing z;
60if __name__ == "__main__":
61 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
62 ws = GamsWorkspace(system_directory=sys_dir)
64 plants = np.array([[
"Seattle",
""], [
"San-Diego",
""]])
65 markets = np.array([[
"New-York",
""], [
"Chicago",
""], [
"Topeka",
""]])
66 capacity = np.array([[
"Seattle", 350.0], [
"San-Diego", 600.0]], dtype=object)
68 [[
"New-York", 325.0], [
"Chicago", 300.0], [
"Topeka", 275.0]], dtype=object
72 [
"Seattle",
"New-York", 2.5],
73 [
"Seattle",
"Chicago", 1.7],
74 [
"Seattle",
"Topeka", 1.8],
75 [
"San-Diego",
"New-York", 2.5],
76 [
"San-Diego",
"Chicago", 1.8],
77 [
"San-Diego",
"Topeka", 1.4],
82 gdx_h = gdx.new_gdxHandle_tp()
83 rc, msg = gdx.gdxCreateD(gdx_h, ws.system_directory, gdx.GMS_SSSIZE)
86 gdx_file = os.path.join(ws.working_directory,
"data.gdx")
87 if not gdx.gdxOpenWrite(gdx_h, gdx_file,
"")[0]:
88 raise Exception(f
"Error opening GDX file {gdx_file}")
90 gams2np = gnp.Gams2Numpy(ws.system_directory)
91 gams2np.gdxWriteSymbolStr(
92 gdx_h,
"i",
"canning plants", 1, gdx.GMS_DT_SET, 0, plants
94 gams2np.gdxWriteSymbolStr(gdx_h,
"j",
"markets", 1, gdx.GMS_DT_SET, 0, markets)
95 gams2np.gdxWriteSymbolStr(
98 "capacity of plant i in cases",
105 gams2np.gdxWriteSymbolStr(
106 gdx_h,
"b",
"demand at market j in cases", 1, gdx.GMS_DT_PAR, 0, demand, [
"j"]
108 gams2np.gdxWriteSymbolStr(
111 "distance in thousands of miles",
118 gams2np.gdxWriteSymbolStr(
121 "freight in dollars per case per thousand miles",
128 if gdx.gdxClose(gdx_h):
129 raise Exception(
"Error in gdxClose")
130 if not gdx.gdxFree(gdx_h):
131 raise Exception(
"Error in gdxFree")
133 opt = ws.add_options()
134 opt.defines[
"gdxincname"] =
"data"
135 opt.all_model_types =
"xpress"
137 job = ws.add_job_from_string(GAMS_MODEL)
139 job.out_db.export(
"results.gdx")
140 for rec
in gams2np.gdxReadSymbolStr(
141 os.path.join(ws.working_directory,
"results.gdx"),
"x"
143 print(f
"x({rec[0]},{rec[1]}): level={rec[2]}, marginal={rec[3]}")