/home/coin/SVN-release/OS-1.0.0/OS/src/OSModelInterfaces/OSMatlab.cpp

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

Generated on Thu May 15 22:15:05 2008 by  doxygen 1.4.7