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

Generated on Sat Mar 29 22:38:02 2008 by  doxygen 1.5.3