12from io
import StringIO
16from threading
import Thread
18from gams
import GamsWorkspace, GamsEngineConfiguration, SolveLink
23 i 'canning plants' / seattle, san-diego /
24 j
'markets' / new-york, chicago, topeka /;
27 a(i)
'capacity of plant i in cases'
31 b(j)
'demand at market j in cases'
36Table d(i,j)
'distance in thousands of miles'
37 new-york chicago topeka
39 san-diego 2.5 1.8 1.4;
42 f
'freight in dollars per case per thousand miles' / 90 /
43 bmult
'demand multiplier' / 1 /;
52 a(i)
'capacity of plant i in cases'
53 b(j)
'demand at market j in cases'
54 d(i,j)
'distance in thousands of miles';
57 f
'freight in dollars per case per thousand miles'
58 bmult
'demand multiplier';
60$
if not set gdxincname $abort
'no include file name for data file provided'
62$load i j a b d f bmult
65$echo
"test" > test.txt
67Parameter c(i,j)
'transport cost in thousands of dollars per case';
68c(i,j) = f*d(i,j)/1000;
71 x(i,j)
'shipment quantities in cases'
72 z
'total transportation costs in thousands of dollars';
77 cost
'define objective function'
78 supply(i)
'observe supply limit at plant i'
79 demand(j)
'satisfy demand at market j';
81cost.. z =e= sum((i,j), c(i,j)*x(i,j));
83supply(i).. sum(j, x(i,j)) =l= a(i);
85demand(j).. sum(i, x(i,j)) =g= bmult*b(j);
89solve transport using lp minimizing z;
98if __name__ == "__main__":
99 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
100 ws = GamsWorkspace(system_directory=sys_dir)
104 engine_configuration = GamsEngineConfiguration(
105 host=os.environ[
"ENGINE_URL"],
106 username=os.environ[
"ENGINE_USER"],
107 password=os.environ[
"ENGINE_PASSWORD"],
108 namespace=os.environ[
"ENGINE_NAMESPACE"],
111 job = ws.add_job_from_string(GAMS_DATA)
112 job.run_engine(engine_configuration)
113 gdx_file_path = os.path.join(ws.working_directory,
"tdata.gdx")
114 job.out_db.export(gdx_file_path)
115 job = ws.add_job_from_string(GAMS_MODEL)
117 opt = ws.add_options()
118 opt.defines[
"gdxincname"] =
"tdata"
119 opt.all_model_types =
"xpress"
121 engine_configuration,
122 extra_model_files=gdx_file_path,
124 "inex_string": json.dumps({
"type":
"include",
"files": [
"*.gdx"]})
129 (
"seattle",
"new-york"): 0.0,
130 (
"seattle",
"chicago"): 300.0,
131 (
"seattle",
"topeka"): 0.0,
132 (
"san-diego",
"new-york"): 325.0,
133 (
"san-diego",
"chicago"): 0.0,
134 (
"san-diego",
"topeka"): 275.0,
137 for rec
in job.out_db[
"x"]:
139 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
141 if expected_levels[tuple(rec.keys)] != rec.level:
142 raise Exception(
"Unexpected results.")
144 if os.path.exists(os.path.join(ws.working_directory,
"test.txt")):
145 raise Exception(
"Did not expected to find 'test.txt' written by GAMS model supposed to run remotely.")
147 cp = ws.add_checkpoint()
149 job_a = ws.add_job_from_string(GAMS_DATA)
150 job_a.run_engine(engine_configuration)
151 opt.defines[
"gdxincname"] = job_a.out_db.name
153 job_b = ws.add_job_from_string(GAMS_MODEL)
155 engine_configuration, gams_options=opt, databases=job_a.out_db, checkpoint=cp
158 for rec
in job.out_db[
"x"]:
159 if expected_levels[tuple(rec.keys)] != rec.level:
160 raise Exception(
"Unexpected results.")
163 {
"bmult": 0.9,
"ms": 1,
"ss": 1,
"obj": 138.31},
164 {
"bmult": 1.2,
"ms": 4,
"ss": 1,
"obj": 184.41},
168 for scen
in expected_bmult:
169 job = ws.add_job_from_string(
170 f
"bmult={scen['bmult']}; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;",
173 job.run_engine(engine_configuration)
174 print(f
"Scenario bmult={scen['bmult']}:")
175 print(f
" Modelstatus: {job.out_db['ms'].find_record().value}")
176 print(f
" Solvestatus: {job.out_db['ss'].find_record().value}")
177 print(f
" Obj: {job.out_db['z'].find_record().level}")
179 if job.out_db[
"bmult"].find_record().value != scen[
"bmult"]:
180 raise Exception(
"Unexpected bmult.")
181 if job.out_db[
"ms"].find_record().value != scen[
"ms"]:
182 raise Exception(
"Unexpected model status.")
183 if job.out_db[
"ss"].find_record().value != scen[
"ss"]:
184 raise Exception(
"Unexpected solve status.")
185 if round(job.out_db[
"z"].find_record().level, 2) != scen[
"obj"]:
186 raise Exception(
"Unexpected obj.")
191 job = ws.add_job_from_file(
"clad")
194 option_file1_path = os.path.join(ws.working_directory,
"cplex.opt")
195 with open(option_file1_path,
"w")
as f:
199 f.write(
"interactive 1\n")
201 f.write(
"iafile cplex.op2\n")
204 option_file2_path = os.path.join(ws.working_directory,
"cplex.op2")
205 with open(option_file2_path,
"w")
as f:
208 opt = ws.add_options()
211 opt.solvelink = SolveLink.LoadLibrary
217 target=job.run_engine,
218 args=(engine_configuration,),
221 "extra_model_files": [option_file1_path, option_file2_path,
"claddat.gdx"],
232 print(
"Interrupted Cplex to continue with new option")
236 if opt_thread.is_alive():
240 if not "Interrupted..." in log:
241 raise Exception(
"Expected the solver to be interrupted at least once.")