11from gams
import GamsWorkspace, GamsModifier
12from multiprocessing
import Lock, Process, Queue
16 i canning plants / seattle, san-diego /
17 j markets / new-york, chicago, topeka / ;
20 a(i) capacity of plant i in cases
24 b(j) demand at market j
in cases
29Table d(i,j) distance
in thousands of miles
30 new-york chicago topeka
32 san-diego 2.5 1.8 1.4 ;
34Scalar f freight
in dollars per case per thousand miles /90/ ;
35Scalar bmult demand multiplier /1/;
37Parameter c(i,j) transport cost
in thousands of dollars per case ;
38 c(i,j) = f * d(i,j) / 1000 ;
41 x(i,j) shipment quantities
in cases
42 z total transportation costs
in thousands of dollars ;
47 cost define objective function
48 supply(i) observe supply limit at plant i
49 demand(j) satisfy demand at market j ;
51cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
53supply(i) .. sum(j, x(i,j)) =l= a(i) ;
55demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
57Model transport /all/ ;
61def scen_solve(cp_file, bmult_queue, queue_lock, io_lock):
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"
70 mi.instantiate(
"transport use lp min z", GamsModifier(bmult), opt)
71 bmult.add_record().value = 1.0
76 if bmult_queue.empty():
81 bmult.first_record().value = b
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))
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()
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"
103 bmult_queue = Queue()
104 bmult = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
114 for i
in range(nr_workers):
115 processes[i] = Process(
116 target=scen_solve, args=(cp_file, bmult_queue, queue_lock, io_lock)
119 for i
in range(nr_workers):