Loading...
Searching...
No Matches
transport6.py
Go to the documentation of this file.
6
7import sys
8from threading import Lock, Thread
9from gams import GamsWorkspace
10
11GAMS_MODEL = """
12Set
13 i 'canning plants' / seattle, san-diego /
14 j 'markets' / new-york, chicago, topeka /;
15
16Parameter
17 a(i) 'capacity of plant i in cases'
18 / seattle 350
19 san-diego 600 /
20
21 b(j) 'demand at market j in cases'
22 / new-york 325
23 chicago 300
24 topeka 275 /;
25
26Table d(i,j) 'distance in thousands of miles'
27 new-york chicago topeka
28 seattle 2.5 1.7 1.8
29 san-diego 2.5 1.8 1.4;
30
31Scalar
32 f 'freight in dollars per case per thousand miles' / 90 /
33 bmult 'demand multiplier' / 1 /;
34
35Parameter c(i,j) 'transport cost in thousands of dollars per case';
36c(i,j) = f*d(i,j)/1000;
37
38Variable
39 x(i,j) 'shipment quantities in cases'
40 z 'total transportation costs in thousands of dollars';
41
42Positive Variable x ;
43
44Equations
45 cost 'define objective function'
46 supply(i) 'observe supply limit at plant i'
47 demand(j) 'satisfy demand at market j';
48
49cost.. z =e= sum((i,j), c(i,j)*x(i,j));
50
51supply(i).. sum(j, x(i,j)) =l= a(i);
52
53demand(j).. sum(i, x(i,j)) =g= bmult*b(j);
54
55Model transport /all/;
56
57Scalar ms 'model status', ss 'solve status';
58"""
59
60
61def run_scenario(workspace, checkpoint, io_lock, b):
62 job = workspace.add_job_from_string(
63 f"bmult={b}; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;",
64 checkpoint,
65 )
66 job.run()
67 # we need to make the ouput a critical section to avoid messed up report informations
68 io_lock.acquire()
69 print(f"Scenario bmult={b}:")
70 print(f" Modelstatus: {job.out_db['ms'].first_record().value}")
71 print(f" Solvestatus: {job.out_db['ss'].first_record().value}")
72 print(f" Obj: {job.out_db['z'].first_record().level}")
73 io_lock.release()
74
75
76if __name__ == "__main__":
77 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
78 ws = GamsWorkspace(system_directory=sys_dir)
79
80 cp = ws.add_checkpoint()
81
82 # initialize a GAMSCheckpoint by running a GAMSJob
83 job = ws.add_job_from_string(GAMS_MODEL)
84 job.run(checkpoint=cp)
85 bmult = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
86
87 # run multiple parallel jobs using the created GAMSCheckpoint
88 io_lock = Lock()
89 threads = {}
90 for b in bmult:
91 threads[b] = Thread(target=run_scenario, args=(ws, cp, io_lock, b))
92 threads[b].start()
93 for b in bmult:
94 threads[b].join()