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