Loading...
Searching...
No Matches
alias.py
Go to the documentation of this file.
14
15import subprocess
16import sys
17import os
18import re
19from gams import GamsWorkspace, GamsSet
20
21GDX_DUMP2 = """
22$onempty
23
24Set i(*) /
25'i1',
26'i2',
27'i3' /;
28
29Parameter aa(i) /
30'i1' 2,
31'i2' 2,
32'i3' 2 /;
33
34$offempty
35"""
36
37GDX_DUMP1 = """
38$onempty
39
40Set i(*) /
41'i1',
42'i2',
43'i3' /;
44
45Set j(*) /
46'j1',
47'j2',
48'j3' /;
49
50Set ij(*,*) /
51'i1'.'j1',
52'i2'.'j2',
53'i3'.'j3' /;
54
55Alias (ii, i);
56
57Alias (jj, j);
58
59Alias (iijj, ij);
60
61Parameter a(i) /
62'i1' 1,
63'i2' 1,
64'i3' 1 /;
65
66Parameter aa(ii) /
67'i1' 2,
68'i2' 2,
69'i3' 2 /;
70
71$offempty
72"""
73
74GAMS_DATA = """
75Set
76 i / i1*i3 /
77 j / j1*j3 /
78 ij / #i:#j /;
79
80Alias
81 (i,ii)
82 (j,jj)
83 (ij,iijj);
84
85Parameter
86 a(i) / #i 1 /
87 aa(ii) / #ii 2 /;
88"""
89
90
91def same_gdx_dump(ws, gdxfile, expected_result):
92 result = ""
93 cmd = [
94 os.path.join(ws.system_directory, "gdxdump"),
95 os.path.join(ws.working_directory, gdxfile),
96 ]
97 p = subprocess.Popen(
98 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE
99 )
100 result, error = p.communicate()
101 if error:
102 raise Exception(error)
103 result = result.decode()
104 return re.sub(r"\s", "", result.lower()) == re.sub(
105 r"\s", "", expected_result.lower()
106 )
107
108
109def check_alias_logic(prefix, db):
110 # check number of symbols
111 if len(db) != 5:
112 raise Exception(
113 f"GamsDatabase '{prefix}' should have five symbols: i, j, ij, a, aa."
114 )
115
116 # check retrival of alias sets
117 if db["ii"].name != "i":
118 raise Exception(f"{prefix}: We should get set 'i' when asking for alias 'ii'.")
119 if db["jj"].name != "j":
120 raise Exception(f"{prefix}: We should get set 'j' when asking for alias 'jj'.")
121 if db["iijj"].name != "ij":
122 raise Exception(
123 f"{prefix}: We should get set 'ij' when asking for alias 'iijj'."
124 )
125
126 # check domain logic
127 if not db.check_domains():
128 raise Exception(f"{prefix}: Check domains should be true.")
129 if not isinstance(db["aa"].domains[0], GamsSet):
130 raise Exception(f"{prefix}: domains[0] of 'aa' should be a set.")
131 if db["aa"].domains[0].name != "i":
132 raise Exception(f"{prefix}: domains[0] of 'aa' should be 'i'.")
133
134 db["ii"].delete_record("i1")
135 if db.check_domains():
136 raise Exception(
137 f"{prefix}: check_domains() should be False after removal of 'i1'."
138 )
139 db["ii"].add_record("i1")
140 if not db.check_domains():
141 raise Exception(
142 f"{prefix}: check_domains() should be True after adding 'i1' again."
143 )
144
145
146if __name__ == "__main__":
147 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
148 ws = GamsWorkspace(system_directory=sys_dir)
149
150 # create initial data containing a GAMS alias
151 # the Control API does not know about aliases and will retrieve it as a set
152 job = ws.add_job_from_string(GAMS_DATA)
153 job.run()
154 check_alias_logic("job.out_db ", job.out_db)
155 job.out_db.export("outdb.gdx")
156 if not same_gdx_dump(ws, "outdb.gdx", GDX_DUMP1):
157 raise Exception("Unexpected result of gdxdump 'outdb.gdx'.")
158
159 # copy constructor should preserve aliases
160 db = ws.add_database(source_database=job.out_db)
161 check_alias_logic("db ", db)
162 db.export("db.gdx")
163 if not same_gdx_dump(ws, "db.gdx", GDX_DUMP1):
164 raise Exception("Unexpected result of gdxdump 'db.gdx'.")
165 db2 = ws.add_database()
166 ii = db2.add_set_dc(db["ii"].name, ["*"], db["ii"].text)
167 db["ii"].copy_symbol(ii)
168
169 aa_orig = db["aa"]
170 aa = db2.add_parameter_dc(db["aa"].name, [ii], db["aa"].text)
171 aa_orig.copy_symbol(aa)
172 db2.export("db2.gdx")
173 if not same_gdx_dump(ws, "db2.gdx", GDX_DUMP2):
174 raise Exception("Unexpected result of gdxdump 'db2.gdx'.")
175
176 # if the domain is an alias, domains should return the aliased set,
177 # but domains_as_strings should return the name of the alias
178 if aa_orig.domains[0].name != "i":
179 raise Exception("The domain set should be the original set.")
180 if aa_orig.domains_as_strings[0] != "ii":
181 raise Exception("The domain as string should be the alias name.")
GamsWorkspace aa
Definition: alias.py:170
def check_alias_logic(prefix, db)
Definition: alias.py:109
def same_gdx_dump(ws, gdxfile, expected_result)
Definition: alias.py:91