17 from threading
import Thread
19 from StringIO
import StringIO
21 from io
import StringIO
27 i canning plants / seattle, san-diego /
28 j markets / new-york, chicago, topeka / ;
32 a(i) capacity of plant i in cases
36 b(j) demand at market j in cases
41 Table d(i,j) distance in thousands of miles
42 new-york chicago topeka
44 san-diego 2.5 1.8 1.4 ;
46 Scalar f freight in dollars per case per thousand miles /90/ ;
47 Scalar bmult demand multiplier /1/; '''
57 a(i) capacity of plant i in cases
58 b(j) demand at market j in cases
59 d(i,j) distance in thousands of miles
60 Scalar f freight in dollars per case per thousand miles;
61 Scalar bmult demand multiplier;
63 $if not set gdxincname $abort 'no include file name for data file provided'
65 $load i j a b d f bmult
68 $echo "test" > test.txt
70 Parameter c(i,j) transport cost in thousands of dollars per case ;
72 c(i,j) = f * d(i,j) / 1000 ;
75 x(i,j) shipment quantities in cases
76 z total transportation costs in thousands of dollars ;
81 cost define objective function
82 supply(i) observe supply limit at plant i
83 demand(j) satisfy demand at market j ;
85 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
87 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
89 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
91 Model transport /all/ ;
93 Solve transport using lp minimizing z ;
95 Scalar ms 'model status', ss 'solve status';
97 Display x.l, x.m ; '''
100 if __name__ ==
"__main__":
101 if len(sys.argv) > 1:
102 ws = GamsWorkspace(system_directory=sys.argv[1])
111 engine_configuration = GamsEngineConfiguration(host=os.environ[
'ENGINE_URL'],
112 username=os.environ[
'ENGINE_USER'],
113 password=os.environ[
'ENGINE_PASSWORD'],
114 namespace=os.environ[
'ENGINE_NAMESPACE'])
117 t3.run_engine(engine_configuration)
118 gdx_file_path = os.path.join(ws.working_directory,
"tdata.gdx")
119 t3.out_db.export(gdx_file_path)
122 opt = ws.add_options()
123 opt.defines[
"gdxincname"] =
"tdata"
124 opt.all_model_types =
"xpress"
125 t3.run_engine(engine_configuration,
126 extra_model_files=gdx_file_path,
127 engine_options={
"inex_string": json.dumps(
128 {
"type":
"include",
"files": [
"*.gdx"]})},
130 expected_levels = {
"seattle.new-york": 0.0,
"seattle.chicago": 300.0,
"seattle.topeka": 0.0,
131 "san-diego.new-york": 325.0,
"san-diego.chicago": 0.0,
"san-diego.topeka": 275.0}
133 for rec
in t3.out_db[
"x"]:
134 print(
"x(" + rec.key(0) +
"," + rec.key(1) +
"): level=" +
135 str(rec.level) +
" marginal=" + str(rec.marginal))
136 assert expected_levels[rec.key(0) +
"." + rec.key(1)] == rec.level
138 assert os.path.exists(os.path.join(
139 ws.working_directory,
"test.txt")) ==
False
141 cp = ws.add_checkpoint()
145 t3a.run_engine(engine_configuration)
146 opt.defines[
"gdxincname"] = t3a.out_db.name
148 t3b.run_engine(engine_configuration,
149 gams_options=opt, databases=t3a.out_db, checkpoint=cp)
151 for rec
in t3.out_db[
"x"]:
152 assert expected_levels[rec.key(0) +
"." + rec.key(1)] == rec.level
155 {
"bmult": 0.9,
"ms": 1,
"ss": 1,
"obj": 138.31},
156 {
"bmult": 1.2,
"ms": 4,
"ss": 1,
"obj": 184.41}]
159 for scen
in bmultExpected:
160 t5 = ws.add_job_from_string(
161 "bmult=" + str(scen[
"bmult"]) +
"; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;", cp)
162 t5.run_engine(engine_configuration)
163 print(
"Scenario bmult=" + str(scen[
"bmult"]) +
":")
164 print(
" Modelstatus: " + str(t5.out_db[
"ms"].find_record().value))
165 print(
" Solvestatus: " + str(t5.out_db[
"ss"].find_record().value))
166 print(
" Obj: " + str(t5.out_db[
"z"].find_record().level))
167 assert t5.out_db[
"bmult"].find_record().value == scen[
"bmult"]
168 assert t5.out_db[
"ms"].find_record().value == scen[
"ms"]
169 assert t5.out_db[
"ss"].find_record().value == scen[
"ss"]
170 assert round(t5.out_db[
"z"].find_record().level, 2) == scen[
"obj"]
177 j1 = ws.add_job_from_file(
"clad")
180 option_file1_path = os.path.join(ws.working_directory,
"cplex.opt")
181 with open(option_file1_path,
"w")
as f:
185 f.write(
"interactive 1\n")
187 f.write(
"iafile cplex.op2\n")
190 option_file2_path = os.path.join(ws.working_directory,
"cplex.op2")
191 with open(option_file2_path,
"w")
as f:
194 opt = ws.add_options()
197 opt.solvelink = SolveLink.LoadLibrary
202 opt_thread = Thread(target=j1.run_engine, args=(
203 engine_configuration,), kwargs={
"output": sw,
204 "extra_model_files": [option_file1_path, option_file2_path,
"claddat.gdx"],
205 "gams_options": opt})
214 print(
"Interrupted Cplex to continue with new option")
218 if opt_thread.is_alive:
222 if not "Interrupted..." in log:
223 raise Exception(
"Expected the solver to be interrupted at least once.")