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