Loading...
Searching...
No Matches
special_values.py
Go to the documentation of this file.
1
7
8import math
9import sys
10from gams import GamsWorkspace, SV_UNDEF, SV_EPS
11
12GAMS_MODEL = """
13Scalar
14 GUndef
15 GNA / NA /
16 GPInf / +INF /
17 GMInf / -INF /
18 GEps / EPS /
19 PythonUndef
20 PythonNA
21 PythonPInf
22 PythonMInf
23 PythonEps;
24
25$onUndf
26$gdxIn %myDB%
27$load PythonUndef PythonNA PythonPInf PythonMInf PythonEps
28$gdxIn
29
30GUndef = 1/0;
31ExecError = 0;
32
33abort$(GUndef <> PythonUndef) 'PythonUndef not as expected', GUndef, PythonUndef;
34abort$(GNA <> PythonNA ) 'PythonNA not as expected', GNA, PythonNA;
35abort$(GPInf <> PythonPInf ) 'PythonPInf not as expected', GPInf, PythonPInf;
36abort$(GMInf <> PythonMInf ) 'PythonMInf not as expected', GMInf, PythonMInf;
37abort$(GEps <> PythonEps ) 'PythonEps not as expected', GEps, PythonEps;
38"""
39
40if __name__ == "__main__":
41 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
42 work_dir = sys.argv[2] if len(sys.argv) > 2 else None
43 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
44
45 db_in = ws.add_database(in_model_name="myDB")
46 db_in.add_parameter("PythonUndef", 0).add_record().value = SV_UNDEF
47 db_in.add_parameter("PythonNA", 0).add_record().value = float("nan")
48 db_in.add_parameter("PythonPInf", 0).add_record().value = float("inf")
49 db_in.add_parameter("PythonMInf", 0).add_record().value = float("-inf")
50 db_in.add_parameter("PythonEps", 0).add_record().value = SV_EPS
51
52 job = ws.add_job_from_string(GAMS_MODEL)
53 job.run(databases=db_in)
54 db_out = job.out_db
55
56 GUndef = db_out["GUndef"].first_record().value
57 if GUndef != SV_UNDEF:
58 raise Exception(f"GUndef not as expected: {GUndef}")
59 GNA = db_out["GNA"].first_record().value
60 if not math.isnan(GNA):
61 raise Exception(f"GNA not as expected: {GNA}")
62 GPInf = db_out["GPInf"].first_record().value
63 if GPInf != float("inf"):
64 raise Exception(f"GPInf not as expected: {GPInf}")
65 GMInf = db_out["GMInf"].first_record().value
66 if GMInf != float("-inf"):
67 raise Exception(f"GMInf not as expected: {GMInf}")
68 GEps = db_out["GEps"].first_record().value
69 if GEps != SV_EPS:
70 raise Exception(f"GEps not as expected: {GEps}")