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