/home/coin/SVN-release/OS-2.4.2/OS/src/OSSolverInterfaces/OSRunSolver.cpp

Go to the documentation of this file.
00001 /* $Id: OSGetSolver.h 4263 2011-09-09 20:19:26Z Martin $ */
00018 #include "OSCoinSolver.h"
00019 #include "OSResult.h"
00020 #include "OSiLReader.h"
00021 #include "OSiLWriter.h"
00022 #include "OSoLReader.h"
00023 #include "OSrLReader.h"
00024 #include "OSrLWriter.h"
00025 #include "OSInstance.h"
00026 #include "OSOption.h"
00027 #include "OSoLWriter.h"
00028 #include "OSFileUtil.h"
00029 #include "OSConfig.h"
00030 #include "OSDefaultSolver.h"
00031 #include "OSWSUtil.h"
00032 #include "OSSolverAgent.h"
00033 #include "OShL.h"
00034 #include "OSErrorClass.h"
00035 //#include "OSmps2osil.h"
00036 #include "OSBase64.h"
00037 
00038 #ifdef COIN_HAS_KNITRO
00039 #include "OSKnitroSolver.h"
00040 #endif
00041 
00042 #ifdef COIN_HAS_LINDO
00043 #include "OSLindoSolver.h"
00044 #endif
00045 
00046 #ifdef COIN_HAS_ASL
00047 //#include "OSnl2osil.h"
00048 #endif
00049 
00050 #ifdef COIN_HAS_GAMSUTILS
00051 #include "OSgams2osil.hpp"
00052 #endif
00053 
00054 //#ifdef COIN_HAS_IPOPT
00055 //#include "OSIpoptSolver.h"
00056 //#endif
00057 
00058 #ifdef COIN_HAS_IPOPT
00059 #ifndef COIN_HAS_ASL
00060 #include "OSIpoptSolver.h"
00061 #undef COIN_HAS_ASL
00062 #else
00063 #include "OSIpoptSolver.h"
00064 #endif
00065 #endif
00066 
00067 #ifdef COIN_HAS_BONMIN
00068 #include "OSBonminSolver.h"
00069 #endif
00070 
00071 #ifdef COIN_HAS_COUENNE
00072 #include "OSCouenneSolver.h"
00073 #endif
00074 
00075 #include "OSOptionsStruc.h"
00076 
00077 #include<stdio.h>
00078 #include <map>
00079 
00080 
00081 using std::cout;
00082 using std::endl;
00083 using std::ostringstream;
00084 using std::string;
00085 using std::map;
00086 
00087 
00088 std::string runSolver(std::string solverName, std::string osol,
00089                         OSInstance *osinstance)
00090 {
00091     DefaultSolver *solverType = NULL;
00092     //std::cout << "SOLVER NAME = " << solverName << std::endl;
00093     try
00094     {
00095         if (solverName == "")  // must determine the default solver
00096         {
00097             if (osinstance == NULL)
00098                 throw ErrorClass(
00099                     "there was a NULL instance sent to buildSolver");
00100             //see if we have an integer program
00101             if (osinstance->getNumberOfIntegerVariables()
00102                     + osinstance->getNumberOfBinaryVariables() > 0)  //we have an integer program
00103             {
00104                 if ((osinstance->getNumberOfNonlinearExpressions() > 0)
00105                         || (osinstance->getNumberOfQuadraticTerms() > 0))   // we are nonlinear and integer
00106                 {
00107                     solverName = "bonmin";
00108                 }
00109                 else    //we are linear integer
00110                 {
00111                     solverName = "cbc";
00112                 }
00113             }
00114             else    // we have a continuous problem
00115             {
00116                 if ((osinstance->getNumberOfNonlinearExpressions() > 0)
00117                         || (osinstance->getNumberOfQuadraticTerms() > 0))   // we are nonlinear and continuous
00118                 {
00119                     solverName = "ipopt";
00120                 }
00121                 else    //we have linear program
00122                 {
00123                     solverName = "clp";
00124                 }
00125             }
00126         }//end of if on solverName
00127 
00128         //now build the solver through its constructor
00129 
00130         //std::cout << "SOLVER NAME =  " << solverName << std::endl;
00131 
00132         if (solverName.find("ipopt") != std::string::npos)
00133         {
00134             // we are requesting the Ipopt solver
00135 
00136 #ifdef COIN_HAS_IPOPT
00137             solverType = new IpoptSolver();
00138             solverType->sSolverName = "ipopt";
00139 #else
00140             throw ErrorClass("the Ipopt solver requested is not present");
00141 #endif                
00142         }
00143         else
00144         {
00145             if (solverName.find("lindo") != std::string::npos)
00146             {
00147                 // we are requesting the Lindo solver
00148 #ifdef COIN_HAS_LINDO
00149                 solverType = new LindoSolver();
00150                 solverType->sSolverName = "lindo";
00151 #else
00152                 throw ErrorClass( "the Lindo solver requested is not present");
00153 #endif
00154             }
00155             else
00156             {
00157                 if (solverName.find("clp") != std::string::npos)
00158                 {
00159                     //std::cout << "NEWING SOLVER TYPE " << std::endl;
00160                     solverType = new CoinSolver();
00161                     //std::cout << "END NEWING SOLVER TYPE " << std::endl;
00162                     solverType->sSolverName = "clp";
00163                 }
00164                 else
00165                 {
00166                     if (solverName.find("cplex") != std::string::npos)
00167                     {
00168 #ifdef COIN_HAS_CPX
00169                         solverType = new CoinSolver();
00170                         solverType->sSolverName = "cplex";
00171 #else
00172                         throw ErrorClass( "the Cplex solver requested is not present");
00173 #endif
00174                     }
00175                     else
00176                     {
00177                         if (solverName.find("glpk") != std::string::npos)
00178                         {
00179 #ifdef COIN_HAS_GLPK
00180                             solverType = new CoinSolver();
00181                             solverType->sSolverName = "glpk";
00182 #else
00183                             throw ErrorClass( "the GLPK solver requested is not present");
00184 #endif
00185                         }
00186                         else
00187                         {
00188                             if (solverName.find("dylp") != std::string::npos)
00189                             {
00190 #ifdef COIN_HAS_DYLP
00191                                 solverType = new CoinSolver();
00192                                 solverType->sSolverName = "dylp";
00193 #else
00194                                 throw ErrorClass( "the DyLP solver requested is not present");
00195 #endif
00196                             }
00197                             else
00198                             {
00199                                 if (solverName.find("symphony")
00200                                         != std::string::npos)
00201                                 {
00202 #ifdef COIN_HAS_SYMPHONY
00203                                     solverType = new CoinSolver();
00204                                     solverType->sSolverName = "symphony";
00205 #else
00206                                         throw ErrorClass( "the SYMPHONY solver requested is not present");
00207 #endif
00208                                 }
00209                                 else
00210                                 {
00211                                     if (solverName.find("knitro")
00212                                             != std::string::npos)
00213                                     {
00214 #ifdef COIN_HAS_KNITRO
00215                     
00216                                         solverType = new KnitroSolver();
00217                                         solverType->sSolverName = "knitro";
00218                                      
00219 #else
00220                                         throw ErrorClass( "the Knitro solver requested is not present");
00221 #endif
00222                                     }
00223                                     else
00224                                     {
00225                                         if (solverName.find("vol")
00226                                                 != std::string::npos)
00227                                         {
00228 #ifdef COIN_HAS_VOL
00229                                             solverType = new CoinSolver();
00230                                             solverType->sSolverName = "vol";
00231 #else
00232                                             throw ErrorClass( "the Vol solver requested is not present");
00233 #endif
00234                                         }
00235                                         else
00236                                         {
00237                                             if (solverName.find("bonmin")
00238                                                     != std::string::npos)
00239                                             {
00240                                                 // we are requesting the Bonmin solver
00241 #ifdef COIN_HAS_BONMIN
00242 
00243                                                 solverType = new BonminSolver();
00244                                                 solverType->sSolverName = "bonmin";
00245 #else
00246                                                     throw ErrorClass( "the Bonmin solver requested is not present");
00247 #endif
00248                                             }
00249                                             else
00250                                             {
00251                                                 if (solverName.find("couenne")
00252                                                         != std::string::npos)
00253                                                 {
00254                                                     // we are requesting the Couenne solver
00255 #ifdef COIN_HAS_COUENNE
00256                                                     solverType = new CouenneSolver();
00257                                                     solverType->sSolverName = "couenne";
00258 #else
00259                                                         throw ErrorClass( "the Couenne solver requested is not present");
00260 #endif
00261                                                 }
00262                                                 else     
00263                                                 {
00264                                                         if(solverName.find("cbc") != std::string::npos)
00265                                                         {
00266                                                         solverType = new CoinSolver();
00267                                                         solverType->sSolverName = "cbc";
00268                                                         }
00269                                                         else
00270                                                         {
00271                                                                 std::string errorMessage;
00272                                                                 errorMessage = "solver type " + solverName + " is not supported";
00273                                                                 throw ErrorClass( errorMessage );
00274 
00275                                                                 
00276                                                         }
00277                                                 }
00278                                             }
00279                                         }
00280                                     }
00281                                 }
00282                             }
00283                         }
00284                     }
00285                 }
00286             }
00287         }
00288 
00289         //std::cout << "SET SOLVER INSTANCE " << std::endl;
00290         
00291         
00292         solverType->osinstance = osinstance;
00293         solverType->osol = osol;
00294         solverType->buildSolverInstance();
00295         solverType->setSolverOptions();
00296         solverType->solve();
00297         std::string resultString = solverType->osrl;
00298         if (solverType != NULL)
00299             delete solverType;
00300         solverType = NULL;
00301         return resultString;
00302         
00303 
00304     }
00305     catch (const ErrorClass& eclass)
00306     {
00307         throw eclass;
00308     }
00309 
00310 }//runSolver
00311 
00312 

Generated on Wed Nov 30 03:04:24 2011 by  doxygen 1.4.7