Loading...
Searching...
No Matches
transport_gdx.py
Go to the documentation of this file.
1
8
9import os
10import subprocess
11import sys
12from gams import GamsWorkspace
13
14if __name__ == "__main__":
15 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
16 work_dir = sys.argv[2] if len(sys.argv) > 2 else None
17 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
18
19 # Python lists and dictionaries holding the data
20 plants = ["Seattle", "San-Diego"]
21 markets = ["New-York", "Chicago", "Topeka"]
22 capacity = {"Seattle": 350.0, "San-Diego": 600.0}
23 demand = {"New-York": 325.0, "Chicago": 300.0, "Topeka": 275.0}
24 distance = {
25 ("Seattle", "New-York"): 2.5,
26 ("Seattle", "Chicago"): 1.7,
27 ("Seattle", "Topeka"): 1.8,
28 ("San-Diego", "New-York"): 2.5,
29 ("San-Diego", "Chicago"): 1.8,
30 ("San-Diego", "Topeka"): 1.4,
31 }
32
33 # create new GamsDatabase instance
34 db = ws.add_database()
35
36 # add 1-dimensional set 'i' with explanatory text 'canning plants' to the GamsDatabase
37 i = db.add_set("i", 1, "canning plants")
38 for p in plants:
39 i.add_record(p)
40
41 # add 1-dimensional set 'j' with explanatory text 'markets' to the GamsDatabase
42 j = db.add_set("j", 1, "markets")
43 for m in markets:
44 j.add_record(m)
45
46 # add parameter 'a' with domain 'i'
47 a = db.add_parameter_dc("a", [i], "capacity of plant i in cases")
48 for p in plants:
49 a.add_record(p).value = capacity[p]
50
51 # add parameter 'b' with domain 'j'
52 b = db.add_parameter_dc("b", [j], "demand at market j in cases")
53 for m in markets:
54 b.add_record(m).value = demand[m]
55
56 # add parameter 'd' with domains 'i' and 'j'
57 d = db.add_parameter_dc("d", [i, j], "distance in thousands of miles")
58 for k, v in iter(distance.items()):
59 d.add_record(k).value = v
60
61 # add scalar 'f'
62 f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
63 f.add_record().value = 90
64
65 # export the GamsDatabase to a GDX file with name 'data.gdx' located in the 'working_directory' of the GamsWorkspace
66 db.export("data.gdx")
67
68 print("Content of GDX file 'data.gdx':")
69 subprocess.call(["gdxdump", os.path.join(ws.working_directory, "data.gdx")])
70
71 # add a new GamsDatabase and initialize it from the GDX file just created
72 db2 = ws.add_database_from_gdx("data.gdx")
73
74 # read data from symbols into Python data structures
75 i = [rec.keys[0] for rec in db2["i"]]
76 j = [rec.keys[0] for rec in db2["j"]]
77
78 a = {rec.keys[0]: rec.value for rec in db2["a"]}
79 b = {rec.keys[0]: rec.value for rec in db2["b"]}
80 d = {tuple(rec.keys): rec.value for rec in db2["d"]}
81
82 f = db2["f"].first_record().value
83
84 print("i:")
85 for rec in i:
86 print(f" {rec}")
87 print("j:")
88 for rec in j:
89 print(f" {rec}")
90 print("a:")
91 for rec in a:
92 print(f" {rec} : {a[rec]}")
93 print("b:")
94 for rec in b:
95 print(f" {rec} : {b[rec]}")
96 print("d:")
97 for rec in d:
98 print(f" {rec} : {d[rec]}")
99 print(f"f:\n {f}")