10from gams
import GamsWorkspace, GamsModifier, UpdateAction, VarType
14 i 'canning plants' / seattle, san-diego /
15 j
'markets' / new-york, chicago, topeka /;
18 a(i)
'capacity of plant i in cases'
22 b(j)
'demand at market j in cases'
27Table d(i,j)
'distance in thousands of miles'
28 new-york chicago topeka
30 san-diego 2.5 1.8 1.4;
33 f
'freight in dollars per case per thousand miles' / 90 /
34 bmult
'demand multiplier' / 1 /;
36Parameter c(i,j)
'transport cost in thousands of dollars per case';
37c(i,j) = f*d(i,j)/1000;
40 x(i,j)
'shipment quantities in cases'
41 z
'total transportation costs in thousands of dollars';
46 cost
'define objective function'
47 supply(i)
'observe supply limit at plant i'
48 demand(j)
'satisfy demand at market j';
50cost.. z =e= sum((i,j), c(i,j)*
x(i,j));
52supply(i).. sum(j,
x(i,j)) =l= a(i);
54demand(j).. sum(i,
x(i,j)) =g= bmult*b(j);
59if __name__ == "__main__":
60 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
61 ws = GamsWorkspace(system_directory=sys_dir)
63 cp = ws.add_checkpoint()
66 job = ws.add_job_from_string(GAMS_MODEL)
67 job.run(checkpoint=cp)
70 mi = cp.add_modelinstance()
71 bmult = mi.sync_db.add_parameter(
"bmult", 0,
"demand multiplier")
72 opt = ws.add_options()
73 opt.all_model_types =
"cplex"
76 mi.instantiate(
"transport use lp min z", GamsModifier(bmult), opt)
78 bmult.add_record().value = 1.0
79 bmult_list = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
82 bmult.first_record().value = b
84 print(f
"Scenario bmult={b}:")
85 print(f
" Modelstatus: {mi.model_status}")
86 print(f
" Solvestatus: {mi.solver_status}")
87 print(f
" Obj: {mi.sync_db['z'].first_record().level}")
90 mi = cp.add_modelinstance()
91 x = mi.sync_db.add_variable(
"x", 2, VarType.Positive)
92 xup = mi.sync_db.add_parameter(
"xup", 2,
"upper bound on x")
95 mi.instantiate(
"transport use lp min z", GamsModifier(x, UpdateAction.Upper, xup))
98 for i
in job.out_db[
"i"]:
99 for j
in job.out_db[
"j"]:
101 xup.add_record((i.key(0), j.key(0))).value = 0
103 print(f
"Scenario link blocked: {i.key(0)} - {j.key(0)}")
104 print(f
" Modelstatus: {mi.model_status}")
105 print(f
" Solvestatus: {mi.solver_status}")
106 print(f
" Obj: {mi.sync_db['z'].find_record().level}")