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