00001
00069 #include "OSCoinSolver.h"
00070 #include "OSResult.h"
00071 #include "OSiLReader.h"
00072 #include "OSiLWriter.h"
00073 #include "OSoLReader.h"
00074 #include "OSrLReader.h"
00075 #include "OSrLWriter.h"
00076 #include "OSInstance.h"
00077 #include "OSOption.h"
00078 #include "OSoLWriter.h"
00079 #include "OSFileUtil.h"
00080 #include "OSConfig.h"
00081 #include "OSDefaultSolver.h"
00082 #include "OSWSUtil.h"
00083 #include "OSSolverAgent.h"
00084 #include "OShL.h"
00085 #include "OSErrorClass.h"
00086 #include "OSmps2osil.h"
00087 #include "OSBase64.h"
00088 #include "OSRunSolver.h"
00089
00090 #ifdef COIN_HAS_KNITRO
00091 #include "OSKnitroSolver.h"
00092 #endif
00093
00094 #ifdef COIN_HAS_LINDO
00095 #include "OSLindoSolver.h"
00096 #endif
00097
00098 #ifdef COIN_HAS_ASL
00099 #include "OSnl2osil.h"
00100 #endif
00101
00102 #ifdef COIN_HAS_GAMSUTILS
00103 #include "OSgams2osil.hpp"
00104 #endif
00105
00106
00107
00108
00109
00110 #ifdef COIN_HAS_IPOPT
00111 #ifndef COIN_HAS_ASL
00112 #include "OSIpoptSolver.h"
00113 #undef COIN_HAS_ASL
00114 #else
00115 #include "OSIpoptSolver.h"
00116 #endif
00117 #endif
00118
00119 #ifdef COIN_HAS_BONMIN
00120 #include "OSBonminSolver.h"
00121 #endif
00122
00123 #ifdef COIN_HAS_COUENNE
00124 #include "OSCouenneSolver.h"
00125 #endif
00126
00127 #include "OSOptionsStruc.h"
00128
00129 #include<stdio.h>
00130 #include <map>
00131
00132
00133 using std::cout;
00134 using std::endl;
00135 using std::ostringstream;
00136 using std::string;
00137 using std::map;
00138
00139 #define DEBUG_CL_INTERFACE
00140
00141
00142 #define MAXCHARS 5000
00143
00144 typedef struct yy_buffer_state *YY_BUFFER_STATE;
00145 YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner);
00146
00147 void setyyextra(osOptionsStruc *osoptions, void* scanner);
00148 int ossslex(void* scanner);
00149 int ossslex_init(void** ptr);
00150 int ossslex_destroy(void* scanner);
00151
00152 void interactiveShell();
00153
00154 std::string get_help();
00155 std::string get_version();
00156 std::string get_options();
00157 void reset_options();
00158
00159
00160 void solve();
00161 void getJobID();
00162 void send();
00163 void kill();
00164 void retrieve();
00165 void knock();
00166
00167
00168 void getOSiLFromNl();
00169 void getOSiLFromMps();
00170 void getOSiLFromGams();
00171
00172
00173 void listOptions(osOptionsStruc *osoptions);
00174 void doPrintModel(osOptionsStruc *osoptions);
00175 void doPrintModel(OSInstance *osinstance);
00176 void doPrintRow(osOptionsStruc *osoptions);
00177 void doPrintRow(OSInstance *osinstance, std::string rownumberstring);
00178
00179
00180
00181
00182
00183 osOptionsStruc *osoptions;
00184
00185 inline void getServiceLocation()
00186 {
00187 std::cout
00188 << std::endl
00189 << "A service location is required"
00190 << std::endl;
00191 std::cout
00192 << "Please type the URL of the remote service: ";
00193 getline(std::cin, osoptions->serviceLocation);
00194 }
00195
00196 int main(int argC, const char* argV[])
00197 {
00198 WindowsErrorPopupBlocker();
00199 std::cout << OSgetVersionInfo();
00200
00201 if (argC < 2)
00202 {
00203 interactiveShell();
00204 return 0;
00205 }
00206
00207 void* scanner;
00208 FileUtil *fileUtil = NULL;
00209 FileUtil *inputFileUtil = NULL;
00210 char osss[MAXCHARS] = " ";
00211 const char *space = " ";
00212 const char *quote = "\"";
00213
00214 std::string configFileName = "";
00215 int i;
00216
00217
00218
00219
00220 osoptions = new osOptionsStruc();
00221 reset_options();
00222 bool scannerActive = false;
00223
00224 try
00225 {
00226
00227
00228 i = 1;
00229 bool addQuotes;
00230 while (i < argC)
00231 {
00232 addQuotes = false;
00233 if (argV[i][0] != '\"')
00234 for (int k=0; k<strlen(argV[i]); k++)
00235 {
00236 if (argV[i][k] == ' ')
00237 {
00238 addQuotes = true;
00239 break;
00240 }
00241 }
00242 if (addQuotes)
00243 {
00244 if (strlen(osss) + strlen(argV[i]) + 3 > MAXCHARS)
00245 throw ErrorClass("The command line exceeds allocated storage. Increase parameter MAXCHARS.");
00246 strcat(osss, quote);
00247 strcat(osss, argV[i]);
00248 strcat(osss, quote);
00249 strcat(osss, space);
00250 }
00251 else
00252 {
00253 if (strlen(osss) + strlen(argV[i]) + 1 > MAXCHARS)
00254 throw ErrorClass("The command line exceeds allocated storage. Increase parameter MAXCHARS.");
00255 strcat(osss, argV[i]);
00256 strcat(osss, space);
00257 }
00258 i++;
00259 }
00260
00261 #ifdef DEBUG_CL_INTERFACE
00262 cout << "Input String = " << osss << endl;
00263 #endif
00264
00265 scannerActive = true;
00266 ossslex_init(&scanner);
00267
00268 setyyextra(osoptions, scanner);
00269
00270 osss_scan_string(osss, scanner);
00271 #ifdef DEBUG_CL_INTERFACE
00272 std::cout << "call ossslex" << std::endl;
00273 #endif
00274 ossslex(scanner);
00275 ossslex_destroy(scanner);
00276 scannerActive = false;
00277 #ifdef DEBUG_CL_INTERFACE
00278 std::cout << "done with call to ossslex" << std::endl;
00279 #endif
00280
00281 if (osoptions->configFile != "")
00282 {
00283 scannerActive = true;
00284 ossslex_init(&scanner);
00285 configFileName = osoptions->configFile;
00286 #ifdef DEBUG_CL_INTERFACE
00287 cout << "configFileName = " << configFileName << endl;
00288 #endif
00289 std::string configFileOptions = fileUtil->getFileAsString(
00290 configFileName.c_str());
00291 #ifdef DEBUG_CL_INTERFACE
00292 std::cout << "Call Text Extra" << std::endl;
00293 #endif
00294 setyyextra(osoptions, scanner);
00295 #ifdef DEBUG_CL_INTERFACE
00296 std::cout << "Done with call Text Extra" << std::endl;
00297 #endif
00298 osss_scan_string(configFileOptions.c_str(), scanner);
00299 ossslex(scanner);
00300 ossslex_destroy(scanner);
00301 scannerActive = false;
00302
00303
00310 scannerActive = true;
00311 ossslex_init(&scanner);
00312
00313 setyyextra(osoptions, scanner);
00314
00315 osss_scan_string(osss, scanner);
00316 #ifdef DEBUG_CL_INTERFACE
00317 std::cout << "call ossslex" << std::endl;
00318 #endif
00319 ossslex(scanner);
00320 ossslex_destroy(scanner);
00321 scannerActive = false;
00322
00323
00325 }
00326 }
00327 catch (const ErrorClass& eclass)
00328 {
00329
00330
00331
00332
00333 OSResult *osresult = NULL;
00334 OSrLWriter *osrlwriter = NULL;
00335 osrlwriter = new OSrLWriter();
00336 osresult = new OSResult();
00337 osresult->setGeneralMessage(eclass.errormsg);
00338 osresult->setGeneralStatusType("error");
00339 std::string osrl = osrlwriter->writeOSrL(osresult);
00340 if (osoptions->osrlFile != "")
00341 {
00342
00343 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00344 if (osoptions->browser != "")
00345 {
00346 std::string str = osoptions->browser + " "
00347 + osoptions->osrlFile;
00348 const char *ch = &str[0];
00349 std::system(ch);
00350 }
00351 }
00352 else
00353 {
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
00366 delete fileUtil;
00367 delete osoptions;
00368 return 1;
00369 }
00370 try
00371 {
00372 if (osoptions->invokeHelp == true)
00373 {
00374 std::cout << std::endl << std::endl << get_help() << std::endl;
00375 delete osoptions;
00376 osoptions = NULL;
00377 return 0;
00378 }
00379 if (osoptions->writeVersion == true)
00380 {
00381 std::cout << std::endl << std::endl << OSgetVersionInfo() << std::endl;
00382 delete osoptions;
00383 osoptions = NULL;
00384 return 0;
00385 }
00386 }
00387 catch (const ErrorClass& eclass)
00388 {
00389
00390
00391
00392
00393
00394 OSResult *osresult = NULL;
00395 OSrLWriter *osrlwriter = NULL;
00396 osrlwriter = new OSrLWriter();
00397 osresult = new OSResult();
00398 osresult->setGeneralMessage(eclass.errormsg);
00399 osresult->setGeneralStatusType("error");
00400 std::string osrl = osrlwriter->writeOSrL(osresult);
00401 if (osoptions->osrlFile != "")
00402 {
00403
00404 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00405 if (osoptions->browser != "")
00406 {
00407 std::string str = osoptions->browser + " "
00408 + osoptions->osrlFile;
00409 const char *ch = &str[0];
00410 std::system(ch);
00411 }
00412 }
00413 else
00414 {
00415
00416 std::cout << osrl << std::endl;
00417 }
00418
00419 delete osresult;
00420 osresult = NULL;
00421 delete osrlwriter;
00422 osrlwriter = NULL;
00423
00424
00425
00426 delete osoptions;
00427 osoptions = NULL;
00428 delete inputFileUtil;
00429 inputFileUtil = NULL;
00430 return 1;
00431 }
00432
00433 #ifdef DEBUG_CL_INTERFACE
00434 cout << "HERE ARE THE OPTION VALUES:" << endl;
00435 if(osoptions->configFile != "") cout << "Config file = " << osoptions->configFile << endl;
00436 if(osoptions->osilFile != "") cout << "OSiL file = " << osoptions->osilFile << endl;
00437 if(osoptions->osolFile != "") cout << "OSoL file = " << osoptions->osolFile << endl;
00438 if(osoptions->osrlFile != "") cout << "OSrL file = " << osoptions->osrlFile << endl;
00439
00440 if(osoptions->osplInputFile != "") cout << "OSpL Input file = " << osoptions->osplInputFile << endl;
00441 if(osoptions->serviceMethod != "") cout << "Service Method = " << osoptions->serviceMethod << endl;
00442 if(osoptions->mpsFile != "") cout << "MPS File Name = " << osoptions->mpsFile << endl;
00443 if(osoptions->nlFile != "") cout << "NL File Name = " << osoptions->nlFile << endl;
00444 if(osoptions->gamsControlFile != "") cout << "gams Control File Name = " << osoptions->gamsControlFile << endl;
00445 if(osoptions->browser != "") cout << "Browser Value = " << osoptions->browser << endl;
00446 if(osoptions->solverName != "") cout << "Selected Solver = " << osoptions->solverName << endl;
00447 if(osoptions->serviceLocation != "") cout << "Service Location = " << osoptions->serviceLocation << endl;
00448 if(osoptions->printModel) cout << "print model prior to solve/send" << endl;
00449 if(osoptions->printRowNumberAsString != "") cout << "print model row " << osoptions->printRowNumberAsString << " prior to solve/send" << endl;
00450
00451 #endif
00452
00453
00454 unsigned int k;
00455 for (k = 0; k < osoptions->solverName.length(); k++)
00456 {
00457 osoptions->solverName[k] = (char)tolower(osoptions->solverName[k]);
00458 }
00459
00460
00461 fileUtil = new FileUtil();
00462 try
00463 {
00464
00465 if (osoptions->osolFile != "")
00466 {
00467
00468 osoptions->osol = fileUtil->getFileAsString(
00469 (osoptions->osolFile).c_str());
00470
00471 }
00472
00473 if (osoptions->osilFile != "")
00474 {
00475
00476 osoptions->osil = fileUtil->getFileAsString(
00477 (osoptions->osilFile).c_str());
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 if (osoptions->osplInputFile != "")
00490 osoptions->osplInput = fileUtil->getFileAsString(
00491 (osoptions->osplInputFile).c_str());
00492
00493
00494
00495
00496 }
00497 catch (const ErrorClass& eclass)
00498 {
00499
00500
00501
00502
00503
00504
00505 OSResult *osresult = NULL;
00506 OSrLWriter *osrlwriter = NULL;
00507 osrlwriter = new OSrLWriter();
00508 osresult = new OSResult();
00509 osresult->setGeneralMessage(eclass.errormsg);
00510 osresult->setGeneralStatusType("error");
00511 std::string osrl = osrlwriter->writeOSrL(osresult);
00512 if (osoptions->osrlFile != "")
00513 {
00514
00515 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00516 if (osoptions->browser != "")
00517 {
00518 std::string str = osoptions->browser + " "
00519 + osoptions->osrlFile;
00520 const char *ch = &str[0];
00521 std::system(ch);
00522 }
00523 }
00524 else
00525 {
00526
00527 std::cout << osrl << std::endl;
00528 }
00529
00530 delete osresult;
00531 osresult = NULL;
00532 delete osrlwriter;
00533 osrlwriter = NULL;
00534
00535
00536
00537 delete osoptions;
00538 osoptions = NULL;
00539 delete fileUtil;
00540 fileUtil = NULL;
00541 return 1;
00542 }
00543
00544
00545 if (osoptions->serviceMethod == "") osoptions->serviceMethod = "solve";
00546 if (osoptions->serviceMethod[0] == 's')
00547 {
00548 if (osoptions->printModel == true)
00549 doPrintModel(osoptions);
00550 else if (osoptions->printRowNumberAsString != "")
00551 doPrintRow(osoptions);
00552 if (osoptions->serviceMethod[1] == 'e')
00553 send();
00554 else
00555 solve();
00556 }
00557 else
00558 {
00559 switch (osoptions->serviceMethod[0])
00560 {
00561 case 'g':
00562 getJobID();
00563 break;
00564 case 'r':
00565 retrieve();
00566 break;
00567 case 'k':
00568 if (osoptions->serviceMethod[1] == 'i')
00569 kill();
00570 else
00571 knock();
00572 break;
00573 default:
00574
00575 break;
00576 }
00577 }
00578 delete osoptions;
00579 osoptions = NULL;
00580 delete fileUtil;
00581 fileUtil = NULL;
00582 return 0;
00583 }
00584
00585 void solve()
00586 {
00587 std::string osrl = "";
00588 OSiLReader *osilreader = NULL;
00589 OSmps2osil *mps2osil = NULL;
00590 #ifdef COIN_HAS_ASL
00591 OSnl2osil *nl2osil = NULL;
00592 #endif
00593 #ifdef COIN_HAS_GAMSUTILS
00594 OSgams2osil *gams2osil = NULL;
00595 #endif
00596 OSSolverAgent* osagent = NULL;
00597 FileUtil *fileUtil = NULL;
00598 fileUtil = new FileUtil();
00599
00600 try
00601 {
00602 if (osoptions->serviceLocation != "")
00603 {
00604
00605 if (osoptions->osil == "")
00606 {
00607
00608 if (osoptions->nlFile != "")
00609 {
00610 getOSiLFromNl();
00611 }
00612 else
00613 {
00614 if (osoptions->mpsFile != "")
00615 {
00616 getOSiLFromMps();
00617 }
00618 else
00619 {
00620 if (osoptions->gamsControlFile != "")
00621 {
00622
00623 getOSiLFromGams();
00624 }
00625 else
00626 {
00627 osoptions->osil = "";
00628 }
00629 }
00630 }
00631 }
00632
00633 if (osoptions->printModel)
00634 doPrintModel(osoptions);
00635 else if (osoptions->printRowNumberAsString != "")
00636 doPrintRow(osoptions);
00637
00638
00639 osagent = new OSSolverAgent(osoptions->serviceLocation);
00640
00641 if (osoptions->osol == "")
00642 {
00643
00644 std::ostringstream outStr;
00645 outStr
00646 << "<?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/";
00647 outStr << OS_SCHEMA_VERSION;
00648 outStr << "/OSoL.xsd\"></osol>";
00649 osoptions->osol = outStr.str();
00650 }
00651 osrl = osagent->solve(osoptions->osil, osoptions->osol);
00652 if (osoptions->osrlFile != "")
00653 {
00654 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00655
00656 if (osoptions->browser != "")
00657 {
00658 std::string str = osoptions->browser + " "
00659 + osoptions->osrlFile;
00660 const char *ch = &str[0];
00661 std::system(ch);
00662 }
00663 }
00664 else
00665 cout << osrl << endl;
00666 delete osagent;
00667 osagent = NULL;
00668
00669 }
00670 else
00671 {
00672 OSInstance *osinstance;
00673 if (osoptions->osil != "")
00674 {
00675 osilreader = new OSiLReader();
00676 osinstance = osilreader->readOSiL(osoptions->osil);
00677 }
00678 else
00679 {
00680
00681 if (osoptions->nlFile != "")
00682 {
00683 #ifdef COIN_HAS_ASL
00684 nl2osil = new OSnl2osil( osoptions->nlFile);
00685 nl2osil->createOSInstance();
00686 osinstance = nl2osil->osinstance;
00687 #else
00688 throw ErrorClass(
00689 "nl file specified locally but ASL not present");
00690 #endif
00691 }
00692 else
00693 {
00694 if (osoptions->mpsFile != "")
00695 {
00696 mps2osil = new OSmps2osil(osoptions->mpsFile);
00697 mps2osil->createOSInstance();
00698 osinstance = mps2osil->osinstance;
00699 }
00700 else
00701 {
00702 if (osoptions->gamsControlFile != "")
00703 {
00704 #ifdef COIN_HAS_GAMSUTILS
00705 gams2osil = new OSgams2osil( osoptions->gamsControlFile);
00706 gams2osil->createOSInstance();
00707 osinstance = gams2osil->osinstance;
00708 #else
00709 throw ErrorClass(
00710 "a Gams Control specified locally but GAMSIP not present");
00711 #endif
00712
00713 }
00714 else
00715 {
00716
00717 throw ErrorClass(
00718 "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.");
00719
00720 }
00721 }
00722 }
00723 }
00724 if (osoptions->printModel)
00725 doPrintModel(osinstance);
00726 else if (osoptions->printRowNumberAsString != "")
00727 doPrintRow(osinstance, osoptions->printRowNumberAsString);
00728
00729 osrl = runSolver(osoptions->solverName, osoptions->osol, osinstance);
00730
00731
00732
00733 if (osoptions->osrlFile != "")
00734 {
00735
00736 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00737
00738
00739 if (osoptions->browser != "")
00740 {
00741 std::string str = osoptions->browser + " "
00742 + osoptions->osrlFile;
00743 const char *ch = &str[0];
00744 std::system(ch);
00745 }
00746 }
00747 else
00748 cout << osrl << endl;
00749
00750 }
00751
00752
00753
00754 if (osilreader != NULL)
00755 delete osilreader;
00756 osilreader = NULL;
00757 if (mps2osil != NULL)
00758 delete mps2osil;
00759 mps2osil = NULL;
00760 #ifdef COIN_HAS_ASL
00761 if(nl2osil != NULL) delete nl2osil;
00762 nl2osil = NULL;
00763 #endif
00764 #ifdef COIN_HAS_GAMSUTILS
00765 if(gams2osil != NULL) delete gams2osil;
00766 gams2osil = NULL;
00767 #endif
00768 delete fileUtil;
00769 fileUtil = NULL;
00770
00771 }
00772 catch (const ErrorClass& eclass)
00773 {
00774 std::string osrl = "";
00775 OSResult *osresult = NULL;
00776 OSrLWriter *osrlwriter = NULL;
00777
00778
00779 std::string::size_type pos1 = eclass.errormsg.find( "<osrl");
00780 if(pos1 == std::string::npos)
00781 {
00782 osrlwriter = new OSrLWriter();
00783 osresult = new OSResult();
00784 osresult->setGeneralMessage(eclass.errormsg);
00785 osresult->setGeneralStatusType("error");
00786 osrl = osrlwriter->writeOSrL(osresult);
00787 }
00788 else
00789 {
00790 osrl = eclass.errormsg;
00791 }
00792 if (osoptions->osrlFile != "")
00793 {
00794
00795 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
00796 if (osoptions->browser != "")
00797 {
00798 std::string str = osoptions->browser + " "
00799 + osoptions->osrlFile;
00800 const char *ch = &str[0];
00801 std::system(ch);
00802 }
00803 }
00804 else
00805 {
00806
00807 std::cout << osrl << std::endl;
00808 }
00809
00810 if(osresult != NULL)
00811 {
00812 delete osresult;
00813 osresult = NULL;
00814 }
00815 if(osrlwriter != NULL)
00816 {
00817 delete osrlwriter;
00818 osrlwriter = NULL;
00819 }
00820
00821
00822 if (osilreader != NULL)
00823 delete osilreader;
00824 osilreader = NULL;
00825 if (mps2osil != NULL)
00826 delete mps2osil;
00827 mps2osil = NULL;
00828 #ifdef COIN_HAS_ASL
00829 if(nl2osil != NULL) delete nl2osil;
00830 nl2osil = NULL;
00831 #endif
00832 #ifdef COIN_HAS_GAMSUTILS
00833 if(gams2osil != NULL) delete gams2osil;
00834 gams2osil = NULL;
00835 #endif
00836 delete fileUtil;
00837 fileUtil = NULL;
00838 }
00839
00840 }
00841
00842 void getJobID()
00843 {
00844 OSSolverAgent* osagent = NULL;
00845 try
00846 {
00847 if (osoptions->serviceLocation != "")
00848 {
00849 osagent = new OSSolverAgent(osoptions->serviceLocation);
00850 osoptions->jobID = osagent->getJobID(osoptions->osol);
00851 cout << osoptions->jobID << endl;
00852 delete osagent;
00853 osagent = NULL;
00854 }
00855 else
00856 {
00857 delete osagent;
00858 osagent = NULL;
00859 throw ErrorClass("please specify service location (url)");
00860 }
00861 }
00862 catch (const ErrorClass& eclass)
00863 {
00864 FileUtil *fileUtil = NULL;
00865 fileUtil = new FileUtil();
00866
00867
00868 std::string osrl = "";
00869 OSResult *osresult = NULL;
00870 OSrLWriter *osrlwriter = NULL;
00871
00872
00873 string::size_type pos1 = eclass.errormsg.find( "<osrl");
00874 if(pos1 == std::string::npos)
00875 {
00876 osrlwriter = new OSrLWriter();
00877 osresult = new OSResult();
00878 osresult->setGeneralMessage(eclass.errormsg);
00879 osresult->setGeneralStatusType("error");
00880 osrl = osrlwriter->writeOSrL(osresult);
00881 }
00882 else
00883 {
00884 osrl = eclass.errormsg;
00885 }
00886
00887
00888
00889 if(osresult != NULL)
00890 {
00891 delete osresult;
00892 osresult = NULL;
00893 }
00894 if(osrlwriter != NULL)
00895 {
00896 delete osrlwriter;
00897 osrlwriter = NULL;
00898 }
00899
00900
00901 delete fileUtil;
00902 fileUtil = NULL;
00903 }
00904 }
00905
00906
00907 void knock()
00908 {
00909 std::string osplOutput = "";
00910 OSSolverAgent* osagent = NULL;
00911 FileUtil *fileUtil = NULL;
00912 fileUtil = new FileUtil();
00913 try
00914 {
00915 if (osoptions->serviceLocation != "")
00916 {
00917 osagent = new OSSolverAgent(osoptions->serviceLocation);
00918
00919
00920 if (osoptions->osol == "")
00921 {
00922
00923 OSOption *osOption = NULL;
00924 osOption = new OSOption();
00925
00926 if(osoptions->jobID == "") osOption->setJobID( osoptions->jobID);
00927
00928 OSoLWriter *osolWriter = NULL;
00929 osolWriter = new OSoLWriter();
00930 osoptions->osol = osolWriter->writeOSoL( osOption);
00931 delete osOption;
00932 osOption = NULL;
00933 delete osolWriter;
00934 osolWriter = NULL;
00935 }
00936
00937
00938 osplOutput = osagent->knock(osoptions->osplInput, osoptions->osol);
00939 if (osoptions->osplOutputFile != "")
00940 fileUtil->writeFileFromString(osoptions->osplOutputFile,
00941 osplOutput);
00942 else
00943 cout << osplOutput << endl;
00944 delete osagent;
00945 }
00946 else
00947 {
00948 delete osagent;
00949 throw ErrorClass("please specify service location (url)");
00950 }
00951 delete fileUtil;
00952 fileUtil = NULL;
00953 }
00954 catch (const ErrorClass& eclass)
00955 {
00956 std::string osrl = "";
00957 OSResult *osresult = NULL;
00958 OSrLWriter *osrlwriter = NULL;
00959
00960
00961 string::size_type pos1 = eclass.errormsg.find( "<osrl");
00962 if(pos1 == std::string::npos)
00963 {
00964 osrlwriter = new OSrLWriter();
00965 osresult = new OSResult();
00966 osresult->setGeneralMessage(eclass.errormsg);
00967 osresult->setGeneralStatusType("error");
00968 std::string osrl = osrlwriter->writeOSrL(osresult);
00969 }
00970 else
00971 {
00972 osrl = eclass.errormsg;
00973 }
00974
00975 if(osresult != NULL)
00976 {
00977 delete osresult;
00978 osresult = NULL;
00979 }
00980 if(osrlwriter != NULL)
00981 {
00982 delete osrlwriter;
00983 osrlwriter = NULL;
00984 }
00985
00986 delete fileUtil;
00987 fileUtil = NULL;
00988 }
00989 }
00990
00991
00992 void send()
00993 {
00994 bool bSend = false;
00995 OSSolverAgent* osagent = NULL;
00996 try
00997 {
00998
00999 if (osoptions->osil == "")
01000 {
01001
01002 if (osoptions->nlFile != "")
01003 {
01004 getOSiLFromNl();
01005 }
01006 else
01007 {
01008 if (osoptions->mpsFile != "")
01009 {
01010 getOSiLFromMps();
01011 }
01012 else
01013 {
01014 osoptions->osil = "";
01015 }
01016 }
01017 }
01018 if (osoptions->serviceLocation != "")
01019 {
01020 osagent = new OSSolverAgent(osoptions->serviceLocation);
01021
01022 if (osoptions->osol == "")
01023 {
01024
01025 OSOption *osOption = NULL;
01026 osOption = new OSOption();
01027
01028 if(osoptions->jobID == "") osoptions->jobID = osagent->getJobID("");
01029
01030
01031 osOption->setJobID( osoptions->jobID);
01032
01033 OSoLWriter *osolWriter = NULL;
01034 osolWriter = new OSoLWriter();
01035 osoptions->osol = osolWriter->writeOSoL( osOption);
01036 delete osOption;
01037 osOption = NULL;
01038 delete osolWriter;
01039 osolWriter = NULL;
01040 }
01041 bSend = osagent->send(osoptions->osil, osoptions->osol);
01042 if(bSend == true){
01043 std::cout << "Successful send " << std::endl;
01044 }else{
01045 std::cout << "Send failed, check to make sure you sent a jobID not on the system. " << std::endl;
01046 }
01047
01048 delete osagent;
01049 }
01050 else
01051 {
01052 delete osagent;
01053 throw ErrorClass("please specify service location (url)");
01054 }
01055 }
01056 catch (const ErrorClass& eclass)
01057 {
01058 std::string osrl = "";
01059 FileUtil *fileUtil = NULL;
01060 fileUtil = new FileUtil();
01061 OSResult *osresult = NULL;
01062 OSrLWriter *osrlwriter = NULL;
01063
01064
01065 string::size_type pos1 = eclass.errormsg.find( "<osrl");
01066 if(pos1 == std::string::npos)
01067 {
01068 osrlwriter = new OSrLWriter();
01069 osresult = new OSResult();
01070 osresult->setGeneralMessage(eclass.errormsg);
01071 osresult->setGeneralStatusType("error");
01072 osrl = osrlwriter->writeOSrL(osresult);
01073 }
01074 else
01075 {
01076 osrl = eclass.errormsg;
01077 }
01078
01079
01080 if (osoptions->osrlFile != "")
01081 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
01082 else
01083 cout << osrl << endl;
01084
01085 if(osresult != NULL)
01086 {
01087 delete osresult;
01088 osresult = NULL;
01089 }
01090 if(osrlwriter != NULL)
01091 {
01092 delete osrlwriter;
01093 osrlwriter = NULL;
01094 }
01095 delete fileUtil;
01096 fileUtil = NULL;
01097 }
01098 }
01099
01100 void retrieve()
01101 {
01102 FileUtil *fileUtil = NULL;
01103 fileUtil = new FileUtil();
01104 std::string osrl = "";
01105 OSSolverAgent* osagent = NULL;
01106 try
01107 {
01108 if (osoptions->serviceLocation != "")
01109 {
01110 osagent = new OSSolverAgent(osoptions->serviceLocation);
01111
01112
01113 if (osoptions->osol == "")
01114 {
01115
01116 OSOption *osOption = NULL;
01117 osOption = new OSOption();
01118
01119 if(osoptions->jobID == "")throw ErrorClass("there is no JobID");
01120
01121 osOption->setJobID( osoptions->jobID);
01122
01123 OSoLWriter *osolWriter = NULL;
01124 osolWriter = new OSoLWriter();
01125 osoptions->osol = osolWriter->writeOSoL( osOption);
01126 delete osOption;
01127 osOption = NULL;
01128 delete osolWriter;
01129 osolWriter = NULL;
01130 }
01131
01132 osrl = osagent->retrieve(osoptions->osol);
01133
01134 if (osoptions->osrlFile != "")
01135 {
01136 fileUtil->writeFileFromString(osoptions->osrlFile, osrl);
01137 cout << "Solver Result Written to File: " << osoptions->osrlFile << endl;
01138 if (osoptions->browser != "")
01139 {
01140 std::string str = osoptions->browser + " "
01141 + osoptions->osrlFile;
01142 const char *ch = &str[0];
01143 std::system(ch);
01144 }
01145 }
01146 else
01147 cout << osrl << endl;
01148 delete osagent;
01149 osagent = NULL;
01150 }
01151 else
01152 {
01153 delete osagent;
01154 osagent = NULL;
01155 throw ErrorClass("please specify service location (url)");
01156 }
01157 delete fileUtil;
01158 fileUtil = NULL;
01159 }
01160 catch (const ErrorClass& eclass)
01161 {
01162
01163 std::string osrl = "";
01164 OSResult *osresult = NULL;
01165 OSrLWriter *osrlwriter = NULL;
01166
01167
01168 string::size_type pos1 = eclass.errormsg.find( "<osrl");
01169 if(pos1 == std::string::npos)
01170 {
01171 osrlwriter = new OSrLWriter();
01172 osresult = new OSResult();
01173 osresult->setGeneralMessage(eclass.errormsg);
01174 osresult->setGeneralStatusType("error");
01175 osrl = osrlwriter->writeOSrL(osresult);
01176 }
01177 else
01178 {
01179 osrl = eclass.errormsg;
01180 }
01181
01182 if(osresult != NULL)
01183 {
01184 delete osresult;
01185 osresult = NULL;
01186 }
01187 if(osrlwriter != NULL)
01188 {
01189 delete osrlwriter;
01190 osrlwriter = NULL;
01191 }
01192
01193
01194 delete fileUtil;
01195 fileUtil = NULL;
01196 }
01197 }
01198
01199 void kill()
01200 {
01201 FileUtil *fileUtil = NULL;
01202 fileUtil = new FileUtil();
01203 std::string osplOutput = "";
01204 OSSolverAgent* osagent = NULL;
01205 try
01206 {
01207 if (osoptions->serviceLocation != "")
01208 {
01209 osagent = new OSSolverAgent(osoptions->serviceLocation);
01210
01211 if (osoptions->osol == "")
01212 {
01213
01214 OSOption *osOption = NULL;
01215 osOption = new OSOption();
01216
01217 if(osoptions->jobID == "")throw ErrorClass("there is no JobID");
01218
01219 osOption->setJobID( osoptions->jobID);
01220
01221 OSoLWriter *osolWriter = NULL;
01222 osolWriter = new OSoLWriter();
01223 osoptions->osol = osolWriter->writeOSoL( osOption);
01224 delete osOption;
01225 osOption = NULL;
01226 delete osolWriter;
01227 osolWriter = NULL;
01228 }
01229
01230 osplOutput = osagent->kill(osoptions->osol);
01231
01232 if (osoptions->osplOutputFile != "")
01233 fileUtil->writeFileFromString(osoptions->osplOutputFile,
01234 osplOutput);
01235 else
01236 cout << osplOutput << endl;
01237 delete osagent;
01238 osagent = NULL;
01239 }
01240 else
01241 {
01242 delete osagent;
01243 osagent = NULL;
01244 throw ErrorClass("please specify service location (url)");
01245 }
01246 delete fileUtil;
01247 fileUtil = NULL;
01248 }
01249 catch (const ErrorClass& eclass)
01250 {
01251 std::string osrl = "";
01252 OSResult *osresult = NULL;
01253 OSrLWriter *osrlwriter = NULL;
01254
01255
01256 string::size_type pos1 = eclass.errormsg.find( "<osrl");
01257 if(pos1 == std::string::npos)
01258 {
01259 osrlwriter = new OSrLWriter();
01260 osresult = new OSResult();
01261 osresult->setGeneralMessage(eclass.errormsg);
01262 osresult->setGeneralStatusType("error");
01263 osrl = osrlwriter->writeOSrL(osresult);
01264 }
01265 else
01266 {
01267 osrl = eclass.errormsg;
01268 }
01269
01270
01271 if(osresult != NULL)
01272 {
01273 delete osresult;
01274 osresult = NULL;
01275 }
01276 if(osrlwriter != NULL)
01277 {
01278 delete osrlwriter;
01279 osrlwriter = NULL;
01280 }
01281
01282
01283 delete fileUtil;
01284 fileUtil = NULL;
01285 }
01286 }
01287
01288 void getOSiLFromNl()
01289 {
01290 try
01291 {
01292 #ifdef COIN_HAS_ASL
01293 OSnl2osil *nl2osil = NULL;
01294 nl2osil = new OSnl2osil( osoptions->nlFile);
01295 nl2osil->createOSInstance();
01296 OSiLWriter *osilwriter = NULL;
01297 osilwriter = new OSiLWriter();
01298 std::string osil;
01299 osil = osilwriter->writeOSiL( nl2osil->osinstance);
01300 osoptions->osil = osil;
01301 delete nl2osil;
01302 nl2osil = NULL;
01303 delete osilwriter;
01304 osilwriter = NULL;
01305 #else
01306 throw ErrorClass(
01307 "trying to convert nl to osil without AMPL ASL configured");
01308 #endif
01309 }
01310 catch (const ErrorClass& eclass)
01311 {
01312 std::cout << eclass.errormsg << std::endl;
01313 throw ErrorClass(eclass.errormsg);
01314 }
01315 }
01316
01317
01318 void getOSiLFromGams()
01319 {
01320 try
01321 {
01322 #ifdef COIN_HAS_GAMSIO
01323 OSgams2osil *gams2osil = NULL;
01324 gams2osil = new OSgams2osil( osoptions->gamsControlFile);
01325 gams2osil->createOSInstance();
01326 OSiLWriter *osilwriter = NULL;
01327 osilwriter = new OSiLWriter();
01328 std::string osil;
01329 osil = osilwriter->writeOSiL( gams2osil->osinstance);
01330 osoptions->osil = osil;
01331 delete gams2osil;
01332 gams2osil = NULL;
01333 delete osilwriter;
01334 osilwriter = NULL;
01335 #else
01336 throw ErrorClass(
01337 "trying to convert Gams control file to osil without GAMSUTILS configured");
01338 #endif
01339 }
01340 catch (const ErrorClass& eclass)
01341 {
01342 std::cout << eclass.errormsg << std::endl;
01343 throw ErrorClass(eclass.errormsg);
01344 }
01345 }
01346
01347
01348 void getOSiLFromMps()
01349 {
01350 try
01351 {
01352 OSmps2osil *mps2osil = NULL;
01353 mps2osil = new OSmps2osil(osoptions->mpsFile);
01354 mps2osil->createOSInstance();
01355 OSiLWriter *osilwriter = NULL;
01356 osilwriter = new OSiLWriter();
01357 std::string osil;
01358 osil = osilwriter->writeOSiL(mps2osil->osinstance);
01359 osoptions->osil = osil;
01360 delete mps2osil;
01361 mps2osil = NULL;
01362 delete osilwriter;
01363 osilwriter = NULL;
01364 }
01365 catch (const ErrorClass& eclass)
01366 {
01367 std::cout << eclass.errormsg << std::endl;
01368 throw ErrorClass(eclass.errormsg);
01369 }
01370
01371 }
01372
01373 void interactiveShell()
01374 {
01375 void* scanner;
01376 FileUtil *fileUtil = NULL;
01377
01378 std::string configFileName = "";
01379
01380
01381 osoptions = new osOptionsStruc();
01382
01383 bool scannerActive = false;
01384
01385
01386 scannerActive = true;
01387 ossslex_init(&scanner);
01388 setyyextra(osoptions, scanner);
01389 std::string lineText;
01390
01391 std::string wordSep = " ";
01392 std::string dblQuote = "\"";
01393 std::string optionName = "";
01394 std::string optionValue = "";
01395 std::string::size_type indexStart;
01396 std::string::size_type indexEnd;
01397 unsigned int k;
01398
01399
01400 std::string commandArray[14] = { "solve", "send", "getJobID", "retrieve", "kill", "knock",
01401 "quit", "exit", "reset", "list", "?", "help", "version",
01402 "printModel"
01403 };
01404
01405
01406 std::string optionArray[11] = { "osil", "osrl", "osol", "mps", "nl", "dat",
01407 "serviceLocation", "solver", "osplInput", "osplOutput",
01408 "printRow"
01409 };
01410
01411
01412 size_t size_of_commandArray = (sizeof commandArray)
01413 / (sizeof commandArray[0]);
01414
01415 size_t size_of_optionArray = (sizeof optionArray)
01416 / (sizeof optionArray[0]);
01417
01418
01419
01420
01421 std::map<string, int> commandMap;
01422
01423
01424 for(k = 0; k < size_of_commandArray; k++)
01425 {
01426 commandMap[ commandArray[ k] ] = k;
01427 }
01428
01429
01430
01431 std::map<string, int> optionMap;
01432
01433
01434 for(k = 0; k < size_of_optionArray; k++)
01435 {
01436 optionMap[ optionArray[ k] ] = k;
01437 }
01438
01439
01440
01441
01442
01443 std::cout << "At the prompt enter a valid command or option value pair.\n";
01444 std::cout << "Enter the \"solve\" command to optimize.\n";
01445 std::cout << "Type \"quit\" or \"exit\" to leave the application. \n";
01446 std::cout << "Type \"help\" or \"?\" for a list of valid options.\n\n";
01447
01448
01449 while (osoptions->quit != true && osoptions->exit != true)
01450 {
01451 std::cout << "Please enter a command, or an option followed by an option value: ";
01452 getline(std::cin, lineText);
01453 lineText = " " + lineText + " ";
01454
01455 indexStart = lineText.find_first_not_of(wordSep);
01456 if (indexStart == string::npos)
01457 {
01458 std::cout << std::endl;
01459 std::cout << "You did not enter a valid option. "
01460 << "Type \"help\" or \"?\" for a list of valid options."
01461 << std::endl;
01462 }
01463 else
01464 {
01465 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
01466 optionName = lineText.substr(indexStart, indexEnd
01467 - indexStart);
01468
01469
01470 if( (commandMap.find(optionName) == commandMap.end() ) &&
01471 (optionMap.find(optionName) == optionMap.end() ) )
01472 {
01473 std::cout << std::endl;
01474 std::cout << "You did not enter a valid option. "
01475 << "Type \"help\" or \"?\" for a list of valid options."
01476 << std::endl;
01477 }
01478 else
01479 {
01480 int skipChars;
01481
01482
01483 indexStart = lineText.find_first_not_of(wordSep,
01484 indexEnd + 1);
01485 if (indexStart != std::string::npos && lineText[indexStart] == '\"')
01486 {
01487 indexEnd = lineText.find_first_of(dblQuote, indexStart + 1);
01488 skipChars = 1;
01489 }
01490 else
01491 {
01492 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
01493 skipChars = 0;
01494 }
01495 if (indexStart != std::string::npos && indexEnd
01496 != std::string::npos)
01497 {
01498 optionValue = lineText.substr(indexStart + skipChars,
01499 indexEnd - indexStart - skipChars);
01500 }
01501 else
01502 {
01503 optionValue = "";
01504 }
01505
01506
01507
01508 try
01509 {
01510
01511 if( commandMap.find(optionName) != commandMap.end() )
01512 {
01513 switch (commandMap[ optionName] )
01514 {
01515
01516 case 0:
01517
01518 if(osoptions->osil == "" && osoptions->mps == "" && osoptions->nl == "")
01519 {
01520 std::cout
01521 << std::endl
01522 << "You did not specify an optimization instance!!!\n"
01523 << "Please enter file format option (osil, nl, or mps) \n"
01524 << "followed by the option value which is the file location. \n"
01525 << std::endl;
01526 }
01527 else
01528 {
01529 solve();
01530 if (osoptions->osrlFile != "")
01531 std::cout << "\nSolve command executed. Please see " << osoptions->osrlFile << " for results." << std::endl;
01532 }
01533 break;
01534
01535 case 1:
01536
01537 if(osoptions->serviceLocation == "")
01538 getServiceLocation();
01539 send();
01540 break;
01541
01542
01543 case 2:
01544
01545 if(osoptions->serviceLocation == "")
01546 getServiceLocation();
01547 getJobID();
01548 break;
01549
01550
01551 case 3:
01552
01553 if(osoptions->serviceLocation == "")
01554 getServiceLocation();
01555
01556 if( (osoptions->osolFile == "") && (osoptions->jobID == "") )
01557 {
01558 std::cout
01559 << std::endl
01560 << "Cannot retrieve: no JobID and no OSoL file"
01561 << std::endl;
01562 }
01563 else
01564 {
01565 retrieve();
01566 }
01567
01568 break;
01569
01570 case 4:
01571
01572
01573 if(osoptions->serviceLocation == "")
01574 getServiceLocation();
01575
01576 if( (osoptions->osolFile == "") && (osoptions->jobID == "") )
01577 {
01578 std::cout
01579 << std::endl
01580 << "Cannot kill: no JobID and no OSoL file"
01581 << std::endl;
01582 }
01583 else
01584 {
01585 kill();
01586 }
01587
01588
01589 break;
01590
01591
01592 case 5:
01593
01594
01595
01596
01597 if(osoptions->serviceLocation == "")
01598 getServiceLocation();
01599
01600 if( osoptions->osplInputFile == "")
01601 {
01602 std::cout
01603 << std::endl
01604 << "Cannot knock -- no OSplInputFile specificed"
01605 << std::endl;
01606 }
01607 else
01608 {
01609 knock();
01610 }
01611
01612 break;
01613
01614
01615 case 6:
01616
01617 return;
01618
01619
01620
01621 case 7:
01622
01623 return;
01624
01625
01626
01627 case 8:
01628
01629 reset_options();
01630 std::cout << "\nAll options reset.\n";
01631 break;
01632
01633
01634
01635 case 9:
01636
01637 listOptions( osoptions);
01638 break;
01639
01640
01641 case 10:
01642
01643 std::cout << get_options() << std::endl;
01644 break;
01645
01646
01647 case 11:
01648
01649 std::cout << get_options() << std::endl;
01650 break;
01651
01652
01653 case 12:
01654
01655 std::cout << OSgetVersionInfo() << std::endl;
01656 break;
01657
01658
01659 case 13:
01660
01661 doPrintModel(osoptions);
01662 break;
01663
01664
01665 default:
01666 throw ErrorClass("we don't have a valid command");
01667
01668
01669 }
01670
01671 }
01672 else
01673 {
01674
01675 if (optionValue == "")
01676 {
01677
01678 if(optionMap.find(optionName) != optionMap.end() )
01679 {
01680
01681 switch (optionMap[ optionName] )
01682 {
01683
01684 case 0:
01685 std::cout
01686 << "Please enter the name of an osil file: ";
01687 break;
01688
01689
01690 case 1:
01691 std::cout
01692 << "Please enter the name of an osrl file: ";
01693 break;
01694
01695 case 2:
01696 std::cout
01697 << "Please enter the name of an osol file: ";
01698 break;
01699
01700 case 3:
01701 std::cout
01702 << "Please enter the name of an mps file: ";
01703 break;
01704
01705 case 4:
01706 std::cout
01707 << "Please enter the name of an AMPL nl file: ";
01708 break;
01709
01710 case 5:
01711 std::cout
01712 << "Please enter the name of a dat file: ";
01713 break;
01714
01715 case 6:
01716 std::cout
01717 << "Please enter the serviceLocation: ";
01718 break;
01719
01720 case 7:
01721 std::cout
01722 << "Please enter the name of the solver: ";
01723 break;
01724
01725 case 8:
01726 std::cout
01727 << "Please enter the name of an osplInput file: ";
01728 break;
01729
01730 case 9:
01731 std::cout
01732 << "Please enter the name of an osplOutput file: ";
01733 break;
01734
01735
01736 case 10:
01737 std::cout
01738 << "Please enter the number of a constraint (>=0) or objective (<0): ";
01739 break;
01740
01741
01742 }
01743
01744
01745
01746 getline(std::cin, lineText);
01747
01748
01749 int skipChars;
01750
01751
01752 indexStart = lineText.find_first_not_of(wordSep, 0);
01753 if (lineText[indexStart] == '\"')
01754 {
01755 indexEnd = lineText.find_first_of(dblQuote, indexStart + 1);
01756 skipChars = 1;
01757 }
01758 else
01759 {
01760 indexEnd = lineText.find_first_of(wordSep, indexStart + 1);
01761 skipChars = 0;
01762 }
01763 if (indexStart != std::string::npos && indexEnd
01764 != std::string::npos)
01765 {
01766 optionValue = lineText.substr(indexStart + skipChars,
01767 indexEnd - indexStart - skipChars);
01768 }
01769 else
01770 {
01771 optionValue = "";
01772 }
01773
01774
01775 }
01776
01777 }
01778
01779
01780 if(optionMap.find(optionName) != optionMap.end() )
01781 {
01782
01783 switch (optionMap[ optionName] )
01784 {
01785
01786 case 0:
01787 osoptions->osilFile = optionValue;
01788 osoptions->osil
01789 = fileUtil->getFileAsString(
01790 (osoptions->osilFile).c_str());
01791 break;
01792
01793
01794 case 1:
01795 osoptions->osrlFile = optionValue;
01796 break;
01797
01798 case 2:
01799 osoptions->osolFile = optionValue;
01800 osoptions->osol
01801 = fileUtil->getFileAsString(
01802 (osoptions->osolFile).c_str());
01803 break;
01804
01805 case 3:
01806 osoptions->mpsFile = optionValue;
01807 osoptions->mps
01808 = fileUtil->getFileAsString(
01809 (osoptions->mpsFile).c_str());
01810 break;
01811
01812 case 4:
01813 osoptions->nlFile = optionValue;
01814 osoptions->nl
01815 = fileUtil->getFileAsString(
01816 (osoptions->nlFile).c_str());
01817 break;
01818
01819 case 5:
01820 osoptions->datFile = optionValue;
01821 osoptions->dat
01822 = fileUtil->getFileAsString(
01823 (osoptions->datFile).c_str());
01824 break;
01825
01826 case 6:
01827 osoptions->serviceLocation = optionValue;
01828 break;
01829
01830 case 7:
01831
01832
01833 for (k = 0; k
01834 < osoptions->solverName.length(); k++)
01835 {
01836 osoptions->solverName[k] =
01837 (char)tolower(osoptions->solverName[k]);
01838 }
01839 osoptions->solverName = optionValue;
01840 break;
01841
01842 case 8:
01843 osoptions->osplInputFile = optionValue;
01844 osoptions->osplInput
01845 = fileUtil->getFileAsString(
01846 (osoptions->osplInputFile).c_str());
01847 break;
01848
01849 case 9:
01850 osoptions->osplOutputFile = optionValue;
01851 break;
01852
01853 case 10:
01854 osoptions->printRowNumberAsString = optionValue;
01855 doPrintRow(osoptions);
01856 break;
01857
01858
01859
01860 }
01861 listOptions( osoptions);
01862
01863 }
01864
01865 }
01866
01867 std::cout << std::endl;
01868 }
01869 catch (const ErrorClass& eclass)
01870 {
01871 std::cout << eclass.errormsg << std::endl;
01872 }
01873 }
01874 }
01875 }
01876 ossslex_destroy(scanner);
01877 scannerActive = false;
01878 delete osoptions;
01879 osoptions = NULL;
01880 delete fileUtil;
01881 fileUtil = NULL;
01882 }
01883
01884 std::string get_help()
01885 {
01886
01887 std::ostringstream helpMsg;
01888
01889 helpMsg << "************************* HELP *************************"
01890 << endl << endl;
01891 helpMsg
01892 << "In this HELP file we assume that the solve service method is used and "
01893 << endl;
01894 helpMsg
01895 << "that we are solving problems locally, that is the solver is on the "
01896 << endl;
01897 helpMsg
01898 << "machine running this OSSolverService. See Section 10.3 of the User\'s "
01899 << endl;
01900 helpMsg
01901 << "Manual for other service methods or calling a server remotely. "
01902 << endl;
01903 helpMsg << "The OSSolverService takes the parameters listed below. "
01904 << endl;
01905 helpMsg
01906 << "The order of the parameters is irrelevant. Not all the parameters "
01907 << endl;
01908 helpMsg << "are required. However, the location of an instance file is "
01909 << endl;
01910 helpMsg
01911 << "required when using the solve service method. The location of the "
01912 << endl;
01913 helpMsg << "instance file is specified using the osil option. " << endl;
01914
01915 helpMsg << endl;
01916
01917 helpMsg
01918 << "-osil xxx.osil this is the name of the file that contains the "
01919 << endl;
01920 helpMsg << "optimization instance in OSiL format. This option may be "
01921 << endl;
01922 helpMsg << "specified in the OSoL solver options file. " << endl;
01923
01924 helpMsg << endl;
01925
01926 helpMsg
01927 << "-osol xxx.osol this is the name of the file that contains the solver options. "
01928 << endl;
01929 helpMsg << "It is not necessary to specify this option. " << endl;
01930
01931 helpMsg << endl;
01932
01933 helpMsg
01934 << "-osrl xxx.osrl this is the name of the file to which the solver solution is written. "
01935 << endl;
01936 helpMsg
01937 << "It is not necessary to specify this option. If this option is not specified, "
01938 << endl;
01939 helpMsg << "the result will be printed to standard out. " << endl;
01940
01941 helpMsg << endl;
01942
01943 helpMsg
01944 << "-osplInput xxx.ospl this is the name of an input file in the OS Process"
01945 << endl;
01946 helpMsg << " Language (OSpL), this is used as input to the knock method."
01947 << endl;
01948
01949 helpMsg << endl;
01950
01951 helpMsg
01952 << "-osplOutput xxx.ospl this is the name of an output file in the OS Process"
01953 << endl;
01954 helpMsg
01955 << "Language (OSpL), this the output string from the knock and kill methods."
01956 << endl;
01957
01958 helpMsg << endl;
01959
01960 helpMsg << "-serviceLocation url is the URL of the solver service. "
01961 << endl;
01962 helpMsg
01963 << "This is not required, and if not specified it is assumed that "
01964 << endl;
01965 helpMsg << "the problem is solved locally. " << endl;
01966
01967 helpMsg << endl;
01968
01969 helpMsg
01970 << "-serviceMethod methodName this is the method on the solver service to be invoked. "
01971 << endl;
01972 helpMsg
01973 << "The options are solve, send, kill, knock, getJobID, and retrieve. "
01974 << endl;
01975 helpMsg
01976 << "This option is not required, and the default value is solve. "
01977 << endl;
01978
01979 helpMsg << endl;
01980
01981 helpMsg
01982 << "-mps xxx.mps this is the name of the mps file if the problem instance "
01983 << endl;
01984 helpMsg
01985 << "is in mps format. The default file format is OSiL so this option is not required. "
01986 << endl;
01987
01988 helpMsg << endl;
01989
01990 helpMsg
01991 << "-nl xxx.nl this is the name of the AMPL nl file if the problem "
01992 << endl;
01993 helpMsg
01994 << "instance is in AMPL nl format. The default file format is OSiL "
01995 << endl;
01996 helpMsg << "so this option is not required. " << endl;
01997
01998 helpMsg << endl;
01999
02000 helpMsg
02001 << "-solver solverName Possible values for default OS installation "
02002 << endl;
02003 helpMsg
02004 << "are bonmin(COIN-OR Bonmin), couenne (COIN-OR Couenne), clp (COIN-OR Clp),"
02005 << endl;
02006 helpMsg << "cbc (COIN-OR Cbc), dylp (COIN-OR DyLP), ipopt (COIN-OR Ipopt),"
02007 << endl;
02008 helpMsg << "and symphony (COIN-OR SYMPHONY). Other solvers supported"
02009 << endl;
02010 helpMsg
02011 << "(if the necessary libraries are present) are cplex (Cplex through COIN-OR Osi),"
02012 << endl;
02013 helpMsg
02014 << "glpk (glpk through COIN-OR Osi), knitro (Knitro), and lindo (LINDO)."
02015 << endl;
02016 helpMsg << "If no value is specified for this parameter," << endl;
02017 helpMsg << "then cbc is the default value of this parameter." << endl;
02018
02019 helpMsg << endl;
02020
02021 helpMsg
02022 << "-browser browserName this paramater is a path to the browser on the "
02023 << endl;
02024 helpMsg
02025 << "local machine. If this optional parameter is specified then the "
02026 << endl;
02027 helpMsg << "solver result in OSrL format is transformed using XSLT into "
02028 << endl;
02029 helpMsg << "HTML and displayed in the browser. " << endl;
02030
02031 helpMsg << endl;
02032
02033 helpMsg
02034 << "-config pathToConfigureFile this parameter specifies a path on "
02035 << endl;
02036 helpMsg
02037 << "the local machine to a text file containing values for the input parameters. "
02038 << endl;
02039 helpMsg
02040 << "This is convenient for the user not wishing to constantly retype parameter values. "
02041 << endl;
02042 helpMsg
02043 << "This configure file can contain values for all of the other parameters. "
02044 << endl;
02045
02046 helpMsg << endl;
02047
02048 helpMsg << "--version or -v get the current version of this executable "
02049 << endl;
02050
02051 helpMsg << endl;
02052
02053 helpMsg << "--help or -h to get this help file " << endl;
02054
02055 helpMsg << endl;
02056
02057 helpMsg
02058 << "Note: If you specify a configure file by using the -config option, you can "
02059 << endl;
02060 helpMsg
02061 << "override the values of the options in the configure file by putting them in "
02062 << endl;
02063 helpMsg << "at the command line. " << endl << endl;
02064
02065 helpMsg
02066 << "See the OS User\' Manual: http://www.coin-or.org/OS/doc/osUsersManual.pdf"
02067 << endl;
02068 helpMsg << "for more detail on how to use the OS project. " << endl;
02069
02070 helpMsg << endl;
02071 helpMsg << "********************************************************"
02072 << endl << endl;
02073
02074 return helpMsg.str();
02075 }
02076
02077
02078 std::string get_version()
02079 {
02080 std::ostringstream versionMsg;
02081 versionMsg << "In order to find the version of this project " << endl;
02082 versionMsg << "connect to the directory where you downloaded " << endl;
02083 versionMsg << "and do: " << endl;
02084 versionMsg << "svn info " << endl;
02085
02086 return versionMsg.str();
02087 }
02088
02089
02090 void reset_options()
02091 {
02092 osoptions->configFile = "";
02093 osoptions->osilFile = "";
02094 osoptions->osil = "";
02095 osoptions->osolFile = "";
02096 osoptions->osol = "";
02097 osoptions->osrlFile = "";
02098 osoptions->osrl = "";
02099 osoptions->insListFile = "";
02100 osoptions->insList = "";
02101 osoptions->serviceLocation = "";
02102 osoptions->serviceMethod = "";
02103 osoptions->osplInputFile = "";
02104 osoptions->osplInput = "";
02105 osoptions->osplOutputFile = "";
02106 osoptions->osplOutput = "";
02107 osoptions->mpsFile = "";
02108 osoptions->mps = "";
02109 osoptions->nlFile = "";
02110 osoptions->nl = "";
02111 osoptions->gamsControlFile = "";
02112 osoptions->solverName = "";
02113 osoptions->browser = "";
02114 osoptions->jobID = "";
02115 osoptions->invokeHelp = false;
02116 osoptions->writeVersion = false;
02117 osoptions->printModel = false;
02118 osoptions->printRowNumberAsString = "";
02119 osoptions->quit = false;
02120 osoptions->exit = false;
02121 }
02122
02123
02124
02125 std::string get_options()
02126 {
02127 std::ostringstream optionMsg;
02128
02129 optionMsg << endl;
02130
02131 optionMsg
02132 << "***************** VALID COMMANDS AND OPTIONS ********************"
02133 << endl ;
02134 optionMsg
02135 << "COMMANDS:"
02136 << endl;
02137 optionMsg
02138 << "quit/exit -- terminate the executable"
02139 << endl;
02140 optionMsg
02141 << "help/? -- produce this list of options"
02142 << endl;
02143 optionMsg
02144 << "reset -- erase all previous option settings"
02145 << endl ;
02146 optionMsg
02147 << "list -- list the current option values"
02148 << endl ;
02149 optionMsg
02150 << "solve -- call the solver synchronously"
02151 << endl ;
02152 optionMsg
02153 << "send -- call the solver asynchronously"
02154 << endl ;
02155 optionMsg
02156 << "kill -- end a job on the remote server"
02157 << endl ;
02158 optionMsg
02159 << "retrieve -- get job result on the remote server"
02160 << endl ;
02161 optionMsg
02162 << "knock -- get job information on the remote server"
02163 << endl ;
02164 optionMsg
02165 << "getJobID -- get a job ID from the remote server"
02166 << endl << endl;
02167
02168
02169 optionMsg
02170 << "OPTIONS (THESE REQUIRE A VALUE):"
02171 << endl;
02172 optionMsg
02173 << "osil -- the location of the model instance in OSiL format"
02174 << endl;
02175 optionMsg
02176 << "mps -- the location of the model instance in MPS format"
02177 << endl ;
02178 optionMsg
02179 << "nl -- the location of the model instance in AMPL nl format"
02180 << endl;
02181 optionMsg
02182 << "osol -- the location of the solver option file in OSoL format"
02183 << endl;
02184 optionMsg
02185 << "osrl -- the location of the solver result file in OSrL format"
02186 << endl;
02187 optionMsg
02188 << "osplInput -- the name of an input file in OSpL format"
02189 << endl;
02190 optionMsg
02191 << "osplOutput -- the name of an output file in the OSpL format"
02192 << endl ;
02193 optionMsg
02194 << "serviceLocation -- the URL of a remote solver service"
02195 << endl;
02196 optionMsg
02197 << "solver -- specify the solver to invoke"
02198 << endl <<endl;
02199 optionMsg
02200 << "See http://www.coin-or.org/OS/"
02201 << endl;
02202 optionMsg
02203 << "for more detail on how to use the OS project."
02204 << endl << endl;
02205
02206 optionMsg
02207 << "PRINT OPTIONS:"
02208 << endl;
02209 optionMsg
02210 << "printModel -- print the currently defined model"
02211 << endl;
02212 optionMsg
02213 << "printRow nnn -- print row n of the currently defined model"
02214 << endl;
02215 optionMsg
02216 << " if nnn >= 0, prints a constraint, otherwise prints an objective row"
02217 << endl << endl;
02218
02219 optionMsg
02220 << "*****************************************************************"
02221 << endl << endl;
02222 optionMsg
02223 << "At the prompt enter a valid command or option value pair."
02224 << endl;
02225 optionMsg
02226 << "Enter the \"solve\" command to optimize."
02227 << endl;
02228 optionMsg
02229 << "Type \"quit/exit\" to leave the application."
02230 << endl;
02231 optionMsg
02232 << "Type \"help\" or \"?\" for a list of valid options."
02233 << endl;
02234
02235
02236 return optionMsg.str();
02237 }
02238
02239
02240 void listOptions(osOptionsStruc *osoptions)
02241 {
02242 cout
02243 << "HERE ARE THE OPTION VALUES SO FAR:"
02244 << endl;
02245 if (osoptions->configFile != "")
02246 cout << "Config file = "
02247 << osoptions->configFile
02248 << endl;
02249 if (osoptions->osilFile != "")
02250 cout << "OSiL file = "
02251 << osoptions->osilFile
02252 << endl;
02253 if (osoptions->osolFile != "")
02254 cout << "OSoL file = "
02255 << osoptions->osolFile
02256 << endl;
02257 if (osoptions->osrlFile != "")
02258 cout << "OSrL file = "
02259 << osoptions->osrlFile
02260 << endl;
02261
02262 if (osoptions->osplInputFile != "")
02263 cout << "OSpL Input file = "
02264 << osoptions->osplInputFile
02265 << endl;
02266 if (osoptions->serviceMethod != "")
02267 cout << "Service Method = "
02268 << osoptions->serviceMethod
02269 << endl;
02270 if (osoptions->mpsFile != "")
02271 cout << "MPS File Name = "
02272 << osoptions->mpsFile
02273 << endl;
02274 if (osoptions->nlFile != "")
02275 cout << "NL File Name = "
02276 << osoptions->nlFile
02277 << endl;
02278 if (osoptions->solverName != "")
02279 cout << "Selected Solver = "
02280 << osoptions->solverName
02281 << endl;
02282 if (osoptions->serviceLocation != "")
02283 cout << "Service Location = "
02284 << osoptions->serviceLocation
02285 << endl;
02286
02287 if (osoptions->jobID != "")
02288 cout << "Job ID = "
02289 << osoptions->jobID
02290 << endl;
02291 }
02292
02293 void doPrintModel(osOptionsStruc *osoptions)
02294 {
02295 if (osoptions->osil == "" && osoptions->mps == "" && osoptions->nl == "")
02296 {
02297 std::cout
02298 << "no instance defined; print command ignored" << std::endl;
02299 }
02300 else
02301 {
02302 if (osoptions->osil != "")
02303 {
02304 OSiLReader *osilreader;
02305 osilreader = new OSiLReader();
02306 std::cout << osilreader->readOSiL(osoptions->osil)->printModel() << std::endl;
02307 delete osilreader;
02308 osilreader = NULL;
02309 }
02310 else if (osoptions->nl != "")
02311 {
02312 #ifdef COIN_HAS_ASL
02313 OSnl2osil *nl2osil;
02314 nl2osil = new OSnl2osil( osoptions->nlFile);
02315 nl2osil->createOSInstance();
02316 std::cout << nl2osil->osinstance->printModel() << std::endl;
02317 delete nl2osil;
02318 nl2osil = NULL;
02319 #else
02320 std::cout << "no ASL present to read nl file; print command ignored" << std::endl;
02321 #endif
02322 }
02323 else if (osoptions->mps != "")
02324 {
02325 OSmps2osil *mps2osil;
02326 mps2osil = new OSmps2osil(osoptions->mpsFile);
02327 mps2osil->createOSInstance();
02328 std::cout << mps2osil->osinstance->printModel() << std::endl;
02329 delete mps2osil;
02330 mps2osil = NULL;
02331 }
02332 }
02333 }
02334
02335 void doPrintModel(OSInstance *osinstance)
02336 {
02337 if (osinstance == NULL)
02338 {
02339 std::cout
02340 << "no instance defined; print command ignored" << std::endl;
02341 }
02342 else
02343 {
02344 std::cout << osinstance->printModel() << std::endl;
02345 }
02346 }
02347
02348 void doPrintRow(osOptionsStruc *osoptions)
02349 {
02350 int rownumber;
02351 if (osoptions->printRowNumberAsString == "")
02352 std::cout << "no line number given; print command ignored" << std::endl;
02353 else
02354 {
02355 try
02356 {
02357 rownumber = atoi((osoptions->printRowNumberAsString).c_str());
02358 }
02359 catch (const ErrorClass& eclass)
02360 {
02361 std::cout << "invalid row number; print command ignored" << std::endl;
02362 }
02363
02364 if (osoptions->osil == "" && osoptions->mps == "" && osoptions->nl == "")
02365 {
02366 std::cout
02367 << "no instance defined; print command ignored" << std::endl;
02368 }
02369 else
02370 {
02371 std::cout << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
02372 if (osoptions->osil != "")
02373 {
02374 OSiLReader *osilreader;
02375 osilreader = new OSiLReader();
02376 std::cout << osilreader->readOSiL(osoptions->osil)->printModel(rownumber) << std::endl;
02377 delete osilreader;
02378 osilreader = NULL;
02379 }
02380 else if (osoptions->nl != "")
02381 {
02382 #ifdef COIN_HAS_ASL
02383 OSnl2osil *nl2osil;
02384 nl2osil = new OSnl2osil( osoptions->nlFile);
02385 nl2osil->createOSInstance();
02386 std::cout << nl2osil->osinstance->printModel(rownumber) << std::endl;
02387 delete nl2osil;
02388 nl2osil = NULL;
02389 #else
02390 std::cout << "no ASL present to read nl file; print command ignored" << std::endl;
02391 #endif
02392 }
02393 else if (osoptions->mps != "")
02394 {
02395 OSmps2osil *mps2osil;
02396 mps2osil = new OSmps2osil(osoptions->mpsFile);
02397 mps2osil->createOSInstance();
02398 std::cout << mps2osil->osinstance->printModel(rownumber) << std::endl;
02399 delete mps2osil;
02400 mps2osil = NULL;
02401 }
02402 }
02403 }
02404 }
02405
02406 void doPrintRow(OSInstance *osinstance, std::string rownumberstring)
02407 {
02408 int rownumber;
02409 if (rownumberstring == "")
02410 std::cout << "no line number given; print command ignored" << std::endl;
02411 else
02412 {
02413 try
02414 {
02415 rownumber = atoi((rownumberstring).c_str());
02416 }
02417 catch (const ErrorClass& eclass)
02418 {
02419 std::cout << "invalid row number; print command ignored" << std::endl;
02420 }
02421
02422 if (osinstance == NULL)
02423 {
02424 std::cout
02425 << "no instance defined; print command ignored" << std::endl;
02426 }
02427 else
02428 {
02429 std::cout << std::endl << "Row " << rownumber << ":" << std::endl << std::endl;
02430 std::cout << osinstance->printModel(rownumber) << std::endl;
02431 }
02432 }
02433 }
02434