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