00001
00071 #include "OSCoinSolver.h"
00072 #include "OSResult.h"
00073 #include "OSiLReader.h"
00074 #include "OSiLWriter.h"
00075 #include "OSoLReader.h"
00076 #include "OSrLReader.h"
00077 #include "OSrLWriter.h"
00078 #include "OSInstance.h"
00079 #include "OSOption.h"
00080 #include "OSoLWriter.h"
00081 #include "OSFileUtil.h"
00082 #include "OSConfig.h"
00083 #include "OSDefaultSolver.h"
00084 #include "OSWSUtil.h"
00085 #include "OSSolverAgent.h"
00086 #include "OShL.h"
00087 #include "OSErrorClass.h"
00088 #include "OSmps2osil.h"
00089 #include "OSBase64.h"
00090
00091
00092 #ifdef COIN_HAS_KNITRO
00093 #include "OSKnitroSolver.h"
00094 #endif
00095
00096 #ifdef COIN_HAS_LINDO
00097 #include "OSLindoSolver.h"
00098 #endif
00099
00100 #ifdef COIN_HAS_ASL
00101 #include "OSnl2osil.h"
00102 #endif
00103
00104 #ifdef COIN_HAS_GAMSUTILS
00105 #include "OSgams2osil.hpp"
00106 #endif
00107
00108
00109
00110
00111
00112 #ifdef COIN_HAS_IPOPT
00113 #ifndef COIN_HAS_ASL
00114 #include "OSIpoptSolver.h"
00115 #undef COIN_HAS_ASL
00116 #else
00117 #include "OSIpoptSolver.h"
00118 #endif
00119 #endif
00120
00121 #ifdef COIN_HAS_BONMIN
00122 #include "OSBonminSolver.h"
00123 #endif
00124
00125 #ifdef COIN_HAS_COUENNE
00126 #include "OSCouenneSolver.h"
00127 #endif
00128
00129 #include "OSOptionsStruc.h"
00130
00131 using std::cout;
00132 using std::endl;
00133 using std::ostringstream;
00134 using std::string;
00135
00136
00137
00138
00139 #define MAXCHARS 5000
00140
00141 typedef struct yy_buffer_state *YY_BUFFER_STATE;
00142 YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner);
00143
00144 void setyyextra(osOptionsStruc *osoptions, void* scanner);
00145 int ossslex(void* scanner);
00146 int ossslex_init(void** ptr);
00147 int ossslex_destroy(void* scanner);
00148
00149 std::string get_help();
00150 std::string get_version();
00151
00152
00153 void solve();
00154 void getJobID();
00155 void send();
00156 void kill();
00157 void retrieve();
00158 void knock();
00159
00160
00161 void getOSiLFromNl();
00162 void getOSiLFromMps();
00163 void getOSiLFromGams();
00164 std::string buildSolver(std::string solverName, std::string osol,
00165 OSInstance *osinstance);
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 osOptionsStruc *osoptions;
00177
00178 int main(int argC, const char* argV[]) {
00179 WindowsErrorPopupBlocker();
00180 void* scanner;
00181 FileUtil *fileUtil = NULL;
00182 FileUtil *inputFileUtil = NULL;
00183 char osss[MAXCHARS] = " ";
00184 const char *space = " ";
00185
00186 std::string configFileName = "";
00187 int i;
00188 std::cout << endl;
00189 std::cout << endl;
00190 std::cout << "------- " ;
00191 std::cout << getVersionInfo() ;
00192 std::cout << " ------- " << std::endl;
00193 std::cout << endl;
00194
00195
00196
00197 osoptions = new osOptionsStruc();
00198 osoptions->configFile = "";
00199 osoptions->osilFile = "";
00200 osoptions->osil = "";
00201 osoptions->osolFile = "";
00202 osoptions->osol = "";
00203 osoptions->osrlFile = "";
00204 osoptions->osrl = "";
00205
00206 osoptions->insList = "";
00207 osoptions->serviceLocation = "";
00208 osoptions->serviceMethod = "";
00209 osoptions->osplInputFile = "";
00210 osoptions->osplOutputFile = "";
00211 osoptions->mpsFile = "";
00212 osoptions->nlFile = "";
00213 osoptions->gamsControlFile = "";
00214 osoptions->solverName = "";
00215 osoptions->browser = "";
00216 osoptions->invokeHelp = false;
00217 osoptions->writeVersion = false;
00218 bool scannerActive = false;
00219 try {
00220 if (argC < 2) {
00221
00222
00223 throw ErrorClass(
00224 "There must be at least one command line argument\n Try -h or --help");
00225
00226
00227 }
00228
00229 i = 1;
00230 while (i < argC) {
00231 if (strlen(osss) + strlen(argV[i]) + 1 > MAXCHARS)
00232 throw ErrorClass("the command line has too many arguments");
00233 strcat(osss, argV[i]);
00234 strcat(osss, space);
00235 i++;
00236 }
00237 #ifdef DEBUG_CL_INTERFACE
00238 cout << "Input String = " << osss << endl;
00239 #endif
00240 scannerActive = true;
00241 ossslex_init(&scanner);
00242
00243 setyyextra(osoptions, scanner);
00244
00245 osss_scan_string(osss, scanner);
00246 #ifdef DEBUG_CL_INTERFACE
00247 std::cout << "call ossslex" << std::endl;
00248 #endif
00249 ossslex(scanner);
00250 ossslex_destroy(scanner);
00251 scannerActive = false;
00252 #ifdef DEBUG_CL_INTERFACE
00253 std::cout << "done with call to ossslex" << std::endl;
00254 #endif
00255
00256 if (osoptions->configFile != "") {
00257 scannerActive = true;
00258 ossslex_init(&scanner);
00259 configFileName = osoptions->configFile;
00260 #ifdef DEBUG_CL_INTERFACE
00261 cout << "configFileName = " << configFileName << endl;
00262 #endif
00263 std::string osolfileOptions = fileUtil->getFileAsString(
00264 configFileName.c_str());
00265 #ifdef DEBUG_CL_INTERFACE
00266 std::cout << "Call Text Extra" << std::endl;
00267 #endif
00268 setyyextra(osoptions, scanner);
00269 #ifdef DEBUG_CL_INTERFACE
00270 std::cout << "Done with call Text Extra" << std::endl;
00271 #endif
00272 osss_scan_string(osolfileOptions.c_str(), scanner);
00273 ossslex(scanner);
00274 ossslex_destroy(scanner);
00275 scannerActive = false;
00276 }
00277 } catch (const ErrorClass& eclass) {
00278
00279
00280
00281
00282 OSResult *osresult = NULL;
00283 OSrLWriter *osrlwriter = NULL;
00284 osrlwriter = new OSrLWriter();
00285 osresult = new OSResult();
00286 osresult->setGeneralMessage(eclass.errormsg);
00287 osresult->setGeneralStatusType("error");
00288 std::string osrl = osrlwriter->writeOSrL(osresult);
00289 if (osoptions->osrlFile != "") {
00290
00291 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00292 if (osoptions->browser != "") {
00293 std::string str = osoptions->browser + " "
00294 + osoptions->osrlFile;
00295 const char *ch = &str[0];
00296 std::system(ch);
00297 }
00298 } else {
00299
00300 std::cout << osrl << std::endl;
00301 }
00302
00303 delete osresult;
00304 osresult = NULL;
00305 delete osrlwriter;
00306 osrlwriter = NULL;
00307
00308
00309 if(scannerActive == true) ossslex_destroy(scanner);
00310 delete fileUtil;
00311 delete osoptions;
00312 return 1;
00313 }
00314 try {
00315 if (osoptions->invokeHelp == true) {
00316 std::string helpTxt = get_help();
00317 std::cout << std::endl << std::endl;
00318 std::cout << helpTxt << std::endl;
00319 delete osoptions;
00320 osoptions = NULL;
00321 return 0;
00322 }
00323 if (osoptions->writeVersion == true) {
00324 std::string writeTxt = get_version();
00325 std::cout << std::endl << std::endl;
00326 std::cout << writeTxt << std::endl;
00327 delete osoptions;
00328 osoptions = NULL;
00329 return 0;
00330 }
00331 } catch (const ErrorClass& eclass) {
00332
00333
00334
00335
00336
00337 OSResult *osresult = NULL;
00338 OSrLWriter *osrlwriter = NULL;
00339 osrlwriter = new OSrLWriter();
00340 osresult = new OSResult();
00341 osresult->setGeneralMessage(eclass.errormsg);
00342 osresult->setGeneralStatusType("error");
00343 std::string osrl = osrlwriter->writeOSrL(osresult);
00344 if (osoptions->osrlFile != "") {
00345
00346 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00347 if (osoptions->browser != "") {
00348 std::string str = osoptions->browser + " "
00349 + osoptions->osrlFile;
00350 const char *ch = &str[0];
00351 std::system(ch);
00352 }
00353 } else {
00354
00355 std::cout << osrl << std::endl;
00356 }
00357
00358 delete osresult;
00359 osresult = NULL;
00360 delete osrlwriter;
00361 osrlwriter = NULL;
00362
00363
00364
00365 delete osoptions;
00366 osoptions = NULL;
00367 delete inputFileUtil;
00368 inputFileUtil = NULL;
00369 return 1;
00370 }
00371
00372 #ifdef DEBUG_CL_INTERFACE
00373 cout << "HERE ARE THE OPTION VALUES:" << endl;
00374 if(osoptions->configFile != "") cout << "Config file = " << osoptions->configFile << endl;
00375 if(osoptions->osilFile != "") cout << "OSiL file = " << osoptions->osilFile << endl;
00376 if(osoptions->osolFile != "") cout << "OSoL file = " << osoptions->osolFile << endl;
00377 if(osoptions->osrlFile != "") cout << "OSrL file = " << osoptions->osrlFile << endl;
00378
00379 if(osoptions->osplInputFile != "") cout << "OSpL Input file = " << osoptions->osplInputFile << endl;
00380 if(osoptions->serviceMethod != "") cout << "Service Method = " << osoptions->serviceMethod << endl;
00381 if(osoptions->mpsFile != "") cout << "MPS File Name = " << osoptions->mpsFile << endl;
00382 if(osoptions->nlFile != "") cout << "NL File Name = " << osoptions->nlFile << endl;
00383 if(osoptions->gamsControlFile != "") cout << "gams Control File Name = " << osoptions->gamsControlFile << endl;
00384 if(osoptions->browser != "") cout << "Browser Value = " << osoptions->browser << endl;
00385 if(osoptions->solverName != "") cout << "Selected Solver = " << osoptions->solverName << endl;
00386 if(osoptions->serviceLocation != "") cout << "Service Location = " << osoptions->serviceLocation << endl;
00387 #endif
00388
00389
00390 unsigned int k;
00391 for (k = 0; k < osoptions->solverName.length(); k++) {
00392 osoptions->solverName[k] = tolower(osoptions->solverName[k]);
00393 }
00394
00395
00396 fileUtil = new FileUtil();
00397 try {
00398
00399 if (osoptions->osolFile != "") {
00400
00401 osoptions->osol = fileUtil->getFileAsString(
00402 (osoptions->osolFile).c_str());
00403
00404 }
00405
00406 if (osoptions->osilFile != "") {
00407
00408 osoptions->osil = fileUtil->getFileAsString(
00409 (osoptions->osilFile).c_str());
00410 }
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 if (osoptions->osplInputFile != "")
00422 osoptions->osplInput = fileUtil->getFileAsString(
00423 (osoptions->osplInputFile).c_str());
00424
00425 if (osoptions->osplOutputFile != "")
00426 osoptions->osplOutput = fileUtil->getFileAsString(
00427 (osoptions->osplOutputFile).c_str());
00428 } catch (const ErrorClass& eclass) {
00429
00430
00431
00432
00433
00434
00435 OSResult *osresult = NULL;
00436 OSrLWriter *osrlwriter = NULL;
00437 osrlwriter = new OSrLWriter();
00438 osresult = new OSResult();
00439 osresult->setGeneralMessage(eclass.errormsg);
00440 osresult->setGeneralStatusType("error");
00441 std::string osrl = osrlwriter->writeOSrL(osresult);
00442 if (osoptions->osrlFile != "") {
00443
00444 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00445 if (osoptions->browser != "") {
00446 std::string str = osoptions->browser + " "
00447 + osoptions->osrlFile;
00448 const char *ch = &str[0];
00449 std::system(ch);
00450 }
00451 } else {
00452
00453 std::cout << osrl << std::endl;
00454 }
00455
00456 delete osresult;
00457 osresult = NULL;
00458 delete osrlwriter;
00459 osrlwriter = NULL;
00460
00461
00462
00463 delete osoptions;
00464 osoptions = NULL;
00465 delete fileUtil;
00466 fileUtil = NULL;
00467 return 1;
00468 }
00469
00470
00471 if (osoptions->serviceMethod == "")
00472 solve();
00473 if ((osoptions->serviceMethod[0] == 's') && (osoptions->serviceMethod[1]
00474 == 'o'))
00475 solve();
00476 else {
00477 switch (osoptions->serviceMethod[0]) {
00478 case 'g':
00479 getJobID();
00480 break;
00481 case 'r':
00482 retrieve();
00483 break;
00484 case 's':
00485 send();
00486 break;
00487 case 'k':
00488 if (osoptions->serviceMethod[1] == 'i')
00489 kill();
00490 else
00491 knock();
00492 break;
00493 default:
00494
00495 break;
00496 }
00497 }
00498 delete osoptions;
00499 osoptions = NULL;
00500 delete fileUtil;
00501 fileUtil = NULL;
00502 return 0;
00503 }
00504
00505 void solve() {
00506 std::string osrl = "";
00507 OSiLReader *osilreader = NULL;
00508 OSmps2osil *mps2osil = NULL;
00509 #ifdef COIN_HAS_ASL
00510 OSnl2osil *nl2osil = NULL;
00511 #endif
00512 #ifdef COIN_HAS_GAMSUTILS
00513 OSgams2osil *gams2osil = NULL;
00514 #endif
00515 OSSolverAgent* osagent = NULL;
00516 FileUtil *fileUtil = NULL;
00517 fileUtil = new FileUtil();
00518
00519 try {
00520 if (osoptions->serviceLocation != "") {
00521
00522 if (osoptions->osil == "") {
00523
00524 if (osoptions->nlFile != "") {
00525 getOSiLFromNl();
00526 } else {
00527 if (osoptions->mpsFile != "") {
00528 getOSiLFromMps();
00529 } else {
00530 if (osoptions->gamsControlFile != "") {
00531
00532 getOSiLFromGams();
00533 } else {
00534 osoptions->osil = "";
00535 }
00536 }
00537 }
00538 }
00539
00540
00541 osagent = new OSSolverAgent(osoptions->serviceLocation);
00542
00543 if (osoptions->osol == "") {
00544
00545 std::ostringstream outStr;
00546 outStr
00547 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <osol xmlns=\"os.optimizationservices.org\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"os.optimizationservices.org http://www.optimizationservices.org/schemas/";
00548 outStr << OS_SCHEMA_VERSION;
00549 outStr << "/OSoL.xsd\"></osol>";
00550 osoptions->osol = outStr.str();
00551 }
00552 osrl = osagent->solve(osoptions->osil, osoptions->osol);
00553 if (osoptions->osrlFile != "") {
00554 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00555
00556 if (osoptions->browser != "") {
00557 std::string str = osoptions->browser + " "
00558 + osoptions->osrlFile;
00559 const char *ch = &str[0];
00560 std::system(ch);
00561 }
00562 } else
00563 cout << osrl << endl;
00564 delete osagent;
00565 osagent = NULL;
00566
00567 } else {
00568
00569 if (osoptions->osil != "") {
00570 osilreader = new OSiLReader();
00571 osrl = buildSolver(osoptions->solverName, osoptions->osol,
00572 osilreader->readOSiL(osoptions->osil));
00573 } else {
00574
00575 if (osoptions->nlFile != "") {
00576 #ifdef COIN_HAS_ASL
00577 nl2osil = new OSnl2osil( osoptions->nlFile);
00578 nl2osil->createOSInstance();
00579 osrl = buildSolver(osoptions->solverName, osoptions->osol, nl2osil->osinstance);
00580 #else
00581 throw ErrorClass(
00582 "nlFile specified locally but ASL not present");
00583 #endif
00584 } else {
00585 if (osoptions->mpsFile != "") {
00586 mps2osil = new OSmps2osil(osoptions->mpsFile);
00587 mps2osil->createOSInstance();
00588 osrl = buildSolver(osoptions->solverName,
00589 osoptions->osol, mps2osil->osinstance);
00590 } else {
00591 if (osoptions->gamsControlFile != "") {
00592 #ifdef COIN_HAS_GAMSUTILS
00593 gams2osil = new OSgams2osil( osoptions->gamsControlFile);
00594 gams2osil->createOSInstance();
00595 osrl = buildSolver(osoptions->solverName, osoptions->osol, gams2osil->osinstance);
00596 #else
00597 throw ErrorClass(
00598 "a Gams Control specified locally but GAMSIP not present");
00599 #endif
00600
00601 } else {
00602
00603 throw ErrorClass(
00604 "Error: no osil, GAMS dat, AMPL nl, or mps file given for a local solve --- \n information in the osol file is ignored for local solves.");
00605
00606 }
00607 }
00608 }
00609 }
00610
00611 if (osoptions->osrlFile != "") {
00612
00613 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00614
00615
00616 if (osoptions->browser != "") {
00617 std::string str = osoptions->browser + " "
00618 + osoptions->osrlFile;
00619 const char *ch = &str[0];
00620 std::system(ch);
00621 }
00622 } else
00623 cout << osrl << endl;
00624
00625 }
00626
00627
00628
00629 if (osilreader != NULL)
00630 delete osilreader;
00631 osilreader = NULL;
00632 if (mps2osil != NULL)
00633 delete mps2osil;
00634 mps2osil = NULL;
00635 #ifdef COIN_HAS_ASL
00636 if(nl2osil != NULL) delete nl2osil;
00637 nl2osil = NULL;
00638 #endif
00639 #ifdef COIN_HAS_GAMSUTILS
00640 if(gams2osil != NULL) delete gams2osil;
00641 gams2osil = NULL;
00642 #endif
00643 delete fileUtil;
00644 fileUtil = NULL;
00645
00646 }
00647 catch (const ErrorClass& eclass) {
00648
00649 OSResult *osresult = NULL;
00650 OSrLWriter *osrlwriter = NULL;
00651 osrlwriter = new OSrLWriter();
00652 osresult = new OSResult();
00653 osresult->setGeneralMessage(eclass.errormsg);
00654 osresult->setGeneralStatusType("error");
00655 std::string osrl = osrlwriter->writeOSrL(osresult);
00656 if (osoptions->osrlFile != "") {
00657
00658 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00659 if (osoptions->browser != "") {
00660 std::string str = osoptions->browser + " "
00661 + osoptions->osrlFile;
00662 const char *ch = &str[0];
00663 std::system(ch);
00664 }
00665 } else {
00666
00667 std::cout << osrl << std::endl;
00668 }
00669
00670 delete osresult;
00671 osresult = NULL;
00672 delete osrlwriter;
00673 osrlwriter = NULL;
00674
00675
00676 if (osilreader != NULL)
00677 delete osilreader;
00678 osilreader = NULL;
00679 if (mps2osil != NULL)
00680 delete mps2osil;
00681 mps2osil = NULL;
00682 #ifdef COIN_HAS_ASL
00683 if(nl2osil != NULL) delete nl2osil;
00684 nl2osil = NULL;
00685 #endif
00686 #ifdef COIN_HAS_GAMSUTILS
00687 if(gams2osil != NULL) delete gams2osil;
00688 gams2osil = NULL;
00689 #endif
00690 delete fileUtil;
00691 fileUtil = NULL;
00692 }
00693
00694 }
00695
00696 void getJobID() {
00697 std::string jobID = "";
00698 OSSolverAgent* osagent = NULL;
00699 try {
00700 if (osoptions->serviceLocation != "") {
00701 osagent = new OSSolverAgent(osoptions->serviceLocation);
00702 jobID = osagent->getJobID(osoptions->osol);
00703 cout << jobID << endl;
00704 delete osagent;
00705 osagent = NULL;
00706 } else {
00707 delete osagent;
00708 osagent = NULL;
00709 throw ErrorClass("please specify service location (url)");
00710 }
00711 } catch (const ErrorClass& eclass) {
00712 FileUtil *fileUtil = NULL;
00713 fileUtil = new FileUtil();
00714 OSResult *osresult = NULL;
00715 OSrLWriter *osrlwriter = NULL;
00716 osrlwriter = new OSrLWriter();
00717 osresult = new OSResult();
00718 osresult->setGeneralMessage(eclass.errormsg);
00719 osresult->setGeneralStatusType("error");
00720 std::string osrl = osrlwriter->writeOSrL(osresult);
00721 if (osoptions->osrlFile != "")
00722 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00723 else
00724 cout << osrl << endl;
00725 delete osresult;
00726 osresult = NULL;
00727 delete osrlwriter;
00728 osrlwriter = NULL;
00729 delete fileUtil;
00730 fileUtil = NULL;
00731 }
00732 }
00733
00734
00735 void knock() {
00736 std::string osplOutput = "";
00737 OSSolverAgent* osagent = NULL;
00738 FileUtil *fileUtil = NULL;
00739 fileUtil = new FileUtil();
00740 try {
00741 if (osoptions->serviceLocation != "") {
00742 osagent = new OSSolverAgent(osoptions->serviceLocation);
00743 osplOutput = osagent->knock(osoptions->osplInput, osoptions->osol);
00744 if (osoptions->osplOutputFile != "")
00745 fileUtil->writeFileFromString(osoptions->osplOutputFile,
00746 osplOutput);
00747 else
00748 cout << osplOutput << endl;
00749 delete osagent;
00750 } else {
00751 delete osagent;
00752 throw ErrorClass("please specify service location (url)");
00753 }
00754 delete fileUtil;
00755 fileUtil = NULL;
00756 } catch (const ErrorClass& eclass) {
00757 OSResult *osresult = NULL;
00758 OSrLWriter *osrlwriter = NULL;
00759 osrlwriter = new OSrLWriter();
00760 osresult = new OSResult();
00761 osresult->setGeneralMessage(eclass.errormsg);
00762 osresult->setGeneralStatusType("error");
00763 std::string osrl = osrlwriter->writeOSrL(osresult);
00764 if (osoptions->osrlFile != "")
00765 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00766 else
00767 cout << osrl << endl;
00768 delete osresult;
00769 osresult = NULL;
00770 delete osrlwriter;
00771 osrlwriter = NULL;
00772 delete fileUtil;
00773 fileUtil = NULL;
00774 }
00775 }
00776
00777
00778 void send() {
00779 bool bSend = false;
00780 std::string jobID = "";
00781 OSSolverAgent* osagent = NULL;
00782
00783 std::string
00784 sOSoL =
00785 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <osol xmlns=\"os.optimizationservices.org\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"os.optimizationservices.org http://www.optimizationservices.org/schemas/OSoL.xsd\"><general> </general></osol>";
00786 string::size_type iStringpos;
00787 try {
00788
00789 if (osoptions->osil == "") {
00790
00791 if (osoptions->nlFile != "") {
00792 getOSiLFromNl();
00793 } else {
00794 if (osoptions->mpsFile != "") {
00795 getOSiLFromMps();
00796 } else {
00797 osoptions->osil = "";
00798 }
00799 }
00800 }
00801 if (osoptions->serviceLocation != "") {
00802 osagent = new OSSolverAgent(osoptions->serviceLocation);
00803
00804 if (osoptions->osolFile == "") {
00805
00806 jobID = osagent->getJobID(sOSoL);
00807
00808 iStringpos = sOSoL.find("</general");
00809 if (iStringpos != std::string::npos)
00810 sOSoL.insert(iStringpos, "<jobID>" + jobID + "</jobID>");
00811 } else {
00812 sOSoL = osoptions->osolFile;
00813 }
00814
00815 bSend = osagent->send(osoptions->osil, sOSoL);
00816 delete osagent;
00817 } else {
00818 delete osagent;
00819 throw ErrorClass("please specify service location (url)");
00820 }
00821 } catch (const ErrorClass& eclass) {
00822 FileUtil *fileUtil = NULL;
00823 fileUtil = new FileUtil();
00824 OSResult *osresult = NULL;
00825 OSrLWriter *osrlwriter = NULL;
00826 osrlwriter = new OSrLWriter();
00827 osresult = new OSResult();
00828 osresult->setGeneralMessage(eclass.errormsg);
00829 osresult->setGeneralStatusType("error");
00830 std::string osrl = osrlwriter->writeOSrL(osresult);
00831 if (osoptions->osrlFile != "")
00832 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00833 else
00834 cout << osrl << endl;
00835 delete osresult;
00836 osresult = NULL;
00837 delete osrlwriter;
00838 osrlwriter = NULL;
00839 delete fileUtil;
00840 fileUtil = NULL;
00841 }
00842 }
00843
00844 void retrieve() {
00845 FileUtil *fileUtil = NULL;
00846 fileUtil = new FileUtil();
00847 std::string osrl = "";
00848 OSSolverAgent* osagent = NULL;
00849 try {
00850 if (osoptions->serviceLocation != "") {
00851 osagent = new OSSolverAgent(osoptions->serviceLocation);
00852 #ifdef DEBUG_CL_INTERFACE
00853 std::cout << "HERE ARE THE OSOL OPTIONS " << osoptions->osol << std::endl;
00854 #endif
00855 osrl = osagent->retrieve(osoptions->osol);
00856 if (osoptions->osrlFile != "") {
00857 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00858 if (osoptions->browser != "") {
00859 std::string str = osoptions->browser + " "
00860 + osoptions->osrlFile;
00861 const char *ch = &str[0];
00862 std::system(ch);
00863 }
00864 } else
00865 cout << osrl << endl;
00866 delete osagent;
00867 osagent = NULL;
00868 } else {
00869 delete osagent;
00870 osagent = NULL;
00871 throw ErrorClass("please specify service location (url)");
00872 }
00873 delete fileUtil;
00874 fileUtil = NULL;
00875 } catch (const ErrorClass& eclass) {
00876 OSResult *osresult = NULL;
00877 OSrLWriter *osrlwriter = NULL;
00878 osrlwriter = new OSrLWriter();
00879 osresult = new OSResult();
00880 osresult->setGeneralMessage(eclass.errormsg);
00881 osresult->setGeneralStatusType("error");
00882 std::string osrl = osrlwriter->writeOSrL(osresult);
00883 if (osoptions->osrlFile != "")
00884 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00885 else
00886 cout << osrl << endl;
00887 delete osresult;
00888 osresult = NULL;
00889 delete osrlwriter;
00890 osrlwriter = NULL;
00891 delete fileUtil;
00892 fileUtil = NULL;
00893 }
00894 }
00895
00896 void kill() {
00897 FileUtil *fileUtil = NULL;
00898 fileUtil = new FileUtil();
00899 std::string osplOutput = "";
00900 OSSolverAgent* osagent = NULL;
00901 try {
00902 if (osoptions->serviceLocation != "") {
00903 osagent = new OSSolverAgent(osoptions->serviceLocation);
00904 osplOutput = osagent->kill(osoptions->osol);
00905 if (osoptions->osplOutputFile != "")
00906 fileUtil->writeFileFromString(osoptions->osplOutputFile,
00907 osplOutput);
00908 else
00909 cout << osplOutput << endl;
00910 delete osagent;
00911 osagent = NULL;
00912 } else {
00913 delete osagent;
00914 osagent = NULL;
00915 throw ErrorClass("please specify service location (url)");
00916 }
00917 delete fileUtil;
00918 fileUtil = NULL;
00919 } catch (const ErrorClass& eclass) {
00920 OSResult *osresult = NULL;
00921 OSrLWriter *osrlwriter = NULL;
00922 osrlwriter = new OSrLWriter();
00923 osresult = new OSResult();
00924 osresult->setGeneralMessage(eclass.errormsg);
00925 osresult->setGeneralStatusType("error");
00926 std::string osrl = osrlwriter->writeOSrL(osresult);
00927 if (osoptions->osrlFile != "")
00928 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00929 else
00930 cout << osrl << endl;
00931 delete osresult;
00932 osresult = NULL;
00933 delete osrlwriter;
00934 osrlwriter = NULL;
00935 delete fileUtil;
00936 fileUtil = NULL;
00937 }
00938 }
00939
00940 void getOSiLFromNl() {
00941 try {
00942 #ifdef COIN_HAS_ASL
00943 OSnl2osil *nl2osil = NULL;
00944 nl2osil = new OSnl2osil( osoptions->nlFile);
00945 nl2osil->createOSInstance();
00946 OSiLWriter *osilwriter = NULL;
00947 osilwriter = new OSiLWriter();
00948 std::string osil;
00949 osil = osilwriter->writeOSiL( nl2osil->osinstance);
00950 osoptions->osil = osil;
00951 delete nl2osil;
00952 nl2osil = NULL;
00953 delete osilwriter;
00954 osilwriter = NULL;
00955 #else
00956 throw ErrorClass(
00957 "trying to convert nl to osil without AMPL ASL configured");
00958 #endif
00959 } catch (const ErrorClass& eclass) {
00960 std::cout << eclass.errormsg << std::endl;
00961 throw ErrorClass(eclass.errormsg);
00962 }
00963 }
00964
00965
00966 void getOSiLFromGams() {
00967 try {
00968 #ifdef COIN_HAS_GAMSIO
00969 OSgams2osil *gams2osil = NULL;
00970 gams2osil = new OSgams2osil( osoptions->gamsControlFile);
00971 gams2osil->createOSInstance();
00972 OSiLWriter *osilwriter = NULL;
00973 osilwriter = new OSiLWriter();
00974 std::string osil;
00975 osil = osilwriter->writeOSiL( gams2osil->osinstance);
00976 osoptions->osil = osil;
00977 delete gams2osil;
00978 gams2osil = NULL;
00979 delete osilwriter;
00980 osilwriter = NULL;
00981 #else
00982 throw ErrorClass(
00983 "trying to convert Gams control file to osil without GAMSUTILS configured");
00984 #endif
00985 } catch (const ErrorClass& eclass) {
00986 std::cout << eclass.errormsg << std::endl;
00987 throw ErrorClass(eclass.errormsg);
00988 }
00989 }
00990
00991
00992 void getOSiLFromMps() {
00993 try {
00994 OSmps2osil *mps2osil = NULL;
00995 mps2osil = new OSmps2osil(osoptions->mpsFile);
00996 mps2osil->createOSInstance();
00997 OSiLWriter *osilwriter = NULL;
00998 osilwriter = new OSiLWriter();
00999 std::string osil;
01000 osil = osilwriter->writeOSiL(mps2osil->osinstance);
01001 osoptions->osil = osil;
01002 delete mps2osil;
01003 mps2osil = NULL;
01004 delete osilwriter;
01005 osilwriter = NULL;
01006 } catch (const ErrorClass& eclass) {
01007 std::cout << eclass.errormsg << std::endl;
01008 throw ErrorClass(eclass.errormsg);
01009 }
01010
01011 }
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01109
01112
01113
01114
01118
01119
01120
01121
01122
01123 std::string get_help() {
01124
01125 std::ostringstream helpMsg;
01126
01127 helpMsg << "************************* HELP *************************"
01128 << endl << endl;
01129 helpMsg
01130 << "In this HELP file we assume that the solve service method is used and "
01131 << endl;
01132 helpMsg
01133 << "that we are solving problems locally, that is the solver is on the "
01134 << endl;
01135 helpMsg
01136 << "machine running this OSSolverService. See Section 10.3 of the User\'s "
01137 << endl;
01138 helpMsg
01139 << "Manual for other service methods or calling a server remotely. "
01140 << endl;
01141 helpMsg << "The OSSolverService takes the parameters listed below. "
01142 << endl;
01143 helpMsg
01144 << "The order of the parameters is irrelevant. Not all the parameters "
01145 << endl;
01146 helpMsg << "are required. However, the location of an instance file is "
01147 << endl;
01148 helpMsg
01149 << "required when using the solve service method. The location of the "
01150 << endl;
01151 helpMsg << "instance file is specified using the osil option. " << endl;
01152
01153 helpMsg << endl;
01154
01155 helpMsg
01156 << "-osil xxx.osil this is the name of the file that contains the "
01157 << endl;
01158 helpMsg << "optimization instance in OSiL format. This option may be "
01159 << endl;
01160 helpMsg << "specified in the OSoL solver options file. " << endl;
01161
01162 helpMsg << endl;
01163
01164 helpMsg
01165 << "-osol xxx.osol this is the name of the file that contains the solver options. "
01166 << endl;
01167 helpMsg << "It is not necessary to specify this option. " << endl;
01168
01169 helpMsg << endl;
01170
01171 helpMsg
01172 << "-osrl xxx.osrl this is the name of the file to which the solver solution is written. "
01173 << endl;
01174 helpMsg
01175 << "It is not necessary to specify this option. If this option is not specified, "
01176 << endl;
01177 helpMsg << "the result will be printed to standard out. " << endl;
01178
01179 helpMsg << endl;
01180
01181 helpMsg
01182 << "-osplInput xxx.ospl this is the name of an input file in the OS Process"
01183 << endl;
01184 helpMsg << " Language (OSpL), this is used as input to the knock method."
01185 << endl;
01186
01187 helpMsg << endl;
01188
01189 helpMsg
01190 << "-osplOutput xxx.ospl this is the name of an output file in the OS Process"
01191 << endl;
01192 helpMsg
01193 << "Language (OSpL), this the output string from the knock and kill methods."
01194 << endl;
01195
01196 helpMsg << endl;
01197
01198 helpMsg << "-serviceLocation url is the URL of the solver service. "
01199 << endl;
01200 helpMsg
01201 << "This is not required, and if not specified it is assumed that "
01202 << endl;
01203 helpMsg << "the problem is solved locally. " << endl;
01204
01205 helpMsg << endl;
01206
01207 helpMsg
01208 << "-serviceMethod methodName this is the method on the solver service to be invoked. "
01209 << endl;
01210 helpMsg
01211 << "The options are solve, send, kill, knock, getJobID, and retrieve. "
01212 << endl;
01213 helpMsg
01214 << "This option is not required, and the default value is solve. "
01215 << endl;
01216
01217 helpMsg << endl;
01218
01219 helpMsg
01220 << "-mps xxx.mps this is the name of the mps file if the problem instance "
01221 << endl;
01222 helpMsg
01223 << "is in mps format. The default file format is OSiL so this option is not required. "
01224 << endl;
01225
01226 helpMsg << endl;
01227
01228 helpMsg
01229 << "-nl xxx.nl this is the name of the AMPL nl file if the problem "
01230 << endl;
01231 helpMsg
01232 << "instance is in AMPL nl format. The default file format is OSiL "
01233 << endl;
01234 helpMsg << "so this option is not required. " << endl;
01235
01236 helpMsg << endl;
01237
01238 helpMsg
01239 << "-solver solverName Possible values for default OS installation "
01240 << endl;
01241 helpMsg
01242 << "are bonmin(COIN-OR Bonmin), couenne (COIN-OR Couenne), clp (COIN-OR Clp),"
01243 << endl;
01244 helpMsg << "cbc (COIN-OR Cbc), dylp (COIN-OR DyLP), ipopt (COIN-OR Ipopt),"
01245 << endl;
01246 helpMsg << "and symphony (COIN-OR SYMPHONY). Other solvers supported"
01247 << endl;
01248 helpMsg
01249 << "(if the necessary libraries are present) are cplex (Cplex through COIN-OR Osi),"
01250 << endl;
01251 helpMsg
01252 << "glpk (glpk through COIN-OR Osi), knitro (Knitro), and lindo (LINDO)."
01253 << endl;
01254 helpMsg << "If no value is specified for this parameter," << endl;
01255 helpMsg << "then cbc is the default value of this parameter." << endl;
01256
01257 helpMsg << endl;
01258
01259 helpMsg
01260 << "-browser browserName this paramater is a path to the browser on the "
01261 << endl;
01262 helpMsg
01263 << "local machine. If this optional parameter is specified then the "
01264 << endl;
01265 helpMsg << "solver result in OSrL format is transformed using XSLT into "
01266 << endl;
01267 helpMsg << "HTML and displayed in the browser. " << endl;
01268
01269 helpMsg << endl;
01270
01271 helpMsg
01272 << "-config pathToConfigureFile this parameter specifies a path on "
01273 << endl;
01274 helpMsg
01275 << "the local machine to a text file containing values for the input parameters. "
01276 << endl;
01277 helpMsg
01278 << "This is convenient for the user not wishing to constantly retype parameter values. "
01279 << endl;
01280 helpMsg
01281 << "This configure file can contain values for all of the other parameters. "
01282 << endl;
01283
01284 helpMsg << endl;
01285
01286 helpMsg << "--version or -v get the current version of this executable "
01287 << endl;
01288
01289 helpMsg << endl;
01290
01291 helpMsg << "--help or -h to get this help file " << endl;
01292
01293 helpMsg << endl;
01294
01295 helpMsg
01296 << "Note: If you specify a configure file by using the -config option, you can "
01297 << endl;
01298 helpMsg
01299 << "override the values of the options in the configure file by putting them in "
01300 << endl;
01301 helpMsg << "at the command line. " << endl << endl;
01302
01303 helpMsg
01304 << "See the OS User\' Manual: http://www.coin-or.org/OS/doc/osUsersManual_2.0.pdf"
01305 << endl;
01306 helpMsg << "for more detail on how to use the OS project. " << endl;
01307
01308 helpMsg << endl;
01309 helpMsg << "********************************************************"
01310 << endl << endl;
01311
01312 return helpMsg.str();
01313 }
01314
01315
01316 std::string get_version() {
01317
01318 std::ostringstream versionMsg;
01319 versionMsg << "In order to find the version of this project " << endl;
01320 versionMsg << "connect to the directory where you downloaded " << endl;
01321 versionMsg << "and do: " << endl;
01322 versionMsg << "svn info " << endl;
01323
01324 return versionMsg.str();
01325 }
01326
01327
01328 std::string buildSolver(std::string solverName, std::string osol,
01329 OSInstance *osinstance) {
01330 DefaultSolver *solverType = NULL;
01331
01332 try {
01333 if (solverName == "") {
01334 if (osinstance == NULL)
01335 throw ErrorClass(
01336 "there was a NULL instance sent to buildSolver");
01337
01338 if (osinstance->getNumberOfIntegerVariables()
01339 + osinstance->getNumberOfBinaryVariables() > 0) {
01340 if ((osinstance->getNumberOfNonlinearExpressions() > 0)
01341 || (osinstance->getNumberOfQuadraticTerms() > 0)) {
01342 solverName = "bonmin";
01343 } else {
01344 solverName = "cbc";
01345 }
01346 } else {
01347 if ((osinstance->getNumberOfNonlinearExpressions() > 0)
01348 || (osinstance->getNumberOfQuadraticTerms() > 0)) {
01349 solverName = "ipopt";
01350 } else {
01351 solverName = "clp";
01352 }
01353 }
01354 }
01355
01356
01357
01358
01359
01360 if (solverName.find("ipopt") != std::string::npos) {
01361
01362 bool bIpoptIsPresent = false;
01363 #ifdef COIN_HAS_IPOPT
01364 bIpoptIsPresent = true;
01365 solverType = new IpoptSolver();
01366 #endif
01367 if (bIpoptIsPresent == false)
01368 throw ErrorClass("the Ipopt solver requested is not present");
01369 } else {
01370 if (solverName.find("lindo") != std::string::npos) {
01371
01372 bool bLindoIsPresent = false;
01373 #ifdef COIN_HAS_LINDO
01374 bLindoIsPresent = true;
01375 std::cout << "calling the LINDO Solver " << std::endl;
01376 solverType = new LindoSolver();
01377 std::cout << "DONE calling the LINDO Solver " << std::endl;
01378 #endif
01379 if (bLindoIsPresent == false)
01380 throw ErrorClass(
01381 "the Lindo solver requested is not present");
01382 } else {
01383 if (solverName.find("clp") != std::string::npos) {
01384
01385 solverType = new CoinSolver();
01386
01387 solverType->sSolverName = "clp";
01388 } else {
01389 if (solverName.find("cplex") != std::string::npos) {
01390 bool bCplexIsPresent = false;
01391 #ifdef COIN_HAS_CPX
01392 bCplexIsPresent = true;
01393 solverType = new CoinSolver();
01394 solverType->sSolverName = "cplex";
01395 #endif
01396 if (bCplexIsPresent == false)
01397 throw ErrorClass(
01398 "the Cplex solver requested is not present");
01399 } else {
01400 if (solverName.find("glpk") != std::string::npos) {
01401 bool bGlpkIsPresent = false;
01402 #ifdef COIN_HAS_GLPK
01403 bGlpkIsPresent = true;
01404 solverType = new CoinSolver();
01405 solverType->sSolverName = "glpk";
01406 #endif
01407 if (bGlpkIsPresent == false)
01408 throw ErrorClass(
01409 "the GLPK solver requested is not present");
01410 } else {
01411 if (solverName.find("dylp") != std::string::npos) {
01412 bool bDyLPIsPresent = false;
01413 #ifdef COIN_HAS_DYLP
01414 bDyLPIsPresent = true;
01415 solverType = new CoinSolver();
01416 solverType->sSolverName = "dylp";
01417 bDyLPIsPresent = true;
01418 #endif
01419 if (bDyLPIsPresent == false)
01420 throw ErrorClass(
01421 "the DyLP solver requested is not present");
01422 } else {
01423 if (solverName.find("symphony")
01424 != std::string::npos) {
01425 bool bSymphonyIsPresent = false;
01426 #ifdef COIN_HAS_SYMPHONY
01427 bSymphonyIsPresent = true;
01428 solverType = new CoinSolver();
01429 solverType->sSolverName = "symphony";
01430 #endif
01431 if (bSymphonyIsPresent == false)
01432 throw ErrorClass(
01433 "the SYMPHONY solver requested is not present");
01434 } else {
01435 if (solverName.find("knitro")
01436 != std::string::npos) {
01437 bool bKnitroIsPresent = false;
01438 #ifdef COIN_HAS_KNITRO
01439 bKnitroIsPresent = true;
01440 std::cout << "calling the KNITRO Solver " << std::endl;
01441 solverType = new KnitroSolver();
01442 std::cout << "DONE calling the KNITRO Solver " << std::endl;
01443 #endif
01444 if (bKnitroIsPresent == false)
01445 throw ErrorClass(
01446 "the Knitro solver requested is not present");
01447 } else {
01448 if (solverName.find("vol")
01449 != std::string::npos) {
01450 bool bVolIsPresent = false;
01451 #ifdef COIN_HAS_VOL
01452 bVolIsPresent = true;
01453 solverType = new CoinSolver();
01454 solverType->sSolverName = "vol";
01455 #endif
01456 if (bVolIsPresent == false)
01457 throw ErrorClass(
01458 "the Vol solver requested is not present");
01459 } else {
01460 if (solverName.find("bonmin")
01461 != std::string::npos) {
01462
01463 bool bBonminIsPresent = false;
01464 #ifdef COIN_HAS_BONMIN
01465 bBonminIsPresent = true;
01466 solverType = new BonminSolver();
01467 #endif
01468 if (bBonminIsPresent == false)
01469 throw ErrorClass(
01470 "the Bonmin solver requested is not present");
01471 } else {
01472 if (solverName.find("couenne")
01473 != std::string::npos) {
01474
01475 bool bCouenneIsPresent =
01476 false;
01477 #ifdef COIN_HAS_COUENNE
01478 bCouenneIsPresent = true;
01479 solverType = new CouenneSolver();
01480 #endif
01481 if (bCouenneIsPresent
01482 == false)
01483 throw ErrorClass(
01484 "the Couenne solver requested is not present");
01485 } else {
01486 solverType
01487 = new CoinSolver();
01488 solverType->sSolverName
01489 = "cbc";
01490 }
01491 }
01492 }
01493 }
01494 }
01495 }
01496 }
01497 }
01498 }
01499 }
01500 }
01501
01502
01503 solverType->osinstance = osinstance;
01504 solverType->osol = osol;
01505 solverType->buildSolverInstance();
01506 solverType->setSolverOptions();
01507 solverType->solve();
01508 std::string resultString = solverType->osrl;
01509 if (solverType != NULL)
01510 delete solverType;
01511 solverType = NULL;
01512 return resultString;
01513
01514 } catch (const ErrorClass& eclass) {
01515 throw eclass;
01516 }
01517
01518 }
01519