Loading...
Searching...
No Matches
transport2.py
Go to the documentation of this file.
6
7import os
8import sys
9from gams import GamsWorkspace
10
11GAMS_DATA = """
12Set
13 i 'canning plants' / seattle, san-diego /
14 j 'markets' / new-york, chicago, topeka /;
15
16Parameter
17 a(i) 'capacity of plant i in cases'
18 / seattle 350
19 san-diego 600 /
20
21 b(j) 'demand at market j in cases'
22 / new-york 325
23 chicago 300
24 topeka 275 /;
25
26Table d(i,j) 'distance in thousands of miles'
27 new-york chicago topeka
28 seattle 2.5 1.7 1.8
29 san-diego 2.5 1.8 1.4;
30
31Scalar f 'freight in dollars per case per thousand miles' / 90 /;
32"""
33
34GAMS_MODEL = """
35Set
36 i 'canning plants'
37 j 'markets';
38
39Parameter
40 a(i) 'capacity of plant i in cases'
41 b(j) 'demand at market j in cases'
42 d(i,j) 'distance in thousands of miles';
43
44$if not set incname $abort 'no include file name for data file provided'
45$include %incname%
46
47Parameter c(i,j) 'transport cost in thousands of dollars per case';
48c(i,j) = f*d(i,j)/1000;
49
50Variable
51 x(i,j) 'shipment quantities in cases'
52 z 'total transportation costs in thousands of dollars';
53
54Positive Variable x;
55
56Equations
57 cost 'define objective function'
58 supply(i) 'observe supply limit at plant i'
59 demand(j) 'satisfy demand at market j';
60
61cost.. z =e= sum((i,j), c(i,j)*x(i,j));
62
63supply(i).. sum(j, x(i,j)) =l= a(i);
64
65demand(j).. sum(i, x(i,j)) =g= b(j);
66
67Model transport /all/;
68
69solve transport using lp minimizing z;
70
71display x.l, x.m;
72"""
73
74if __name__ == "__main__":
75 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
76 ws = GamsWorkspace(system_directory=sys_dir)
77
78 with open(os.path.join(ws.working_directory, "tdata.gms"), "w") as file:
79 file.write(GAMS_DATA)
80
81 job = ws.add_job_from_string(GAMS_MODEL)
82 opt = ws.add_options()
83 opt.defines["incname"] = "tdata"
84 job.run(opt)
85 for rec in job.out_db["x"]:
86 print(
87 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
88 )