transport8.py
Go to the documentation of this file.
10
11from __future__ import print_function
12from gams import *
13import threading
14import sys
15
17 return '''
18 Sets
19 i canning plants / seattle, san-diego /
20 j markets / new-york, chicago, topeka / ;
21
22 Parameters
23
24 a(i) capacity of plant i in cases
25 / seattle 350
26 san-diego 600 /
27
28 b(j) demand at market j in cases
29 / new-york 325
30 chicago 300
31 topeka 275 / ;
32
33 Table d(i,j) distance in thousands of miles
34 new-york chicago topeka
35 seattle 2.5 1.7 1.8
36 san-diego 2.5 1.8 1.4 ;
37
38 Scalar f freight in dollars per case per thousand miles /90/ ;
39 Scalar bmult demand multiplier /1/;
40
41 Parameter c(i,j) transport cost in thousands of dollars per case ;
42
43 c(i,j) = f * d(i,j) / 1000 ;
44
45 Variables
46 x(i,j) shipment quantities in cases
47 z total transportation costs in thousands of dollars ;
48
49 Positive Variable x ;
50
51 Equations
52 cost define objective function
53 supply(i) observe supply limit at plant i
54 demand(j) satisfy demand at market j ;
55
56 cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
57
58 supply(i) .. sum(j, x(i,j)) =l= a(i) ;
59
60 demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
61
62 Model transport /all/ ;
63'''
64
65
66def scen_solve(workspace, checkpoint, bmult_list, list_lock, io_lock):
67 list_lock.acquire()
68 mi = checkpoint.add_modelinstance()
69 list_lock.release()
70 bmult = mi.sync_db.add_parameter("bmult", 0, "demand multiplier")
71 opt = ws.add_options()
72 opt.all_model_types = "cplexd"
73
74 # instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
75 mi.instantiate("transport use lp min z", GamsModifier(bmult), opt)
76 bmult.add_record().value = 1.0
77
78 while True:
79 # dynamically get a bmult value from the queue instead of passing it to the different threads at creation time
80 list_lock.acquire()
81 if 0 == len(bmult_list):
82 list_lock.release()
83 return
84 b = bmult_list.pop()
85 list_lock.release()
86 bmult.first_record().value = b
87 mi.solve()
88
89 # we need to make the ouput a critical section to avoid messed up report informations
90 io_lock.acquire()
91 print("Scenario bmult=" + str(b) + ":")
92 print(" Modelstatus: " + str(mi.model_status))
93 print(" Solvestatus: " + str(mi.solver_status))
94 print(" Obj: " + str(mi.sync_db.get_variable("z").find_record().level))
95 io_lock.release()
96
97if __name__ == "__main__":
98 if len(sys.argv) > 1:
99 ws = GamsWorkspace(system_directory = sys.argv[1])
100 else:
101 ws = GamsWorkspace()
102
103 cp = ws.add_checkpoint()
104
105 # initialize a GAMSCheckpoint by running a GAMSJob
106 t8 = ws.add_job_from_string(get_model_text())
107 t8.run(checkpoint=cp)
108
109 bmult_list = [ 1.3, 1.2, 1.1, 1.0, 0.9, 0.8, 0.7, 0.6 ]
110
111 # solve multiple model instances in parallel
112 list_lock = threading.Lock()
113 io_lock = threading.Lock()
114
115 # start 2 threads
116 nr_workers = 2
117 threads = {}
118 for i in range(nr_workers):
119 threads[i] = threading.Thread(target=scen_solve, args=(ws, cp, bmult_list, list_lock, io_lock))
120 threads[i].start()
121 for i in range(nr_workers):
122 threads[i].join()
123
124
125
def get_model_text()
Definition: transport8.py:16