9from gams
import GamsWorkspace, Action
14 i(*) 'canning plants' / /
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' / /;
22Scalar f
'freight in dollars per case per thousand miles' / 0 /;
24Parameter c(i,j)
'transport cost in thousands of dollars per case';
25c(i,j) = f*
d(i,j)/1000;
28 x(i,j)
'shipment quantities in cases'
29 z
'total transportation costs in thousands of dollars';
34 cost
'define objective function'
35 supply(i)
'observe supply limit at plant i'
36 demand(j)
'satisfy demand at market j';
38cost.. z =e= sum((i,j), c(i,j)*x(i,j));
40supply(i).. sum(j, x(i,j)) =l=
a(i);
42demand(j).. sum(i, x(i,j)) =g=
b(j);
46solve transport using lp minimizing z;
50$
if not set gdxincname $abort
'no include file name for data file provided'
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))
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)
78 plants = [
"Seattle",
"San-Diego"]
80 markets = [
"New-York",
"Chicago",
"Topeka"]
82 capacity = {
"Seattle": 350.0,
"San-Diego": 600.0}
84 demand = {
"New-York": 325.0,
"Chicago": 300.0,
"Topeka": 275.0}
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,
95 db = ws.add_database()
98 i = db.add_set(
"i", 1,
"canning plants")
102 j = db.add_set(
"j", 1,
"markets")
106 a = db.add_parameter_dc(
"a", [i],
"capacity of plant i in cases")
108 a.add_record(p).value = capacity[p]
110 b = db.add_parameter_dc(
"b", [j],
"demand at market j in cases")
112 b.add_record(m).value = demand[m]
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
118 f = db.add_parameter(
"f", 0,
"freight in dollars per case per thousand miles")
119 f.add_record().value = 90
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)
129 for rec
in job.out_db[
"x"]:
131 f
"x({rec.key(0)},{rec.key(1)}): level={rec.level} marginal={rec.marginal}"
def create_save_restart(sys_dir, cp_file_name)