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