transport8a.py
Go to the documentation of this file.
9
10from __future__ import print_function
11from gams import *
12from multiprocessing import Lock, Process, Queue
13import sys
14import os
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(cp_file, bmult_queue, queue_lock, io_lock):
67 ws = GamsWorkspace()
70 bmult = mi.sync_db.add_parameter("bmult", 0, "demand multiplier")
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)
77
78 while True:
79 # dynamically get a bmult value from the queue instead of passing it to the different processes at creation time
80 queue_lock.acquire()
81 if bmult_queue.empty():
82 queue_lock.release()
83 return
84 b = bmult_queue.get()
85 queue_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(debug=DebugLevel.KeepFiles)
102
104
105 # initialize a GamsCheckpoint by running a GamsJob
107 t8.run(checkpoint=cp)
108 cp_file = os.path.join(ws.working_directory, cp.name) + ".g00"
109
110 bmult_queue = Queue()
111 bmult_data = [ 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 ]
112 for bmult in bmult_data:
113 bmult_queue.put(bmult)
114
115 # solve multiple model instances in parallel
116 io_lock = Lock()
117 queue_lock = Lock()
118
119 nr_workers = 4
120 processes = {}
121 for i in range(nr_workers):
122 processes[i] = Process(target=scen_solve, args=(cp_file, bmult_queue, queue_lock, io_lock))
123 processes[i].start()
124 for i in range(nr_workers):
125 processes[i].join()
126
127
def get_model_text()
Definition: transport8a.py:16