26#include "gamsengineconfiguration.h"
27#include "gamsvariablerecord.h"
42using namespace std::string_literals;
44static string getDataText()
49 i canning plants / seattle, san-diego /
50 j markets / new-york, chicago, topeka / ;
54 a(i) capacity of plant i in cases
58 b(j) demand at market j in cases
63Table d(i,j) distance in thousands of miles
64 new-york chicago topeka
66 san-diego 2.5 1.8 1.4 ;
68Scalar f freight in dollars per case per thousand miles / 90 /
69 bmult demand multiplier / 1 /;
75static string getModelText()
84 a(i) capacity of plant i in cases
85 b(j) demand at market j in cases
86 d(i,j) distance in thousands of miles
87Scalar f freight in dollars per case per thousand miles
88 bmult demand multiplier;
90$if not set gdxincname $abort 'no include file name for data file provided'
91$gdxLoad %gdxincname% i j a b d f bmult
95Parameter c(i,j) transport cost in thousands of dollars per case ;
96 c(i,j) = f * d(i,j) / 1000 ;
99 x(i,j) shipment quantities in cases
100 z total transportation costs in thousands of dollars ;
105 cost define objective function
106 supply(i) observe supply limit at plant i
107 demand(j) satisfy demand at market j ;
109cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
110supply(i) .. sum(j, x(i,j)) =l= a(i) ;
111demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
113Model transport /all/ ;
115Solve transport using lp minimizing z ;
117Scalar ms 'model status', ss 'solve status';
134int main(
int argc,
char* argv[])
136 cout <<
"---------- Transport Engine --------------" << endl;
143 filesystem::remove(ws.workingDirectory().append(
"/test.txt"));
145 string envValues[4] = {};
147 string envPrefix(
"ENGINE_");
149 for (
const string &
id : {
"URL",
"USER",
"PASSWORD",
"NAMESPACE"}) {
154 errno_t err = _dupenv_s( &buffer, &len, (envPrefix +
id).c_str() );
155 if (err == 0 && buffer) {
156 envValues[index++] = string(buffer);
160 cerr <<
"No ENGINE_" <<
id <<
" set" << endl;
164 const char* value = getenv((envPrefix +
id).c_str());
166 cerr <<
"No ENGINE_" <<
id <<
" set" << endl;
169 envValues[index++] = value;
174 GAMSEngineConfiguration engineConf(envValues[0], envValues[1],
175 envValues[2], envValues[3]);
178 GAMSJob jobData = ws.addJobFromString(getDataText());
181 cout <<
"Run 1" << endl;
182 jobData.
runEngine(engineConf, &defaultOptions,
nullptr, &cout);
183 jobData.
outDB().
doExport(filesystem::absolute(ws.workingDirectory().append(
"/tdata.gdx")).string());
185 map<string, double> expectedLevels = { {
"seattle.new-york", 0.0 },
186 {
"seattle.chicago", 300.0 },
187 {
"seattle.topeka", 0.0 },
188 {
"san-diego.new-york", 325.0 },
189 {
"san-diego.chicago", 0.0 },
190 {
"san-diego.topeka", 275.0 }
194 GAMSJob jobModel = ws.addJobFromString(getModelText());
197 opt.
setDefine(
"gdxincname",
"tdata.gdx");
201 cout <<
"Run 2" << endl;
202 jobModel.
runEngine(engineConf, &opt,
nullptr, &cout, {}, {
"tdata.gdx" },
204 {
"inex_string",
"{\"type\": \"include\", \"files\": [\"*.gdx\"]}" }
208 cout <<
"x(" << rec.key(0) <<
"," + rec.key(1) <<
"): level=" << rec.level()
209 <<
"marginal=" << rec.marginal() <<
"\n";
211 if (expectedLevels.at(rec.key(0).append(
".").append(rec.key(1))) != rec.level()) {
212 cout <<
"Unexpected result, expected level: "
213 << expectedLevels.at(rec.key(0).append(
".").append(rec.key(1)))
219 if (filesystem::exists(filesystem::path(ws.workingDirectory()).append(
"test.txt"))) {
220 cout <<
"Test.txt should not have sent back - inex_string failed" << endl;
228 GAMSJob jobA = ws.addJobFromString(getDataText());
229 GAMSJob jobB = ws.addJobFromString(getModelText());
231 cout <<
"Run 3" << endl;
232 jobA.
runEngine(engineConf, &defaultOptions,
nullptr, &cout);
237 cout <<
"Run 4" << endl;
238 jobB.
runEngine(engineConf, &opt, &cp, &cout, vector<GAMSDatabase>{jobA.
outDB()});
241 cout <<
"x(" << rec.key(0) <<
"," + rec.key(1) <<
"): level=" << rec.level()
242 <<
"marginal=" << rec.marginal() <<
"\n";
244 if (expectedLevels.at(rec.key(0).append(
".").append(rec.key(1))) != rec.level()) {
245 cout <<
"Unexpected result, expected level: "
246 << expectedLevels.at(rec.key(0).append(
".").append(rec.key(1)))
251 vector<map<string, double>> bmultExpected {
252 {{
"bmult", 0.9 }, {
"ms", 1 }, {
"ss", 1 }, {
"obj", 138.31 } },
253 {{
"bmult", 1.2 }, {
"ms", 4 }, {
"ss", 1 }, {
"obj", 184.41 } }
256 for (map<string, double> &m : bmultExpected) {
257 GAMSJob tEbmult = ws.addJobFromString(
"bmult=" + to_string(m[
"bmult"])
258 +
"; solve transport min z use lp; ms=transport.modelstat; "
259 "ss=transport.solvestat;", cp);
261 cout <<
"Run 5" << endl;
262 tEbmult.
runEngine(engineConf, &defaultOptions,
nullptr, &cout);
263 cout <<
"Scenario bmult=" << to_string(m[
"bmult"]) <<
":" << endl;
268 cout <<
"Unexpected input, expected bmult: " + to_string(m[
"bmult"]) << endl;
272 cout <<
"Unexpected result, expected ms: " + to_string(m[
"ms"]);
276 cout <<
"Unexpected result, expected ss: " + to_string(m[
"ss"]);
280 cout <<
"Unexpected result, expected obj: " + to_string(m[
"obj"]);
285 catch (exception ex) {
286 cout <<
"Exception caught:" << ex.what() << endl;
293 GAMSJob jc = ws.addJobFromGamsLib(
"clad");
296 string optFile1Path = filesystem::path(ws.workingDirectory().append(
"/cplex.opt")).string();
298 ofstream optFile1(optFile1Path);
301 optFile1 <<
"epgap 0" << endl;
303 optFile1 <<
"interactive 1" << endl;
305 optFile1 <<
"iafile cplex.op2" << endl;
309 string optFile2Path = filesystem::path(ws.workingDirectory().append(
"/cplex.op2")).string();
311 ofstream optFile2(filesystem::path(ws.workingDirectory().append(
"/cplex.op2")));
312 optFile2.open(optFile2Path);
313 optFile2 <<
"epgap 0.1" << endl;
317 opt = ws.addOptions();
323 cout <<
"Run 6" << endl;
325 string logPath = filesystem::path(ws.workingDirectory().append(
"/ej.log")).string();
327 ofstream logFile(logPath);
329 vector<GAMSDatabase>(), set<string> {optFile1Path, optFile2Path,
"claddat.gdx"},
330 unordered_map<string, string>(),
true,
true);
332 while (filesystem::exists(logPath) && filesystem::is_empty(logPath))
333 this_thread::sleep_for(500ms);
336 cout <<
"Interrupted Cplex to continue with new option" << endl;
338 if (optThread.joinable())
344 ifstream inLog(logPath);
345 while (getline(inLog, line)) {
346 if (line.find(
"Interrupted") != string::npos) {
347 cout <<
"Interrupted succesfully" << endl;
351 cout <<
"Expected the solver to be interrupted at least once." << endl;
355 cout <<
"Interrupted succesfully" << endl;
void doExport(const std::string &filePath="")
GAMSParameter getParameter(const std::string &name)
GAMSVariable getVariable(const std::string &name)
void runEngine(const GAMSEngineConfiguration &engineConfiguration, GAMSOptions *gamsOptions, gams::GAMSCheckpoint *checkpoint, std::ostream *output=nullptr, const std::vector< gams::GAMSDatabase > &databases={}, const std::set< std::string > &extraModelFiles={}, const std::unordered_map< std::string, std::string > &engineOptions={}, bool createOutDB=true, bool removeResults=true)
void setAllModelTypes(const std::string &solver)
void setMIP(const std::string &value)
void setDefine(const std::string &key, const std::string &value)
void setSolveLink(const GAMSOptions::ESolveLink::ESolveLinkEnum value)
void setOptFile(const int value)
GAMSParameterRecord firstRecord(const std::vector< std::string > &slice)
GAMSVariableRecord firstRecord(const std::vector< std::string > &slice)
void setSystemDirectory(std::string systemDir)