9 from gams
import GamsWorkspace, GamsModifier, UpdateAction, VarType
15 i canning plants / seattle, san-diego /
16 j markets / new-york, chicago, topeka / ;
20 a(i) capacity of plant i in cases
24 b(j) demand at market j in cases
29 Table d(i,j) distance in thousands of miles
30 new-york chicago topeka
32 san-diego 2.5 1.8 1.4 ;
34 Scalar f freight in dollars per case per thousand miles /90/ ;
35 Scalar bmult demand multiplier /1/;
37 Parameter c(i,j) transport cost in thousands of dollars per case ;
39 c(i,j) = f * d(i,j) / 1000 ;
42 x(i,j) shipment quantities in cases
43 z total transportation costs in thousands of dollars ;
48 cost define objective function
49 supply(i) observe supply limit at plant i
50 demand(j) satisfy demand at market j ;
52 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
54 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
56 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
58 Model transport /all/ ; '''
61 if __name__ ==
"__main__":
63 ws = GamsWorkspace(system_directory = sys.argv[1])
67 cp = ws.add_checkpoint()
74 mi = cp.add_modelinstance()
75 bmult = mi.sync_db.add_parameter(
"bmult", 0,
"demand multiplier")
76 opt = ws.add_options()
77 opt.all_model_types =
"cplex"
80 mi.instantiate(
"transport use lp min z", GamsModifier(bmult), opt)
82 bmult.add_record().value = 1.0
83 bmultlist = [ 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 ]
86 bmult.first_record().value = b
88 print(
"Scenario bmult=" + str(b) +
":")
89 print(
" Modelstatus: " + str(mi.model_status))
90 print(
" Solvestatus: " + str(mi.solver_status))
91 print(
" Obj: " + str(mi.sync_db.get_variable(
"z").find_record().level))
95 mi = cp.add_modelinstance()
96 x = mi.sync_db.add_variable(
"x", 2, VarType.Positive)
97 xup = mi.sync_db.add_parameter(
"xup", 2,
"upper bound on x")
100 mi.instantiate(
"transport use lp min z", GamsModifier(x, UpdateAction.Upper, xup))
103 for i
in t7.out_db[
"i"]:
104 for j
in t7.out_db[
"j"]:
106 xup.add_record((i.key(0),j.key(0))).value = 0
108 print(
"Scenario link blocked: " + i.key(0) +
" - " + j.key(0))
109 print(
" Modelstatus: " + str(mi.model_status))
110 print(
" Solvestatus: " + str(mi.solver_status))
111 print(
" Obj: " + str(mi.sync_db[
"z"].find_record().level))