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

Generated on Tue Sep 30 03:01:24 2008 by  doxygen 1.4.7