Searching...
No Matches
transport_gt.py
Go to the documentation of this file.
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 ws = GamsWorkspace(system_directory=sys_dir)
61
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"])
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]]))
69
72 opt.defines["gdxincname"] = "gtin.gdx"
73 opt.all_model_types = "xpress"
74 opt.gdx = "gtout.gdx"
75
76 m.write(os.path.join(ws.working_directory, opt.defines["gdxincname"]))
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)
80
81 # create a GamsModelInstance and solve it multiple times with different scalar bmult
83 bmult = mi.sync_db.add_parameter("bmult", 0, "demand multiplier")
85 opt.all_model_types = "cplex"
86
87 # instantiate the GamsModelInstance and pass a model definition and GamsModifier to declare bmult mutable
88 mi.instantiate("transport use lp min z", GamsModifier(bmult), opt)
89
91 bmultlist = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
92
93 m = gt.Container(system_directory=sys_dir)
95 for b in bmultlist:
96 gt_bmult.setRecords(b)
97 m.write(mi.sync_db)
98 mi.solve()
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]}")
104
105 # create a GamsModelInstance and solve it with single links in the network blocked
107 x = mi.sync_db.add_variable("x", 2, VarType.Positive)
108 xup = mi.sync_db.add_parameter("xup", 2, "upper bound on x")
109
110 # instantiate the GamsModelInstance and pass a model definition and GamsModifier to declare upper bound of X mutable
111 mi.instantiate("transport use lp min z", GamsModifier(x, UpdateAction.Upper, xup))
112 mi.solve()
113
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)]) # -0.0 turns the 0 into an EPS
119 m.write(mi.sync_db)
120 mi.solve()
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]}")
GamsWorkspace x
GAMS Development Corp.
GAMS Software GmbH

General Information and Sales
U.S. (+1) 202 342-0180
Europe: (+49) 221 949-9170