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