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

Generated on Thu Nov 10 03:05:46 2011 by  doxygen 1.4.7