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