00001
00017 #include <iostream>
00018 #include <sstream>
00019 #include <string>
00020 #include "OSMatlab.h"
00021
00022
00023
00024 using std::cout;
00025 using std::endl;
00026 using std::ostringstream;
00027
00028
00029
00030
00031 OSMatlab::OSMatlab() {
00032 sparseMat = NULL;
00033 bu = NULL;
00034 bl = NULL;
00035 obj = NULL;
00036 vl = NULL;
00037 vu = NULL;
00038 objType = 0;
00039 osinstance = NULL;
00040 varType = NULL;
00041 numQTerms = 0;
00042 qRows = NULL;
00043 qIndex1 = NULL;
00044 qIndex2 = NULL;
00045 qVal = NULL;
00046 numVar = 0;
00047 numCon = 0;
00048 instanceName=" ";
00049 sSolverName="";
00050 sAgentAddress="";
00051 solverType = NULL;
00052 }
00053
00054
00055 OSMatlab::~OSMatlab() {
00056 delete osinstance;
00057 }
00058
00059 std::string OSMatlab::solve() {
00060 ostringstream outStr;
00061 std::string osol = "<osol> <optimization> <other name=\"os_solver\">" +
00062 sSolverName + "</other> </optimization></osol>";
00063 osil = "";
00064
00065
00066
00067
00068 try{
00069 if(osinstance == NULL ) throw ErrorClass( "there is no problem instance");
00070 else{
00071 OSiLWriter *osilwriter;
00072 osilwriter = new OSiLWriter();
00073 osilwriter->m_bWhiteSpace = true;
00074 osil = osilwriter->writeOSiL( osinstance);
00075 if( sSolverName.find( "lindo") != std::string::npos) {
00076
00077 bool bLindoIsPresent = false;
00078 #ifdef COIN_HAS_LINDO
00079 bLindoIsPresent = true;
00080 solverType = new LindoSolver();
00081 #endif
00082 if(bLindoIsPresent == false) throw ErrorClass( "the Lindo solver requested is not present");
00083 }
00084 else{
00085 if( sSolverName.find( "clp") != std::string::npos){
00086 solverType = new CoinSolver();
00087 solverType->sSolverName = "clp";
00088 }
00089 else{
00090 if( sSolverName.find( "cbc") != std::string::npos){
00091 solverType = new CoinSolver();
00092 solverType->sSolverName = "cbc";
00093 }
00094 else{
00095 if( sSolverName.find( "cplex") != std::string::npos){
00096 solverType = new CoinSolver();
00097 solverType->sSolverName = "cplex";
00098 }
00099 else{
00100 if( sSolverName.find( "glpk") != std::string::npos){
00101 solverType = new CoinSolver();
00102 solverType->sSolverName = "glpk";
00103
00104
00105 }
00106 else{
00107 if( sSolverName.find( "ipopt") != std::string::npos){
00108
00109
00110 bool bIpoptIsPresent = false;
00111 if(sAgentAddress == "" ){
00112 #ifdef COIN_HAS_IPOPT
00113 bIpoptIsPresent = true;
00114
00115 IpoptSolver *ipoptSolver = new IpoptSolver();
00116 ipoptSolver->osol = osol;
00117 ipoptSolver->osinstance = osinstance;
00118 ipoptSolver->solve();
00119
00120 return ipoptSolver->osrl ;
00121
00122 #endif
00123 if(bIpoptIsPresent == false) throw ErrorClass( "the Ipopt solver requested is not present");
00124 }
00125 }
00126 else{
00127 if( sSolverName.find( "symphony") != std::string::npos){
00128 solverType = new CoinSolver();
00129 solverType->sSolverName = "symphony";
00130 }
00131 else{
00132 if( sSolverName.find( "dylp") != std::string::npos){
00133 solverType = new CoinSolver();
00134 solverType->sSolverName = "dylp";
00135 }
00136 else{
00137 throw ErrorClass( "a supported solver is not present");
00138 }
00139 }
00140 }
00141 }
00142 }
00143 }
00144 }
00145 }
00146 }
00147
00148 if( (sSolverName.find( "ipopt") == std::string::npos) && (sAgentAddress == "")){
00149 solverType->osol = osol;
00150 solverType->osinstance = osinstance;
00151 solverType->solve();
00152 return solverType->osrl ;
00153 }
00154 }
00155 catch(const ErrorClass& eclass){
00156 return eclass.errormsg;
00157 }
00158
00159
00160 if(sAgentAddress != ""){
00161 OSSolverAgent* osagent = NULL;
00162 OSiLWriter *osilwriter = NULL;
00163 osilwriter = new OSiLWriter();
00164 std::string osil = osilwriter->writeOSiL( osinstance);
00165 osagent = new OSSolverAgent( sAgentAddress);
00166
00167 return osagent->solve(osil, osol);
00168 }
00169 return "";
00170
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 void OSMatlab::createOSInstance(){
00216 ostringstream outStr;
00217
00218 int i;
00219 osinstance = new OSInstance();
00220
00221
00222
00223 osinstance->setInstanceDescription("A MATLAB Created Problem");
00224 osinstance->setInstanceName( instanceName);
00225
00226
00227
00228
00229 std::string *varNames;
00230 varNames = new std::string[ numVar];
00231 osinstance->setVariableNumber( numVar);
00232 for(i = 0; i < numVar; i++){
00233 outStr << "x";
00234 outStr << i ;
00235 varNames[ i] = outStr.str();
00236 osinstance->addVariable(i, varNames[ i] , vl[ i], vu[ i], varType[ i], OSNAN, "");
00237 outStr.str("");
00238 }
00239
00240
00241
00242
00243 osinstance->setObjectiveNumber( 1);
00244
00245 SparseVector *objcoeff;
00246 objcoeff = new SparseVector( numVar);
00247 objcoeff->indexes = new int[ numVar];
00248 objcoeff->values = new double[ numVar];
00249 for(i = 0; i < numVar; i++){
00250 objcoeff->indexes[ i] = i;
00251 objcoeff->values[ i] = obj[ i];
00252 }
00253 std::string maxOrMin = "min";
00254 if( objType == true) maxOrMin = "max";
00255
00256 osinstance->addObjective(-1, "objfunction", maxOrMin, 0.0, 1.0, objcoeff);
00257
00258
00259
00260 std::string *conNames;
00261 conNames = new std::string[ numCon];
00262 outStr.str("");
00263 osinstance->setConstraintNumber( numCon);
00264 for(i = 0; i < numCon; i++){
00265 outStr << "r";
00266 outStr << i;
00267 conNames[ i] = outStr.str();
00268 osinstance->addConstraint(i, "row" , bl[ i], bu[ i], 0);
00269
00270 outStr.str("");
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 osinstance->setLinearConstraintCoefficients(sparseMat->valueSize, true, sparseMat->values, 0, sparseMat->valueSize - 1,
00286 sparseMat->indexes, 0, sparseMat->valueSize - 1, sparseMat->starts, 0, sparseMat->startSize - 1);
00287 if(numQTerms > 0){
00288 osinstance->setQuadraticTermsInNonlinearExpressions(numQTerms, qRows, qIndex1, qIndex2, qVal);
00289 }
00290 return;
00291 }