12from gams
import GamsWorkspace
14if __name__ ==
"__main__":
15 sys_dir = sys.argv[1]
if len(sys.argv) > 1
else None
16 ws = GamsWorkspace(system_directory=sys_dir)
19 plants = [
"Seattle",
"San-Diego"]
20 markets = [
"New-York",
"Chicago",
"Topeka"]
21 capacity = {
"Seattle": 350.0,
"San-Diego": 600.0}
22 demand = {
"New-York": 325.0,
"Chicago": 300.0,
"Topeka": 275.0}
24 (
"Seattle",
"New-York"): 2.5,
25 (
"Seattle",
"Chicago"): 1.7,
26 (
"Seattle",
"Topeka"): 1.8,
27 (
"San-Diego",
"New-York"): 2.5,
28 (
"San-Diego",
"Chicago"): 1.8,
29 (
"San-Diego",
"Topeka"): 1.4,
33 db = ws.add_database()
36 i = db.add_set(
"i", 1,
"canning plants")
41 j = db.add_set(
"j", 1,
"markets")
46 a = db.add_parameter_dc(
"a", [i],
"capacity of plant i in cases")
48 a.add_record(p).value = capacity[p]
51 b = db.add_parameter_dc(
"b", [j],
"demand at market j in cases")
53 b.add_record(m).value = demand[m]
56 d = db.add_parameter_dc(
"d", [i, j],
"distance in thousands of miles")
57 for k, v
in iter(distance.items()):
58 d.add_record(k).value = v
61 f = db.add_parameter(
"f", 0,
"freight in dollars per case per thousand miles")
62 f.add_record().value = 90
67 print(
"Content of GDX file 'data.gdx':")
68 subprocess.call([
"gdxdump", os.path.join(ws.working_directory,
"data.gdx")])
71 db2 = ws.add_database_from_gdx(
"data.gdx")
74 i = [rec.keys[0]
for rec
in db2[
"i"]]
75 j = [rec.keys[0]
for rec
in db2[
"j"]]
77 a = {rec.keys[0]: rec.value
for rec
in db2[
"a"]}
78 b = {rec.keys[0]: rec.value
for rec
in db2[
"b"]}
79 d = {tuple(rec.keys): rec.value
for rec
in db2[
"d"]}
81 f = db2[
"f"].first_record().value
91 print(f
" {rec} : {a[rec]}")
94 print(f
" {rec} : {b[rec]}")
97 print(f
" {rec} : {d[rec]}")