5from gams.core
import gdx, opt
15 error_msg = gdx.gdxErrorStr(gdx_h, gdx.gdxGetLastError(gdx_h))[1]
16 print(f
"Error {s}: {error_msg}")
20 rc, error_nr = gdx.gdxOpenWrite(gdx_h, gdx_file,
"xp_example1")
22 print(f
"Error gdxOpenWrite: {gdx.gdxErrorStr(gdx_h, error_nr)[1]}")
25 if not gdx.gdxDataWriteStrStart(
26 gdx_h,
"Demand",
"Demand data", 1, gdx.GMS_DT_PAR, 0
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)
39 if not gdx.gdxDataWriteDone(gdx_h):
42 error_nr = gdx.gdxGetLastError(gdx_h)
44 print(f
"Error while writing GDX File: {gdx.gdxErrorStr(gdx_h, error_nr)[1]}")
47 error_nr = gdx.gdxClose(gdx_h)
49 print(f
"Error gdxClose: {gdx.gdxErrorStr(gdx_h, error_nr)[1]}")
56 def_file = os.path.join(sys_dir,
"optgams.def")
58 if opt.optReadDefinition(opt_h, def_file):
59 print(f
"Error optReadDefinition, cannot read def file: {def_file}")
62 opt.optSetStrStr(opt_h,
"Input", model)
63 opt.optSetIntStr(opt_h,
"LogOption", 3)
64 opt.optWriteParameterFile(opt_h,
"gams.opt")
68 [os.path.join(sys_dir,
"gams"),
"dummy",
"pf=gams.opt"], check=
True
75 rc = gdx.gdxOpenRead(gdx_h, gdx_file)[1]
77 print(f
"Error gdxOpenRead: {gdx.gdxErrorStr(gdx_h, rc)[1]}")
80 rc, sym_nr = gdx.gdxFindSymbol(gdx_h,
"result")
82 print(
"Error gdxFindSymbol: Could not find variable result")
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")
90 rc, nr_recs = gdx.gdxDataReadStrStart(gdx_h, sym_nr)
95 for i
in range(nr_recs):
96 rc, elements, values, afdim = gdx.gdxDataReadStr(gdx_h)
98 print(f
"Error gdxDataReadStr: {gdx.gdxErrorStr(gdx_h, rc)[1]}")
100 level = values[gdx.GMS_VAL_LEVEL]
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)
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()
116 gdx_h = gdx.new_gdxHandle_tp()
117 opt_h = opt.new_optHandle_tp()
119 sys_dir = args.sysDir
123 else os.path.join(sys_dir,
"apifiles",
"GAMS",
"model2.gms")
126 print(f
"Using GAMS system directory: {sys_dir}")
128 rc = gdx.gdxCreateD(gdx_h, sys_dir, gdx.GMS_SSSIZE)
130 raise Exception(rc[1])
132 rc = opt.optCreateD(opt_h, sys_dir, gdx.GMS_SSSIZE)
137 print(
"Model data not written")
140 status =
call_gams(opt_h, sys_dir, model)
142 print(
"Call to GAMS failed")
147 print(
"Could not read solution back")
def terminate(gdx_h, opt_h, rc)
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)