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