- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/OS-2.3.5/OS/applications/columnGen/code/OSColGenApp.cpp

Go to the documentation of this file.

    00001 /* $Id: OSColGenApp.cpp 3038 2009-11-07 11:43:44Z kmartin $ */
    00017 #include "OSColGenApp.h"
    00018 #include "OSErrorClass.h"
    00019 #include "OSDataStructures.h"
    00020
    00021 #include "OSInstance.h"
    00022 #include "OSCoinSolver.h"
    00023 #include "OSConfig.h"
    00024 #include "OSResult.h"
    00025 #include "OSOption.h"
    00026 #include "OSiLReader.h"
    00027 #include "OSiLWriter.h"
    00028 #include "OSoLReader.h"
    00029 #include "OSoLWriter.h"
    00030 #include "OSrLReader.h"
    00031 #include "OSrLWriter.h"
    00032 #include "OSInstance.h"
    00033 #include "OSFileUtil.h"
    00034
    00035 #ifdef COIN_HAS_COUENNE
    00036 #include "OSCouenneSolver.h"
    00037 #endif
    00038
    00039 #ifdef COIN_HAS_IPOPT
    00040 #include "OSIpoptSolver.h"
    00041 #endif
    00042
    00043
    00044
    00045 #include <sstream>
    00046 using std::ostringstream;
    00047
    00048
    00049
    00050
    00051 OSColGenApp::OSColGenApp():
    00052         m_osinstanceMaster(NULL) {
    00053         std::cout << "INSIDE OSColGenApp CONSTRUCTOR" << std::endl;
    00054 }//end OSColGenApp Constructor
    00055
    00056
    00057 OSColGenApp::OSColGenApp(   OSOption *osoption) {
    00058           std::cout << "INSIDE OSColGenApp CONSTRUCTOR" << std::endl;
    00059           //std::cout << "the contructor things whichBlock = " << m_whichBlock<< std::endl;
    00060           m_osinstanceMaster = NULL;
    00061           m_osrouteSolver = NULL;
    00062           m_osoption = osoption;
    00063           m_osrouteSolver = new OSRouteSolver( m_osoption);
    00064           getOptions( m_osoption);
    00065
    00066
    00067 } //end OSColGenApp Constructor
    00068
    00069
    00070 OSColGenApp::~OSColGenApp(){
    00071
    00072         std::cout << "INSIDE ~OSColGenApp DESTRUCTOR" << std::endl;
    00073
    00074         if( m_osinstanceMaster != NULL) delete  m_osinstanceMaster;
    00075
    00076         if( m_osrouteSolver != NULL) delete  m_osrouteSolver;
    00077
    00078 }//end ~OSColGenApp() destructor
    00079
    00080
    00081 void OSColGenApp::getInitialRestrictedMaster( ){
    00082
    00083         m_osinstanceMaster = m_osrouteSolver->getInitialRestrictedMaster( );
    00084         //std::cout << m_osinstanceMaster->printModel(  ) << std::endl;
    00085
    00086 }//end generateInitialRestrictedMaster
    00087
    00088
    00089 void OSColGenApp::getCuts(const  double* x) {
    00090
    00091
    00092 }//end generateCuts
    00093
    00094 void OSColGenApp::getColumns(const  double* y, const int numRows,
    00095                 int &numColumns, int* numNonz, double* cost, double* rcost,
    00096                 int** rowIdx, double** values, double &lowerBound)
    00097  {
    00098
    00099
    00100         m_osrouteSolver->getColumns(y, numRows,
    00101                         numColumns, numNonz, cost, rcost,
    00102                         rowIdx, values,  lowerBound);
    00103
    00104
    00105 }//end generateColumns
    00106
    00107 void OSColGenApp::getOptions(OSOption *osoption) {
    00108
    00109
    00110         //get any options relevant to OSColGenApp
    00111
    00112         try{
    00113
    00114
    00115         } catch (const ErrorClass& eclass) {
    00116
    00117                 throw ErrorClass(eclass.errormsg);
    00118
    00119         }
    00120
    00121 }//end getOptions
    00122
    00123
    00124 void OSColGenApp::solveRestrictedMasterRelaxation(){
    00125
    00126         int i;
    00127         int j;
    00128         int k;
    00129         int numRows;
    00130         // y holds the dual values
    00131         double *y = NULL;
    00132         int numColumns;
    00133         int* numNonz = NULL;
    00134         double* cost = NULL;
    00135         double* rcost;
    00136         int** rowIdx = NULL;
    00137         double** values = NULL ;
    00138         double lowerBound;
    00139         double collb;
    00140         double colub;
    00141         collb = 0.0;
    00142         colub = 1.0;
    00143
    00144
    00145         try{
    00146                 numRows = m_osinstanceMaster->getConstraintNumber();
    00147
    00148                 CoinSolver *solver = NULL;
    00149                 solver = new CoinSolver();
    00150                 //kipp -- later have clp be an option
    00151                 //I guess for now it must be an Osi solver
    00152                 solver->sSolverName ="clp";
    00153                 std::cout << m_osinstanceMaster->printModel(  ) << std::endl;
    00154                 solver->osinstance = m_osinstanceMaster;
    00155                 solver->osoption = m_osoption;
    00156                 std::cout << "CALL Solveee  " << std::endl;
    00157                 solver->solve();
    00158                 std::cout << "Solution Status =  " << solver->osresult->getSolutionStatusType( 0 ) << std::endl;
    00159                 //std::cout <<  solver->osrl << std::endl;
    00160
    00161                 //get the solver interface
    00162                 OsiSolverInterface *si = solver->osiSolver;
    00163                 //get the dual solution
    00164
    00165
    00166
    00167                 if(si->getNumRows() != numRows )
    00168                         throw ErrorClass("detect a row number inconsistency in solveRestrictedMasterRelaxation");
    00169
    00170                 if( numRows > 0 ) y = new double[m_osinstanceMaster->getConstraintNumber() ];
    00171
    00172                 if(si->getRowPrice() == NULL  )
    00173                         throw ErrorClass("problem getting dual values in solveRestrictedMasterRelaxation");
    00174
    00175
    00176                 for(i = 0; i <  numRows; i++){
    00177
    00178                         *(y + i) = si->getRowPrice()[ i];
    00179
    00180                 }
    00181                 lowerBound = -1;
    00182
    00183                 int loopKount = 0;
    00184                 while(lowerBound < -.01 && loopKount < 1000){
    00185                         loopKount++;
    00186
    00187                         //kipp here is where the while loop goes
    00188                         //start while loop
    00189                         getColumns(y, numRows, numColumns, numNonz,
    00190                                         cost, rcost, rowIdx, values,  lowerBound);
    00191
    00192                         std::cout << "LOWER BOUND = " <<  lowerBound   << std::endl;
    00193
    00194
    00195                         numNonz = m_osrouteSolver->m_nonzVec;
    00196                         cost =  m_osrouteSolver->m_costVec;
    00197                         rcost = m_osrouteSolver->m_optValHub;
    00198                         rowIdx = m_osrouteSolver->m_newColumnRowIdx;
    00199                         values = m_osrouteSolver->m_newColumnRowValue;
    00200
    00201
    00202                         /*
    00203                         double tmpVal;
    00204
    00205                         for(i = 0; i < numColumns; i++){
    00206
    00207                                 tmpVal = cost[ i];
    00208
    00209                                 //std::cout << "OBJ  COST = " << cost[ i] << std::endl;
    00210
    00211                                 for(j = 0; j < numNonz[i] ; j++){
    00212
    00213                                         tmpVal = tmpVal -  y[ rowIdx[i][j] ]*values[i][j];
    00214
    00215                                         //std::cout << "NODE INDEX  = "  << rowIdx[i][j] + numColumns << std::endl;
    00216                                         //std::cout << "row value = "  << values[i][j] << std::endl;
    00217
    00218                                 }
    00219
    00220
    00221
    00222
    00223                                 //if(rcost[ i] > tmpVal + .1 || rcost[ i] < tmpVal - .1) exit( 1);
    00224                                 std::cout << "Kipp REDUCED COST = " << rcost[ i] << std::endl;
    00225                                 std::cout << "Clp REDUCED COST = " << tmpVal << std::endl;
    00226
    00227
    00228                         }
    00229                         */
    00230
    00231
    00232                         //add columns
    00233
    00234                         for(k = 0; k < numColumns; k++){
    00235
    00236                                 si->addCol(numNonz[ k], rowIdx[k], values[k],
    00237                                                 collb, colub,   cost[ k]) ;
    00238
    00239
    00240
    00241                         }
    00242
    00243                         std::cout << std::endl  << std::endl << std::endl;
    00244                         std::cout << "CALL Solve  " << std::endl;
    00245
    00246                         solver->solve();
    00247                         std::cout << "Solution Status =  " << solver->osresult->getSolutionStatusType( 0 ) << std::endl;
    00248                         std::cout << "Number of solver interface columns =  " <<  si->getNumCols()  << std::endl;
    00249
    00250                         //std::cout << "Number of master columns =  " <<  m_osrouteSolver->m_numThetaVar  << std::endl;
    00251
    00252                         if(si->getNumCols() != m_osrouteSolver->m_numThetaVar - 1) throw ErrorClass("number variables in solver not consistent with master");
    00253
    00254                         //for(i = 0; i < si->getNumCols(); i++){
    00255
    00256                                 //std::cout << "REDUCED COST  =  " <<  si->getReducedCost()[ i]  << std::endl;
    00257
    00258
    00259
    00260                         //}
    00261
    00262
    00263                         for(i = 0; i <  numRows; i++){
    00264
    00265                                 *(y + i) = si->getRowPrice()[ i];
    00266
    00267                                 //std::cout << "DUAL VALUE " << *(y + i) << std::endl;
    00268                         }
    00269
    00270
    00271                 }//end while
    00272
    00273
    00274
    00275                 //get a primal solution
    00276
    00277                 int numCols = 0;
    00278
    00279                 double* theta = NULL;
    00280                 numCols = si->getNumCols();
    00281                 theta = new double[ numCols];
    00282
    00283                 for(i=0; i < numCols; i++){
    00284                         *(theta + i) = si->getColSolution()[i];
    00285                 }
    00286
    00287                 m_osrouteSolver->pauHana( theta);
    00288
    00289                 if(numRows > 0) delete[] y;
    00290                 y = NULL;
    00291
    00292                 if(numCols > 0) delete[] theta;
    00293                 theta = NULL;
    00294
    00295
    00296                 delete solver;
    00297
    00298
    00299
    00300
    00301         } catch (const ErrorClass& eclass) {
    00302
    00303                 throw ErrorClass(eclass.errormsg);
    00304
    00305         }
    00306
    00307
    00308 }// end solveRelaxation
    00309
    00310
    00311