1function transportEngine(varargin)
3 % check workspace info from arguments
5 wsInfo = gams.control.WorkspaceInfo();
6 wsInfo.systemDirectory = varargin{1};
7 ws = gams.control.Workspace(wsInfo);
9 ws = gams.control.Workspace();
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');
17 engine_user = getenv(
'ENGINE_USER');
18 if isempty(engine_user)
19 error(
'environment variable ENGINE_USER not set');
21 engine_password = getenv(
'ENGINE_PASSWORD');
22 if isempty(engine_password)
23 error(
'environment variable ENGINE_PASSWORD not set');
25 engine_namespace = getenv(
'ENGINE_NAMESPACE');
26 if isempty(engine_namespace)
27 error(
'environment variable ENGINE_NAMESPACE not set');
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);
34 output = gams.control.PrintStream();
36 runEngineJob1(ws, engine_configuration, output);
37 runEngineJob2(ws, engine_configuration, output);
38 runEngineJob3(ws, engine_configuration, output);
41function runEngineJob1(ws, engine_configuration, output)
42 % Run with data from a
string with GAMS syntax with
explicit export to GDX file
44 job = ws.addJobFromString(data());
46 engine_run_parameters = gams.control.engine.RunParameters();
47 engine_run_parameters.configuration = engine_configuration;
48 engine_run_parameters.output = output;
50 output.println(
'Running data from string...');
51 job.runEngine(engine_run_parameters);
52 job.outDB.export(
'tdata.gdx');
55function runEngineJob2(ws, engine_configuration, output)
56 % Run a job
using an instance of Options that defines the data include file
58 job = ws.addJobFromString(model());
59 opt = ws.addOptions();
60 opt.defines(
'gdxincname',
'tdata');
61 opt.setAllModelTypes(
'xpress');
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"]}'});
71 output.println(
'Running using an instance of Options that defines the data include file...');
72 job.runEngine(engine_run_parameters);
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);
80function runEngineJob3(ws, engine_configuration, output)
81 % Same as 2 but with implicit database communication
83 job1 = ws.addJobFromString(data());
85 engine_run_parameters = gams.control.engine.RunParameters();
86 engine_run_parameters.configuration = engine_configuration;
87 engine_run_parameters.output = output;
89 output.println('Running data from string...
');
90 job1.runEngine(engine_run_parameters);
92 job2 = ws.addJobFromString(model());
93 cp = ws.addCheckpoint();
94 opt = ws.addOptions();
95 opt.defines('gdxincname
', job1.outDB.name);
96 opt.setAllModelTypes('xpress
');
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};
105 output.println('Running model from a string with checkpoint implicit database communication...
');
106 job2.runEngine(engine_run_parameters);
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);
114function data = data()
117 ' i canning plants / seattle, san-diego / '
118 ' j markets / new-york, chicago, topeka / ; '
121 ' a(i) capacity of plant i in cases '
125 ' b(j) demand at market j in cases '
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 ; '
135 'Scalar f freight in dollars per case per thousand miles /90/ '
136 ' bmult demand multiplier /1/; '};
137 data = sprintf(
'%s\n', data{:});
140function model = model()
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 '
151 ' Scalar f freight in dollars per case per thousand miles; '
152 ' Scalar bmult demand multiplier; '
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 '
159 ' Parameter c(i,j) transport cost in thousands of dollars per case ; '
160 ' c(i,j) = f * d(i,j) / 1000 ; '
163 ' x(i,j) shipment quantities in cases '
164 ' z total transportation costs in thousands of dollars ; '
166 ' Positive Variable x ; '
169 ' cost define objective function '
170 ' supply(i) observe supply limit at plant i '
171 ' demand(j) satisfy demand at market j ; '
173 ' cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; '
175 ' supply(i) .. sum(j, x(i,j)) =l= a(i) ; '
177 ' demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; '
179 ' Model transport /all/ ; '
181 ' Solve transport using lp minimizing z ; '
187 ' Display x.l, x.m ; '};
188 model = sprintf(
'%s\n', model{:});