Loading...
Searching...
No Matches
transport11.py
Go to the documentation of this file.
6
7import os
8import sys
9from gams import GamsWorkspace, Action
10
11GAMS_BASE_MODEL = """
12$onEmpty
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' / 0 /;
23
24Parameter c(i,j) 'transport cost in thousands of dollars per case';
25c(i,j) = f*d(i,j)/1000;
26
27Variable
28 x(i,j) 'shipment quantities in cases'
29 z 'total transportation costs in thousands of dollars';
30
31Positive Variable x;
32
33Equations
34 cost 'define objective function'
35 supply(i) 'observe supply limit at plant i'
36 demand(j) 'satisfy demand at market j';
37
38cost.. z =e= sum((i,j), c(i,j)*x(i,j));
39
40supply(i).. sum(j, x(i,j)) =l= a(i);
41
42demand(j).. sum(i, x(i,j)) =g= b(j);
43
44Model transport /all/;
45
46solve transport using lp minimizing z;
47"""
48
49GAMS_MODEL = """
50$if not set gdxincname $abort 'no include file name for data file provided'
51$gdxIn %gdxincname%
52$onMulti
53$load i j a b d f
54$gdxIn
55
56display x.l, x.m;
57"""
58
59
60def create_save_restart(sys_dir, cp_file_name):
61 ws = GamsWorkspace(os.path.dirname(cp_file_name), sys_dir)
62 job_1 = ws.add_job_from_string(GAMS_BASE_MODEL)
63 opt = ws.add_options()
64 opt.action = Action.CompileOnly
65 cp = ws.add_checkpoint(os.path.basename(cp_file_name))
66 job_1.run(opt, cp)
67
68
69if __name__ == "__main__":
70 sys_dir = sys.argv[1] if len(sys.argv) > 1 else None
71 working_dir = os.path.join(os.curdir, "tmp")
72 ws = GamsWorkspace(working_dir, sys_dir)
73
74 # Create a save/restart file usually supplied by an application provider
75 # We create it for demonstration purpose
76 create_save_restart(sys_dir, os.path.join(working_dir, "tbase"))
77
78 plants = ["Seattle", "San-Diego"]
79
80 markets = ["New-York", "Chicago", "Topeka"]
81
82 capacity = {"Seattle": 350.0, "San-Diego": 600.0}
83
84 demand = {"New-York": 325.0, "Chicago": 300.0, "Topeka": 275.0}
85
86 distance = {
87 ("Seattle", "New-York"): 2.5,
88 ("Seattle", "Chicago"): 1.7,
89 ("Seattle", "Topeka"): 1.8,
90 ("San-Diego", "New-York"): 2.5,
91 ("San-Diego", "Chicago"): 1.8,
92 ("San-Diego", "Topeka"): 1.4,
93 }
94
95 db = ws.add_database()
96
97 # prepare a GamsDatabase with data from the Python data structures
98 i = db.add_set("i", 1, "canning plants")
99 for p in plants:
100 i.add_record(p)
101
102 j = db.add_set("j", 1, "markets")
103 for m in markets:
104 j.add_record(m)
105
106 a = db.add_parameter_dc("a", [i], "capacity of plant i in cases")
107 for p in plants:
108 a.add_record(p).value = capacity[p]
109
110 b = db.add_parameter_dc("b", [j], "demand at market j in cases")
111 for m in markets:
112 b.add_record(m).value = demand[m]
113
114 d = db.add_parameter_dc("d", [i, j], "distance in thousands of miles")
115 for k, v in distance.items():
116 d.add_record(k).value = v
117
118 f = db.add_parameter("f", 0, "freight in dollars per case per thousand miles")
119 f.add_record().value = 90
120
121 # run a job using data from the created GamsDatabase
122 cp_base = ws.add_checkpoint("tbase")
123 job = ws.add_job_from_string(GAMS_MODEL, cp_base)
124 opt = ws.add_options()
125 opt.defines["gdxincname"] = db.name
126 opt.all_model_types = "xpress"
127 job.run(opt, databases=db)
128
129 for rec in job.out_db["x"]:
130 print(
131 f"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
132 )
GamsWorkspace a
Definition: transport11.py:106
def create_save_restart(sys_dir, cp_file_name)
Definition: transport11.py:60
GamsWorkspace j
Definition: transport11.py:102
GamsWorkspace b
Definition: transport11.py:110
GamsWorkspace d
Definition: transport11.py:114
GamsWorkspace i
Definition: transport11.py:98