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