00001 /* $Id: OSBlockSolver.cpp 3038 2009-11-07 11:43:44Z Gassmann $ */ 00017 #include "OSDipBlockCoinSolver.h" 00018 #include "OSErrorClass.h" 00019 00020 00021 00022 OSDipBlockCoinSolver::OSDipBlockCoinSolver(): 00023 m_osinstance(NULL) { 00024 std::cout << "INSIDE OSDipBlockCoinSolver CONSTRUCTOR" << std::endl; 00025 }//end OSDipBlockCoinSolver 00026 00027 00028 OSDipBlockCoinSolver::OSDipBlockCoinSolver( OSInstance *osinstance) { 00029 00030 try{ 00031 m_osinstance = osinstance; 00032 //initialize the solver 00033 m_solver = NULL; 00034 m_solver = new CoinSolver(); 00035 //lets just start with Cbc 00036 m_solver->sSolverName ="cbc"; 00037 m_solver->osinstance = m_osinstance; 00038 m_solver->buildSolverInstance(); 00039 //m_osrlreader = NULL; 00040 //m_osrlreader = new OSrLReader(); 00041 m_numberOfVar = m_osinstance->getVariableNumber(); 00042 00043 } catch (const ErrorClass& eclass) { 00044 00045 throw ErrorClass(eclass.errormsg); 00046 00047 } 00048 00049 }//end OSDipBlockCoinSolver 00050 00051 OSDipBlockCoinSolver::~OSDipBlockCoinSolver(){ 00052 00053 if(m_solver != NULL) delete m_solver; 00054 std::cout << "INSIDED ~OSDipBlockCoinSolver()" << std::endl; 00055 //if(m_osrlreader != NULL) delete m_osrlreader; 00056 }//end ~OSDipBlockCoinSolver 00057 00058 void OSDipBlockCoinSolver::solve(double *cost, std::vector<IndexValuePair*> *solIndexValPair, double *optVal){ 00059 00060 try{ 00061 //set the objective function 00062 //here we are using the Osi Interface 00063 //we have already built the model 00064 m_solver->osiSolver->setObjective( cost); 00065 00066 //an echo check -- kipp put this inside a statement using a print log 00067 for(int i = 0 ; i < m_numberOfVar; i++){ 00068 00069 m_osinstance->instanceData->objectives->obj[0]->coef[i]->value 00070 = cost[ i]; 00071 00072 } 00073 m_osinstance->bObjectivesModified = true; 00074 std::cout << m_osinstance->printModel( ) << std::endl; 00075 // 00076 00077 00078 //solve the model 00079 m_solver->solve(); 00080 m_osresult = m_solver->osresult; 00081 std::string solStatus; 00082 // the argument is the solution index 00083 00084 00085 solStatus = m_osresult->getSolutionStatusType( 0 ); 00086 00087 std::cout << "SOLUTION STATUS " << solStatus << std::endl; 00088 // if solStatus is optimal get the optimal solution value 00089 if( solStatus.find("ptimal") != string::npos ){ 00090 //first index is objIdx, second is solution index 00091 *optVal = m_osresult->getOptimalObjValue( -1, 0); 00092 *solIndexValPair = m_osresult->getOptimalPrimalVariableValues( 0); 00093 }else{ 00094 throw ErrorClass("problem -- did not optimize a subproblem"); 00095 } 00096 00097 } catch (const ErrorClass& eclass) { 00098 00099 throw ErrorClass(eclass.errormsg); 00100 00101 } 00102 00103 }//end solve 00104 00105 00106 void OSDipBlockCoinSolver::solve(double *cost, std::string *osrl){ 00107 00108 00109 try{ 00110 //set the objective function 00111 //here we are using the Osi Interface 00112 //we have already built the model 00113 m_solver->osiSolver->setObjective( cost); 00114 00115 //std::cout << m_osinstance->printModel( ) << std::endl; 00116 //solve the model 00117 m_solver->solve(); 00118 //std::cout << "MODEL BEING SOLVED " << std::endl; 00119 //get the solution 00120 //m_osresult = m_osrlreader->readOSrL( m_solver->osrl ); 00121 m_osresult = m_solver->osresult; 00122 std::string solStatus; 00123 // the argument is the solution index 00124 00125 00126 solStatus = m_osresult->getSolutionStatusType( 0 ); 00127 00128 //std::cout << "SOLUTION STATUS " << solStatus << std::endl; 00129 // if solStatus is optimal get the optimal solution value 00130 if( solStatus.find("ptimal") != string::npos ){ 00131 //first index is objIdx, second is solution index 00132 *osrl = m_solver->osrl; 00133 }else{ 00134 throw ErrorClass("problem -- did not optimize a subproblem"); 00135 } 00136 00137 } catch (const ErrorClass& eclass) { 00138 00139 throw ErrorClass(eclass.errormsg); 00140 00141 } 00142 00143 }//end solve 00144 00145