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