Loading...
Searching...
No Matches
transport_gt.py
Go to the documentation of this file.
1
7
8import os
9import sys
10from gams import GamsWorkspace, GamsModifier, UpdateAction, VarType
11import gams.transfer as gt
12import numpy as np
13
14GAMS_MODEL = """
15Set
16 i 'canning plants'
17 j 'markets';
18
19Parameter
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';
23
24Scalar f 'freight in dollars per case per thousand miles';
25
26$if not set gdxincname $abort 'no include file name for data file provided'
27$gdxIn %gdxincname%
28$load i j a b d f
29$gdxIn
30
31Parameter c(i,j) 'transport cost in thousands of dollars per case';
32c(i,j) = f*d(i,j)/1000;
33
34Variable
35 x(i,j) 'shipment quantities in cases'
36 z 'total transportation costs in thousands of dollars';
37
38Positive Variable x;
39
40Equations
41 cost 'define objective function'
42 supply(i) 'observe supply limit at plant i'
43 demand(j) 'satisfy demand at market j';
44
45Scalar bmult 'demand multiplier' / 1 /;
46
47cost .. z =e= sum((i,j), c(i,j)*x(i,j));
48
49supply(i) .. sum(j, x(i,j)) =l= a(i);
50
51demand(j) .. sum(i, x(i,j)) =g= bmult*b(j);
52
53Model transport /all/;
54
55Solve transport using lp minimizing z;
56"""
57
58if __name__ == "__main__":
59 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
60 work_dir = sys.argv[2] if len(sys.argv) > 2 else None
61 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
62
63 m = gt.Container(system_directory=sys_dir)
64 i = m.addSet("i", records=["Seattle", "San-Diego"])
65 j = m.addSet("j", records=["New-York", "Chicago", "Topeka"])
66 m.addParameter("a", [i], records=np.array([350, 600]))
67 m.addParameter("b", [j], records=np.array([325, 300, 275]))
68 m.addParameter("d", [i, j], records=np.array([[2.5, 1.7, 1.8], [2.5, 1.8, 1.4]]))
69 m.addParameter("f", records=90)
70
71 job = ws.add_job_from_string(GAMS_MODEL)
72 opt = ws.add_options()
73 opt.defines["gdxincname"] = "gtin.gdx"
74 opt.all_model_types = "xpress"
75 opt.gdx = "gtout.gdx"
76
77 m.write(os.path.join(ws.working_directory, opt.defines["gdxincname"]))
78 cp = ws.add_checkpoint()
79 job.run(gams_options=opt, checkpoint=cp, create_out_db=False)
80 job_out = gt.Container(os.path.join(ws.working_directory, opt.gdx), sys_dir)
81
82 # create a GamsModelInstance and solve it multiple times with different scalar bmult
83 mi = cp.add_modelinstance()
84 bmult = mi.sync_db.add_parameter("bmult", 0, "demand multiplier")
85 opt = ws.add_options()
86 opt.all_model_types = "cplex"
87
88 # instantiate the GamsModelInstance and pass a model definition and GamsModifier to declare bmult mutable
89 mi.instantiate("transport use lp min z", GamsModifier(bmult), opt)
90
91 bmult.add_record().value = 1.0
92 bmultlist = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
93
94 m = gt.Container(system_directory=sys_dir)
95 gt_bmult = m.addParameter("bmult", records=1.0)
96 for b in bmultlist:
97 gt_bmult.setRecords(b)
98 m.write(mi.sync_db)
99 mi.solve()
100 m_out = gt.Container(mi.sync_db, sys_dir)
101 print(f"Scenario bmult={b}:")
102 print(f" Modelstatus: {mi.model_status}")
103 print(f" Solvestatus: {mi.solver_status}")
104 print(f" Obj: {m_out.data['z'].records['level'].iloc[0]}")
105
106 # create a GamsModelInstance and solve it with single links in the network blocked
107 mi = cp.add_modelinstance()
108 x = mi.sync_db.add_variable("x", 2, VarType.Positive)
109 xup = mi.sync_db.add_parameter("xup", 2, "upper bound on x")
110
111 # instantiate the GamsModelInstance and pass a model definition and GamsModifier to declare upper bound of X mutable
112 mi.instantiate("transport use lp min z", GamsModifier(x, UpdateAction.Upper, xup))
113 mi.solve()
114
115 m = gt.Container(system_directory=sys_dir)
116 gt_xup = m.addParameter("xup", ["i", "j"])
117 for i in job_out.data["i"].records["uni"]:
118 for j in job_out.data["j"].records["uni"]:
119 gt_xup.setRecords([(i, j, -0.0)]) # -0.0 turns the 0 into an EPS
120 m.write(mi.sync_db)
121 mi.solve()
122 m_out = gt.Container(mi.sync_db, sys_dir)
123 print(f"Scenario link blocked: {i} - {j}")
124 print(f" Modelstatus: {mi.model_status}")
125 print(f" Solvestatus: {mi.solver_status}")
126 print(f" Obj: {m_out.data['z'].records['level'].iloc[0]}")