- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

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

Go to the documentation of this file.

    00001 /* $Id: OSMatlabSolver.cpp 2831 2009-07-21 07:34:44Z kmartin $ */
    00018 #include <iostream>
    00019 #include <sstream>
    00020 #include <string>
    00021 #include "OSMatlabSolver.h"
    00022
    00023
    00024
    00025 using std::cout;
    00026 using std::endl;
    00027 using std::ostringstream;
    00028
    00029
    00030
    00031
    00032 OSMatlab::OSMatlab() {
    00033         sparseMat = NULL;
    00034         bu = NULL;
    00035         bl = NULL;
    00036         obj = 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
    00219         int i;
    00220         osinstance = new OSInstance();
    00221         //
    00222         // put in some of the OSInstance <instanceHeader> information
    00223         //osinstance->setInstanceSource("An example from the LINDO API samples directory");
    00224         osinstance->setInstanceDescription("A MATLAB Created Problem");
    00225         osinstance->setInstanceName( instanceName);
    00226         //
    00227         // now put in the OSInstance <instanceData> information
    00228         //
    00229         // first the variables
    00230         std::string *varNames;
    00231         varNames = new std::string[ numVar];
    00232         osinstance->setVariableNumber( numVar);
    00233         for(i = 0; i < numVar; i++){
    00234                 outStr << "x";
    00235                 outStr << i ;
    00236                 varNames[ i] = outStr.str();
    00237                 osinstance->addVariable(i, varNames[ i] , vl[ i], vu[ i], varType[ i]);
    00238                 outStr.str("");
    00239         }
    00240         //
    00241         //osinstance->setVariables(numVar, varNames, vl, vu, varType, NULL, NULL);
    00242         //
    00243         // now add the objective function
    00244         osinstance->setObjectiveNumber( 1);
    00245         // now the coefficient
    00246         SparseVector *objcoeff;
    00247         objcoeff = new SparseVector( numVar);
    00248         objcoeff->indexes = new int[ numVar];
    00249         objcoeff->values = new double[ numVar];
    00250         for(i = 0; i < numVar; i++){
    00251                 objcoeff->indexes[ i] = i;
    00252                 objcoeff->values[ i] = obj[ i];
    00253         }
    00254         std::string maxOrMin = "min";
    00255         if( objType == true) maxOrMin = "max";
    00256         //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
    00257         osinstance->addObjective(-1, "objfunction", maxOrMin, 0.0, 1.0, objcoeff);
    00258         //
    00259         // now the constraints
    00260
    00261         std::string *conNames;
    00262         conNames = new std::string[ numCon];
    00263         outStr.str("");
    00264         osinstance->setConstraintNumber( numCon);
    00265         for(i = 0; i < numCon; i++){
    00266                 outStr << "r";
    00267                 outStr << i;
    00268                 conNames[ i] = outStr.str();
    00269                 osinstance->addConstraint(i, "row" , bl[ i], bu[ i], 0);
    00270
    00271                 outStr.str("");
    00272         }
    00273         //osinstance->addConstraint(0, "row0", -OSDBL_MAX, 4, 0);
    00274         //osinstance->addConstraint(1, "row1", -OSDBL_MAX, 6, 0);
    00275         //osinstance->addConstraint(2, "row2", -OSDBL_MAX, 0, 0);
    00276         //osinstance->addConstraint(3, "row3", 0 , OSDBL_MAX, 0);
    00277
    00278
    00279         //osinstance->setConstraints( numCon, conNames, bl, bu, NULL);
    00280         //
    00281         // now add the <linearConstraintCoefficients>
    00282         //bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor,
    00283         //double* values, int valuesBegin, int valuesEnd,
    00284         //int* indexes, int indexesBegin, int indexesEnd,
    00285         //int* starts, int startsBegin, int startsEnd);
    00286         osinstance->setLinearConstraintCoefficients(sparseMat->valueSize, true, sparseMat->values, 0, sparseMat->valueSize - 1,
    00287                 sparseMat->indexes, 0, sparseMat->valueSize - 1, sparseMat->starts, 0, sparseMat->startSize - 1);
    00288         if(numQTerms > 0){
    00289                 osinstance->setQuadraticTermsInNonlinearExpressions(numQTerms, qRows, qIndex1, qIndex2, qVal);
    00290         }
    00291         return;
    00292 }// end createOSInstance