g2np_example1.py
Go to the documentation of this file.
8 
9 from gams import *
10 import numpy as np
11 import gams2numpy
12 import sys
13 
15  return '''
16  Sets
17  i canning plants
18  j markets
19 
20  Parameters
21  a(i) capacity of plant i in cases
22  b(j) demand at market j in cases
23  d(i,j) distance in thousands of miles
24  Scalar 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 
31  Parameter c(i,j) transport cost in thousands of dollars per case ;
32 
33  c(i,j) = f * d(i,j) / 1000 ;
34 
35  Variables
36  x(i,j) shipment quantities in cases
37  z total transportation costs in thousands of dollars ;
38 
39  Positive Variable x ;
40 
41  Equations
42  cost define objective function
43  supply(i) observe supply limit at plant i
44  demand(j) satisfy demand at market j ;
45 
46  cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
47 
48  supply(i) .. sum(j, x(i,j)) =l= a(i) ;
49 
50  demand(j) .. sum(i, x(i,j)) =g= b(j) ;
51 
52  Model transport /all/ ;
53 
54  Solve transport using lp minimizing z ;
55 
56  Display x.l, x.m ; '''
57 
58 
59 if __name__ == "__main__":
60  if len(sys.argv) > 1:
61  ws = GamsWorkspace(system_directory = sys.argv[1])
62  else:
63  ws = GamsWorkspace()
64 
65  plants = np.array([["Seattle", ""], ["San-Diego", ""]])
66  markets = np.array([["New-York", ""], ["Chicago", ""], ["Topeka", ""]])
67  capacity = np.array([["Seattle", 350.0], ["San-Diego", 600.0]], dtype=object)
68  demand = np.array([["New-York", 325.0], ["Chicago", 300.0], ["Topeka", 275.0]], dtype=object)
69  distance = np.array([["Seattle", "New-York", 2.5],
70  ["Seattle", "Chicago", 1.7],
71  ["Seattle", "Topeka", 1.8],
72  ["San-Diego", "New-York", 2.5],
73  ["San-Diego", "Chicago", 1.8],
74  ["San-Diego", "Topeka", 1.4]], dtype=object)
75 
76  db = ws.add_database()
77  g2np = gams2numpy.Gams2Numpy(ws.system_directory)
78 
79  i = db.add_set("i", 1, "canning plants")
80  g2np.gmdFillSymbolStr(db, i, plants)
81 
82  j = db.add_set("j", 1, "markets")
83  g2np.gmdFillSymbolStr(db, j, markets)
84 
85  a = db.add_parameter_dc("a", [i], "capacity of plant i in cases")
86  g2np.gmdFillSymbolStr(db, a, capacity)
87 
88  b = db.add_parameter_dc("b", [j], "demand at market j in cases")
89  g2np.gmdFillSymbolStr(db, b, demand)
90 
91  d = db.add_parameter_dc("d", [i,j], "distance in thousands of miles")
92  g2np.gmdFillSymbolStr(db, d, distance)
93 
94  f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
95  g2np.gmdFillSymbolStr(db, f, np.array([[90]]))
96 
97  job = ws.add_job_from_string(get_model_text())
98  opt = ws.add_options()
99 
100  opt.defines["gdxincname"] = db.name
101  opt.all_model_types = "xpress"
102 
103  job.run(opt, databases = db)
104  for rec in g2np.gmdReadSymbolStr(job.out_db, "x"):
105  print("x({},{}): level={}, marginal={}".format(*rec[0:-3]))
106 
def get_model_text()