Loading...
Searching...
No Matches
transport4.py
Go to the documentation of this file.
1
7
8import sys
9from gams import GamsWorkspace
10
11GAMS_MODEL = """
12Set
13 i 'canning plants'
14 j 'markets';
15
16Parameter
17 a(i) 'capacity of plant i in cases'
18 b(j) 'demand at market j in cases'
19 d(i,j) 'distance in thousands of miles';
20
21Scalar f 'freight in dollars per case per thousand miles';
22
23$if not set gdxincname $abort 'no include file name for data file provided'
24$gdxIn %gdxincname%
25$load i j a b d f
26$gdxIn
27
28Parameter c(i,j) 'transport cost in thousands of dollars per case';
29c(i,j) = f*d(i,j)/1000;
30
31Variable
32 x(i,j) 'shipment quantities in cases'
33 z 'total transportation costs in thousands of dollars';
34
35Positive Variable x;
36
37Equations
38 cost 'define objective function'
39 supply(i) 'observe supply limit at plant i'
40 demand(j) 'satisfy demand at market j';
41
42cost.. z =e= sum((i,j), c(i,j)*x(i,j));
43
44supply(i).. sum(j, x(i,j)) =l= a(i);
45
46demand(j).. sum(i, x(i,j)) =g= b(j);
47
48Model transport /all/;
49
50solve transport using lp minimizing z;
51
52display x.l, x.m;
53"""
54
55if __name__ == "__main__":
56 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
57 work_dir = sys.argv[2] if len(sys.argv) > 2 else None
58 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
59
60 plants = ["Seattle", "San-Diego"]
61 markets = ["New-York", "Chicago", "Topeka"]
62 capacity = {"Seattle": 350.0, "San-Diego": 600.0}
63 demand = {"New-York": 325.0, "Chicago": 300.0, "Topeka": 275.0}
64 distance = {
65 ("Seattle", "New-York"): 2.5,
66 ("Seattle", "Chicago"): 1.7,
67 ("Seattle", "Topeka"): 1.8,
68 ("San-Diego", "New-York"): 2.5,
69 ("San-Diego", "Chicago"): 1.8,
70 ("San-Diego", "Topeka"): 1.4,
71 }
72
73 db = ws.add_database()
74
75 i = db.add_set("i", 1, "canning plants")
76 for p in plants:
77 i.add_record(p)
78
79 j = db.add_set("j", 1, "markets")
80 for m in markets:
81 j.add_record(m)
82
83 a = db.add_parameter_dc("a", [i], "capacity of plant i in cases")
84 for p in plants:
85 a.add_record(p).value = capacity[p]
86
87 b = db.add_parameter_dc("b", [j], "demand at market j in cases")
88 for m in markets:
89 b.add_record(m).value = demand[m]
90
91 d = db.add_parameter_dc("d", [i, j], "distance in thousands of miles")
92 for k, v in distance.items():
93 d.add_record(k).value = v
94
95 f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
96 f.add_record().value = 90
97
98 job = ws.add_job_from_string(GAMS_MODEL)
99 opt = ws.add_options()
100 opt.defines["gdxincname"] = db.name
101 opt.all_model_types = "xpress"
102 job.run(opt, databases=db)
103 for rec in job.out_db["x"]:
104 print(
105 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
106 )