23 a(i) capacity of plant i in cases
24 b(j) demand at market j in cases
25 d(i,j) distance in thousands of miles
26 Scalar f freight in dollars per case per thousand miles;
28 $if not set gdxincname $abort 'no include file name for data file provided'
33 Parameter c(i,j) transport cost in thousands of dollars per case ;
35 c(i,j) = f * d(i,j) / 1000 ;
38 x(i,j) shipment quantities in cases
39 z total transportation costs in thousands of dollars ;
44 cost define objective function
45 supply(i) observe supply limit at plant i
46 demand(j) satisfy demand at market j ;
48 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
50 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
52 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
54 Model transport /all/ ;
56 Solve transport using lp minimizing z ;
58 Display x.l, x.m ; '''
61 if __name__ ==
"__main__":
63 ws = GamsWorkspace(system_directory = sys.argv[1])
67 plants = np.array([[
"Seattle",
""], [
"San-Diego",
""]])
68 markets = np.array([[
"New-York",
""], [
"Chicago",
""], [
"Topeka",
""]])
69 capacity = np.array([[
"Seattle", 350.0], [
"San-Diego", 600.0]], dtype=object)
70 demand = np.array([[
"New-York", 325.0], [
"Chicago", 300.0], [
"Topeka", 275.0]], dtype=object)
71 distance = np.array([[
"Seattle",
"New-York", 2.5],
72 [
"Seattle",
"Chicago", 1.7],
73 [
"Seattle",
"Topeka", 1.8],
74 [
"San-Diego",
"New-York", 2.5],
75 [
"San-Diego",
"Chicago", 1.8],
76 [
"San-Diego",
"Topeka", 1.4]], dtype=object)
78 gdx = new_gdxHandle_tp()
79 rc, msg = gdxCreateD(gdx, ws.system_directory, GMS_SSSIZE)
82 if not gdxOpenWrite(gdx, os.path.join(ws.working_directory,
'data.gdx'),
"")[0]:
83 raise Exception(
"Error opening GDX file "+gdx)
85 g2np = gams2numpy.Gams2Numpy(ws.system_directory)
86 g2np.gdxWriteSymbolStr(gdx,
"i",
"canning plants", 1, GMS_DT_SET, 0, plants)
87 g2np.gdxWriteSymbolStr(gdx,
"j",
"markets", 1, GMS_DT_SET, 0, markets)
88 g2np.gdxWriteSymbolStr(gdx,
"a",
"capacity of plant i in cases", 1, GMS_DT_PAR, 0, capacity, [
'i'])
89 g2np.gdxWriteSymbolStr(gdx,
"b",
"demand at market j in cases", 1, GMS_DT_PAR, 0, demand, [
'j'])
90 g2np.gdxWriteSymbolStr(gdx,
"d",
"distance in thousands of miles", 2, GMS_DT_PAR, 0, distance, [
'i',
'j'])
91 g2np.gdxWriteSymbolStr(gdx,
"f",
"freight in dollars per case per thousand miles", 0, GMS_DT_PAR, 0, np.array([[90]]))
96 opt = ws.add_options()
97 opt.defines[
"gdxincname"] =
"data"
98 opt.all_model_types =
"xpress"
102 job.out_db.export(
'results.gdx')
103 for rec
in g2np.gdxReadSymbolStr(os.path.join(ws.working_directory,
'results.gdx'),
"x"):
104 print(
"x({},{}): level={}, marginal={}".format(*rec[0:-3]))