27 i canning plants / seattle, san-diego /;
31 if __name__ ==
"__main__":
33 ws = GamsWorkspace(system_directory = sys.argv[1])
38 plants = [
"Seattle",
"San-Diego" ]
39 markets = [
"New-York",
"Chicago",
"Topeka" ]
40 capacity = {
"Seattle": 350.0,
"San-Diego": 600.0 }
41 demand = {
"New-York": 325.0,
"Chicago": 300.0,
"Topeka": 275.0 }
42 distance = { (
"Seattle",
"New-York") : 2.5,
43 (
"Seattle",
"Chicago") : 1.7,
44 (
"Seattle",
"Topeka") : 1.8,
45 (
"San-Diego",
"New-York") : 2.5,
46 (
"San-Diego",
"Chicago") : 1.8,
47 (
"San-Diego",
"Topeka") : 1.4
51 db = ws.add_database()
54 i = db.add_set(
"i", 1,
"canning plants")
58 j = GamsSet(db,
"j", 1,
"markets")
63 a = db.add_parameter_dc(
"a", [i],
"capacity at plant")
65 a.add_record(p).value = capacity[p]
68 if not a.check_domains():
69 raise Exception(
"*** Unexpected domain violation in a")
72 b = db.add_parameter_dc(
"b", [
"j"],
"demand at market j in cases")
74 b.add_record(m).value = demand[m]
77 if not b.check_domains():
78 raise Exception(
"*** Unexpected domain violation in b")
81 d = db.add_parameter_dc(
"d",[i,j],
"distance in thousands of miles")
82 for k,v
in iter(distance.items()):
83 d.add_record((k[0], k[1])).value = v
86 if not d.check_domains():
87 raise Exception(
"*** Unexpected domain violation in d")
90 if not db.check_domains():
91 raise Exception(
"*** Unexpected domain violation in db")
94 d.add_record((
"Seattle",
"aa")).value = 1
95 d.add_record((
"bb",
"Seattle")).value = 1
96 a.add_record(
"aa").value = 1
97 a.add_record(
"bb").value = 1
98 b.add_record(
"bb").value = 1
99 b.add_record(
"aa").value = 1
102 if db.check_domains():
103 raise Exception(
"*** Domain violation for db not recognized")
105 if a.check_domains():
106 raise Exception(
"*** Domain violation for a not recognized")
108 if d.check_domains():
109 raise Exception(
"*** Domain violation for d not recognized")
112 if not b.check_domains():
113 raise Exception(
"*** Unexpected domain violation in b")
117 print(
"Domain Violations of a:")
118 for sdv
in a.get_symbol_dvs():
120 for vi
in sdv.violation_idx:
123 print(sdv.symbol_record.keys, end=
"")
128 raise Exception(
"*** Number of domain violations for a should be 2 but saw " + str(dvCnt))
132 print(
"Domain Violations of d:")
133 for sdv
in d.get_symbol_dvs():
135 for vi
in sdv.violation_idx:
141 print(sdv.symbol_record.keys, end=
"")
145 raise Exception(
"*** Number of domain violations for a should be 3 but saw ", str(dv_cnt))
149 print(
"Domain Violations of db:")
150 for ddv
in db.get_database_dvs():
151 print(
" > " + ddv.symbol.name +
": ")
152 for sdv
in ddv.symbol_dvs:
154 for vi
in sdv.violation_idx:
159 print(sdv.symbol_record.keys, end=
"")
163 raise Exception(
"*** Number of domain violations for db should be 5 but saw " + str(dv_cnt))
167 print(
"Domain Violations of db:")
168 for ddv
in db.get_database_dvs(3):
169 print(
" > " + ddv.symbol.name +
": ")
170 for sdv
in ddv.symbol_dvs:
172 for vi
in sdv.violation_idx:
175 print(sdv.symbol_record.keys, end=
"")
179 raise Exception(
"*** Number of domain violations for db should be 3 but saw " + str(dv_cnt))
183 print(
"Domain Violations of db:")
184 for ddv
in db.get_database_dvs(0, 1):
185 print(
" > " + ddv.symbol.name +
": ")
186 for sdv
in ddv.symbol_dvs:
188 for vi
in sdv.violation_idx:
191 print(sdv.symbol_record.keys, end=
"")
196 raise Exception(
"*** Number of domain violations for db should be 2 but saw " + str(dv_cnt))
199 saw_exception =
False
201 db.export(
"test.gdx")
204 db.suppress_auto_domain_checking =
True
205 db.export(
"test.gdx")
206 if not saw_exception:
207 raise Exception(
"*** It should not be possible to export a GamsDatabase containing domain violations by default")
210 db2 = ws.add_database_from_gdx(
"test.gdx")
211 d2 = db2.get_parameter(
"d")
214 for item
in d2.domains:
215 if isinstance(item, GamsSet):
218 if not uel.key(0)
in plants:
219 raise Exception(
"*** Unexpected uel " + str(uel.key(0)) +
" found in domain i")
221 elif item.name ==
"j":
223 if not uel.key(0)
in markets:
224 raise Exception(
"*** Unexpected uel " + str(uel.key(0)) +
" found in domain j")
227 raise Exception(
"*** Expected GamsSet i and j but found " + item.name)
229 raise Exception(
"*** Expected GamsSet as domain but found relaxed domain " + item)
232 This next section is acutally not about domain checking, but we
233 make sure that certain things are working as expected.
239 ii = j_alias.out_db.get_set(
"ii")
240 print(
"Elements of aliased Set:")
242 print(
" > " + item.key(0))
244 test_db = ws.add_database()
245 test_set = test_db.add_set(
"test", 1)
248 test_set.add_record((
""))
249 print(
"Elements of test Set after adding empty UEL:")
250 print(
" > " + str(test_set.number_records))
253 test_set.add_record(
" a ").text =
"a"
254 print(
"Record 'a' should be the same as 'a':")
255 print(
" > " + test_set.find_record(
" a").text)
259 test_set.add_record(
"123456789012345678901234567890123456789012345678901234567890123 ").text =
"OK"
262 saw_exception =
False
264 test_set.add_record(
"1234567890123456789012345678901234567890123456789012345678901234").text =
"not OK"
267 if not saw_exception:
268 raise Exception(
"*** It should not be possible to add a record with more than 63 characters")
271 test_db.add_set(
"textOK", 1,
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")
272 saw_exception =
False
274 test_db.add_set(
"textNotOK", 1,
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456")
277 if not saw_exception:
278 raise Exception(
"*** It should not be possible to add an explanatory text with more than 255 characters")
280 test_set.add_record(
"OK").text =
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"
281 saw_exception =
False
283 testSet.AddRecord(
"notOK").Text =
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"
286 if not saw_exception:
287 raise Exception(
"*** It should not be possible to add an explanatory text with more than 255 characters")
290 test_set.add_record(
"quote'")
291 test_set.add_record(
"quote\"")
292 saw_exception =
False
294 test_set.add_record(
"quote'\"")
297 if not saw_exception:
298 raise Exception(
"*** It should not be possible to add a record single AND double quote")
299 test_db.export(
"test.gdx")