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