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 work_dir = sys.argv[2]
if len(sys.argv) > 2
else None
58 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
60 plants = [
"Seattle",
"San-Diego"]
61 markets = [
"New-York",
"Chicago",
"Topeka"]
62 capacity = {
"Seattle": 350.0,
"San-Diego": 600.0}
63 demand = {
"New-York": 325.0,
"Chicago": 300.0,
"Topeka": 275.0}
65 (
"Seattle",
"New-York"): 2.5,
66 (
"Seattle",
"Chicago"): 1.7,
67 (
"Seattle",
"Topeka"): 1.8,
68 (
"San-Diego",
"New-York"): 2.5,
69 (
"San-Diego",
"Chicago"): 1.8,
70 (
"San-Diego",
"Topeka"): 1.4,
73 db = ws.add_database()
75 i = db.add_set(
"i", 1,
"canning plants")
79 j = db.add_set(
"j", 1,
"markets")
83 a = db.add_parameter_dc(
"a", [i],
"capacity of plant i in cases")
85 a.add_record(p).value = capacity[p]
87 b = db.add_parameter_dc(
"b", [j],
"demand at market j in cases")
89 b.add_record(m).value = demand[m]
91 d = db.add_parameter_dc(
"d", [i, j],
"distance in thousands of miles")
92 for k, v
in distance.items():
93 d.add_record(k).value = v
95 f = db.add_parameter(
"f", 0,
"freight in dollars per case per thousand miles")
96 f.add_record().value = 90
98 job = ws.add_job_from_string(GAMS_MODEL)
99 opt = ws.add_options()
100 opt.defines[
"gdxincname"] = db.name
101 opt.all_model_types =
"xpress"
102 job.run(opt, databases=db)
103 for rec
in job.out_db[
"x"]:
105 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"