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