00001
00016 #include <iostream>
00017 #include <sstream>
00018 #include <string>
00019 #include "OSMatlabSolver.h"
00020 #include "OSConfig.h"
00021 #include "OSOutput.h"
00022
00023 #ifdef COIN_HAS_LINDO
00024 #include "OSLindoSolver.h"
00025 #endif
00026
00027 #ifdef COIN_HAS_IPOPT
00028 #include "OSIpoptSolver.h"
00029 #endif
00030
00031
00032 using std::endl;
00033 using std::ostringstream;
00034
00035
00036 OSMatlab::OSMatlab()
00037 {
00038 sparseMat = NULL;
00039 bu = NULL;
00040 bl = NULL;
00041 obj = NULL;
00042 vl = NULL;
00043 vu = NULL;
00044 objType = 0;
00045 osinstance = NULL;
00046 varType = NULL;
00047 numQTerms = 0;
00048 qRows = NULL;
00049 qIndex1 = NULL;
00050 qIndex2 = NULL;
00051 qVal = NULL;
00052 numVar = 0;
00053 numCon = 0;
00054 instanceName=" ";
00055 sSolverName="";
00056 sAgentAddress="";
00057 solverType = NULL;
00058 }
00059
00060
00061 OSMatlab::~OSMatlab()
00062 {
00063 delete osinstance;
00064 }
00065
00066 std::string OSMatlab::solve()
00067 {
00068 ostringstream outStr;
00069
00070
00071 std::string osol = "";
00072 osil = "";
00073
00074
00075
00076
00077 try
00078 {
00079 if(osinstance == NULL ) throw ErrorClass( "there is no problem instance");
00080 else
00081 {
00082 OSiLWriter *osilwriter;
00083 osilwriter = new OSiLWriter();
00084 osilwriter->m_bWhiteSpace = true;
00085 osil = osilwriter->writeOSiL( osinstance);
00086 if( sSolverName.find( "lindo") != std::string::npos)
00087 {
00088
00089 bool bLindoIsPresent = false;
00090 #ifdef COIN_HAS_LINDO
00091 bLindoIsPresent = true;
00092 solverType = new LindoSolver();
00093 #endif
00094 if(bLindoIsPresent == false) throw ErrorClass( "the Lindo solver requested is not present");
00095 }
00096 else
00097 {
00098 if( sSolverName.find( "clp") != std::string::npos)
00099 {
00100 solverType = new CoinSolver();
00101 solverType->sSolverName = "clp";
00102 }
00103 else
00104 {
00105 if( sSolverName.find( "cbc") != std::string::npos)
00106 {
00107 solverType = new CoinSolver();
00108 solverType->sSolverName = "cbc";
00109 }
00110 else
00111 {
00112 if( sSolverName.find( "cplex") != std::string::npos)
00113 {
00114 solverType = new CoinSolver();
00115 solverType->sSolverName = "cplex";
00116 }
00117 else
00118 {
00119 if( sSolverName.find( "glpk") != std::string::npos)
00120 {
00121 solverType = new CoinSolver();
00122 solverType->sSolverName = "glpk";
00123
00124
00125 }
00126 else
00127 {
00128 if( sSolverName.find( "ipopt") != std::string::npos)
00129 {
00130
00131
00132 bool bIpoptIsPresent = false;
00133 if(sAgentAddress == "" )
00134 {
00135 #ifdef COIN_HAS_IPOPT
00136 bIpoptIsPresent = true;
00137 IpoptSolver *ipoptSolver = new IpoptSolver();
00138 ipoptSolver->osol = osol;
00139 ipoptSolver->osinstance = osinstance;
00140 ipoptSolver->solve();
00141 return ipoptSolver->osrl ;
00142 #endif
00143 if(bIpoptIsPresent == false) throw ErrorClass( "the Ipopt solver requested is not present");
00144 }
00145 }
00146 else
00147 {
00148 if( sSolverName.find( "symphony") != std::string::npos)
00149 {
00150 solverType = new CoinSolver();
00151 solverType->sSolverName = "symphony";
00152 }
00153 else
00154 {
00155 if( sSolverName.find( "dylp") != std::string::npos)
00156 {
00157 solverType = new CoinSolver();
00158 solverType->sSolverName = "dylp";
00159 }
00160 else
00161 {
00162 throw ErrorClass( "a supported solver is not present");
00163 }
00164 }
00165 }
00166 }
00167 }
00168 }
00169 }
00170 }
00171 }
00172
00173 if( (sSolverName.find( "ipopt") == std::string::npos) && (sAgentAddress == ""))
00174 {
00175 solverType->osol = osol;
00176 solverType->osinstance = osinstance;
00177 solverType->solve();
00178 return solverType->osrl ;
00179 }
00180 }
00181 catch(const ErrorClass& eclass)
00182 {
00183 return eclass.errormsg;
00184 }
00185
00186
00187 if(sAgentAddress != "")
00188 {
00189 OSSolverAgent* osagent = NULL;
00190 OSiLWriter *osilwriter = NULL;
00191 osilwriter = new OSiLWriter();
00192 std::string osil = osilwriter->writeOSiL( osinstance);
00193 osagent = new OSSolverAgent( sAgentAddress);
00194 return osagent->solve(osil, osol);
00195 }
00196 return "";
00197
00198 }
00199
00200 void OSMatlab::createOSInstance()
00201 {
00202 ostringstream outStr;
00203
00204 int i;
00205 osinstance = new OSInstance();
00206
00207
00208
00209 osinstance->setInstanceDescription("A MATLAB Created Problem");
00210 osinstance->setInstanceName( instanceName);
00211
00212
00213
00214
00215 std::string *varNames;
00216 varNames = new std::string[ numVar];
00217 osinstance->setVariableNumber( numVar);
00218 for(i = 0; i < numVar; i++)
00219 {
00220 outStr << "x";
00221 outStr << i ;
00222 varNames[ i] = outStr.str();
00223 osinstance->addVariable(i, varNames[ i] , vl[ i], vu[ i], varType[ i]);
00224 outStr.str("");
00225 }
00226
00227
00228 osinstance->setObjectiveNumber( 1);
00229
00230 SparseVector *objcoeff;
00231 objcoeff = new SparseVector( numVar);
00232 objcoeff->indexes = new int[ numVar];
00233 objcoeff->values = new double[ numVar];
00234 for(i = 0; i < numVar; i++)
00235 {
00236 objcoeff->indexes[ i] = i;
00237 objcoeff->values[ i] = obj[ i];
00238 }
00239 std::string maxOrMin = "min";
00240 if( objType == true) maxOrMin = "max";
00241 osinstance->addObjective(-1, "objfunction", maxOrMin, 0.0, 1.0, objcoeff);
00242
00243
00244 std::string *conNames;
00245 conNames = new std::string[ numCon];
00246 outStr.str("");
00247 osinstance->setConstraintNumber( numCon);
00248 for(i = 0; i < numCon; i++)
00249 {
00250 outStr << "r";
00251 outStr << i;
00252 conNames[ i] = outStr.str();
00253 osinstance->addConstraint(i, "row" , bl[ i], bu[ i], 0);
00254
00255 outStr.str("");
00256 }
00257 osinstance->setLinearConstraintCoefficients(sparseMat->valueSize, true, sparseMat->values, 0, sparseMat->valueSize - 1,
00258 sparseMat->indexes, 0, sparseMat->valueSize - 1, sparseMat->starts, 0, sparseMat->startSize - 1);
00259 if(numQTerms > 0)
00260 {
00261 osinstance->setQuadraticTermsInNonlinearExpressions(numQTerms, qRows, qIndex1, qIndex2, qVal);
00262 }
00263 return;
00264 }