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