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 vu = 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 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 for(i = 0; i < numVar; i++){
00232 outStr << "x";
00233 outStr << i ;
00234 varNames[ i] = outStr.str();
00235
00236 outStr.str("");
00237 }
00238
00239 osinstance->setVariables(numVar, varNames, vl, vu, varType, NULL, NULL);
00240
00241
00242 osinstance->setObjectiveNumber( 1);
00243
00244 SparseVector *objcoeff;
00245 objcoeff = new SparseVector( numVar);
00246 objcoeff->indexes = new int[ numVar];
00247 objcoeff->values = new double[ numVar];
00248 for(i = 0; i < numVar; i++){
00249 objcoeff->indexes[ i] = i;
00250 objcoeff->values[ i] = obj[ i];
00251 }
00252 std::string maxOrMin = "min";
00253 if( objType == true) maxOrMin = "max";
00254
00255 osinstance->addObjective(-1, "objfunction", maxOrMin, 0.0, 1.0, objcoeff);
00256
00257
00258
00259 std::string *conNames;
00260 conNames = new std::string[ numCon];
00261 outStr.str("");
00262 for(i = 0; i < numCon; i++){
00263 outStr << "r";
00264 outStr << i;
00265 conNames[ i] = outStr.str();
00266
00267 outStr.str("");
00268 }
00269
00270 osinstance->setConstraints( numCon, conNames, bl, bu, NULL);
00271
00272
00273
00274
00275
00276
00277 osinstance->setLinearConstraintCoefficients(sparseMat->valueSize, true, sparseMat->values, 0, sparseMat->valueSize - 1,
00278 sparseMat->indexes, 0, sparseMat->valueSize - 1, sparseMat->starts, 0, sparseMat->startSize - 1);
00279 if(numQTerms > 0){
00280 osinstance->setQuadraticTermsInNonlinearExpressions(numQTerms, qRows, qIndex1, qIndex2, qVal);
00281 }
00282 return;
00283 }