Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

OsiVolSolverInterfaceIO.cpp

00001 // Copyright (C) 2001, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 #if defined(_MSC_VER)
00005 // Turn off compiler warning about long names
00006 #  pragma warning(disable:4786)
00007 #endif
00008 
00009 #include <cmath>
00010 
00011 #include "CoinHelperFunctions.hpp"
00012 #include "CoinMpsIO.hpp"
00013 #include "OsiVolSolverInterface.hpp"
00014 
00015 //#############################################################################
00016 
00017 void
00018 OsiVolSolverInterface::initFromRlbRub(const int rownum,
00019                                       const double* rowlb,
00020                                       const double* rowub)
00021 {
00022    if (maxNumrows_ > 0) {
00023       rowRimAllocator_();
00024       if (rowub) {
00025          CoinDisjointCopyN(rowub, rownum, rowupper_);
00026       } else {
00027          CoinFillN(rowupper_, rownum, OsiVolInfinity);
00028       }
00029       if (rowlb) {
00030          CoinDisjointCopyN(rowlb, rownum, rowlower_);
00031       } else {
00032          CoinFillN(rowlower_, rownum, -OsiVolInfinity);
00033       }
00034       // Set the initial dual solution
00035       CoinFillN(rowprice_, rownum, 0.0);
00036       convertBoundsToSenses_();
00037    }
00038 }
00039 
00040 //#############################################################################
00041 
00042 void
00043 OsiVolSolverInterface::initFromRhsSenseRange(const int rownum,
00044                                              const char* rowsen,
00045                                              const double* rowrhs,   
00046                                              const double* rowrng)
00047 {
00048    if (maxNumrows_ > 0) {
00049       rowRimAllocator_();
00050       if (rowsen) {
00051          CoinDisjointCopyN(rowsen, rownum, rowsense_);
00052       } else {
00053          CoinFillN(rowsense_, rownum, 'G');
00054       }
00055       if (rowrhs) {
00056          CoinDisjointCopyN(rowrhs, rownum, rhs_);
00057       } else {
00058          CoinFillN(rhs_, rownum, 0.0);
00059       }
00060       if (rowrng) {
00061          CoinDisjointCopyN(rowrng, rownum, rowrange_);
00062       } else {
00063          CoinFillN(rowrange_, rownum, 0.0);
00064       }
00065       // Set the initial dual solution
00066       CoinFillN(rowprice_, rownum, 0.0);
00067       convertSensesToBounds_();
00068    }
00069 }
00070 //#############################################################################
00071 
00072 void
00073 OsiVolSolverInterface::initFromClbCubObj(const int colnum,
00074                                          const double* collb,
00075                                          const double* colub,   
00076                                          const double* obj)
00077 {
00078   if (maxNumcols_ > 0) {
00079     colRimAllocator_();
00080     if (colub) {
00081       CoinDisjointCopyN(colub, colnum, colupper_);
00082     } else {
00083       CoinFillN(colupper_, colnum, OsiVolInfinity);
00084     }
00085     if (collb) {
00086       CoinDisjointCopyN(collb, colnum, collower_);
00087     } else {
00088       CoinFillN(collower_, colnum, 0.0);
00089     }
00090     CoinFillN(continuous_,colnum,true);
00091     if (obj) {
00092       CoinDisjointCopyN(obj, colnum, objcoeffs_);
00093     } else {
00094       CoinFillN(objcoeffs_, colnum, 0.0);
00095     }
00096     int c;
00097     for ( c=0; c<colnum; c++ ) {
00098       if ( fabs(collower_[c]) < fabs(colupper_[c]) ) {
00099         colsol_[c] = collower_[c];
00100       }
00101       else {
00102         colsol_[c] = colupper_[c];
00103       }
00104     }
00105   }
00106 }
00107 
00108 //#############################################################################
00109 // Problem input methods
00110 //#############################################################################
00111 
00112 void
00113 OsiVolSolverInterface::loadProblem(const CoinPackedMatrix& matrix,
00114                                    const double* collb, const double* colub,   
00115                                    const double* obj,
00116                                    const double* rowlb, const double* rowub)
00117 {
00118    gutsOfDestructor_();
00119    const int rownum = matrix.getNumRows();
00120    const int colnum = matrix.getNumCols();
00121 
00122    if (matrix.isColOrdered()) {
00123       colMatrix_.setExtraGap(matrix.getExtraGap());
00124       colMatrix_.setExtraMajor(matrix.getExtraMajor());
00125       colMatrix_ = matrix;
00126       colMatrixCurrent_ = true;
00127       rowMatrixCurrent_ = false;
00128       maxNumcols_ = colMatrix_.getMaxMajorDim();
00129       maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) *
00130                                      colMatrix_.getMinorDim());
00131    } else {
00132       rowMatrix_.setExtraGap(matrix.getExtraGap());
00133       rowMatrix_.setExtraMajor(matrix.getExtraMajor());
00134       rowMatrix_ = matrix;
00135       rowMatrixCurrent_ = true;
00136       colMatrixCurrent_ = false;
00137       maxNumcols_ = static_cast<int>((1+rowMatrix_.getExtraGap()) *
00138                                      rowMatrix_.getMinorDim());
00139       maxNumrows_ = rowMatrix_.getMaxMajorDim();
00140    }
00141 
00142    initFromRlbRub(rownum, rowlb, rowub);
00143    initFromClbCubObj(colnum, collb, colub, obj);
00144 }
00145 
00146 //-----------------------------------------------------------------------
00147 
00148 void
00149 OsiVolSolverInterface::assignProblem(CoinPackedMatrix*& matrix,
00150                                      double*& collb, double*& colub,
00151                                      double*& obj,
00152                                      double*& rowlb, double*& rowub)
00153 {
00154    gutsOfDestructor_();
00155    const int rownum = matrix->getNumRows();
00156    const int colnum = matrix->getNumCols();
00157    maxNumcols_ = colnum;
00158    maxNumrows_ = rownum;
00159 
00160    if (matrix->isColOrdered()) {
00161       colMatrix_.swap(*matrix);
00162       colMatrixCurrent_ = true;
00163       rowMatrixCurrent_ = false;
00164    } else {
00165       rowMatrix_.swap(*matrix);
00166       rowMatrixCurrent_ = true;
00167       colMatrixCurrent_ = false;
00168    }
00169    delete matrix; matrix = 0;
00170       
00171    rowupper_  = rowub;     rowub  = 0;
00172    rowlower_  = rowlb;     rowlb  = 0;
00173    colupper_  = colub;     colub  = 0;
00174    collower_  = collb;     collb  = 0;
00175    objcoeffs_ = obj;       obj    = 0;
00176 
00177    if (maxNumrows_ > 0) {
00178       if (!rowupper_) {
00179          rowupper_ = new double[maxNumrows_];
00180          CoinFillN(rowupper_, rownum, OsiVolInfinity);
00181       }
00182       if (!rowlower_) {
00183          rowlower_ = new double[maxNumrows_];
00184          CoinFillN(rowlower_, rownum, -OsiVolInfinity);
00185       }
00186       rowsense_ = new char[maxNumrows_];
00187       rhs_      = new double[maxNumrows_];
00188       rowrange_ = new double[maxNumrows_];
00189       rowprice_ = new double[maxNumrows_];
00190       lhs_      = new double[maxNumrows_];
00191       // Set the initial dual solution
00192       CoinFillN(rowprice_, rownum, 0.0);
00193       convertBoundsToSenses_();
00194    }
00195    if (maxNumcols_ > 0) {
00196       if (!colupper_) {
00197          colupper_ = new double[maxNumcols_];
00198          CoinFillN(colupper_, colnum, OsiVolInfinity);
00199       }
00200       if (!collower_) {
00201          collower_ = new double[maxNumcols_];
00202          CoinFillN(collower_, colnum, -OsiVolInfinity);
00203       }
00204       if (!objcoeffs_) {
00205          objcoeffs_ = new double[maxNumcols_];
00206          CoinFillN(objcoeffs_, colnum, -OsiVolInfinity);
00207       }
00208 
00209     colsol_    = new double[maxNumcols_];
00210     int c;
00211     for ( c=0; c<colnum; c++ ) {
00212       if ( fabs(collower_[c]) < fabs(colupper_[c]) ) {
00213         colsol_[c] = collower_[c];
00214       }
00215       else {
00216         colsol_[c] = colupper_[c];
00217       }
00218     }
00219 
00220       rc_        = new double[maxNumcols_];
00221    }
00222 }
00223 
00224 //-----------------------------------------------------------------------
00225 
00226 void
00227 OsiVolSolverInterface::loadProblem(const CoinPackedMatrix& matrix,
00228                                    const double* collb, const double* colub,
00229                                    const double* obj,
00230                                    const char* rowsen, const double* rowrhs,   
00231                                    const double* rowrng)
00232 {
00233    gutsOfDestructor_();
00234    const int rownum = matrix.getNumRows();
00235    const int colnum = matrix.getNumCols();
00236 
00237    if (matrix.isColOrdered()) {
00238       colMatrix_ = matrix;
00239       colMatrixCurrent_ = true;
00240       rowMatrixCurrent_ = false;
00241       maxNumcols_ = colMatrix_.getMaxMajorDim();
00242       maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) *
00243                                      colMatrix_.getMinorDim());
00244    } else {
00245       rowMatrix_ = matrix;
00246       rowMatrixCurrent_ = true;
00247       colMatrixCurrent_ = false;
00248       maxNumcols_ = static_cast<int>((1+rowMatrix_.getExtraGap()) *
00249                                      rowMatrix_.getMinorDim());
00250       maxNumrows_ = rowMatrix_.getMaxMajorDim();
00251    }
00252 
00253    initFromRhsSenseRange(rownum, rowsen, rowrhs, rowrng);
00254    initFromClbCubObj(colnum, collb, colub, obj);
00255 }
00256 
00257 //-----------------------------------------------------------------------
00258 
00259 void
00260 OsiVolSolverInterface::assignProblem(CoinPackedMatrix*& matrix,
00261                                      double*& collb, double*& colub,
00262                                      double*& obj,
00263                                      char*& rowsen, double*& rowrhs,
00264                                      double*& rowrng)
00265 {
00266    gutsOfDestructor_();
00267    const int rownum = matrix->getNumRows();
00268    const int colnum = matrix->getNumCols();
00269    maxNumcols_ = colnum;
00270    maxNumrows_ = rownum;
00271 
00272    if (matrix->isColOrdered()) {
00273       colMatrix_.swap(*matrix);
00274       colMatrixCurrent_ = true;
00275       rowMatrixCurrent_ = false;
00276    } else {
00277       rowMatrix_.swap(*matrix);
00278       rowMatrixCurrent_ = true;
00279       colMatrixCurrent_ = false;
00280    }
00281    delete matrix; matrix = 0;
00282       
00283    rowsense_  = rowsen;   rowsen = 0;
00284    rhs_       = rowrhs;   rowrhs = 0;
00285    rowrange_  = rowrng;   rowrng = 0;
00286    colupper_  = colub;    colub  = 0;
00287    collower_  = collb;    collb  = 0;
00288    objcoeffs_ = obj;      obj    = 0;
00289 
00290    if (maxNumrows_ > 0) {
00291       if (!rowsense_) {
00292          rowsense_ = new char[maxNumrows_];
00293          CoinFillN(rowsense_, rownum, 'G');
00294       }
00295       if (!rhs_) {
00296          rhs_ = new double[maxNumrows_];
00297          CoinFillN(rhs_, rownum, 0.0);
00298       }
00299       if (!rowrange_) {
00300          rowrange_ = new double[maxNumrows_];
00301          CoinFillN(rowrange_, rownum, 0.0);
00302       }
00303       rowlower_ = new double[maxNumrows_];
00304       rowupper_ = new double[maxNumrows_];
00305       rowprice_ = new double[maxNumrows_];
00306       lhs_      = new double[maxNumrows_];
00307       // Set the initial dual solution
00308       CoinFillN(rowprice_, rownum, 0.0);
00309       convertSensesToBounds_();
00310    }
00311    if (maxNumcols_ > 0) {
00312       if (!colupper_) {
00313          colupper_ = new double[maxNumcols_];
00314          CoinFillN(colupper_, colnum, OsiVolInfinity);
00315       }
00316       if (!collower_) {
00317          collower_ = new double[maxNumcols_];
00318          CoinFillN(collower_, colnum, -OsiVolInfinity);
00319       }
00320       if (!objcoeffs_) {
00321          objcoeffs_ = new double[maxNumcols_];
00322          CoinFillN(objcoeffs_, colnum, -OsiVolInfinity);
00323       }
00324 
00325     colsol_    = new double[maxNumcols_];
00326     int c;
00327     for ( c=0; c<colnum; c++ ) {
00328       if ( fabs(collower_[c]) < fabs(colupper_[c]) ) {
00329         colsol_[c] = collower_[c];
00330       }
00331       else {
00332         colsol_[c] = colupper_[c];
00333       }
00334     }
00335 
00336       rc_        = new double[maxNumcols_];
00337    }
00338 }
00339 
00340 //-----------------------------------------------------------------------
00341 
00342 void
00343 OsiVolSolverInterface::loadProblem(const int numcols, const int numrows,
00344                                    const int* start, const int* index,
00345                                    const double* value,
00346                                    const double* collb, const double* colub,
00347                                    const double* obj,
00348                                    const double* rowlb, const double* rowub)
00349 {
00350    gutsOfDestructor_();
00351 
00352    colMatrix_.copyOf(true, numrows, numcols, start[numcols],
00353                      value, index, start, 0);
00354    colMatrixCurrent_ = true;
00355    rowMatrixCurrent_ = false;
00356    maxNumcols_ = colMatrix_.getMaxMajorDim();
00357    maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) *
00358                                   colMatrix_.getMinorDim());
00359 
00360    initFromRlbRub(numrows, rowlb, rowub);
00361    initFromClbCubObj(numcols, collb, colub, obj);
00362 }
00363 
00364 //-----------------------------------------------------------------------
00365 
00366 void
00367 OsiVolSolverInterface::loadProblem(const int numcols, const int numrows,
00368                                    const int* start, const int* index,
00369                                    const double* value,
00370                                    const double* collb, const double* colub,
00371                                    const double* obj,
00372                                    const char* rowsen, const double* rowrhs,   
00373                                    const double* rowrng)
00374 {
00375    gutsOfDestructor_();
00376 
00377    colMatrix_.copyOf(true, numrows, numcols, start[numcols],
00378                      value, index, start, 0);
00379    colMatrixCurrent_ = true;
00380    rowMatrixCurrent_ = false;
00381    maxNumcols_ = colMatrix_.getMaxMajorDim();
00382    maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) *
00383                                   colMatrix_.getMinorDim());
00384 
00385    initFromRhsSenseRange(numrows, rowsen, rowrhs, rowrng);
00386    initFromClbCubObj(numcols, collb, colub, obj);
00387 }
00388 
00389 //-----------------------------------------------------------------------
00390 
00391 
00392 int 
00393 OsiVolSolverInterface::readMps(const char *filename, const char *extension)
00394 {
00395    CoinMpsIO reader;
00396    reader.setInfinity(getInfinity());
00397    int retVal = reader.readMps(filename, extension);
00398    loadProblem(*reader.getMatrixByCol(),
00399                reader.getColLower(), reader.getColUpper(),
00400                reader.getObjCoefficients(),
00401                reader.getRowLower(), reader.getRowUpper());
00402    int nc = getNumCols();
00403    continuous_= new bool[maxNumcols_];
00404    CoinFillN(continuous_, nc, true);
00405    return retVal;
00406 }
00407 
00408 
00409 //-----------------------------------------------------------------------
00410 
00411 void 
00412 OsiVolSolverInterface::writeMps(const char *filename,
00413                                 const char *extension,
00414                                 double objSense) const
00415 {
00416    CoinMpsIO writer;
00417    writer.setMpsData(*getMatrixByCol(), getInfinity(),
00418                      getColLower(), getColUpper(),
00419                      getObjCoefficients(), (const char*) 0 /*integrality*/,
00420                      getRowLower(), getRowUpper(),
00421                      (const char**) 0 /*colnam*/, (const char**) 0 /*rownam*/);
00422    std::string fname = filename;
00423    fname += extension;
00424    writer.writeMps(fname.c_str());
00425 }

Generated on Wed Dec 3 14:35:35 2003 for Osi by doxygen 1.3.5