22from gams
import GamsWorkspace, GamsSet
24if __name__ ==
"__main__":
25 sys_dir = sys.argv[1]
if len(sys.argv) > 1
else None
26 work_dir = sys.argv[2]
if len(sys.argv) > 2
else None
27 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
30 plants = [
"Seattle",
"San-Diego"]
31 markets = [
"New-York",
"Chicago",
"Topeka"]
32 capacity = {
"Seattle": 350.0,
"San-Diego": 600.0}
33 demand = {
"New-York": 325.0,
"Chicago": 300.0,
"Topeka": 275.0}
35 (
"Seattle",
"New-York"): 2.5,
36 (
"Seattle",
"Chicago"): 1.7,
37 (
"Seattle",
"Topeka"): 1.8,
38 (
"San-Diego",
"New-York"): 2.5,
39 (
"San-Diego",
"Chicago"): 1.8,
40 (
"San-Diego",
"Topeka"): 1.4,
44 db = ws.add_database()
47 i = db.add_set(
"i", 1,
"canning plants")
51 j = GamsSet(db,
"j", 1,
"markets")
56 a = db.add_parameter_dc(
"a", [i],
"capacity at plant")
58 a.add_record(p).value = capacity[p]
61 if not a.check_domains():
62 raise Exception(
"Unexpected domain violation in a")
65 b = db.add_parameter_dc(
"b", [
"j"],
"demand at market j in cases")
67 b.add_record(m).value = demand[m]
70 if not b.check_domains():
71 raise Exception(
"Unexpected domain violation in b")
74 d = db.add_parameter_dc(
"d", [i, j],
"distance in thousands of miles")
75 for k, v
in iter(distance.items()):
76 d.add_record((k[0], k[1])).value = v
79 if not d.check_domains():
80 raise Exception(
"Unexpected domain violation in d")
83 if not db.check_domains():
84 raise Exception(
"Unexpected domain violation in db")
87 d.add_record((
"Seattle",
"aa")).value = 1
88 d.add_record((
"bb",
"Seattle")).value = 1
89 a.add_record(
"aa").value = 1
90 a.add_record(
"bb").value = 1
91 b.add_record(
"bb").value = 1
92 b.add_record(
"aa").value = 1
95 if db.check_domains():
96 raise Exception(
"Domain violation for db not recognized")
99 raise Exception(
"Domain violation for a not recognized")
101 if d.check_domains():
102 raise Exception(
"Domain violation for d not recognized")
105 if not b.check_domains():
106 raise Exception(
"Unexpected domain violation in b")
110 print(
"Domain Violations of a:")
111 for sdv
in a.get_symbol_dvs():
113 for vi
in sdv.violation_idx:
116 print(f
"<> {sdv.symbol_record.keys}<<")
120 f
"Number of domain violations for a should be 2 but saw {dv_count}"
125 print(
"Domain Violations of d:")
126 for sdv
in d.get_symbol_dvs():
128 for vi
in sdv.violation_idx:
132 print(f
"<> {sdv.symbol_record.keys}<<")
136 f
"Number of domain violations for a should be 3 but saw {dv_count}"
141 print(
"Domain Violations of db:")
142 for ddv
in db.get_database_dvs():
143 print(f
" > {ddv.symbol.name}:")
144 for sdv
in ddv.symbol_dvs:
146 for vi
in sdv.violation_idx:
150 print(f
"<> {sdv.symbol_record.keys}<<")
154 f
"Number of domain violations for db should be 5 but saw {dv_count}"
159 print(
"Domain Violations of db:")
160 for ddv
in db.get_database_dvs(3):
161 print(f
" > {ddv.symbol.name}:")
162 for sdv
in ddv.symbol_dvs:
164 for vi
in sdv.violation_idx:
168 print(f
"<> {sdv.symbol_record.keys}<<")
172 f
"Number of domain violations for db should be 3 but saw {dv_count}"
177 print(
"Domain Violations of db:")
178 for ddv
in db.get_database_dvs(0, 1):
179 print(f
" > {ddv.symbol.name}:")
180 for sdv
in ddv.symbol_dvs:
182 for vi
in sdv.violation_idx:
186 print(f
"<> {sdv.symbol_record.keys}<<")
190 f
"Number of domain violations for db should be 2 but saw {dv_count}"
195 db.export(
"test.gdx")
197 db.suppress_auto_domain_checking =
True
198 db.export(
"test.gdx")
201 "It should not be possible to export a GamsDatabase containing domain violations by default"
205 db2 = ws.add_database_from_gdx(
"test.gdx")
210 if isinstance(s, GamsSet):
213 if not rec.key(0)
in plants:
215 f
"Unexpected label {rec.key(0)} found in domain i"
219 if not rec.key(0)
in markets:
221 f
"Unexpected label {rec.key(0)} found in domain j"
224 raise Exception(f
"Expected GamsSet i and j but found {s.name}")
226 raise Exception(f
"Expected GamsSet as domain but found relaxed domain {s}")
232 j_alias = ws.add_job_from_string(
233 "Set i 'canning plants' / seattle, san-diego /;\nAlias (i,ii);"
236 ii = j_alias.out_db[
"ii"]
237 print(
"Elements of aliased set:")
239 print(f
" > {rec.key(0)}")
241 test_db = ws.add_database()
242 test_set = test_db.add_set(
"test", 1)
245 test_set.add_record((
""))
246 print(
"Element count of test set after adding empty UEL:")
247 print(f
" > {test_set.number_records}")
250 test_set.add_record(
" a ").text =
"a"
251 print(
"Record ' a ' should be the same as ' a':")
252 print(f
" > {test_set.find_record(' a').text}")
257 "123456789012345678901234567890123456789012345678901234567890123 "
263 "1234567890123456789012345678901234567890123456789012345678901234"
269 "It should not be possible to add a record with more than 63 characters"
276 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345",
282 "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456",
288 "It should not be possible to add an explanatory text with more than 255 characters"
291 test_set.add_record(
"OK").text = (
292 "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
295 test_set.add_record(
"notOK").text = (
296 "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"
302 "It should not be possible to add an set element text with more than 255 characters"
306 test_set.add_record(
"quote'")
307 test_set.add_record(
'quote"')
309 test_set.add_record(
"quote'\"")
314 "It should not be possible to add a record containing single and double quotes simultaneously"
316 test_db.export(
"test.gdx")