Loading...
Searching...
No Matches
core_example2 Namespace Reference

Functions

def terminate (gdx_h, opt_h, rc)
 
def report_gdx_error (gdx_h, s)
 
def write_model_data (gdx_h, gdx_file)
 
def call_gams (opt_h, sys_dir, model)
 
def read_solution_data (gdx_h, gdx_file)
 

Variables

argparse parser = argparse.ArgumentParser()
 
 nargs
 
 default
 
argparse args = parser.parse_args()
 
gdx gdx_h = gdx.new_gdxHandle_tp()
 
opt opt_h = opt.new_optHandle_tp()
 
argparse sys_dir = args.sysDir
 
tuple model
 
gdx rc = gdx.gdxCreateD(gdx_h, sys_dir, gdx.GMS_SSSIZE)
 
def status = write_model_data(gdx_h, "demanddata.gdx")
 

Function Documentation

◆ call_gams()

def core_example2.call_gams (   opt_h,
  sys_dir,
  model 
)

Definition at line 55 of file core_example2.py.

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

◆ read_solution_data()

def core_example2.read_solution_data (   gdx_h,
  gdx_file 
)

Definition at line 74 of file core_example2.py.

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

References report_gdx_error().

◆ report_gdx_error()

def core_example2.report_gdx_error (   gdx_h,
  s 
)

Definition at line 14 of file core_example2.py.

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

Referenced by read_solution_data(), and write_model_data().

◆ terminate()

def core_example2.terminate (   gdx_h,
  opt_h,
  rc 
)

Definition at line 8 of file core_example2.py.

8def terminate(gdx_h, opt_h, rc):
9 opt.optFree(opt_h)
10 gdx.gdxFree(gdx_h)
11 sys.exit(rc)
12
13

◆ write_model_data()

def core_example2.write_model_data (   gdx_h,
  gdx_file 
)

Definition at line 19 of file core_example2.py.

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

References report_gdx_error().

Variable Documentation

◆ args

argparse core_example2.args = parser.parse_args()

Definition at line 114 of file core_example2.py.

◆ default

core_example2.default

Definition at line 113 of file core_example2.py.

◆ gdx_h

gdx core_example2.gdx_h = gdx.new_gdxHandle_tp()

Definition at line 116 of file core_example2.py.

◆ model

tuple core_example2.model
Initial value:
1= (
2 args.model
3 if args.model
4 else os.path.join(sys_dir, "apifiles", "GAMS", "model2.gms")
5 )

Definition at line 120 of file core_example2.py.

◆ nargs

core_example2.nargs

Definition at line 113 of file core_example2.py.

◆ opt_h

opt core_example2.opt_h = opt.new_optHandle_tp()

Definition at line 117 of file core_example2.py.

◆ parser

argparse core_example2.parser = argparse.ArgumentParser()

Definition at line 111 of file core_example2.py.

◆ rc

opt core_example2.rc = gdx.gdxCreateD(gdx_h, sys_dir, gdx.GMS_SSSIZE)

Definition at line 128 of file core_example2.py.

◆ status

def core_example2.status = write_model_data(gdx_h, "demanddata.gdx")

Definition at line 135 of file core_example2.py.

◆ sys_dir

argparse core_example2.sys_dir = args.sysDir

Definition at line 119 of file core_example2.py.