g2np_example2.py
Go to the documentation of this file.
8 
9 from gams import *
10 from gdxcc import *
11 import numpy as np
12 import gams2numpy
13 import sys
14 import os
15 
17  return '''
18  Sets
19  i canning plants
20  j markets
21 
22  Parameters
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;
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 
33  Parameter c(i,j) transport cost in thousands of dollars per case ;
34 
35  c(i,j) = f * d(i,j) / 1000 ;
36 
37  Variables
38  x(i,j) shipment quantities in cases
39  z total transportation costs in thousands of dollars ;
40 
41  Positive Variable x ;
42 
43  Equations
44  cost define objective function
45  supply(i) observe supply limit at plant i
46  demand(j) satisfy demand at market j ;
47 
48  cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
49 
50  supply(i) .. sum(j, x(i,j)) =l= a(i) ;
51 
52  demand(j) .. sum(i, x(i,j)) =g= b(j) ;
53 
54  Model transport /all/ ;
55 
56  Solve transport using lp minimizing z ;
57 
58  Display x.l, x.m ; '''
59 
60 
61 if __name__ == "__main__":
62  if len(sys.argv) > 1:
63  ws = GamsWorkspace(system_directory = sys.argv[1])
64  else:
65  ws = GamsWorkspace()
66 
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)
77 
78  gdx = new_gdxHandle_tp()
79  rc, msg = gdxCreateD(gdx, ws.system_directory, GMS_SSSIZE)
80  if not rc:
81  raise Exception(msg)
82  if not gdxOpenWrite(gdx, os.path.join(ws.working_directory, 'data.gdx'), "")[0]:
83  raise Exception("Error opening GDX file "+gdx)
84 
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]]))
92 
93  gdxClose(gdx)
94  gdxFree(gdx)
95 
96  opt = ws.add_options()
97  opt.defines["gdxincname"] = "data"
98  opt.all_model_types = "xpress"
99 
100  job = ws.add_job_from_string(get_model_text())
101  job.run(opt)
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]))
def get_model_text()