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