00001
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 }
00054
00055
00056 OSMatlab::~OSMatlab() {
00057 delete osinstance;
00058 }
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
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
00110
00111 bool bIpoptIsPresent = false;
00112 if(sAgentAddress == "" ){
00113 #ifdef COIN_HAS_IPOPT
00114 bIpoptIsPresent = true;
00115
00116 IpoptSolver *ipoptSolver = new IpoptSolver();
00117 ipoptSolver->osol = osol;
00118 ipoptSolver->osinstance = osinstance;
00119 ipoptSolver->solve();
00120
00121 return ipoptSolver->osrl ;
00122
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
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
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
00168 return osagent->solve(osil, osol);
00169 }
00170 return "";
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 }
00215
00216 void OSMatlab::createOSInstance(){
00217 ostringstream outStr;
00218
00219 int i;
00220 osinstance = new OSInstance();
00221
00222
00223
00224 osinstance->setInstanceDescription("A MATLAB Created Problem");
00225 osinstance->setInstanceName( instanceName);
00226
00227
00228
00229
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
00242
00243
00244 osinstance->setObjectiveNumber( 1);
00245
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
00257 osinstance->addObjective(-1, "objfunction", maxOrMin, 0.0, 1.0, objcoeff);
00258
00259
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
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
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 }