22from gams
import GamsWorkspace, GamsSet
24if __name__ ==
"__main__":
25 sys_dir = sys.argv[1]
if len(sys.argv) > 1
else None
26 ws = GamsWorkspace(system_directory=sys_dir)
29 plants = [
"Seattle",
"San-Diego"]
30 markets = [
"New-York",
"Chicago",
"Topeka"]
31 capacity = {
"Seattle": 350.0,
"San-Diego": 600.0}
32 demand = {
"New-York": 325.0,
"Chicago": 300.0,
"Topeka": 275.0}
34 (
"Seattle",
"New-York"): 2.5,
35 (
"Seattle",
"Chicago"): 1.7,
36 (
"Seattle",
"Topeka"): 1.8,
37 (
"San-Diego",
"New-York"): 2.5,
38 (
"San-Diego",
"Chicago"): 1.8,
39 (
"San-Diego",
"Topeka"): 1.4,
43 db = ws.add_database()
46 i = db.add_set(
"i", 1,
"canning plants")
50 j = GamsSet(db,
"j", 1,
"markets")
55 a = db.add_parameter_dc(
"a", [i],
"capacity at plant")
57 a.add_record(p).value = capacity[p]
60 if not a.check_domains():
61 raise Exception(
"Unexpected domain violation in a")
64 b = db.add_parameter_dc(
"b", [
"j"],
"demand at market j in cases")
66 b.add_record(m).value = demand[m]
69 if not b.check_domains():
70 raise Exception(
"Unexpected domain violation in b")
73 d = db.add_parameter_dc(
"d", [i, j],
"distance in thousands of miles")
74 for k, v
in iter(distance.items()):
75 d.add_record((k[0], k[1])).value = v
78 if not d.check_domains():
79 raise Exception(
"Unexpected domain violation in d")
82 if not db.check_domains():
83 raise Exception(
"Unexpected domain violation in db")
86 d.add_record((
"Seattle",
"aa")).value = 1
87 d.add_record((
"bb",
"Seattle")).value = 1
88 a.add_record(
"aa").value = 1
89 a.add_record(
"bb").value = 1
90 b.add_record(
"bb").value = 1
91 b.add_record(
"aa").value = 1
94 if db.check_domains():
95 raise Exception(
"Domain violation for db not recognized")
98 raise Exception(
"Domain violation for a not recognized")
100 if d.check_domains():
101 raise Exception(
"Domain violation for d not recognized")
104 if not b.check_domains():
105 raise Exception(
"Unexpected domain violation in b")
109 print(
"Domain Violations of a:")
110 for sdv
in a.get_symbol_dvs():
112 for vi
in sdv.violation_idx:
115 print(f
"<> {sdv.symbol_record.keys}<<")
119 f
"Number of domain violations for a should be 2 but saw {dv_count}"
124 print(
"Domain Violations of d:")
125 for sdv
in d.get_symbol_dvs():
127 for vi
in sdv.violation_idx:
131 print(f
"<> {sdv.symbol_record.keys}<<")
135 f
"Number of domain violations for a should be 3 but saw {dv_count}"
140 print(
"Domain Violations of db:")
141 for ddv
in db.get_database_dvs():
142 print(f
" > {ddv.symbol.name}:")
143 for sdv
in ddv.symbol_dvs:
145 for vi
in sdv.violation_idx:
149 print(f
"<> {sdv.symbol_record.keys}<<")
153 f
"Number of domain violations for db should be 5 but saw {dv_count}"
158 print(
"Domain Violations of db:")
159 for ddv
in db.get_database_dvs(3):
160 print(f
" > {ddv.symbol.name}:")
161 for sdv
in ddv.symbol_dvs:
163 for vi
in sdv.violation_idx:
167 print(f
"<> {sdv.symbol_record.keys}<<")
171 f
"Number of domain violations for db should be 3 but saw {dv_count}"
176 print(
"Domain Violations of db:")
177 for ddv
in db.get_database_dvs(0, 1):
178 print(f
" > {ddv.symbol.name}:")
179 for sdv
in ddv.symbol_dvs:
181 for vi
in sdv.violation_idx:
185 print(f
"<> {sdv.symbol_record.keys}<<")
189 f
"Number of domain violations for db should be 2 but saw {dv_count}"
194 db.export(
"test.gdx")
196 db.suppress_auto_domain_checking =
True
197 db.export(
"test.gdx")
200 "It should not be possible to export a GamsDatabase containing domain violations by default"
204 db2 = ws.add_database_from_gdx(
"test.gdx")
209 if isinstance(s, GamsSet):
212 if not rec.key(0)
in plants:
214 f
"Unexpected label {rec.key(0)} found in domain i"
218 if not rec.key(0)
in markets:
220 f
"Unexpected label {rec.key(0)} found in domain j"
223 raise Exception(f
"Expected GamsSet i and j but found {s.name}")
225 raise Exception(f
"Expected GamsSet as domain but found relaxed domain {s}")
231 j_alias = ws.add_job_from_string(
232 "Set i 'canning plants' / seattle, san-diego /;\nAlias (i,ii);"
235 ii = j_alias.out_db[
"ii"]
236 print(
"Elements of aliased set:")
238 print(f
" > {rec.key(0)}")
240 test_db = ws.add_database()
241 test_set = test_db.add_set(
"test", 1)
244 test_set.add_record((
""))
245 print(
"Element count of test set after adding empty UEL:")
246 print(f
" > {test_set.number_records}")
249 test_set.add_record(
" a ").text =
"a"
250 print(
"Record ' a ' should be the same as ' a':")
251 print(f
" > {test_set.find_record(' a').text}")
256 "123456789012345678901234567890123456789012345678901234567890123 "
262 "1234567890123456789012345678901234567890123456789012345678901234"
268 "It should not be possible to add a record with more than 63 characters"
275 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345",
281 "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456",
287 "It should not be possible to add an explanatory text with more than 255 characters"
292 ).text =
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
296 ).text =
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"
301 "It should not be possible to add an set element text with more than 255 characters"
305 test_set.add_record(
"quote'")
306 test_set.add_record(
'quote"')
308 test_set.add_record(
"quote'\"")
313 "It should not be possible to add a record containing single and double quotes simultaneously"
315 test_db.export(
"test.gdx")