9from gams
import GamsWorkspace
17 a(i)
'capacity of plant i in cases'
18 b(j)
'demand at market j in cases'
19 d(i,j)
'distance in thousands of miles';
21Scalar f
'freight in dollars per case per thousand miles';
23$
if not set gdxincname $abort
'no include file name for data file provided'
28Parameter c(i,j)
'transport cost in thousands of dollars per case';
29c(i,j) = f*
d(i,j)/1000;
32 x(i,j)
'shipment quantities in cases'
33 z
'total transportation costs in thousands of dollars';
38 cost
'define objective function'
39 supply(i)
'observe supply limit at plant i'
40 demand(j)
'satisfy demand at market j';
42cost.. z =e= sum((i,j), c(i,j)*x(i,j));
44supply(i).. sum(j, x(i,j)) =l=
a(i);
46demand(j).. sum(i, x(i,j)) =g=
b(j);
50solve transport using lp minimizing z;
55if __name__ == "__main__":
56 sys_dir = sys.argv[1] if len(sys.argv) > 1
else None
57 ws = GamsWorkspace(system_directory=sys_dir)
59 plants = [
"Seattle",
"San-Diego"]
60 markets = [
"New-York",
"Chicago",
"Topeka"]
61 capacity = {
"Seattle": 350.0,
"San-Diego": 600.0}
62 demand = {
"New-York": 325.0,
"Chicago": 300.0,
"Topeka": 275.0}
64 (
"Seattle",
"New-York"): 2.5,
65 (
"Seattle",
"Chicago"): 1.7,
66 (
"Seattle",
"Topeka"): 1.8,
67 (
"San-Diego",
"New-York"): 2.5,
68 (
"San-Diego",
"Chicago"): 1.8,
69 (
"San-Diego",
"Topeka"): 1.4,
72 db = ws.add_database()
74 i = db.add_set(
"i", 1,
"canning plants")
78 j = db.add_set(
"j", 1,
"markets")
82 a = db.add_parameter_dc(
"a", [i],
"capacity of plant i in cases")
84 a.add_record(p).value = capacity[p]
86 b = db.add_parameter_dc(
"b", [j],
"demand at market j in cases")
88 b.add_record(m).value = demand[m]
90 d = db.add_parameter_dc(
"d", [i, j],
"distance in thousands of miles")
91 for k, v
in distance.items():
92 d.add_record(k).value = v
94 f = db.add_parameter(
"f", 0,
"freight in dollars per case per thousand miles")
95 f.add_record().value = 90
97 job = ws.add_job_from_string(GAMS_MODEL)
98 opt = ws.add_options()
99 opt.defines[
"gdxincname"] = db.name
100 opt.all_model_types =
"xpress"
101 job.run(opt, databases=db)
102 for rec
in job.out_db[
"x"]:
104 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"