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