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