Loading...
Searching...
No Matches
transport10.py
Go to the documentation of this file.
1
6
7import os
8import sys
9from gams import GamsWorkspace
10from openpyxl import load_workbook
11
12GAMS_MODEL = """
13Set
14 i 'canning plants'
15 j 'markets';
16
17Parameter
18 a(i) 'capacity of plant i in cases'
19 b(j) 'demand at market j in cases'
20 d(i,j) 'distance in thousands of miles';
21
22Scalar f 'freight in dollars per case per thousand miles' / 90 /;
23
24$if not set gdxincname $abort 'no include file name for data file provided'
25$gdxIn %gdxincname%
26$load i j a b d
27$gdxIn
28
29Parameter c(i,j) 'transport cost in thousands of dollars per case';
30c(i,j) = f*d(i,j)/1000;
31
32Variable
33 x(i,j) 'shipment quantities in cases'
34 z 'total transportation costs in thousands of dollars';
35
36Positive Variable x;
37
38Equations
39 cost 'define objective function'
40 supply(i) 'observe supply limit at plant i'
41 demand(j) 'satisfy demand at market j';
42
43cost.. z =e= sum((i,j), c(i,j)*x(i,j));
44
45supply(i).. sum(j, x(i,j)) =l= a(i);
46
47demand(j).. sum(i, x(i,j)) =g= b(j);
48
49Model transport /all/;
50
51solve transport using lp minimizing z;
52
53display x.l, x.m;
54"""
55
56if __name__ == "__main__":
57 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
58 work_dir = sys.argv[2] if len(sys.argv) > 2 else None
59 ws = GamsWorkspace(system_directory=sys_dir, working_directory=work_dir)
60
61 wb = load_workbook(
62 os.path.join(ws.system_directory, "apifiles", "Data", "transport.xlsx")
63 )
64 capacity = wb["capacity"]
65 demand = wb["demand"]
66 distance = wb["distance"]
67
68 # number of markets/plants have to be the same in all spreadsheets
69 if (
70 distance.max_column - 1 != demand.max_column
71 or distance.max_row - 1 != capacity.max_row
72 ):
73 raise Exception("Size of the spreadsheets doesn't match")
74
75 db = ws.add_database()
76 i = db.add_set("i", 1, "Plants")
77 j = db.add_set("j", 1, "Markets")
78 capacity_param = db.add_parameter_dc("a", [i], "Capacity")
79 demand_param = db.add_parameter_dc("b", [j], "Demand")
80 distance_param = db.add_parameter_dc("d", [i, j], "Distance")
81
82 for c in capacity.iter_cols():
83 key = c[0].value
84 i.add_record(key)
85 capacity_param.add_record(key).value = c[1].value
86
87 for c in demand.iter_cols():
88 key = c[0].value
89 j.add_record(key)
90 demand_param.add_record(key).value = c[1].value
91
92 for c in range(2, distance.max_column + 1):
93 for r in range(2, distance.max_row + 1):
94 keys = (
95 distance.cell(row=r, column=1).value,
96 distance.cell(row=1, column=c).value,
97 )
98 v = distance.cell(row=r, column=c).value
99 distance_param.add_record(keys).value = v
100
101 # create and run the GamsJob
102 job = ws.add_job_from_string(GAMS_MODEL)
103 opt = ws.add_options()
104 opt.defines["gdxincname"] = db.name
105 opt.all_model_types = "xpress"
106 job.run(opt, databases=db)
107 for rec in job.out_db["x"]:
108 print(
109 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
110 )