00001 /* $Id: OSBlockSlpSolver.cpp 3038 2009-11-07 11:43:44Z Gassmann $ */ 00015 #include "OSDipBlockSlpSolver.h" 00016 #include "OSErrorClass.h" 00017 #include "OSDataStructures.h" 00018 00019 00020 00021 OSDipBlockSlpSolver::OSDipBlockSlpSolver(): 00022 m_osinstance(NULL) { 00023 std::cout << "INSIDE OSDipBlockCoinSolver CONSTRUCTOR" << std::endl; 00024 }//end OSDipBlockSlpSolver 00025 00026 00027 OSDipBlockSlpSolver::OSDipBlockSlpSolver( OSInstance *osinstance) { 00028 00029 try{ 00030 m_osinstance = osinstance; 00031 m_numberOfVar = m_osinstance->getVariableNumber(); 00032 00033 } catch (const ErrorClass& eclass) { 00034 00035 throw ErrorClass(eclass.errormsg); 00036 00037 } 00038 00039 }//end OSDipBlockSlpSolver 00040 00041 OSDipBlockSlpSolver::~OSDipBlockSlpSolver(){ 00042 00043 std::cout << "INSIDED ~OSDipBlockSlpSolver()" << std::endl; 00044 00045 00046 std::vector<IndexValuePair*>::iterator vit; 00047 00048 for (vit = m_primalVals.begin(); vit != m_primalVals.end(); vit++) { 00049 00050 delete *vit; 00051 } 00052 //if(m_osrlreader != NULL) delete m_osrlreader; 00053 }//end ~OSDipBlockSlpSolver 00054 00055 void OSDipBlockSlpSolver::solve(double *cost, std::vector<IndexValuePair*> *solIndexValPair, double *optVal){ 00056 00057 try{ 00058 struct IndexValuePair *primalValPair; 00059 00060 std::vector<IndexValuePair*>::iterator vit; 00061 00062 for (vit = m_primalVals.begin(); vit != m_primalVals.end(); vit++) { 00063 00064 delete *vit; 00065 } 00066 m_primalVals.clear(); 00067 //sum up negative coefficient 00068 // -- critical -- we assume last variable is the y variable 00069 // this is just for illustration 00070 *optVal = 0; 00071 for(int i = 0 ; i < m_numberOfVar - 1; i++){ 00072 00073 m_osinstance->instanceData->objectives->obj[0]->coef[i]->value 00074 = cost[ i]; 00075 00076 00077 primalValPair = new IndexValuePair(); 00078 00079 primalValPair->value = 0.0; 00080 primalValPair->idx = i; 00081 00082 if (cost[ i] < 0){ 00083 00084 *optVal = *optVal + cost[ i]; 00085 primalValPair->value = 1.0; 00086 } 00087 00088 m_primalVals.push_back( primalValPair); 00089 00090 00091 }//end for 00092 00093 //now set the y variable to 1 if optVal is negative 00094 //otherwise 0 00095 primalValPair = new IndexValuePair(); 00096 primalValPair->idx = m_numberOfVar - 1; 00097 00098 if(*optVal < 0){ 00099 00100 primalValPair->value = 1.0; 00101 *optVal = *optVal + cost[ m_numberOfVar - 1]; 00102 00103 }else{ 00104 00105 primalValPair->value = 0.0; 00106 } 00107 00108 m_primalVals.push_back( primalValPair); 00109 00110 00111 m_osinstance->bObjectivesModified = true; 00112 std::cout << m_osinstance->printModel( ) << std::endl; 00113 // 00114 00115 std::cout << "NUMBER OF VARIABLES m_primalVals = " << m_primalVals.size() << std::endl; 00116 00117 for (vit = m_primalVals.begin(); vit != m_primalVals.end(); vit++) { 00118 00119 //solIndexValPair.push_back( *vit); 00120 00121 } 00122 00123 *solIndexValPair = m_primalVals; 00124 // the argument is the solution index 00125 00126 00127 //solStatus = m_osresult->getSolutionStatusType( 0 ); 00128 00129 //std::cout << "SOLUTION STATUS " << solStatus << std::endl; 00130 // if solStatus is optimal get the optimal solution value 00131 //if( solStatus.find("ptimal") != string::npos ){ 00132 //first index is objIdx, second is solution index 00133 // *optVal = m_osresult->getOptimalObjValue( -1, 0); 00134 // *solIndexValPair = m_osresult->getOptimalPrimalVariableValues( 0); 00135 //}else{ 00136 // throw ErrorClass("problem -- did not optimize a subproblem"); 00137 //} 00138 00139 } catch (const ErrorClass& eclass) { 00140 00141 throw ErrorClass(eclass.errormsg); 00142 00143 } 00144 00145 }//end solve 00146 00147 00148 void OSDipBlockSlpSolver::solve(double *cost, std::string *osrl){ 00149 00150 00151 try{ 00152 //not implemented 00153 00154 } catch (const ErrorClass& eclass) { 00155 00156 throw ErrorClass(eclass.errormsg); 00157 00158 } 00159 00160 }//end solve 00161