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