/home/coin/SVN-release/OS-2.4.0/OS/applications/amplClient/OSAmplClient.cpp

Go to the documentation of this file.
00001 
00061 #include "OSCoinSolver.h"
00062 #include "OSConfig.h"
00063 #include "OSnl2osil.h"
00064 #include "OSiLReader.h"
00065 #include "OSrLReader.h"
00066 #include "OSiLWriter.h"
00067 #include "OSrLWriter.h"
00068 #include "OSInstance.h"
00069 #include "OSResult.h"
00070 #include "OSOption.h"
00071 #include "OSoLReader.h"
00072 #include "OSoLWriter.h"
00073 #ifdef COIN_HAS_LINDO    
00074 #include "OSLindoSolver.h"
00075 #endif 
00076 #ifdef COIN_HAS_IPOPT    
00077 #include "OSIpoptSolver.h"
00078 #endif  
00079 #ifdef COIN_HAS_BONMIN    
00080 #include "OSBonminSolver.h"
00081 #endif 
00082 #ifdef COIN_HAS_COUENNE    
00083 #include "OSCouenneSolver.h"
00084 #endif
00085 #include "OSFileUtil.h"
00086 #include "OSDefaultSolver.h"
00087 #include "OSSolverAgent.h"
00088 #include "OShL.h"
00089 #include "OSErrorClass.h"
00090 #include "CoinError.hpp"
00091 #include "OSOption.h"
00092 #include "OSOptionsStruc.h" 
00093 #include "OSRunSolver.h"
00094 #include <sstream>
00095 
00096 #ifdef HAVE_CSTRING
00097 # include <cstring>
00098 #else
00099 # ifdef HAVE_STRING_H
00100 #  include <string.h>
00101 # else
00102 #  error "don't have header file for string"
00103 # endif
00104 #endif
00105 
00106 #include "CoinError.hpp"
00107 #include "CoinHelperFunctions.hpp"
00108 
00109 
00110 #include <iostream>
00111 //AMPL includes must be last.
00112 #include <asl.h>
00113 
00114 
00115 
00116 
00117 #define MAXCHARS 5000 
00118 
00119 typedef struct yy_buffer_state *YY_BUFFER_STATE;
00120 YY_BUFFER_STATE osss_scan_string(const char* osss, void* scanner ); 
00121 //void osssset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
00122 void setyyextra(osOptionsStruc *osoptions, void* scanner);
00123 int ossslex(void* scanner );
00124 int ossslex_init(void** ptr);
00125 int ossslex_destroy (void* scanner );
00126 
00127 
00128 using std::cerr;
00129 using std::cout;
00130 using std::endl;
00131 using std::ostringstream;
00132 
00133 void getAmplClientOptions(char *options, std::string *solverName, 
00134    std::string *optionFile, std::string *serviceLocation);
00135 
00136 
00137 
00138 
00139 
00140 int main(int argc, char **argv)
00141 {
00142         WindowsErrorPopupBlocker();
00143         char *stub;
00144         // set AMPL structures
00145         ASL *asl;
00146         asl = ASL_alloc(ASL_read_fg);
00147     stub = argv[1];
00148         jac0dim((char*)stub, (fint)strlen(stub));
00149         OSnl2osil *nl2osil = NULL;
00150         //initialize object with stub -- the nl file
00151         nl2osil = new OSnl2osil( stub);
00152         // create an osinstance object
00153         OSInstance *osinstance;
00154         std::cout << " call nl2osil" << std::endl;
00155 
00156 /*      Parse the .nl file to create an in-memory representation
00157         in form of an OSInstance object
00158  */
00159         try{
00160                 nl2osil->createOSInstance() ;
00161         }
00162         catch(const ErrorClass& eclass){
00163                 std::cout << eclass.errormsg << std::endl;
00164                 return 0;
00165         }
00166         std::cout << " return from  nl2osil" << std::endl;
00167         osinstance = nl2osil->osinstance;
00168         std::cout << " osinstance created" << std::endl;
00169 
00170 /*      Parse the options (passed through ASL as the string OSAmplClient_options)
00171  *
00172  *      There are three possible options:
00173  *      1. solver:
00174  *              possible values - name of a supported solver (installation-dependent)
00175  *      2. serviceLocation:
00176  *              possible values - NULL (empty) or URL of the solver service
00177  *      3. optionFile:
00178  *              specify the location of the OSoL file (on the local system)
00179  *      
00180  */
00181         char *amplclient_options = NULL;
00182         DefaultSolver *solverType  = NULL;      
00183         std::string sSolverName = "";
00184         std::string osolFileName = "";
00185         std::string osol ="";
00186         std::string serviceLocation = "";
00187 
00188         amplclient_options = getenv("OSAmplClient_options");
00189         if( amplclient_options != NULL){
00190                 cout << "HERE ARE THE AMPLCLIENT OPTIONS " <<   amplclient_options << endl;
00191                 getAmplClientOptions(amplclient_options, &sSolverName, &osolFileName, &serviceLocation);
00192         }
00193 
00194 /* If an OSoL file was given, read it into a string (don't parse)
00195  */
00196     if(osolFileName.size() > 0){
00197         FileUtil *fileUtil;
00198                 fileUtil = new FileUtil();
00199         osol = fileUtil->getFileAsString( osolFileName.c_str() ); 
00200         delete fileUtil;
00201     }
00202         //std::cout << " solver Name = " << sSolverName << std::endl;
00203         //std::cout << " solver Options = " << osol << std::endl;
00204     
00205     //convert solver name to lower case for testing purposes
00206     
00207     unsigned int k;
00208     for(k = 0; k < sSolverName.length(); k++){
00209         sSolverName[ k] = tolower( sSolverName[ k]);
00210     }
00211 
00212         
00213         OSrLReader *osrlreader = NULL;
00214         OSrLWriter *osrlwriter;
00215         osrlwriter = new OSrLWriter();
00216         OSResult *osresult = NULL;
00217         std::string osrl = "";
00218 
00219 
00220         try{
00221                 if(serviceLocation.size() == 0 ){
00222                         //determine the solver
00223                         osrl = runSolver(sSolverName, osol, osinstance);
00224                 }// end if serviceLocation.size() == 0
00225 
00226 /* ------------------------------------------------------- */
00227                 else{// do a remote solve
00228                         OSSolverAgent* osagent = NULL;
00229                         OSiLWriter *osilwriter = NULL;
00230                         osilwriter = new OSiLWriter();
00231                         std::string  osil = osilwriter->writeOSiL( osinstance);
00233                         
00234                         //agent_address = strstr(solver_option, "service");
00235                         //agent_address += 7;
00236                         //URL = strtok( agent_address, delims );
00237                         //std::string sURL = URL;
00239                         // we should be pointing to the start of the address
00240                         osagent = new OSSolverAgent( serviceLocation);
00241                         cout << "Place remote synchronous call: " + serviceLocation << endl << endl << endl;
00242                         //cout << osol << endl;
00243                         osrl = osagent->solve(osil, osol);
00244                         if (osrl.size() == 0) throw ErrorClass("Nothing was returned from the server, please check service address");
00245                         delete osilwriter;
00246                         delete osagent; 
00247                 }
00248         }//end try
00249         catch(const ErrorClass& eclass){
00250                 osresult = new OSResult();      
00251                 osresult->setGeneralMessage( eclass.errormsg);
00252                 osresult->setGeneralStatusType( "error");
00253                 osrl = osrlwriter->writeOSrL( osresult);
00254                 std::cout  << osrl << std::endl;
00255                 osrl = " ";
00256                 write_sol(const_cast<char*>(osrl.c_str()), NULL, NULL, NULL);
00257                 delete osresult;
00258                 return 0;
00259         }
00260 
00261         try{ // now put solution back to ampl
00262                 //need_nl = 0;
00263                 std::string sResultFileName = "solutionResult.osrl";
00264                 FileUtil *fileUtil;
00265                 fileUtil = new FileUtil();
00266                 fileUtil->writeFileFromString(sResultFileName, osrl);
00267                 delete fileUtil;
00268                 //cout << "WRITE THE SOLUTION BACK INTO AMPL" <<endl;
00269                 std::string::size_type pos1 = osrl.find( "error");
00270                 if(pos1 == std::string::npos){
00271                         std::string sReport = "model was solved";
00272                         std::cout << sReport << std::endl;
00273                         //std::cout << osrl << std::endl;
00274                         osrlreader = new OSrLReader();
00275                         osresult = osrlreader->readOSrL( osrl);
00276                         // do the following so output is not written twice
00277                         // see page 23 of hooking solver to AMPL
00278                         //need_nl = printf( sReport.c_str());
00279 
00280                         //
00281                         sReport = " ";
00282                         int i;
00283                         int vecSize;
00284                         double *x;
00285                         double *y;
00286                         int numVars = osresult->getVariableNumber();
00287                         int numCons = osresult->getConstraintNumber();
00288                         x = new double[ numVars];
00289                         y = new double[ numCons];
00290                         
00291                         std::vector<IndexValuePair*> primalValPair;
00292                         std::vector<IndexValuePair*> dualValPair;
00293                         dualValPair = osresult->getOptimalDualVariableValues( 0);
00294                         primalValPair = osresult->getOptimalPrimalVariableValues( 0);
00295                         
00296                         for(i = 0; i < numVars; i++){
00297                                 x[ i] = 0.0;
00298                         }
00299                         vecSize = primalValPair.size();
00300                         for(i = 0; i < vecSize; i++){
00301                                 x[ primalValPair[i]->idx ] = primalValPair[i]->value;
00302                                 //std::cout << "index =  " <<   primalValPair[i]->idx  << std::endl;
00303                                 //std::cout << "value =  " <<   primalValPair[i]->value  << std::endl;
00304                         }
00305                         
00306                         
00307                         for(i = 0; i < numCons; i++){
00308                                 y[ i] = 0.0;
00309                         }
00310                         vecSize = dualValPair.size();
00311                         for(i = 0; i < vecSize; i++){
00312                                 y[ dualValPair[i]->idx ] = dualValPair[i]->value;  
00313                                 //std::cout << "index =  " <<   primalValPair[i]->idx  << std::endl;
00314                                 //std::cout << "value =  " <<   primalValPair[i]->value  << std::endl;
00315                         }
00316                         
00317 
00318                         
00319                         write_sol(  const_cast<char*>(sReport.c_str()),  x, y , NULL);
00320                         
00321                         delete osrlreader;
00322                         osrlreader = NULL;
00323                         //delete[] x;
00324                         //x = NULL;
00325                         //delete y;
00326                         //y = NULL;
00327                 }else{
00328                         // do the following so output is not written twice
00329                         // see page 23 of hooking solver to AMPL
00330                         std::cout  << osrl << std::endl;
00331                         osrl = " ";
00332                         //
00333                         write_sol(  const_cast<char*>(osrl.c_str()), NULL, NULL, NULL);
00334                         need_nl = 0;
00335                 }
00336                 //cout << "DONE WRITING THE SOLUTION BACK INTO AMPL" <<endl;
00337         }
00338         catch(const ErrorClass& eclass){
00339                 cout << "There was an error parsing the OSrL" << endl << eclass.errormsg << endl << endl;
00340         }
00341         if(  solverType != NULL ){
00342                 //cout << "TRY TO DELETE solverType" <<endl;
00343                 delete solverType;
00344                 //cout << "solverType JUST DELETED" <<endl;
00345                 solverType = NULL;
00346         }
00347         delete osrlwriter;
00348         //cout << "osrlwriter JUST DELETED" <<endl;
00349         osrlwriter = NULL;
00350         delete nl2osil;
00351         //cout << "nl2osil JUST DELETED" <<endl;
00352         nl2osil = NULL;
00353         ASL_free(&asl);
00354         return 0; 
00355 } // end main
00356 
00357 
00358 //void getAmplClientOptions(char *amplclient_options, std::string *solverName, 
00359 //      std::string *solverOptions, std::string *serviceLocation){
00360 //
00361 //      
00362 //      std::string amplOptions = "";
00363 //      std::ostringstream outStr; 
00364 //      std::string::size_type  pos1;
00365 //      std::string::size_type  pos2;
00366 //      std::string osolFileName = "";
00367 //      FileUtil *fileUtil = NULL;
00368 //
00369 //      size_t i = 0;
00370 //      size_t n = strlen( amplclient_options);
00371 //      try{
00372 //              while( i < n){
00373 //                      //std::cout << amplclient_options[ i] << std::endl;
00374 //                      if( amplclient_options[ i]  == ' '){
00375 //                              outStr << ',';
00376 //                              i++;
00377 //                              while( amplclient_options[ i] == ' '  ){
00378 //                                      i++;
00379 //                              }
00380 //                      }
00381 //                      else{
00382 //                              outStr << amplclient_options[ i];
00383 //                              i++;
00384 //                      }
00385 //
00386 //              }
00387 //              //end with a comma
00388 //              outStr << ',';
00389 //              amplOptions = outStr.str();
00390 //              
00391 //              
00392 //              // see if a solver has been specified
00393 //              pos1 = amplOptions.find( "solver");
00394 //              if(pos1 != std::string::npos){
00395 //                      //we have a solver specified
00396 //                      pos1 += 6;
00397 //                      //std::cout << "position 1 = " << pos1 << std::endl;
00398 //                      // we are at at the comma after solver
00399 //                      pos2 = amplOptions.find( ",", pos1 + 1);
00400 //                      //std::cout << "position 2 = " << pos2 << std::endl;
00401 //                      if(pos2 != std::string::npos){
00402 //                              //std::cout << "solverName = " <<  amplOptions.substr(pos1 + 1, pos2-pos1 - 1) << std::endl;
00403 //                              *solverName = amplOptions.substr(pos1 + 1, pos2-pos1 - 1);
00404 //                              
00405 //                      }
00406 //              }
00407 //              
00408 //              
00409 //              // see if an option file has been specified
00410 //              pos1 = amplOptions.find( "optionFile");
00411 //              if(pos1 != std::string::npos){
00412 //                      //we have a option file specified  specified
00413 //                      pos1 += 10;
00414 //                      //std::cout << "position 1 = " << pos1 << std::endl;
00415 //                      // we are at at the comma after optionfile
00416 //                      pos2 = amplOptions.find( ",", pos1 + 1);
00417 //                      //std::cout << "position 2 = " << pos2 << std::endl;
00418 //                      if(pos2 != std::string::npos){
00419 //                              //std::cout << "optionFile Name = " <<  amplOptions.substr(pos1 + 1, pos2-pos1 - 1) << std::endl;
00420 //                              osolFileName = amplOptions.substr(pos1 + 1, pos2-pos1 - 1);
00421 //                      }
00422 //              }
00423 //              
00424 //              //now go ahead and read the OSoL file if specified
00425 //              
00426 //              if( osolFileName.size() > 0 ){
00427 //                      fileUtil = new FileUtil();
00428 //                      *solverOptions  = fileUtil->getFileAsString( osolFileName.c_str());
00429 //                      delete fileUtil;
00430 //                      fileUtil = NULL;
00431 //              }else{ // go ahead and create the osol string
00432 //                      *solverOptions = "<?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/2.0/OSoL.xsd\"></osol>";
00433 //              }
00434 //              
00435 //              // see if a serviceLocation has been specified
00436 //              pos1 = amplOptions.find( "serviceLocation");
00437 //              if(pos1 != std::string::npos){
00438 //                      //we have a serviceLocation specified
00439 //                      pos1 += 15;
00440 //                      //std::cout << "position 1 = " << pos1 << std::endl;
00441 //                      // we are at at the comma after solver
00442 //                      pos2 = amplOptions.find( ",", pos1 + 1);
00443 //                      //std::cout << "position 2 = " << pos2 << std::endl;
00444 //                      if(pos2 != std::string::npos){
00445 //                              //std::cout << "solverName = " <<  amplOptions.substr(pos1 + 1, pos2-pos1 - 1) << std::endl;
00446 //                              *serviceLocation = amplOptions.substr(pos1 + 1, pos2-pos1 - 1);
00447 //                              
00448 //                      }
00449 //              }
00450 //              
00451 //
00452 //      }//end try
00453 //      catch(const ErrorClass& eclass){
00454 //              cout << "There was an error processing OSAmplClient options: " << endl << eclass.errormsg << endl << endl;
00455 //              throw ErrorClass( eclass.errormsg) ;
00456 //      }       
00457 //}//getAmplClientOptions
00458 
00459 
00460 
00461 
00462 
00463 void getAmplClientOptions(char *amplclient_options, std::string *solverName, 
00464                                                   std::string *solverOptions, std::string *serviceLocation){
00465         
00466         osOptionsStruc *osoptions; 
00467         
00468         void* scanner;
00469         // initialize the OS options structure
00470         
00471         osoptions = new osOptionsStruc();
00472         osoptions->configFile = ""; 
00473         osoptions->osilFile = ""; 
00474         osoptions->osil = ""; 
00475         osoptions->osolFile = ""; 
00476         osoptions->osol = "";  
00477         osoptions->osrlFile = ""; 
00478         osoptions->osrl = ""; 
00479         //osoptions->insListFile = ""; 
00480         osoptions->insList = ""; 
00481         osoptions->serviceLocation = "";
00482         osoptions->serviceMethod = ""; 
00483         osoptions->osplInputFile = ""; 
00484         osoptions->osplOutputFile = ""; 
00485         osoptions->mpsFile = ""; 
00486         osoptions->nlFile = ""; 
00487         osoptions->gamsControlFile = "";
00488         osoptions->solverName = ""; 
00489         osoptions->browser = ""; 
00490         osoptions->invokeHelp = false;
00491         osoptions->writeVersion = false;
00492         try{    
00493                 //cout << "Input String = "  << amplclient_options << endl;
00494                 ossslex_init( &scanner);
00495                 //std::cout << "Call Text Extra" << std::endl;
00496                 setyyextra( osoptions, scanner);
00497                 //std::cout << "Call scan string " << std::endl;
00498                 osss_scan_string( amplclient_options, scanner); 
00499                 //std::cout << "call ossslex" << std::endl;
00500                 ossslex( scanner);
00501                 ossslex_destroy( scanner);
00502                 //std::cout << "done with call to ossslex" << std::endl;
00503 //              
00504 //              cout << "HERE ARE THE OPTION VALUES:" << endl;
00505 //              if(osoptions->configFile != "") cout << "Config file = " << osoptions->configFile << endl;
00506 //              if(osoptions->osilFile != "") cout << "OSiL file = " << osoptions->osilFile << endl;
00507 //              if(osoptions->osolFile != "") cout << "OSoL file = " << osoptions->osolFile << endl;
00508 //              if(osoptions->osrlFile != "") cout << "OSrL file = " << osoptions->osrlFile << endl;
00509 //              //if(osoptions->insListFile != "") cout << "Instruction List file = " << osoptions->insListFile << endl;
00510 //              if(osoptions->osplInputFile != "") cout << "OSpL Input file = " << osoptions->osplInputFile << endl;
00511 //              if(osoptions->serviceMethod != "") cout << "Service Method = " << osoptions->serviceMethod << endl;
00512 //              if(osoptions->mpsFile != "") cout << "MPS File Name = " << osoptions->mpsFile << endl;
00513 //              if(osoptions->nlFile != "") cout << "NL File Name = " << osoptions->nlFile << endl;
00514 //              if(osoptions->gamsControlFile != "") cout << "gams Control File Name = " << osoptions->gamsControlFile << endl;
00515 //              if(osoptions->browser != "") cout << "Browser Value = " << osoptions->browser << endl;
00516 //              if(osoptions->solverName != "") cout << "Selected Solver = " << osoptions->solverName << endl;
00517 //              if(osoptions->serviceLocation != "") cout << "Service Location = " << osoptions->serviceLocation << endl;
00518 //              
00519                 
00520                 *solverName = osoptions->solverName;
00521                 *solverOptions = osoptions->osolFile;
00522         
00523                 *serviceLocation = osoptions->serviceLocation;
00524                 
00525         }//end try
00526         catch(const ErrorClass& eclass){
00527                 cout << "There was an error processing OSAmplClient options: " << endl << eclass.errormsg << endl << endl;
00528                 throw ErrorClass( eclass.errormsg) ;
00529         }       
00530 }//getAmplClientOptions

Generated on Thu Sep 22 03:06:00 2011 by  doxygen 1.4.7