transport7.py
Go to the documentation of this file.
8 
9 from gams import GamsWorkspace, GamsModifier, UpdateAction, VarType
10 import sys
11 
13  return '''
14  Sets
15  i canning plants / seattle, san-diego /
16  j markets / new-york, chicago, topeka / ;
17 
18  Parameters
19 
20  a(i) capacity of plant i in cases
21  / seattle 350
22  san-diego 600 /
23 
24  b(j) demand at market j in cases
25  / new-york 325
26  chicago 300
27  topeka 275 / ;
28 
29  Table d(i,j) distance in thousands of miles
30  new-york chicago topeka
31  seattle 2.5 1.7 1.8
32  san-diego 2.5 1.8 1.4 ;
33 
34  Scalar f freight in dollars per case per thousand miles /90/ ;
35  Scalar bmult demand multiplier /1/;
36 
37  Parameter c(i,j) transport cost in thousands of dollars per case ;
38 
39  c(i,j) = f * d(i,j) / 1000 ;
40 
41  Variables
42  x(i,j) shipment quantities in cases
43  z total transportation costs in thousands of dollars ;
44 
45  Positive Variable x ;
46 
47  Equations
48  cost define objective function
49  supply(i) observe supply limit at plant i
50  demand(j) satisfy demand at market j ;
51 
52  cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
53 
54  supply(i) .. sum(j, x(i,j)) =l= a(i) ;
55 
56  demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
57 
58  Model transport /all/ ; '''
59 
60 
61 if __name__ == "__main__":
62  if len(sys.argv) > 1:
63  ws = GamsWorkspace(system_directory = sys.argv[1])
64  else:
65  ws = GamsWorkspace()
66 
67  cp = ws.add_checkpoint()
68 
69  # initialize a GAMSCheckpoint by running a GAMSJob
70  t7 = ws.add_job_from_string(get_model_text())
71  t7.run(checkpoint=cp)
72 
73  # create a GAMSModelInstance and solve it multiple times with different scalar bmult
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"
78 
79  # instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
80  mi.instantiate("transport use lp min z", GamsModifier(bmult), opt)
81 
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 ]
84 
85  for b in bmultlist:
86  bmult.first_record().value = b
87  mi.solve()
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))
92 
93 
94  # create a GAMSModelInstance and solve it with single links in the network blocked
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")
98 
99  # instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare upper bound of X mutable
100  mi.instantiate("transport use lp min z", GamsModifier(x, UpdateAction.Upper, xup))
101  mi.solve()
102 
103  for i in t7.out_db["i"]:
104  for j in t7.out_db["j"]:
105  xup.clear()
106  xup.add_record((i.key(0),j.key(0))).value = 0
107  mi.solve()
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))
112 
113 
def get_model_text()
Definition: transport7.py:12