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