Loading...
Searching...
No Matches
g2np_example2.py
Go to the documentation of this file.
8
9import os
10import sys
11from gams import GamsWorkspace
12import gams.core.gdx as gdx
13import gams.numpy as gnp
14import numpy as np
15
16GAMS_MODEL = """
17Set
18 i 'canning plants'
19 j 'markets';
20
21Parameter
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';
25
26Scalar f 'freight in dollars per case per thousand miles';
27
28$if not set gdxincname $abort 'no include file name for data file provided'
29$gdxIn %gdxincname%
30$load i j a b d f
31$gdxIn
32
33Parameter c(i,j) 'transport cost in thousands of dollars per case';
34c(i,j) = f*d(i,j)/1000;
35
36Variable
37 x(i,j) 'shipment quantities in cases'
38 z 'total transportation costs in thousands of dollars';
39
40Positive Variable x;
41
42Equations
43 cost 'define objective function'
44 supply(i) 'observe supply limit at plant i'
45 demand(j) 'satisfy demand at market j';
46
47cost.. z =e= sum((i,j), c(i,j)*x(i,j));
48
49supply(i).. sum(j, x(i,j)) =l= a(i);
50
51demand(j).. sum(i, x(i,j)) =g= b(j);
52
53Model transport /all/;
54
55solve transport using lp minimizing z;
56
57display x.l, x.m;
58"""
59
60if __name__ == "__main__":
61 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
62 ws = GamsWorkspace(system_directory=sys_dir)
63
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)
67 demand = np.array(
68 [["New-York", 325.0], ["Chicago", 300.0], ["Topeka", 275.0]], dtype=object
69 )
70 distance = np.array(
71 [
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],
78 ],
79 dtype=object,
80 )
81
82 gdx_h = gdx.new_gdxHandle_tp()
83 rc, msg = gdx.gdxCreateD(gdx_h, ws.system_directory, gdx.GMS_SSSIZE)
84 if not rc:
85 raise Exception(msg)
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}")
89
90 gams2np = gnp.Gams2Numpy(ws.system_directory)
91 gams2np.gdxWriteSymbolStr(
92 gdx_h, "i", "canning plants", 1, gdx.GMS_DT_SET, 0, plants
93 )
94 gams2np.gdxWriteSymbolStr(gdx_h, "j", "markets", 1, gdx.GMS_DT_SET, 0, markets)
95 gams2np.gdxWriteSymbolStr(
96 gdx_h,
97 "a",
98 "capacity of plant i in cases",
99 1,
100 gdx.GMS_DT_PAR,
101 0,
102 capacity,
103 ["i"],
104 )
105 gams2np.gdxWriteSymbolStr(
106 gdx_h, "b", "demand at market j in cases", 1, gdx.GMS_DT_PAR, 0, demand, ["j"]
107 )
108 gams2np.gdxWriteSymbolStr(
109 gdx_h,
110 "d",
111 "distance in thousands of miles",
112 2,
113 gdx.GMS_DT_PAR,
114 0,
115 distance,
116 ["i", "j"],
117 )
118 gams2np.gdxWriteSymbolStr(
119 gdx_h,
120 "f",
121 "freight in dollars per case per thousand miles",
122 0,
123 gdx.GMS_DT_PAR,
124 0,
125 np.array([[90]]),
126 )
127
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")
132
133 opt = ws.add_options()
134 opt.defines["gdxincname"] = "data"
135 opt.all_model_types = "xpress"
136
137 job = ws.add_job_from_string(GAMS_MODEL)
138 job.run(opt)
139 job.out_db.export("results.gdx")
140 for rec in gams2np.gdxReadSymbolStr(
141 os.path.join(ws.working_directory, "results.gdx"), "x"
142 ):
143 print(f"x({rec[0]},{rec[1]}): level={rec[2]}, marginal={rec[3]}")