transport_class.py
Go to the documentation of this file.
7 
8 from gams import *
9 
10 class Transport():
11 
12  def __init__(self, ws):
13 
14  self._ws = ws
15  self.opt = ws.add_options()
16 
17  self._dbin1 = ws.add_database(in_model_name = "dbIn1")
18  self._dbin2 = ws.add_database(in_model_name = "dbIn2")
19 
20  self.opt.solvelink = SolveLink.LoadLibrary
21  self.opt.all_model_types = "Cplex"
22  self.opt.defines["dbOut1"] = "dbOut1"
23 
24  self.i = self._dbin1.add_set("i", 1, "canning plants")
25  self.j = self._dbin1.add_set("j", 1, "markets")
26  self.a = self._dbin1.add_parameter_dc("a", [self.i], "capacity of plant i in cases")
27  self.b = self._dbin1.add_parameter_dc("b", [self.j], "demand at market j in cases")
28  self.d = self._dbin1.add_parameter_dc("d", [self.i, self.j], "distance in thousands of miles")
29  self.f = self._dbin2.add_parameter("f", 0, "freight in dollars per case per thousand miles")
30  self.x = None
31  self.z = None
32 
33 
34 
35  self._job = ws.add_job_from_string(self.get_model_source())
36 
37  def get_model_source(self):
38  return '''
39  Sets
40  i canning plants
41  j markets
42 
43  Parameters
44  a(i) capacity of plant i in cases
45  b(j) demand at market j in cases
46  d(i,j) distance in thousands of miles
47  Scalar f freight in dollars per case per thousand miles;
48 
49 $if not set dbIn1 $abort 'no file name for in-database 1 file provided'
50 $gdxin %dbIn1%
51 $load i j a b d
52 
53 $if not set dbIn2 $abort 'no file name for in-database 2 file provided'
54 $gdxin %dbIn2%
55 $load f
56 $gdxin
57 
58  Parameter c(i,j) transport cost in thousands of dollars per case ;
59 
60  c(i,j) = f * d(i,j) / 1000 ;
61 
62  Variables
63  x(i,j) shipment quantities in cases
64  z total transportation costs in thousands of dollars ;
65 
66  Positive Variable x ;
67 
68  Equations
69  cost define objective function
70  supply(i) observe supply limit at plant i
71  demand(j) satisfy demand at market j ;
72 
73  cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
74 
75  supply(i) .. sum(j, x(i,j)) =l= a(i) ;
76 
77  demand(j) .. sum(i, x(i,j)) =g= b(j) ;
78 
79  Model transport /all/ ;
80 
81  Solve transport using lp minimizing z ;
82 
83 $if not set dbOut1 $abort 'no file name for out-database 1 file provided'
84  execute_unload '%dbOut1%', x, z;
85 '''
86 
87  def run(self, checkpoint=None, output=None):
88  self._job.run(self.opt, checkpoint, output, False, [self._dbin1, self._dbin2])
89 
90  self._dbout1 = self._ws.add_database_from_gdx(self.opt.defines["dbOut1"] + ".gdx")
91  self.x = self._dbout1.get_variable("x")
92  self.z = self._dbout1.get_variable("z")
def run(self, checkpoint=None, output=None)