/home/coin/SVN-release/OS-2.4.2/Bcp/src/Member/BCP_matrix.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #include <numeric>
00004 #include <algorithm>
00005 #include <functional>
00006 
00007 using std::sort;
00008 using std::copy;
00009 using std::transform;
00010 using std::bind2nd;
00011 using std::partial_sum;
00012 using std::rotate;
00013 using std::fill;
00014 
00015 #include "BCP_error.hpp"
00016 #include "BCP_matrix.hpp"
00017 
00018 //#############################################################################
00019 
00020 BCP_lp_relax::BCP_lp_relax(const BCP_lp_relax& mat) :
00021    CoinPackedMatrix(mat),
00022    _Objective(mat._Objective),
00023    _ColLowerBound(mat._ColLowerBound),
00024    _ColUpperBound(mat._ColUpperBound),
00025    _RowLowerBound(mat._RowLowerBound),
00026    _RowUpperBound(mat._RowUpperBound) {}
00027 
00028 //-----------------------------------------------------------------------------
00029 
00030 BCP_lp_relax&
00031 BCP_lp_relax::operator=(const BCP_lp_relax& mat)
00032 {
00033    CoinPackedMatrix::operator=(mat);
00034    _Objective = mat._Objective;
00035    _ColLowerBound = mat._ColLowerBound;
00036    _ColUpperBound = mat._ColUpperBound;
00037    _RowLowerBound = mat._RowLowerBound;
00038    _RowUpperBound = mat._RowUpperBound;
00039    return *this;
00040 }
00041 
00042 //-----------------------------------------------------------------------------
00043 
00044 void
00045 BCP_lp_relax::reserve(const int MaxColNum, const int MaxRowNum,
00046                       const int MaxNonzeros)
00047 {
00048    CoinPackedMatrix::reserve(isColOrdered() ? MaxColNum : MaxRowNum,
00049                             MaxNonzeros);
00050    _Objective.reserve(MaxColNum);
00051    _ColLowerBound.reserve(MaxColNum);
00052    _ColUpperBound.reserve(MaxColNum);
00053    _RowLowerBound.reserve(MaxRowNum);
00054    _RowUpperBound.reserve(MaxRowNum);
00055 }
00056 
00057 //-----------------------------------------------------------------------------
00058 
00059 void
00060 BCP_lp_relax::clear() {
00061    CoinPackedMatrix::clear();
00062    _ColLowerBound.clear();  _ColUpperBound.clear();  _Objective.clear();
00063    _RowLowerBound.clear();  _RowUpperBound.clear();
00064 }
00065 
00066 //-----------------------------------------------------------------------------
00067 
00068 void
00069 BCP_lp_relax::copyOf(const CoinPackedMatrix& m,
00070                      const double* OBJ, const double* CLB, const double* CUB,
00071                      const double* RLB, const double* RUB)
00072 {
00073    clear();
00074    const int colnum = m.getNumCols();
00075    const int rownum = m.getNumRows();
00076    _ColLowerBound.insert(_ColLowerBound.end(), CLB, CLB+colnum);
00077    _ColUpperBound.insert(_ColUpperBound.end(), CUB, CUB+colnum);
00078    _Objective.insert(_Objective.end(), OBJ, OBJ+colnum);
00079    _RowLowerBound.insert(_RowLowerBound.end(), RLB, RLB+rownum);
00080    _RowUpperBound.insert(_RowUpperBound.end(), RUB, RUB+rownum);
00081 
00082    CoinPackedMatrix::copyOf(m);
00083 }
00084 
00085 //-----------------------------------------------------------------------------
00086 
00087 void
00088 BCP_lp_relax::assign(CoinPackedMatrix& m,
00089                      double*& OBJ, double*& CLB, double*& CUB,
00090                      double*& RLB, double*& RUB)
00091 {
00092    clear();
00093    const int colnum = m.getNumCols();
00094    const int rownum = m.getNumRows();
00095    _ColLowerBound.insert(_ColLowerBound.end(), CLB, CLB+colnum);
00096    delete[] CLB;
00097    CLB = 0;
00098    _ColUpperBound.insert(_ColUpperBound.end(), CUB, CUB+colnum);
00099    delete[] CUB;
00100    CUB = 0;
00101    _Objective.insert(_Objective.end(), OBJ, OBJ+colnum);
00102    delete[] OBJ;
00103    OBJ = 0;
00104    _RowLowerBound.insert(_RowLowerBound.end(), RLB, RLB+rownum);
00105    delete[] RLB;
00106    RLB = 0;
00107    _RowUpperBound.insert(_RowUpperBound.end(), RUB, RUB+rownum);
00108    delete[] RUB;
00109    RUB = 0;
00110    
00111    CoinPackedMatrix::gutsOfDestructor();
00112    CoinPackedMatrix::swap(m);
00113 }
00114 
00115 //-----------------------------------------------------------------------------
00116 #if 0
00117 void 
00118 BCP_lp_relax::add_col_set(const BCP_col_set& Cols)
00119 {
00120    rightAppendPackedMatrix(Cols);
00121    _Objective.append(Cols.Objective());
00122    _ColLowerBound.append(Cols.LowerBound());
00123    _ColUpperBound.append(Cols.UpperBound());
00124 }
00125 
00126 //-----------------------------------------------------------------------------
00127 
00128 void 
00129 BCP_lp_relax::add_row_set(const BCP_row_set& Rows)
00130 {
00131    bottomAppendPackedMatrix(Rows);
00132    _RowLowerBound.append(Rows.LowerBound());
00133    _RowUpperBound.append(Rows.UpperBound());
00134 }
00135 #endif
00136 //-----------------------------------------------------------------------------
00137 
00138 void 
00139 BCP_lp_relax::erase_col_set(const BCP_vec<int>& pos)
00140 {
00141    deleteCols(pos.size(), pos.begin());
00142    _Objective.erase_by_index(pos);
00143    _ColLowerBound.erase_by_index(pos);
00144    _ColUpperBound.erase_by_index(pos);
00145 }
00146 
00147 //-----------------------------------------------------------------------------
00148 
00149 void 
00150 BCP_lp_relax::erase_row_set(const BCP_vec<int>& pos)
00151 {
00152    deleteRows(pos.size(), pos.begin());
00153    _RowLowerBound.erase_by_index(pos);
00154    _RowUpperBound.erase_by_index(pos);
00155 }
00156 
00157 //-----------------------------------------------------------------------------
00158 #if 0
00159 double 
00160 BCP_lp_relax::dot_product_col(const int index,
00161                               const BCP_vec<double>& col) const
00162 {
00163    if (! isColOrdered())
00164       throw BCP_fatal_error("\
00165 LP: dot_product_col() called for a row ordered matrix...\n");
00166    return dot_product(index, col);
00167 }
00168 
00169 double 
00170 BCP_lp_relax::dot_product_col(const int index,
00171                               const double* col) const
00172 {
00173    if (_Ordering != ColWise)
00174       throw BCP_fatal_error("\
00175 LP: dot_product_col() called for a row ordered matrix...\n");
00176    return dot_product(index, col);
00177 }
00178 
00179 //-----------------------------------------------------------------------------
00180 
00181 double 
00182 BCP_lp_relax::dot_product_row(const int index,
00183                             const BCP_vec<double>& row) const
00184 {
00185    if (_Ordering != RowWise)
00186       throw BCP_fatal_error("\
00187 LP: dot_product_col() called for a col ordered matrix...\n");
00188    return dot_product(index, row);
00189 }
00190 
00191 double 
00192 BCP_lp_relax::dot_product_row(const int index,
00193                             const double* row) const
00194 {
00195    if (_Ordering != RowWise)
00196       throw BCP_fatal_error("\
00197 LP: dot_product_col() called for a col ordered matrix...\n");
00198    return dot_product(index, row);
00199 }
00200 #endif
00201 
00202 //#############################################################################
00203 
00204 void
00205 BCP_lp_relax::BCP_createColumnOrderedMatrix(BCP_vec<BCP_row*>& rows,
00206                                             BCP_vec<double>& CLB,
00207                                             BCP_vec<double>& CUB,
00208                                             BCP_vec<double>& OBJ)
00209 {
00210   int i, nzcnt = 0;
00211   const int rownum = rows.size();
00212   
00213   for (i = 0; i < rownum; ++i)
00214     nzcnt += rows[i]->getNumElements();
00215   CoinPackedMatrix::clear();
00216   CoinPackedMatrix::reserve(rownum, nzcnt);
00217   CoinPackedMatrix::setDimensions(0, CLB.size());
00218   _RowLowerBound.reserve(rownum);
00219   _RowUpperBound.reserve(rownum);
00220   for (i = 0; i < rownum; ++i) {
00221     BCP_row* row = rows[i];
00222     CoinPackedMatrix::appendMajorVector(*row);
00223     _RowLowerBound.unchecked_push_back(row->LowerBound());
00224     _RowUpperBound.unchecked_push_back(row->UpperBound());
00225   }
00226   _ColLowerBound.swap(CLB);
00227   _ColUpperBound.swap(CUB);
00228   _Objective.swap(OBJ);
00229 }
00230 
00231 //-----------------------------------------------------------------------------
00232 
00233 BCP_lp_relax::BCP_lp_relax(BCP_vec<BCP_row*>& rows,
00234                            BCP_vec<double>& CLB, BCP_vec<double>& CUB,
00235                            BCP_vec<double>& OBJ) :
00236   CoinPackedMatrix(false /*rowordered*/, 0, 0, 0, NULL, NULL, NULL, NULL)
00237 {
00238   BCP_createColumnOrderedMatrix(rows, CLB, CUB, OBJ);
00239 }
00240 
00241 //-----------------------------------------------------------------------------
00242 
00243 BCP_lp_relax::BCP_lp_relax(BCP_vec<BCP_row*>& rows,
00244                            BCP_vec<double>& CLB, BCP_vec<double>& CUB,
00245                            BCP_vec<double>& OBJ,
00246                            double extra_gap, double extra_major) :
00247   CoinPackedMatrix(false /*rowordered*/, 0, 0, 0, NULL, NULL, NULL, NULL)
00248 {
00249   setExtraGap(extra_gap);
00250   setExtraMajor(extra_major);
00251   BCP_createColumnOrderedMatrix(rows, CLB, CUB, OBJ);
00252 }
00253 
00254 //#############################################################################
00255 
00256 void
00257 BCP_lp_relax::BCP_createRowOrderedMatrix(BCP_vec<BCP_col*>& cols,
00258                                          BCP_vec<double>& RLB,
00259                                          BCP_vec<double>& RUB)
00260 {
00261   int i, nzcnt = 0;
00262   const int colnum = cols.size();
00263   
00264   for (i = 0; i < colnum; ++i)
00265     nzcnt += cols[i]->getNumElements();
00266   CoinPackedMatrix::clear();
00267   CoinPackedMatrix::reserve(colnum, nzcnt);
00268   CoinPackedMatrix::setDimensions(RLB.size(), 0);
00269   _ColLowerBound.reserve(colnum);
00270   _ColUpperBound.reserve(colnum);
00271   _Objective.reserve(colnum);
00272   for (i = 0; i < colnum; ++i) {
00273     BCP_col* col = cols[i];
00274     CoinPackedMatrix::appendMajorVector(*col);
00275     _ColLowerBound.unchecked_push_back(col->LowerBound());
00276     _ColUpperBound.unchecked_push_back(col->UpperBound());
00277     _Objective.unchecked_push_back(col->Objective());
00278   }
00279   _RowLowerBound.swap(RLB);
00280   _RowUpperBound.swap(RUB);
00281 }
00282 
00283 //-----------------------------------------------------------------------------
00284 
00285 BCP_lp_relax::BCP_lp_relax(BCP_vec<BCP_col*>& cols,
00286                            BCP_vec<double>& RLB, BCP_vec<double>& RUB) :
00287   CoinPackedMatrix(true /*colordered*/, 0, 0, 0, NULL, NULL, NULL, NULL)
00288 {
00289   BCP_createRowOrderedMatrix(cols, RLB, RUB);
00290 }
00291 
00292 //-----------------------------------------------------------------------------
00293 
00294 BCP_lp_relax::BCP_lp_relax(BCP_vec<BCP_col*>& cols,
00295                            BCP_vec<double>& RLB, BCP_vec<double>& RUB,
00296                            double extra_gap, double extra_major) :
00297   CoinPackedMatrix(true /*colordered*/, 0, 0, 0, NULL, NULL, NULL, NULL)
00298 {
00299   setExtraGap(extra_gap);
00300   setExtraMajor(extra_major);
00301   BCP_createRowOrderedMatrix(cols, RLB, RUB);
00302 }
00303 
00304 //-----------------------------------------------------------------------------
00305 
00306 BCP_lp_relax::BCP_lp_relax(const bool colordered,
00307                            const BCP_vec<int>& VB, const BCP_vec<int>& EI,
00308                            const BCP_vec<double>& EV,
00309                            const BCP_vec<double>& OBJ,
00310                            const BCP_vec<double>& CLB,
00311                            const BCP_vec<double>& CUB,
00312                            const BCP_vec<double>& RLB,
00313                            const BCP_vec<double>& RUB) :
00314    CoinPackedMatrix(),
00315    _Objective(OBJ), _ColLowerBound(CLB), _ColUpperBound(CUB),
00316    _RowLowerBound(RLB), _RowUpperBound(RUB)
00317 {
00318    const int minor = colordered ? RLB.size() : CLB.size();
00319    const int major = colordered ? CLB.size() : RLB.size();
00320    CoinPackedMatrix::copyOf(colordered, minor, major, EI.size(),
00321                            EV.begin(), EI.begin(), VB.begin(), 0);
00322 }
00323 
00324 //-----------------------------------------------------------------------------
00325 
00326 BCP_lp_relax::BCP_lp_relax(const bool colordered,
00327                            const int rownum, const int colnum, const int nznum,
00328                            int*& VB, int*& EI, double*& EV,
00329                            double*& OBJ, double*& CLB, double*& CUB,
00330                            double*& RLB, double*& RUB) :
00331    CoinPackedMatrix(),
00332    _Objective(OBJ, OBJ+colnum),
00333    _ColLowerBound(CLB, CLB+colnum), _ColUpperBound(CUB, CUB+colnum),
00334    _RowLowerBound(RLB, RLB+rownum), _RowUpperBound(RUB, RUB+rownum)
00335 {
00336    const int minor = colordered ? rownum : colnum;
00337    const int major = colordered ? colnum : rownum;
00338    int * nullp = 0;
00339    CoinPackedMatrix::assignMatrix(colordered, minor, major, nznum,
00340                                  EV, EI, VB, nullp);
00341    delete[] OBJ; OBJ = 0;
00342    delete[] CLB; CLB = 0;
00343    delete[] CUB; CUB = 0;
00344    delete[] RLB; RLB = 0;
00345    delete[] RUB; RUB = 0;
00346 }

Generated on Wed Nov 30 03:03:49 2011 by  doxygen 1.4.7