Loading...
Searching...
No Matches
special_values.py
Go to the documentation of this file.
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 ws = GamsWorkspace(system_directory=sys_dir)
43
44 db_in = ws.add_database(in_model_name="myDB")
45 db_in.add_parameter("PythonUndef", 0).add_record().value = SV_UNDEF
46 db_in.add_parameter("PythonNA", 0).add_record().value = float("nan")
47 db_in.add_parameter("PythonPInf", 0).add_record().value = float("inf")
48 db_in.add_parameter("PythonMInf", 0).add_record().value = float("-inf")
49 db_in.add_parameter("PythonEps", 0).add_record().value = SV_EPS
50
51 job = ws.add_job_from_string(GAMS_MODEL)
52 job.run(databases=db_in)
53 db_out = job.out_db
54
55 GUndef = db_out["GUndef"].first_record().value
56 if GUndef != SV_UNDEF:
57 raise Exception(f"GUndef not as expected: {GUndef}")
58 GNA = db_out["GNA"].first_record().value
59 if not math.isnan(GNA):
60 raise Exception(f"GNA not as expected: {GNA}")
61 GPInf = db_out["GPInf"].first_record().value
62 if GPInf != float("inf"):
63 raise Exception(f"GPInf not as expected: {GPInf}")
64 GMInf = db_out["GMInf"].first_record().value
65 if GMInf != float("-inf"):
66 raise Exception(f"GMInf not as expected: {GMInf}")
67 GEps = db_out["GEps"].first_record().value
68 if GEps != SV_EPS:
69 raise Exception(f"GEps not as expected: {GEps}")