Loading...
Searching...
No Matches
transport14.py
Go to the documentation of this file.
6
7import sys
8from threading import Lock, Thread
9from gams import GamsWorkspace
10
11
12class Optimizer():
13 def __init__(self, system_directory=None):
14 self._ws = GamsWorkspace(system_directory=system_directory)
15
16 def solve(self, bmult, lock):
17 lock.acquire()
18 db = self._ws.add_database()
19 lock.release()
20 f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
21 f.add_record().value = 90 * bmult
22 job = self._ws.add_job_from_string(Optimizer.GAMS_MODEL)
23 opt = self._ws.add_options()
24 opt.defines["gdxincname"] = db.name
25 job.run(opt, databases=db)
26 return job.out_db["z"].first_record().level
27
28 GAMS_MODEL = """
29Set
30 i 'canning plants' / seattle, san-diego /
31 j 'markets' / new-york, chicago, topeka /;
32
33Parameter
34 a(i) 'capacity of plant i in cases'
35 / seattle 350
36 san-diego 600 /
37
38 b(j) 'demand at market j in cases'
39 / new-york 325
40 chicago 300
41 topeka 275 /;
42
43Table d(i,j) 'distance in thousands of miles'
44 new-york chicago topeka
45 seattle 2.5 1.7 1.8
46 san-diego 2.5 1.8 1.4;
47
48Scalar f 'freight in dollars per case per thousand miles';
49
50$if not set gdxincname $abort 'no include file name for data file provided'
51$gdxIn %gdxincname%
52$load f
53$gdxIn
54
55Parameter c(i,j) 'transport cost in thousands of dollars per case';
56c(i,j) = f*d(i,j)/1000;
57
58Variable
59 x(i,j) 'shipment quantities in cases'
60 z 'total transportation costs in thousands of dollars';
61
62Positive Variable x;
63
64Equations
65 cost 'define objective function'
66 supply(i) 'observe supply limit at plant i'
67 demand(j) 'satisfy demand at market j';
68
69cost.. z =e= sum((i,j), c(i,j)*x(i,j));
70
71supply(i).. sum(j, x(i,j)) =l= a(i);
72
73demand(j).. sum(i, x(i,j)) =g= b(j);
74
75Model transport /all/;
76
77solve transport using lp minimizing z;
78
79display x.l, x.m;
80"""
81
82
83if __name__ == "__main__":
84 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
85 optimizer = Optimizer(sys_dir)
86 bmult_list = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
87 lock = Lock()
88
89 def run_scenario(optimizer, bmult, lock):
90 obj = optimizer.solve(bmult, lock)
91 lock.acquire()
92 print(f"Scenario bmult={bmult}, Obj:{obj}")
93 lock.release()
94
95 for bmult in bmult_list:
96 t = Thread(target=run_scenario, args=(optimizer, bmult, lock))
97 t.start()
def __init__(self, system_directory=None)
Definition: transport14.py:13
def solve(self, bmult, lock)
Definition: transport14.py:16
def run_scenario(optimizer, bmult, lock)
Definition: transport14.py:89