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

Go to the documentation of this file.
00001 /* $Id: OSMatlabSolver.cpp 4292 2011-09-21 05:47:18Z kmartin $ */
00016 #include <iostream>
00017 #include <sstream>
00018 #include <string>
00019 #include "OSMatlabSolver.h"
00020 #include "OSConfig.h"
00021 
00022 #ifdef COIN_HAS_LINDO
00023 #include "OSLindoSolver.h"
00024 #endif
00025 
00026 #ifdef COIN_HAS_IPOPT
00027 #include "OSIpoptSolver.h"
00028 #endif
00029 
00030 
00031 using std::cout;
00032 using std::endl;
00033 using std::ostringstream;
00034 
00035 
00036 
00037 
00038 OSMatlab::OSMatlab()
00039 {
00040     sparseMat = NULL;
00041     bu = NULL;
00042     bl = NULL;
00043     obj = NULL;
00044     vl = NULL;
00045     vu = NULL;
00046     objType = 0;
00047     osinstance = NULL;
00048     varType = NULL;
00049     numQTerms = 0;
00050     qRows = NULL;
00051     qIndex1 = NULL;
00052     qIndex2 = NULL;
00053     qVal = NULL;
00054     numVar = 0;
00055     numCon = 0;
00056     instanceName=" ";
00057     sSolverName="";
00058     sAgentAddress="";
00059     solverType = NULL;
00060 }//end OSMatlab
00061 
00062 
00063 OSMatlab::~OSMatlab()
00064 {
00065     delete osinstance;
00066 }//end ~OSMatlab
00067 
00068 std::string OSMatlab::solve()
00069 {
00070     ostringstream outStr;
00071     //std::string osol = "<osol> <optimization>  <other name=\"os_solver\">" +
00072     //sSolverName + "</other> </optimization></osol>";
00073     std::string osol = "";
00074     osil = "";
00075 //
00076 //
00077 //
00078 //
00079     try
00080     {
00081         if(osinstance == NULL ) throw ErrorClass( "there is no problem instance");
00082         else
00083         {
00084             OSiLWriter *osilwriter;
00085             osilwriter = new OSiLWriter();
00086             osilwriter->m_bWhiteSpace = true;
00087             osil =  osilwriter->writeOSiL( osinstance);
00088             if( sSolverName.find( "lindo") != std::string::npos)
00089             {
00090                 // we are requesting the Lindo solver
00091                 bool bLindoIsPresent = false;
00092 #ifdef COIN_HAS_LINDO
00093                 bLindoIsPresent = true;
00094                 solverType = new LindoSolver();
00095 #endif
00096                 if(bLindoIsPresent == false) throw ErrorClass( "the Lindo solver requested is not present");
00097             }
00098             else
00099             {
00100                 if( sSolverName.find( "clp") != std::string::npos)
00101                 {
00102                     solverType = new CoinSolver();
00103                     solverType->sSolverName = "clp";
00104                 }
00105                 else
00106                 {
00107                     if( sSolverName.find( "cbc") != std::string::npos)
00108                     {
00109                         solverType = new CoinSolver();
00110                         solverType->sSolverName = "cbc";
00111                     }
00112                     else
00113                     {
00114                         if( sSolverName.find( "cplex") != std::string::npos)
00115                         {
00116                             solverType = new CoinSolver();
00117                             solverType->sSolverName = "cplex";
00118                         }
00119                         else
00120                         {
00121                             if( sSolverName.find( "glpk") != std::string::npos)
00122                             {
00123                                 solverType = new CoinSolver();
00124                                 solverType->sSolverName = "glpk";
00125 
00126 
00127                             }
00128                             else
00129                             {
00130                                 if( sSolverName.find( "ipopt") != std::string::npos)
00131                                 {
00132                                     // have to act differently since Ipopt uses smart pointers
00133                                     // we are requesting the Ipopt solver
00134                                     bool bIpoptIsPresent = false;
00135                                     if(sAgentAddress == "" )
00136                                     {
00137 #ifdef COIN_HAS_IPOPT
00138                                         bIpoptIsPresent = true;
00139                                         //std::cout << "Create an Ipopt solver and optimize"<< std::endl;
00140                                         IpoptSolver *ipoptSolver  = new IpoptSolver();
00141                                         ipoptSolver->osol = osol;
00142                                         ipoptSolver->osinstance = osinstance;
00143                                         ipoptSolver->solve();
00144                                         //std::cout << "Done optimizing with Ipopt"<< std::endl;
00145                                         return  ipoptSolver->osrl ;
00146                                         //std::cout << "Have Ipopt writ out osrl"<< std::endl;
00147 #endif
00148                                         if(bIpoptIsPresent == false) throw ErrorClass( "the Ipopt solver requested is not present");
00149                                     }
00150                                 }
00151                                 else
00152                                 {
00153                                     if( sSolverName.find( "symphony") != std::string::npos)
00154                                     {
00155                                         solverType = new CoinSolver();
00156                                         solverType->sSolverName = "symphony";
00157                                     }
00158                                     else
00159                                     {
00160                                         if( sSolverName.find( "dylp") != std::string::npos)
00161                                         {
00162                                             solverType = new CoinSolver();
00163                                             solverType->sSolverName = "dylp";
00164                                         }
00165                                         else
00166                                         {
00167                                             throw ErrorClass( "a supported solver is not present");
00168                                         }
00169                                     }
00170                                 }
00171                             }
00172                         }
00173                     }
00174                 }
00175             }
00176         }
00177         // do a local solve
00178         if( (sSolverName.find( "ipopt") == std::string::npos) && (sAgentAddress == ""))
00179         {
00180             solverType->osol = osol;
00181             solverType->osinstance = osinstance;
00182             solverType->solve();
00183             return solverType->osrl ;
00184         }
00185     }
00186     catch(const ErrorClass& eclass)
00187     {
00188         return eclass.errormsg;
00189     }
00190 
00191     // do the following for a remote solve
00192     if(sAgentAddress != "")
00193     {
00194         OSSolverAgent* osagent = NULL;
00195         OSiLWriter *osilwriter = NULL;
00196         osilwriter = new OSiLWriter();
00197         std::string  osil = osilwriter->writeOSiL( osinstance);
00198         osagent = new OSSolverAgent( sAgentAddress);
00199         //cout << "Place remote synchronous call" << endl;
00200         return osagent->solve(osil, osol);
00201     }
00202     return "";
00203 
00204 //
00205 //
00206 //
00207 //      DefaultSolver *solver  = NULL;
00208 //      try{
00209 //              if(osinstance != NULL){
00210 //                      OSiLWriter *osilwriter;
00211 //                      osilwriter = new OSiLWriter();
00212 //                      osilwriter->m_bWhiteSpace = true;
00213 //                      osil =  osilwriter->writeOSiL( osinstance);
00214 //                      solver = new CoinSolver();
00215 //                      solver->sSolverName = "cbc";
00216 //                      //SmartPtr<IpoptSolver> ipoptSolver  = new IpoptSolver();
00217 //                      //outStr << osil;
00218 //                      //outStr << endl;
00219 //                      //outStr << "Now Solve remotely with  LINDO" << endl;
00220 //                      //outStr << "create a new LINDO Solver for OSiL string solution" << endl;
00221 //                      // below for solving on a remote machine
00222 //                      //
00223 //                      //
00224 //                      //OSSolverAgent* osagent = NULL;
00225 //                      //osagent = new OSSolverAgent( "http://128.135.130.17:8080/os/OSSolverService.jws" );
00226 //                      //outStr << osagent->solve(osil, osol);
00227 //                      ///
00228 //                      //
00229 //                      // do the garbage collection
00230 //                      //delete osilwriter;
00231 //                      //delete osagent;
00232 //                      solver->osinstance = NULL;
00233 //                      solver->osol = osol;
00234 //                      solver->osil = osil;
00235 //                      solver->solve();
00236 //                      outStr << solver->osrl;
00237 //                      return outStr.str();
00238 //                      //return osil;
00239 //              }else{
00240 //                      return "there was no instance";
00241 //              }
00242 //      }
00243 //      catch(const ErrorClass& eclass){
00244 //              return eclass.errormsg;
00245 //      }
00246 }//end solve
00247 
00248 void OSMatlab::createOSInstance()
00249 {
00250     ostringstream outStr;
00251 
00252     int i;
00253     osinstance = new OSInstance();
00254     //
00255     // put in some of the OSInstance <instanceHeader> information
00256     //osinstance->setInstanceSource("An example from the LINDO API samples directory");
00257     osinstance->setInstanceDescription("A MATLAB Created Problem");
00258     osinstance->setInstanceName( instanceName);
00259     //
00260     // now put in the OSInstance <instanceData> information
00261     //
00262     // first the variables
00263     std::string *varNames;
00264     varNames = new std::string[ numVar];
00265     osinstance->setVariableNumber( numVar);
00266     for(i = 0; i < numVar; i++)
00267     {
00268         outStr << "x";
00269         outStr << i ;
00270         varNames[ i] = outStr.str();
00271         osinstance->addVariable(i, varNames[ i] , vl[ i], vu[ i], varType[ i]);
00272         outStr.str("");
00273     }
00274     //
00275     //osinstance->setVariables(numVar, varNames, vl, vu, varType, NULL, NULL);
00276     //
00277     // now add the objective function
00278     osinstance->setObjectiveNumber( 1);
00279     // now the coefficient
00280     SparseVector *objcoeff;
00281     objcoeff = new SparseVector( numVar);
00282     objcoeff->indexes = new int[ numVar];
00283     objcoeff->values = new double[ numVar];
00284     for(i = 0; i < numVar; i++)
00285     {
00286         objcoeff->indexes[ i] = i;
00287         objcoeff->values[ i] = obj[ i];
00288     }
00289     std::string maxOrMin = "min";
00290     if( objType == true) maxOrMin = "max";
00291     //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
00292     osinstance->addObjective(-1, "objfunction", maxOrMin, 0.0, 1.0, objcoeff);
00293     //
00294     // now the constraints
00295 
00296     std::string *conNames;
00297     conNames = new std::string[ numCon];
00298     outStr.str("");
00299     osinstance->setConstraintNumber( numCon);
00300     for(i = 0; i < numCon; i++)
00301     {
00302         outStr << "r";
00303         outStr << i;
00304         conNames[ i] = outStr.str();
00305         osinstance->addConstraint(i, "row" , bl[ i], bu[ i], 0);
00306 
00307         outStr.str("");
00308     }
00309     //osinstance->addConstraint(0, "row0", -OSDBL_MAX, 4, 0);
00310     //osinstance->addConstraint(1, "row1", -OSDBL_MAX, 6, 0);
00311     //osinstance->addConstraint(2, "row2", -OSDBL_MAX, 0, 0);
00312     //osinstance->addConstraint(3, "row3", 0 , OSDBL_MAX, 0);
00313 
00314 
00315     //osinstance->setConstraints( numCon, conNames, bl, bu, NULL);
00316     //
00317     // now add the <linearConstraintCoefficients>
00318     //bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor,
00319     //double* values, int valuesBegin, int valuesEnd,
00320     //int* indexes, int indexesBegin, int indexesEnd,
00321     //int* starts, int startsBegin, int startsEnd);
00322     osinstance->setLinearConstraintCoefficients(sparseMat->valueSize, true, sparseMat->values, 0, sparseMat->valueSize - 1,
00323             sparseMat->indexes, 0, sparseMat->valueSize - 1, sparseMat->starts, 0, sparseMat->startSize - 1);
00324     if(numQTerms > 0)
00325     {
00326         osinstance->setQuadraticTermsInNonlinearExpressions(numQTerms, qRows, qIndex1, qIndex2, qVal);
00327     }
00328     return;
00329 }// end createOSInstance

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