Loading...
Searching...
No Matches
transport4.py
Go to the documentation of this file.
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 ws = GamsWorkspace(system_directory=sys_dir)
58
59 plants = ["Seattle", "San-Diego"]
60 markets = ["New-York", "Chicago", "Topeka"]
61 capacity = {"Seattle": 350.0, "San-Diego": 600.0}
62 demand = {"New-York": 325.0, "Chicago": 300.0, "Topeka": 275.0}
63 distance = {
64 ("Seattle", "New-York"): 2.5,
65 ("Seattle", "Chicago"): 1.7,
66 ("Seattle", "Topeka"): 1.8,
67 ("San-Diego", "New-York"): 2.5,
68 ("San-Diego", "Chicago"): 1.8,
69 ("San-Diego", "Topeka"): 1.4,
70 }
71
72 db = ws.add_database()
73
74 i = db.add_set("i", 1, "canning plants")
75 for p in plants:
76 i.add_record(p)
77
78 j = db.add_set("j", 1, "markets")
79 for m in markets:
80 j.add_record(m)
81
82 a = db.add_parameter_dc("a", [i], "capacity of plant i in cases")
83 for p in plants:
84 a.add_record(p).value = capacity[p]
85
86 b = db.add_parameter_dc("b", [j], "demand at market j in cases")
87 for m in markets:
88 b.add_record(m).value = demand[m]
89
90 d = db.add_parameter_dc("d", [i, j], "distance in thousands of miles")
91 for k, v in distance.items():
92 d.add_record(k).value = v
93
94 f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
95 f.add_record().value = 90
96
97 job = ws.add_job_from_string(GAMS_MODEL)
98 opt = ws.add_options()
99 opt.defines["gdxincname"] = db.name
100 opt.all_model_types = "xpress"
101 job.run(opt, databases=db)
102 for rec in job.out_db["x"]:
103 print(
104 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
105 )
GamsWorkspace d
Definition: transport4.py:90
dict demand
Definition: transport4.py:62
GamsWorkspace b
Definition: transport4.py:86
GamsWorkspace a
Definition: transport4.py:82