transport2.py
Go to the documentation of this file.
7
8from __future__ import print_function
9from gams import *
10import os
11import 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
37
39 return '''
40 Sets
41 i canning plants
42 j markets
43
44 Parameters
45 a(i) capacity of plant i in cases
46 b(j) demand at market j in cases
47 d(i,j) distance in thousands of miles
48 Scalar f freight in dollars per case per thousand miles;
49
50\$if not set incname \$abort 'no include file name for data file provided'
51\$include %incname%
52
53 Parameter c(i,j) transport cost in thousands of dollars per case ;
54
55 c(i,j) = f * d(i,j) / 1000 ;
56
57 Variables
58 x(i,j) shipment quantities in cases
59 z total transportation costs in thousands of dollars ;
60
61 Positive Variable x ;
62
63 Equations
64 cost define objective function
65 supply(i) observe supply limit at plant i
66 demand(j) satisfy demand at market j ;
67
68 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
69
70 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
71
72 demand(j) .. sum(i, x(i,j)) =g= b(j) ;
73
74 Model transport /all/ ;
75
76 Solve transport using lp minimizing z ;
77
78 Display x.l, x.m ; '''
79
80
81if __name__ == "__main__":
82 if len(sys.argv) > 1:
83 ws = GamsWorkspace(system_directory = sys.argv[1])
84 else:
85 ws = GamsWorkspace()
86
87 file = open(os.path.join(ws.working_directory, "tdata.gms"), "w")
88 file.write(get_data_text())
89 file.close()
90
91 t2 = ws.add_job_from_string(get_model_text())
92 opt = ws.add_options()
93 opt.defines["incname"] = "tdata"
94 t2.run(opt)
95 for rec in t2.out_db["x"]:
96 print("x(" + rec.key(0) + "," + rec.key(1) + "): level=" + str(rec.level) + " marginal=" + str(rec.marginal))
97
def get_model_text()
Definition: transport2.py:38
def get_data_text()
Definition: transport2.py:13