Loading...
Searching...
No Matches
transport_engine.m
1function transportEngine(varargin)
2
3 % check workspace info from arguments
4 if nargin > 0
5 wsInfo = gams.control.WorkspaceInfo();
6 wsInfo.systemDirectory = varargin{1};
7 ws = gams.control.Workspace(wsInfo);
8 else
9 ws = gams.control.Workspace();
10 end
11
12 % read Engine configuration from environment variables
13 engine_url = getenv('ENGINE_URL');
14 if isempty(engine_url)
15 error('environment variable ENGINE_URL not set');
16 end
17 engine_user = getenv('ENGINE_USER');
18 if isempty(engine_user)
19 error('environment variable ENGINE_USER not set');
20 end
21 engine_password = getenv('ENGINE_PASSWORD');
22 if isempty(engine_password)
23 error('environment variable ENGINE_PASSWORD not set');
24 end
25 engine_namespace = getenv('ENGINE_NAMESPACE');
26 if isempty(engine_namespace)
27 error('environment variable ENGINE_NAMESPACE not set');
28 end
29
30 % set up configuration required for any job on GAMS Engine
31 engine_configuration = gams.control.engine.Configuration(engine_url, engine_user, ...
32 engine_password, "", engine_namespace);
33
34 output = gams.control.PrintStream();
35
36 runEngineJob1(ws, engine_configuration, output);
37 runEngineJob2(ws, engine_configuration, output);
38 runEngineJob3(ws, engine_configuration, output);
39end
40
41function runEngineJob1(ws, engine_configuration, output)
42 % Run with data from a string with GAMS syntax with explicit export to GDX file
43
44 job = ws.addJobFromString(data());
45
46 engine_run_parameters = gams.control.engine.RunParameters();
47 engine_run_parameters.configuration = engine_configuration;
48 engine_run_parameters.output = output;
49
50 output.println('Running data from string...');
51 job.runEngine(engine_run_parameters);
52 job.outDB.export('tdata.gdx');
53end
54
55function runEngineJob2(ws, engine_configuration, output)
56 % Run a job using an instance of Options that defines the data include file
57
58 job = ws.addJobFromString(model());
59 opt = ws.addOptions();
60 opt.defines('gdxincname', 'tdata');
61 opt.setAllModelTypes('xpress');
62
63 engine_run_parameters = gams.control.engine.RunParameters();
64 engine_run_parameters.configuration = engine_configuration;
65 engine_run_parameters.output = output;
66 engine_run_parameters.options = opt;
67 engine_run_parameters.extraModelFiles = {"tdata.gdx"};
68 engine_run_parameters.engineOptions = containers.Map(...
69 {'inex_string'}, {'{"type": "include", "files": ["*.gdx"]}'});
70
71 output.println('Running using an instance of Options that defines the data include file...');
72 job.runEngine(engine_run_parameters);
73
74 % retrieve Variable 'x' from Job's output databases
75 for x = job.outDB.getVariable('x').records
76 fprintf('x(%s,%s): level=%g marginal=%g\n', x{1}.keys{:}, x{1}.level, x{1}.marginal);
77 end
78end
79
80function runEngineJob3(ws, engine_configuration, output)
81 % Same as 2 but with implicit database communication
82
83 job1 = ws.addJobFromString(data());
84
85 engine_run_parameters = gams.control.engine.RunParameters();
86 engine_run_parameters.configuration = engine_configuration;
87 engine_run_parameters.output = output;
88
89 output.println('Running data from string...');
90 job1.runEngine(engine_run_parameters);
91
92 job2 = ws.addJobFromString(model());
93 cp = ws.addCheckpoint();
94 opt = ws.addOptions();
95 opt.defines('gdxincname', job1.outDB.name);
96 opt.setAllModelTypes('xpress');
97
98 engine_run_parameters = gams.control.engine.RunParameters();
99 engine_run_parameters.configuration = engine_configuration;
100 engine_run_parameters.output = output;
101 engine_run_parameters.options = opt;
102 engine_run_parameters.checkpoint = cp;
103 engine_run_parameters.databases = {job1.outDB};
104
105 output.println('Running model from a string with checkpoint implicit database communication...');
106 job2.runEngine(engine_run_parameters);
107
108 % retrieve Variable 'x' from Job's output databases
109 for x = job2.outDB.getVariable('x').records
110 fprintf('x(%s,%s): level=%g marginal=%g\n', x{1}.keys{:}, x{1}.level, x{1}.marginal);
111 end
112end
113
114function data = data()
115 data = {
116 'Sets '
117 ' i canning plants / seattle, san-diego / '
118 ' j markets / new-york, chicago, topeka / ; '
119 'Parameters '
120 ' '
121 ' a(i) capacity of plant i in cases '
122 ' / seattle 350 '
123 ' san-diego 600 / '
124 ' '
125 ' b(j) demand at market j in cases '
126 ' / new-york 325 '
127 ' chicago 300 '
128 ' topeka 275 / ; '
129 ' '
130 'Table d(i,j) distance in thousands of miles '
131 ' new-york chicago topeka '
132 ' seattle 2.5 1.7 1.8 '
133 ' san-diego 2.5 1.8 1.4 ; '
134 ' '
135 'Scalar f freight in dollars per case per thousand miles /90/ '
136 ' bmult demand multiplier /1/; '};
137 data = sprintf('%s\n', data{:});
138end
139
140function model = model()
141 model = {
142 'Sets '
143 ' i canning plants '
144 ' j markets '
145 ' '
146 ' Parameters '
147 ' a(i) capacity of plant i in cases '
148 ' b(j) demand at market j in cases '
149 ' d(i,j) distance in thousands of miles '
150 ' '
151 ' Scalar f freight in dollars per case per thousand miles; '
152 ' Scalar bmult demand multiplier; '
153 ' '
154 '$if not set gdxincname $abort ''no include file name for data file provided'''
155 '$gdxin %gdxincname% '
156 '$load i j a b d f bmult '
157 '$gdxin '
158 ' '
159 ' Parameter c(i,j) transport cost in thousands of dollars per case ; '
160 ' c(i,j) = f * d(i,j) / 1000 ; '
161 ' '
162 ' Variables '
163 ' x(i,j) shipment quantities in cases '
164 ' z total transportation costs in thousands of dollars ; '
165 ' '
166 ' Positive Variable x ; '
167 ' '
168 ' Equations '
169 ' cost define objective function '
170 ' supply(i) observe supply limit at plant i '
171 ' demand(j) satisfy demand at market j ; '
172 ' '
173 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; '
174 ' '
175 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ; '
176 ' '
177 ' demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; '
178 ' '
179 ' Model transport /all/ ; '
180 ' '
181 ' Solve transport using lp minimizing z ; '
182 ' '
183 'Scalar '
184 ' ms model status '
185 ' ss solve status; '
186 ' '
187 ' Display x.l, x.m ; '};
188 model = sprintf('%s\n', model{:});
189end