8from gams
import GamsWorkspace, SolveLink
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';
20Scalar f 'freight in dollars per case per thousand miles';
22$if not set dbIn1 $abort 'no file name for in-database 1 file provided'
27$if not set dbIn2 $abort 'no file name for in-database 2 file provided'
32Parameter c(i,j) 'transport cost in thousands of dollars per case';
33c(i,j) = f*d(i,j)/1000;
36 x(i,j) 'shipment quantities in cases'
37 z 'total transportation costs in thousands of dollars';
42 cost 'define objective function'
43 supply(i) 'observe supply limit at plant i'
44 demand(j) 'satisfy demand at market j';
46cost.. z =e= sum((i,j), c(i,j)*x(i,j));
48supply(i).. sum(j, x(i,j)) =l= a(i);
50demand(j).. sum(i, x(i,j)) =g= b(j);
54solve transport using lp minimizing z;
56$if not set dbOut1 $abort 'no file name for out-database 1 file provided'
57execute_unload '%dbOut1%', x, z;
62 def __init__(self, system_directory, working_directory):
63 self._ws = GamsWorkspace(
64 system_directory=system_directory, working_directory=working_directory
66 self.opt = self._ws.add_options()
68 self._dbin1 = self._ws.add_database(in_model_name=
"dbIn1")
69 self._dbin2 = self._ws.add_database(in_model_name=
"dbIn2")
71 self.opt.solvelink = SolveLink.LoadLibrary
72 self.opt.all_model_types =
"Cplex"
73 self.opt.defines[
"dbOut1"] =
"dbOut1"
75 self.i = self._dbin1.add_set(
"i", 1,
"canning plants")
76 self.j = self._dbin1.add_set(
"j", 1,
"markets")
77 self.a = self._dbin1.add_parameter_dc(
78 "a", [self.i],
"capacity of plant i in cases"
80 self.b = self._dbin1.add_parameter_dc(
81 "b", [self.j],
"demand at market j in cases"
83 self.d = self._dbin1.add_parameter_dc(
84 "d", [self.i, self.j],
"distance in thousands of miles"
86 self.f = self._dbin2.add_parameter(
87 "f", 0,
"freight in dollars per case per thousand miles"
92 self._job = self._ws.add_job_from_string(GAMS_MODEL)
94 def run(self, checkpoint=None, output=None):
95 self._job.run(self.opt, checkpoint, output,
False, [self._dbin1, self._dbin2])
97 self._dbout1 = self._ws.add_database_from_gdx(
98 self.opt.defines[
"dbOut1"] +
".gdx"
100 self.x = self._dbout1[
"x"]
101 self.z = self._dbout1[
"z"]