27#include "gamsengineconfiguration.h"
28#include "gamsvariablerecord.h"
43using namespace std::string_literals;
45static string getDataText()
50 i canning plants / seattle, san-diego /
51 j markets / new-york, chicago, topeka / ;
55 a(i) capacity of plant i in cases
59 b(j) demand at market j in cases
64Table d(i,j) distance in thousands of miles
65 new-york chicago topeka
67 san-diego 2.5 1.8 1.4 ;
69Scalar f freight in dollars per case per thousand miles / 90 /
70 bmult demand multiplier / 1 /;
76static string getModelText()
85 a(i) capacity of plant i in cases
86 b(j) demand at market j in cases
87 d(i,j) distance in thousands of miles
88Scalar f freight in dollars per case per thousand miles
89 bmult demand multiplier;
91$if not set gdxincname $abort 'no include file name for data file provided'
92$gdxLoad %gdxincname% i j a b d f bmult
96Parameter c(i,j) transport cost in thousands of dollars per case ;
97 c(i,j) = f * d(i,j) / 1000 ;
100 x(i,j) shipment quantities in cases
101 z total transportation costs in thousands of dollars ;
106 cost define objective function
107 supply(i) observe supply limit at plant i
108 demand(j) satisfy demand at market j ;
110cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ;
111supply(i) .. sum(j, x(i,j)) =l= a(i) ;
112demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ;
114Model transport /all/ ;
116Solve transport using lp minimizing z ;
118Scalar ms 'model status', ss 'solve status';
135int main(
int argc,
char* argv[])
137 cout <<
"---------- Transport Engine --------------" << endl;
144 filesystem::remove(ws.workingDirectory().append(
"/test.txt"));
146 string envValues[4] = {};
148 string envPrefix(
"ENGINE_");
150 for (
const string &
id : {
"URL",
"USER",
"PASSWORD",
"NAMESPACE"}) {
155 errno_t err = _dupenv_s( &buffer, &len, (envPrefix +
id).c_str() );
156 if (err == 0 && buffer) {
157 envValues[index++] = string(buffer);
161 cerr <<
"No ENGINE_" <<
id <<
" set" << endl;
165 const char* value = getenv((envPrefix +
id).c_str());
167 cerr <<
"No ENGINE_" <<
id <<
" set" << endl;
170 envValues[index++] = value;
175 GAMSEngineConfiguration engineConf(envValues[0], envValues[1],
176 envValues[2], envValues[3]);
179 GAMSJob jobData = ws.addJobFromString(getDataText());
182 cout <<
"Run 1" << endl;
183 jobData.
runEngine(engineConf, &defaultOptions,
nullptr, &cout);
184 jobData.
outDB().
doExport(filesystem::absolute(ws.workingDirectory().append(
"/tdata.gdx")).string());
186 map<string, double> expectedLevels = { {
"seattle.new-york", 0.0 },
187 {
"seattle.chicago", 300.0 },
188 {
"seattle.topeka", 0.0 },
189 {
"san-diego.new-york", 325.0 },
190 {
"san-diego.chicago", 0.0 },
191 {
"san-diego.topeka", 275.0 }
195 GAMSJob jobModel = ws.addJobFromString(getModelText());
198 opt.
setDefine(
"gdxincname",
"tdata.gdx");
202 cout <<
"Run 2" << endl;
203 jobModel.
runEngine(engineConf, &opt,
nullptr, &cout, {}, {
"tdata.gdx" },
205 {
"inex_string",
"{\"type\": \"include\", \"files\": [\"*.gdx\"]}" }
209 cout <<
"x(" << rec.key(0) <<
"," + rec.key(1) <<
"): level=" << rec.level()
210 <<
"marginal=" << rec.marginal() <<
"\n";
212 if (expectedLevels.at(rec.key(0).append(
".").append(rec.key(1))) != rec.level()) {
213 cout <<
"Unexpected result, expected level: "
214 << expectedLevels.at(rec.key(0).append(
".").append(rec.key(1)))
220 if (filesystem::exists(filesystem::path(ws.workingDirectory()).append(
"test.txt"))) {
221 cout <<
"Test.txt should not have sent back - inex_string failed" << endl;
229 GAMSJob jobA = ws.addJobFromString(getDataText());
230 GAMSJob jobB = ws.addJobFromString(getModelText());
232 cout <<
"Run 3" << endl;
233 jobA.
runEngine(engineConf, &defaultOptions,
nullptr, &cout);
238 cout <<
"Run 4" << endl;
239 jobB.
runEngine(engineConf, &opt, &cp, &cout, vector<GAMSDatabase>{jobA.
outDB()});
242 cout <<
"x(" << rec.key(0) <<
"," + rec.key(1) <<
"): level=" << rec.level()
243 <<
"marginal=" << rec.marginal() <<
"\n";
245 if (expectedLevels.at(rec.key(0).append(
".").append(rec.key(1))) != rec.level()) {
246 cout <<
"Unexpected result, expected level: "
247 << expectedLevels.at(rec.key(0).append(
".").append(rec.key(1)))
252 vector<map<string, double>> bmultExpected {
253 {{
"bmult", 0.9 }, {
"ms", 1 }, {
"ss", 1 }, {
"obj", 138.31 } },
254 {{
"bmult", 1.2 }, {
"ms", 4 }, {
"ss", 1 }, {
"obj", 184.41 } }
257 for (map<string, double> &m : bmultExpected) {
258 GAMSJob tEbmult = ws.addJobFromString(
"bmult=" + to_string(m[
"bmult"])
259 +
"; solve transport min z use lp; ms=transport.modelstat; "
260 "ss=transport.solvestat;", cp);
262 cout <<
"Run 5" << endl;
263 tEbmult.
runEngine(engineConf, &defaultOptions,
nullptr, &cout);
264 cout <<
"Scenario bmult=" << to_string(m[
"bmult"]) <<
":" << endl;
269 cout <<
"Unexpected input, expected bmult: " + to_string(m[
"bmult"]) << endl;
273 cout <<
"Unexpected result, expected ms: " + to_string(m[
"ms"]);
277 cout <<
"Unexpected result, expected ss: " + to_string(m[
"ss"]);
281 cout <<
"Unexpected result, expected obj: " + to_string(m[
"obj"]);
286 catch (exception ex) {
287 cout <<
"Exception caught:" << ex.what() << endl;
294 GAMSJob jc = ws.addJobFromGamsLib(
"clad");
297 string optFile1Path = filesystem::path(ws.workingDirectory().append(
"/cplex.opt")).string();
299 ofstream optFile1(optFile1Path);
302 optFile1 <<
"epgap 0" << endl;
304 optFile1 <<
"interactive 1" << endl;
306 optFile1 <<
"iafile cplex.op2" << endl;
310 string optFile2Path = filesystem::path(ws.workingDirectory().append(
"/cplex.op2")).string();
312 ofstream optFile2(filesystem::path(ws.workingDirectory().append(
"/cplex.op2")));
313 optFile2.open(optFile2Path);
314 optFile2 <<
"epgap 0.1" << endl;
318 opt = ws.addOptions();
324 cout <<
"Run 6" << endl;
326 string logPath = filesystem::path(ws.workingDirectory().append(
"/ej.log")).string();
328 ofstream logFile(logPath);
330 vector<GAMSDatabase>(), set<string> {optFile1Path, optFile2Path,
"claddat.gdx"},
331 unordered_map<string, string>(),
true,
true);
333 while (filesystem::exists(logPath) && filesystem::is_empty(logPath))
334 this_thread::sleep_for(500ms);
337 cout <<
"Interrupted Cplex to continue with new option" << endl;
339 if (optThread.joinable())
345 ifstream inLog(logPath);
346 while (getline(inLog, line)) {
347 if (line.find(
"Interrupted") != string::npos) {
348 cout <<
"Interrupted succesfully" << endl;
352 cout <<
"Expected the solver to be interrupted at least once." << endl;
356 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)