transport12.py
Go to the documentation of this file.
7
8from __future__ import print_function
9from gams import *
10import sys
11
13 return '''
14 Sets
15 i canning plants / seattle, san-diego /
16 j markets / new-york, chicago, topeka / ;
17
18 Parameters
19
20 a(i) capacity of plant i in cases
21 / seattle 350
22 san-diego 600 /
23
24 b(j) demand at market j in cases
25 / new-york 325
26 chicago 300
27 topeka 275 / ;
28
29 Table d(i,j) distance in thousands of miles
30 new-york chicago topeka
31 seattle 2.5 1.7 1.8
32 san-diego 2.5 1.8 1.4 ;
33
34 Scalar f freight in dollars per case per thousand miles /90/ ;
35 Scalar bmult demand multiplier /1/;
36
37 Parameter c(i,j) transport cost in thousands of dollars per case ;
38
39 c(i,j) = f * d(i,j) / 1000 ;
40
41 Variables
42 x(i,j) shipment quantities in cases
43 z total transportation costs in thousands of dollars ;
44
45 Positive Variable x ;
46
47 Equations
48 cost define objective function
49 supply(i) observe supply limit at plant i
50 demand(j) satisfy demand at market j ;
51
52 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
53
54 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
55
56 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
57
58 Model transport /all/ ;
59'''
60
61
62# Needs to be called with an uninstantiated GAMSModelInstance
63def guss_call(dict, mi, solve_statement, opt = None, mi_opt = None, output=None):
64 modifier_list = []
65
66 if dict.dimension != 3:
67 raise GamsException("Dict needs to be 3-dimensional")
68
69 scen_name = dict.first_record((" ", "scenario", " ")).key(0)
70 scen_symbol = dict.database[scen_name]
71
72 for rec in dict:
73 if rec.key(1).lower() == "scenario":
74 continue
75 if rec.key(1).lower() == "param":
76 modifier_dim = dict.database[rec.key(2)].dimension - scen_symbol.dimension
77 if modifier_dim < 0:
78 raise GamsException("Dimension of " + rec.key(2) + " too small")
80 elif (rec.key(1).lower() == "lower") or (rec.key(1).lower() == "upper") or (rec.key(1).lower() == "fixed"):
81 modifier_dim = dict.database[rec.key(2)].dimension - scen_symbol.dimension
82 if modifier_dim < 0:
83 raise GamsException("Dimension of " + rec.key(2) + " too small")
84 modifier_var = None
85 try:
86 modifier_var = dict.database[rec.key(0)]
87 except:
88 modifier_var = mi.sync_db.add_variable(rec.key(0), modifier_dim, VarType.Free, "")
89 if (rec.key(1).lower() == "lower"):
90 modifier_list.append((GamsModifier(modifier_var, UpdateAction.Lower, mi.sync_db.add_parameter(rec.key(2), modifier_dim, "")), dict.database[rec.key(2)]))
91 elif rec.key(1).lower() == "upper":
92 modifier_list.append((GamsModifier(modifier_var, UpdateAction.Upper, mi.sync_db.add_parameter(rec.key(2), modifier_dim, "")), dict.database[rec.key(2)]))
93 else: #fixed
94 modifier_list.append((GamsModifier(modifier_var, UpdateAction.Fixed, mi.sync_db.add_parameter(rec.key(2), modifier_dim, "")), dict.database[rec.key(2)]))
95 elif (rec.key(1).lower() == "level") or (rec.key(1).lower() == "marginal"):
96 # Check that parameter exists in GAMSDatabase, will throw an exception if not
97 x = dict.database[rec.key(2)]
98 else:
99 raise GamsException("Cannot handle UpdateAction " + rec.key(1))
100
101 ml = []
102 for tup in modifier_list:
103 ml.append(tup[0])
104 mi.instantiate(solve_statement, ml, opt)
105
106 out_list = []
107
108 for s in scen_symbol:
109 for tup in modifier_list:
110 p = None
111 pscen = tup[1]
112
113 if tup[0].data_symbol == None:
114 p = tup[0].gams_symbol
115 else:
116 p = tup[0].data_symbol
117
118 # Implemented SymbolUpdateType=BaseCase
119 p.clear()
120
121 rec = None
122 filter = [""]*pscen.dimension
123 for i in range(scen_symbol.dimension):
124 filter[i] = s.key(i)
125 for i in range (scen_symbol.dimension, pscen.dimension):
126 filter[i] = " "
127 try:
128 rec = pscen.first_record(filter)
129 except:
130 continue
131
132 while True:
133 my_keys = []
134 for i in range(p.dimension):
135 my_keys.append(rec.key(scen_symbol.dimension+i))
137 if not rec.move_next():
138 break
139
140 mi.solve(SymbolUpdateType.BaseCase, output, mi_opt)
141 if len(out_list) == 0:
142 for rec in dict:
143 if (rec.key(1).lower() == "level") or (rec.key(1).lower() == "marginal"):
144 out_list.append((mi.sync_db[rec.key(0)], dict.database[rec.key(2)], rec.key(1).lower()))
145
146 for tup in out_list:
147 my_keys = [""]*(scen_symbol.dimension + len(tup[0].first_record().keys))
148 for i in range(scen_symbol.dimension):
149 my_keys[i] = s.key(i)
150
151 if (tup[2] == "level") and (isinstance(tup[0],GamsVariable)):
152 for rec in tup[0]:
153 for i in range(len(rec.keys)):
154 my_keys[scen_symbol.dimension + i] = s.key(i)
156 elif (tup[2] == "level") and (isinstance(tup[0], GamsEquation)):
157 for rec in tup[0]:
158 for i in range(len(rec.keys)):
159 my_keys[scen_symbol.dimension + i] = s.key(i)
161 elif (tup[2] == "marginal") and (isinstance(tup[0], GamsVariable)):
162 for rec in tup[0]:
163 for i in range(len(rec.keys)):
164 my_keys[scen_symbol.dimension + i] = s.key(i)
166 elif (tup[2] == "marginal") and (isinstance(tup[0], GamsEquation)):
167 for rec in tup[0]:
168 for i in range(len(rec.keys)):
169 my_keys[scen_symbol.dimension + i] = s.key(i)
171
172
173if __name__ == "__main__":
174 if len(sys.argv) > 1:
175 ws = GamsWorkspace(system_directory = sys.argv[1])
176 else:
177 ws = GamsWorkspace()
178
180
181 # initialize a GAMSCheckpoint by running a GAMSJob
183 t12.run(checkpoint=cp)
184
185 # create a GAMSModelInstance and solve it multiple times with different scalar bmult
187
188 bmultlist = [ 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 ]
189
191
192 scen = db.add_set("scen", 1, "")
195
196 i = 0
197 for b in bmultlist:
198 bmult.add_record("s" + str(i)).value = b
200 i += 1
201
202
207
208 guss_call(dict, mi, "transport use lp min z");
209
210 for rec in db[zscen.name]:
211 print(rec.key(0) + " obj: " + str(rec.value))
212
213 #*******************
214
217
218 scen2 = db2.add_set("scen", 1, "")
220 xup = db2.add_parameter("xup", 3, "")
221
222 for j in range(4):
223 for irec in t12.out_db["i"]:
224 for jrec in t12.out_db["j"]:
225 xup.add_record(("s" + str(j), irec.key(0), jrec.key(0))).value = j+1
227
228 dict2 = db2.add_set("dict", 3, "")