transport8.py
Go to the documentation of this file.
9 
10 from gams import *
11 import threading
12 import sys
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(workspace, checkpoint, bmult_list, list_lock, io_lock):
65  list_lock.acquire()
66  mi = checkpoint.add_modelinstance()
67  list_lock.release()
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 threads at creation time
78  list_lock.acquire()
79  if 0 == len(bmult_list):
80  list_lock.release()
81  return
82  b = bmult_list.pop()
83  list_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()
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 
107  bmult_list = [ 1.3, 1.2, 1.1, 1.0, 0.9, 0.8, 0.7, 0.6 ]
108 
109  # solve multiple model instances in parallel
110  list_lock = threading.Lock()
111  io_lock = threading.Lock()
112 
113  # start 2 threads
114  nr_workers = 2
115  threads = {}
116  for i in range(nr_workers):
117  threads[i] = threading.Thread(target=scen_solve, args=(ws, cp, bmult_list, list_lock, io_lock))
118  threads[i].start()
119  for i in range(nr_workers):
120  threads[i].join()
121 
122 
123 
def get_model_text()
Definition: transport8.py:14