Searching...
No Matches
g2np_example1.py
Go to the documentation of this file.
8
9import sys
10from gams import GamsWorkspace
11import gams.numpy as gnp
12import numpy as np
13
14GAMS_MODEL = """
15Set
16 i 'canning plants'
17 j 'markets';
18
19Parameter
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';
23
24Scalar f 'freight in dollars per case per thousand miles';
25
26\$if not set gdxincname \$abort 'no include file name for data file provided'
27\$gdxIn %gdxincname%
28\$load i j a b d f
29\$gdxIn
30
31Parameter c(i,j) 'transport cost in thousands of dollars per case';
32c(i,j) = f*d(i,j)/1000;
33
34Variable
35 x(i,j) 'shipment quantities in cases'
36 z 'total transportation costs in thousands of dollars';
37
38Positive Variable x;
39
40Equations
41 cost 'define objective function'
42 supply(i) 'observe supply limit at plant i'
43 demand(j) 'satisfy demand at market j';
44
45cost.. z =e= sum((i,j), c(i,j)*x(i,j));
46
47supply(i).. sum(j, x(i,j)) =l= a(i);
48
49demand(j).. sum(i, x(i,j)) =g= b(j);
50
51Model transport /all/;
52
53solve transport using lp minimizing z;
54
55display x.l, x.m;
56"""
57
58if __name__ == "__main__":
59 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
60 ws = GamsWorkspace(system_directory=sys_dir)
61
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)
65 demand = np.array(
66 [["New-York", 325.0], ["Chicago", 300.0], ["Topeka", 275.0]], dtype=object
67 )
68 distance = np.array(
69 [
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],
76 ],
77 dtype=object,
78 )
79
81 gams2np = gnp.Gams2Numpy(ws.system_directory)
82
83 i = db.add_set("i", 1, "canning plants")
84 gams2np.gmdFillSymbolStr(db, i, plants)
85
86 j = db.add_set("j", 1, "markets")
87 gams2np.gmdFillSymbolStr(db, j, markets)
88
89 a = db.add_parameter_dc("a", [i], "capacity of plant i in cases")
90 gams2np.gmdFillSymbolStr(db, a, capacity)
91
92 b = db.add_parameter_dc("b", [j], "demand at market j in cases")
93 gams2np.gmdFillSymbolStr(db, b, demand)
94
95 d = db.add_parameter_dc("d", [i, j], "distance in thousands of miles")
96 gams2np.gmdFillSymbolStr(db, d, distance)
97
98 f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
99 gams2np.gmdFillSymbolStr(db, f, np.array([[90]]))
100
103
104 opt.defines["gdxincname"] = db.name
105 opt.all_model_types = "xpress"
106
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]}")
GamsWorkspace d
GamsWorkspace b
GamsWorkspace a
GAMS Development Corp.
GAMS Software GmbH

General Information and Sales
U.S. (+1) 202 342-0180
Europe: (+49) 221 949-9170