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