8from threading
import Lock, Thread
9from gams
import GamsWorkspace
13 i 'canning plants' / seattle, san-diego /
14 j
'markets' / new-york, chicago, topeka /;
17 a(i)
'capacity of plant i in cases'
21 b(j)
'demand at market j in cases'
26Table d(i,j)
'distance in thousands of miles'
27 new-york chicago topeka
29 san-diego 2.5 1.8 1.4;
32 f
'freight in dollars per case per thousand miles' / 90 /
33 bmult
'demand multiplier' / 1 /;
35Parameter c(i,j)
'transport cost in thousands of dollars per case';
36c(i,j) = f*d(i,j)/1000;
39 x(i,j)
'shipment quantities in cases'
40 z
'total transportation costs in thousands of dollars';
45 cost
'define objective function'
46 supply(i)
'observe supply limit at plant i'
47 demand(j)
'satisfy demand at market j';
49cost.. z =e= sum((i,j), c(i,j)*x(i,j));
51supply(i).. sum(j, x(i,j)) =l= a(i);
53demand(j).. sum(i, x(i,j)) =g= bmult*b(j);
57Scalar ms
'model status', ss
'solve status';
61def run_scenario(workspace, checkpoint, io_lock, b):
62 job = workspace.add_job_from_string(
63 f"bmult={b}; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;",
69 print(f
"Scenario bmult={b}:")
70 print(f
" Modelstatus: {job.out_db['ms'].first_record().value}")
71 print(f
" Solvestatus: {job.out_db['ss'].first_record().value}")
72 print(f
" Obj: {job.out_db['z'].first_record().level}")
76if __name__ ==
"__main__":
77 sys_dir = sys.argv[1]
if len(sys.argv) > 1
else None
78 ws = GamsWorkspace(system_directory=sys_dir)
80 cp = ws.add_checkpoint()
83 job = ws.add_job_from_string(GAMS_MODEL)
84 job.run(checkpoint=cp)
85 bmult = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
91 threads[b] = Thread(target=run_scenario, args=(ws, cp, io_lock, b))