Loading...
Searching...
No Matches
transport7.py
Go to the documentation of this file.
1
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 work_dir = sys.argv[2] if len(sys.argv) > 2 else None
62 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
63
64 cp = ws.add_checkpoint()
65
66 # initialize a GamsCheckpoint by running a GamsJob
67 job = ws.add_job_from_string(GAMS_MODEL)
68 job.run(checkpoint=cp)
69
70 # create a GamsModelInstance and solve it multiple times with different scalar bmult
71 mi = cp.add_modelinstance()
72 bmult = mi.sync_db.add_parameter("bmult", 0, "demand multiplier")
73 opt = ws.add_options()
74 opt.all_model_types = "cplex"
75
76 # instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
77 mi.instantiate("transport use lp min z", GamsModifier(bmult), opt)
78
79 bmult.add_record().value = 1.0
80 bmult_list = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
81
82 for b in bmult_list:
83 bmult.first_record().value = b
84 mi.solve()
85 print(f"Scenario bmult={b}:")
86 print(f" Modelstatus: {mi.model_status}")
87 print(f" Solvestatus: {mi.solver_status}")
88 print(f" Obj: {mi.sync_db['z'].first_record().level}")
89
90 # create a GamsModelInstance and solve it with single links in the network blocked
91 mi = cp.add_modelinstance()
92 x = mi.sync_db.add_variable("x", 2, VarType.Positive)
93 xup = mi.sync_db.add_parameter("xup", 2, "upper bound on x")
94
95 # instantiate the GamsModelInstance and pass a model definition and GamsModifier to declare upper bound of x mutable
96 mi.instantiate("transport use lp min z", GamsModifier(x, UpdateAction.Upper, xup))
97 mi.solve()
98
99 for i in job.out_db["i"]:
100 for j in job.out_db["j"]:
101 xup.clear()
102 xup.add_record((i.key(0), j.key(0))).value = 0
103 mi.solve()
104 print(f"Scenario link blocked: {i.key(0)} - {j.key(0)}")
105 print(f" Modelstatus: {mi.model_status}")
106 print(f" Solvestatus: {mi.solver_status}")
107 print(f" Obj: {mi.sync_db['z'].find_record().level}")