special_values.py
Go to the documentation of this file.
7 
8 from gams import *
9 import sys
10 import math
11 
12 def get_model():
13  return '''
14 Scalar 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 
30 GUndef = 1/0;
31 ExecError = 0;
32 
33 abort$(GUndef <> pythonUndef) 'pythonUndef not as expected', GUndef, pythonUndef;
34 abort$(GNA <> pythonNA ) 'pythonNA not as expected', GNA, pythonNA;
35 abort$(GPInf <> pythonPInf ) 'pythonPInf not as expected', GPInf, pythonPInf;
36 abort$(GMInf <> pythonMInf ) 'pythonMInf not as expected', GMInf, pythonMInf;
37 abort$(GEps <> pythonEps ) 'pythonEps not as expected', GEps, pythonEps;'''
38 
39 
40 if __name__ == "__main__":
41  try:
42  if len(sys.argv) > 1:
43  ws = GamsWorkspace(system_directory = sys.argv[1])
44  else:
45  ws = GamsWorkspace()
46 
47  dbIn = ws.add_database(in_model_name = "myDB")
48  dbIn.add_parameter("pythonUndef", 0).add_record().value = SV_UNDEF
49  dbIn.add_parameter("pythonNA", 0).add_record().value = float('nan')
50  dbIn.add_parameter("pythonPInf", 0).add_record().value = float('inf')
51  dbIn.add_parameter("pythonMInf", 0).add_record().value = float('-inf')
52  dbIn.add_parameter("pythonEps", 0).add_record().value = SV_EPS
53 
54  gj = ws.add_job_from_string(get_model())
55 
56  gj.run(databases = dbIn)
57  dbOut = gj.out_db
58 
59  GUndef = dbOut["GUndef"].first_record().value
60  if GUndef != SV_UNDEF:
61  raise Exception("GUndef not as expected: " + str(GUndef))
62  GNA = dbOut["GNA"].first_record().value
63  if not math.isnan(GNA):
64  raise Exception("GNA not as expected: " + str(GNA))
65  GPInf = dbOut["GPInf"].first_record().value
66  if GPInf != float('inf'):
67  raise Exception("GPInf not as expected: " + str(GPInf))
68  GMInf = dbOut["GMInf"].first_record().value
69  if GMInf != float('-inf'):
70  raise Exception("GMInf not as expected: " + str(GMInf))
71  GEps = dbOut["GEps"].first_record().value
72  if GEps != SV_EPS:
73  raise Exception("GEps not as expected: " + GEps);
74  except Exception as e:
75  raise Exception("\n###\n### Exception caught:" + str(e) + "\n###")
76