transport6.py
Go to the documentation of this file.
7
8from __future__ import print_function
9from gams import *
10import threading
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 Scalar ms 'model status', ss 'solve status'; '''
61
62
63def run_scenario(workspace, checkpoint, io_lock, b):
64 t6 = workspace.add_job_from_string("bmult=" + str(b) + "; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;", checkpoint)
65 t6.run()
66 # we need to make the ouput a critical section to avoid messed up report informations
67 io_lock.acquire()
68 print("Scenario bmult=" + str(b) + ":")
69 print(" Modelstatus: " + str(t6.out_db["ms"][()].value))
70 print(" Solvestatus: " + str(t6.out_db["ss"][()].value))
71 print(" Obj: " + str(t6.out_db["z"][()].level))
72 io_lock.release()
73
74
75if __name__ == "__main__":
76 if len(sys.argv) > 1:
77 ws = GamsWorkspace(system_directory = sys.argv[1])
78 else:
79 ws = GamsWorkspace()
80
81 cp = ws.add_checkpoint()
82
83 # initialize a GAMSCheckpoint by running a GAMSJob
84 t6 = ws.add_job_from_string(get_model_text())
85 t6.run(checkpoint=cp)
86 bmultlist = [ 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 ]
87
88 # run multiple parallel jobs using the created GAMSCheckpoint
89 io_lock = threading.Lock()
90 threads = {}
91 for b in bmultlist:
92 threads[b] = threading.Thread(target=run_scenario, args=(ws, cp, io_lock, b))
93 threads[b].start()
94 for b in bmultlist:
95 threads[b].join()
96
97
def get_model_text()
Definition: transport6.py:13