10from gams
import GamsWorkspace, GamsModifier, UpdateAction, VarType
11import gams.transfer
as gt
20 a(i)
'capacity of plant i in cases'
21 b(j)
'demand at market j in cases'
22 d(i,j)
'distance in thousands of miles';
24Scalar f
'freight in dollars per case per thousand miles';
26$
if not set gdxincname $abort
'no include file name for data file provided'
31Parameter c(i,j)
'transport cost in thousands of dollars per case';
32c(i,j) = f*d(i,j)/1000;
35 x(i,j)
'shipment quantities in cases'
36 z
'total transportation costs in thousands of dollars';
41 cost
'define objective function'
42 supply(i)
'observe supply limit at plant i'
43 demand(j)
'satisfy demand at market j';
45Scalar bmult
'demand multiplier' / 1 /;
47cost .. z =e= sum((i,j), c(i,j)*
x(i,j));
49supply(i) .. sum(j,
x(i,j)) =l= a(i);
51demand(j) .. sum(i,
x(i,j)) =g= bmult*b(j);
55Solve transport using lp minimizing z;
58if __name__ == "__main__":
59 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
60 ws = GamsWorkspace(system_directory=sys_dir)
62 m = gt.Container(system_directory=sys_dir)
63 i = m.addSet(
"i", records=[
"Seattle",
"San-Diego"])
64 j = m.addSet(
"j", records=[
"New-York",
"Chicago",
"Topeka"])
65 m.addParameter(
"a", [i], records=np.array([350, 600]))
66 m.addParameter(
"b", [j], records=np.array([325, 300, 275]))
67 m.addParameter(
"d", [i, j], records=np.array([[2.5, 1.7, 1.8], [2.5, 1.8, 1.4]]))
68 m.addParameter(
"f", records=90)
70 job = ws.add_job_from_string(GAMS_MODEL)
71 opt = ws.add_options()
72 opt.defines[
"gdxincname"] =
"gtin.gdx"
73 opt.all_model_types =
"xpress"
76 m.write(os.path.join(ws.working_directory, opt.defines[
"gdxincname"]))
77 cp = ws.add_checkpoint()
78 job.run(gams_options=opt, checkpoint=cp, create_out_db=
False)
79 job_out = gt.Container(os.path.join(ws.working_directory, opt.gdx), sys_dir)
82 mi = cp.add_modelinstance()
83 bmult = mi.sync_db.add_parameter(
"bmult", 0,
"demand multiplier")
84 opt = ws.add_options()
85 opt.all_model_types =
"cplex"
88 mi.instantiate(
"transport use lp min z", GamsModifier(bmult), opt)
90 bmult.add_record().value = 1.0
91 bmultlist = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
93 m = gt.Container(system_directory=sys_dir)
94 gt_bmult = m.addParameter(
"bmult", records=1.0)
96 gt_bmult.setRecords(b)
99 m_out = gt.Container(mi.sync_db, sys_dir)
100 print(f
"Scenario bmult={b}:")
101 print(f
" Modelstatus: {mi.model_status}")
102 print(f
" Solvestatus: {mi.solver_status}")
103 print(f
" Obj: {m_out.data['z'].records['level'].iloc[0]}")
106 mi = cp.add_modelinstance()
107 x = mi.sync_db.add_variable(
"x", 2, VarType.Positive)
108 xup = mi.sync_db.add_parameter(
"xup", 2,
"upper bound on x")
111 mi.instantiate(
"transport use lp min z", GamsModifier(x, UpdateAction.Upper, xup))
114 m = gt.Container(system_directory=sys_dir)
115 gt_xup = m.addParameter(
"xup", [
"i",
"j"])
116 for i
in job_out.data[
"i"].records[
"i"]:
117 for j
in job_out.data[
"j"].records[
"j"]:
118 gt_xup.setRecords([(i, j, -0.0)])
121 m_out = gt.Container(mi.sync_db, sys_dir)
122 print(f
"Scenario link blocked: {i} - {j}")
123 print(f
" Modelstatus: {mi.model_status}")
124 print(f
" Solvestatus: {mi.solver_status}")
125 print(f
" Obj: {m_out.data['z'].records['level'].iloc[0]}")