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