Loading...
Searching...
No Matches
core_example2.py
Go to the documentation of this file.
1import argparse
2import os
3import subprocess
4import sys
5from gams.core import gdx, opt
6
7
8def terminate(gdx_h, opt_h, rc):
9 opt.optFree(opt_h)
10 gdx.gdxFree(gdx_h)
11 sys.exit(rc)
12
13
14def report_gdx_error(gdx_h, s):
15 error_msg = gdx.gdxErrorStr(gdx_h, gdx.gdxGetLastError(gdx_h))[1]
16 print(f"Error {s}: {error_msg}")
17
18
19def write_model_data(gdx_h, gdx_file):
20 rc, error_nr = gdx.gdxOpenWrite(gdx_h, gdx_file, "xp_example1")
21 if not rc:
22 print(f"Error gdxOpenWrite: {gdx.gdxErrorStr(gdx_h, error_nr)[1]}")
23 return False
24
25 if not gdx.gdxDataWriteStrStart(
26 gdx_h, "Demand", "Demand data", 1, gdx.GMS_DT_PAR, 0
27 ):
28 report_gdx_error(gdx_h, "gdxDataWriteStrStart")
29 return False
30
31 values = gdx.doubleArray(gdx.GMS_VAL_MAX)
32 values[gdx.GMS_VAL_LEVEL] = 324.0
33 gdx.gdxDataWriteStr(gdx_h, ["New-York"], values)
34 values[gdx.GMS_VAL_LEVEL] = 299.0
35 gdx.gdxDataWriteStr(gdx_h, ["Chicago"], values)
36 values[gdx.GMS_VAL_LEVEL] = 274.0
37 gdx.gdxDataWriteStr(gdx_h, ["Topeka"], values)
38
39 if not gdx.gdxDataWriteDone(gdx_h):
40 report_gdx_error(gdx_h, "gdxDataWriteDone")
41
42 error_nr = gdx.gdxGetLastError(gdx_h)
43 if error_nr:
44 print(f"Error while writing GDX File: {gdx.gdxErrorStr(gdx_h, error_nr)[1]}")
45 return False
46
47 error_nr = gdx.gdxClose(gdx_h)
48 if error_nr:
49 print(f"Error gdxClose: {gdx.gdxErrorStr(gdx_h, error_nr)[1]}")
50 return False
51
52 return True
53
54
55def call_gams(opt_h, sys_dir, model):
56 def_file = os.path.join(sys_dir, "optgams.def")
57
58 if opt.optReadDefinition(opt_h, def_file):
59 print(f"Error optReadDefinition, cannot read def file: {def_file}")
60 return False
61
62 opt.optSetStrStr(opt_h, "Input", model)
63 opt.optSetIntStr(opt_h, "LogOption", 3)
64 opt.optWriteParameterFile(opt_h, "gams.opt")
65
66 return (
67 subprocess.run(
68 [os.path.join(sys_dir, "gams"), "dummy", "pf=gams.opt"], check=True
69 ).returncode
70 == 0
71 )
72
73
74def read_solution_data(gdx_h, gdx_file):
75 rc = gdx.gdxOpenRead(gdx_h, gdx_file)[1]
76 if rc:
77 print(f"Error gdxOpenRead: {gdx.gdxErrorStr(gdx_h, rc)[1]}")
78 return False
79
80 rc, sym_nr = gdx.gdxFindSymbol(gdx_h, "result")
81 if not rc:
82 print("Error gdxFindSymbol: Could not find variable result")
83 return False
84
85 rc, sym_name, dim, sym_type = gdx.gdxSymbolInfo(gdx_h, sym_nr)
86 if dim != 2 or sym_type != gdx.GMS_DT_VAR:
87 print("Error gdxSymbolInfo: result is not a a two dimensional variable")
88 return False
89
90 rc, nr_recs = gdx.gdxDataReadStrStart(gdx_h, sym_nr)
91 if not rc:
92 report_gdx_error(gdx_h, "gdxDataReadStrStart")
93 return False
94
95 for i in range(nr_recs):
96 rc, elements, values, afdim = gdx.gdxDataReadStr(gdx_h)
97 if not rc:
98 print(f"Error gdxDataReadStr: {gdx.gdxErrorStr(gdx_h, rc)[1]}")
99 return False
100 level = values[gdx.GMS_VAL_LEVEL]
101 if level != 0:
102 elements = [elements[d] for d in range(dim)]
103 print(f"{'.'.join(elements)} = {level}")
104 print("All solution values shown")
105 gdx.gdxDataReadDone(gdx_h)
106 gdx.gdxClose(gdx_h)
107 return True
108
109
110if __name__ == "__main__":
111 parser = argparse.ArgumentParser()
112 parser.add_argument("sysDir")
113 parser.add_argument("model", nargs="?", default=None)
114 args = parser.parse_args()
115
116 gdx_h = gdx.new_gdxHandle_tp()
117 opt_h = opt.new_optHandle_tp()
118
119 sys_dir = args.sysDir
120 model = (
121 args.model
122 if args.model
123 else os.path.join(sys_dir, "apifiles", "GAMS", "model2.gms")
124 )
125
126 print(f"Using GAMS system directory: {sys_dir}")
127
128 rc = gdx.gdxCreateD(gdx_h, sys_dir, gdx.GMS_SSSIZE)
129 if not rc[0]:
130 raise Exception(rc[1])
131
132 rc = opt.optCreateD(opt_h, sys_dir, gdx.GMS_SSSIZE)
133 assert rc[0], rc[1]
134
135 status = write_model_data(gdx_h, "demanddata.gdx")
136 if not status:
137 print("Model data not written")
138 terminate(gdx_h, opt_h, 1)
139
140 status = call_gams(opt_h, sys_dir, model)
141 if not status:
142 print("Call to GAMS failed")
143 terminate(gdx_h, opt_h, 1)
144
145 status = read_solution_data(gdx_h, "results.gdx")
146 if not status:
147 print("Could not read solution back")
148 terminate(gdx_h, opt_h, 1)
149
150 terminate(gdx_h, opt_h, 0)
def terminate(gdx_h, opt_h, rc)
Definition: core_example2.py:8
def report_gdx_error(gdx_h, s)
def read_solution_data(gdx_h, gdx_file)
def call_gams(opt_h, sys_dir, model)
def write_model_data(gdx_h, gdx_file)