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