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