transport11.py
Go to the documentation of this file.
6
7 from gams import *
8 import os
9 import sys
10
12  return '''
13 \$onempty
14  Sets
15  i(*) canning plants / /
16  j(*) markets / /
17
18  Parameters
19  a(i) capacity of plant i in cases / /
20  b(j) demand at market j in cases / /
21  d(i,j) distance in thousands of miles / /
22  Scalar f freight in dollars per case per thousand miles /0/;
23
24  Parameter c(i,j) transport cost in thousands of dollars per case ;
25
26  c(i,j) = f * d(i,j) / 1000 ;
27
28  Variables
29  x(i,j) shipment quantities in cases
30  z total transportation costs in thousands of dollars ;
31
32  Positive Variable x ;
33
34  Equations
35  cost define objective function
36  supply(i) observe supply limit at plant i
37  demand(j) satisfy demand at market j ;
38
39  cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
40
41  supply(i) .. sum(j, x(i,j)) =l= a(i) ;
42
43  demand(j) .. sum(i, x(i,j)) =g= b(j) ;
44
45  Model transport /all/ ;
46
47  Solve transport using lp minimizing z ; '''
48
50  return '''
51 \$if not set gdxincname \$abort 'no include file name for data file provided'
52 \$gdxin %gdxincname%
53 \$onMulti
54 \$load i j a b d f
55 \$gdxin
56
57  Display x.l, x.m ; '''
58
59 def create_save_restart(cp_file_name):
60  if len(sys.argv) > 1:
61  ws = GamsWorkspace(os.path.dirname(cp_file_name), sys.argv[1])
62  else:
63  ws = GamsWorkspace(os.path.dirname(cp_file_name))
64
67
68  opt.action = Action.CompileOnly
70  j1.run(opt, cp)
71  del opt
72
73
74 if __name__ == "__main__":
75  # Create a save/restart file usually supplied by an application provider
76  # We create it for demonstration purpose
77  w_dir = os.path.join(".", "tmp")
78  create_save_restart(os.path.join(w_dir, "tbase"));
79
80  plants = [ "Seattle", "San-Diego" ]
81  markets = [ "New-York", "Chicago", "Topeka" ]
82  capacity = { "Seattle": 350.0, "San-Diego": 600.0 }
83  demand = { "New-York": 325.0, "Chicago": 300.0, "Topeka": 275.0 }
84
85  distance = { ("Seattle", "New-York") : 2.5,
86  ("Seattle", "Chicago") : 1.7,
87  ("Seattle", "Topeka") : 1.8,
88  ("San-Diego", "New-York") : 2.5,
89  ("San-Diego", "Chicago") : 1.8,
90  ("San-Diego", "Topeka") : 1.4
91  }
92
93  if len(sys.argv) > 1:
94  ws = GamsWorkspace(w_dir, sys.argv[1])
95  else:
96  ws = GamsWorkspace(w_dir)
97
99
100  # prepare a GAMSDatabase with data from the Python data structures
101  i = db.add_set("i", 1, "canning plants")
102  for p in plants:
104
105  j = db.add_set("j", 1, "markets")
106  for m in markets:
108
109  a = db.add_parameter_dc("a", [i], "capacity of plant i in cases")
110  for p in plants:
112
113  b = db.add_parameter_dc("b", [j], "demand at market j in cases")
114  for m in markets:
116
117  d = db.add_parameter_dc("d", [i,j], "distance in thousands of miles")
118  for k, v in iter(distance.items()):
120
121  f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
123
124  # run a job using data from the created GAMSDatabase
128  opt.defines["gdxincname"] = db.name
129  opt.all_model_types = "xpress"
130  t4.run(opt, databases=db)
131
132  for rec in t4.out_db["x"]:
133  print("x(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))
134
def get_model_text()
Definition: transport11.py:49
def get_base_model_text()
Definition: transport11.py:11
def create_save_restart(cp_file_name)
Definition: transport11.py:59
GAMS Development Corp.
GAMS Software GmbH

General Information and Sales
U.S. (+1) 202 342-0180
Europe: (+49) 221 949-9170