Ignore:
Timestamp:
Sep 21, 2007 4:13:41 PM (12 years ago)
Author:
forrest
Message:

trying to improve networks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Clp/src/ClpNetworkMatrix.cpp

    r1034 r1111  
    88#include "CoinIndexedVector.hpp"
    99#include "CoinHelperFunctions.hpp"
     10#include "CoinPackedVector.hpp"
    1011
    1112#include "ClpSimplex.hpp"
     
    1617#include "ClpMessage.hpp"
    1718#include <iostream>
     19#include <cassert>
    1820
    1921//#############################################################################
     
    2830{
    2931  setType(11);
    30   elements_ = NULL;
    31   starts_ = NULL;
     32  matrix_ = NULL;
    3233  lengths_=NULL;
    3334  indices_=NULL;
     
    4344{
    4445  setType(11);
    45   elements_ = NULL;
    46   starts_ = NULL;
     46  matrix_ = NULL;
    4747  lengths_=NULL;
    4848  indices_=new int[2*numberColumns];;
     
    6868: ClpMatrixBase(rhs)
    6969
    70   elements_ = NULL;
    71   starts_ = NULL;
     70  matrix_ = NULL;
    7271  lengths_=NULL;
    7372  indices_=NULL;
     
    9190
    9291  setType(11);
    93   elements_ = NULL;
    94   starts_ = NULL;
     92  matrix_ = NULL;
    9593  lengths_=NULL;
    9694  indices_=NULL;
     
    187185ClpNetworkMatrix::~ClpNetworkMatrix ()
    188186{
    189   delete [] elements_;
    190   delete [] starts_;
     187  delete matrix_;
    191188  delete [] lengths_;
    192189  delete [] indices_;
     
    201198  if (this != &rhs) {
    202199    ClpMatrixBase::operator=(rhs);
    203     delete [] elements_;
    204     delete [] starts_;
     200    delete matrix_;
    205201    delete [] lengths_;
    206202    delete [] indices_;
    207     elements_ = NULL;
    208     starts_ = NULL;
     203    matrix_ = NULL;
    209204    lengths_=NULL;
    210205    indices_=NULL;
     
    669664ClpNetworkMatrix::getPackedMatrix() const
    670665{
    671   return new CoinPackedMatrix(true,numberRows_,numberColumns_,
    672                               2*numberColumns_,
    673                               getElements(),indices_,
    674                               getVectorStarts(),getVectorLengths());
    675 
     666  if (!matrix_) {
     667    assert (trueNetwork_); // fix later
     668    int numberElements = 2*numberColumns_;
     669    double * elements = new double [numberElements];
     670    CoinBigIndex i;
     671    for (i=0;i<2*numberColumns_;i+=2) {
     672      elements[i]=-1.0;
     673      elements[i+1]=1.0;
     674    }
     675    CoinBigIndex * starts = new CoinBigIndex [numberColumns_+1];
     676    for (i=0;i<numberColumns_+1;i++) {
     677      starts[i]=2*i;
     678    }
     679    // use assignMatrix to save space
     680    delete [] lengths_;
     681    lengths_ = NULL;
     682    matrix_ = new CoinPackedMatrix();
     683    int * indices = CoinCopyOfArray(indices_,2*numberColumns_);
     684    matrix_->assignMatrix(true,numberRows_,numberColumns_,
     685                          getNumElements(),
     686                          elements,indices,
     687                          starts,lengths_);
     688    assert(!elements);
     689    assert(!starts);
     690    assert (!indices);
     691    assert (!lengths_);
     692  }
     693  return matrix_;
    676694}
    677695/* A vector containing the elements in the packed matrix. Note that there
     
    682700ClpNetworkMatrix::getElements() const
    683701{
    684   assert (trueNetwork_); // fix later
    685   if (!elements_) {
    686     elements_ = new double [2*numberColumns_];
    687     int i;
    688     for (i=0;i<2*numberColumns_;i+=2) {
    689       elements_[i]=-1.0;
    690       elements_[i+1]=1.0;
    691     }
    692   }
    693   return elements_;
     702  if (!matrix_)
     703    getPackedMatrix();
     704  return matrix_->getElements();
    694705}
    695706
     
    697708ClpNetworkMatrix::getVectorStarts() const
    698709{
    699   assert (trueNetwork_); // fix later
    700   if (!starts_) {
    701     starts_ = new CoinBigIndex [numberColumns_+1];
    702     int i;
    703     for (i=0;i<numberColumns_+1;i++) {
    704       starts_[i]=2*i;
    705     }
    706   }
    707   return starts_;
     710  if (!matrix_)
     711    getPackedMatrix();
     712  return matrix_->getVectorStarts();
    708713}
    709714/* The lengths of the major-dimension vectors. */
     
    724729void ClpNetworkMatrix::deleteCols(const int numDel, const int * indDel)
    725730{
    726   std::cerr<<"deleteCols not implemented in ClpNetworkMatrix"<<std::endl;
    727   abort();
     731  assert (trueNetwork_);
     732  int iColumn;
     733  int numberBad=0;
     734  // Use array to make sure we can have duplicates
     735  char * which = new char[numberColumns_];
     736  memset(which,0,numberColumns_);
     737  int nDuplicate=0;
     738  for (iColumn=0;iColumn<numDel;iColumn++) {
     739    int jColumn = indDel[iColumn];
     740    if (jColumn<0||jColumn>=numberColumns_) {
     741      numberBad++;
     742    } else {
     743      if (which[jColumn])
     744        nDuplicate++;
     745      else
     746        which[jColumn]=1;
     747    }
     748  }
     749  if (numberBad)
     750    throw CoinError("Indices out of range", "deleteCols", "ClpNetworkMatrix");
     751  int newNumber = numberColumns_-numDel+nDuplicate;
     752  // Get rid of temporary arrays
     753  delete [] lengths_;
     754  lengths_=NULL;
     755  delete matrix_;
     756  matrix_= NULL;
     757  int newSize=2*newNumber;
     758  int * newIndices = new int [newSize];
     759  newSize=0;
     760  for (iColumn=0;iColumn<numberColumns_;iColumn++) {
     761    if (!which[iColumn]) {
     762      CoinBigIndex start;
     763      CoinBigIndex i;
     764      start = 2*iColumn;
     765      for (i=start;i<start+2;i++)
     766        newIndices[newSize++]=indices_[i];
     767    }
     768  }
     769  delete [] which;
     770  delete [] indices_;
     771  indices_= newIndices;
     772  numberColumns_ = newNumber;
    728773}
    729774/* Delete the rows whose indices are listed in <code>indDel</code>. */
    730775void ClpNetworkMatrix::deleteRows(const int numDel, const int * indDel)
    731776{
    732   std::cerr<<"deleteRows not implemented in ClpNetworkMatrix"<<std::endl;
    733   abort();
     777  int iRow;
     778  int numberBad=0;
     779  // Use array to make sure we can have duplicates
     780  int * which = new int [numberRows_];
     781  memset(which,0,numberRows_*sizeof(int));
     782  for (iRow=0;iRow<numDel;iRow++) {
     783    int jRow = indDel[iRow];
     784    if (jRow<0||jRow>=numberRows_) {
     785      numberBad++;
     786    } else {
     787      which[jRow]=1;
     788    }
     789  }
     790  if (numberBad)
     791    throw CoinError("Indices out of range", "deleteRows", "ClpNetworkMatrix");
     792  // Only valid of all columns have 0 entries
     793  int iColumn;
     794  for (iColumn=0;iColumn<numberColumns_;iColumn++) {
     795    CoinBigIndex start;
     796    CoinBigIndex i;
     797    start = 2*iColumn;
     798    for (i=start;i<start+2;i++) {
     799      int iRow = indices_[i];
     800      if (which[iRow])
     801        numberBad++;
     802    }
     803  }
     804  if (numberBad)
     805    throw CoinError("Row has entries", "deleteRows", "ClpNetworkMatrix");
     806  int newNumber=0;
     807  for (iRow=0;iRow<numberRows_;iRow++) {
     808    if (!which[iRow])
     809      which[iRow]=newNumber++;
     810    else
     811      which[iRow]=-1;
     812  }
     813  for (iColumn=0;iColumn<numberColumns_;iColumn++) {
     814    CoinBigIndex start;
     815    CoinBigIndex i;
     816    start = 2*iColumn;
     817    for (i=start;i<start+2;i++) {
     818      int iRow = indices_[i];
     819      indices_[i]=which[iRow];
     820    }
     821  }
     822  delete [] which;
     823  numberRows_ = newNumber;
    734824}
    735825/* Given positive integer weights for each row fills in sum of weights
     
    10181108ClpNetworkMatrix::releasePackedMatrix() const
    10191109{
    1020   delete [] elements_;
     1110  delete matrix_;
    10211111  delete [] lengths_;
    1022   elements_=NULL;
     1112  matrix_=NULL;
    10231113  lengths_=NULL;
    10241114}
     1115// Append Columns
     1116void
     1117ClpNetworkMatrix::appendCols(int number, const CoinPackedVectorBase * const * columns)
     1118{
     1119  int iColumn;
     1120  int numberBad=0;
     1121  for (iColumn=0;iColumn<number;iColumn++) {
     1122    int n=columns[iColumn]->getNumElements();
     1123    const double * element = columns[iColumn]->getElements();
     1124    if (n!=2)
     1125      numberBad++;
     1126    if (fabs(element[0])!=1.0||fabs(element[1])!=1.0)
     1127        numberBad++;
     1128    else if (element[0]*element[1]!=-1.0)
     1129        numberBad++;
     1130  }
     1131  if (numberBad)
     1132    throw CoinError("Not network", "appendCols", "ClpNetworkMatrix");
     1133  // Get rid of temporary arrays
     1134  delete [] lengths_;
     1135  lengths_=NULL;
     1136  delete matrix_;
     1137  matrix_= NULL;
     1138  CoinBigIndex size = 2*number;
     1139  int * temp2 = new int [numberColumns_*2+size];
     1140  memcpy(temp2,indices_,numberColumns_*2*sizeof(int));
     1141  delete [] indices_;
     1142  indices_= temp2;
     1143  // now add
     1144  size=2*numberColumns_;
     1145  for (iColumn=0;iColumn<number;iColumn++) {
     1146    const int * row = columns[iColumn]->getIndices();
     1147    const double * element = columns[iColumn]->getElements();
     1148    if (element[0]==-1.0) {
     1149      indices_[size++]=row[0];
     1150      indices_[size++]=row[1];
     1151    } else {
     1152      indices_[size++]=row[1];
     1153      indices_[size++]=row[0];
     1154    }
     1155  }
     1156 
     1157  numberColumns_ += number;
     1158}
     1159// Append Rows
     1160void
     1161ClpNetworkMatrix::appendRows(int number, const CoinPackedVectorBase * const * rows)
     1162{
     1163  // must be zero arrays
     1164  int numberBad=0;
     1165  int iRow;
     1166  for (iRow=0;iRow<number;iRow++) {
     1167    numberBad += rows[iRow]->getNumElements();
     1168  }
     1169  if (numberBad)
     1170    throw CoinError("Not NULL rows", "appendRows", "ClpNetworkMatrix");
     1171  numberRows_ += number;
     1172}
     1173#ifndef SLIM_CLP
     1174/* Append a set of rows/columns to the end of the matrix. Returns number of errors
     1175   i.e. if any of the new rows/columns contain an index that's larger than the
     1176   number of columns-1/rows-1 (if numberOther>0) or duplicates
     1177   If 0 then rows, 1 if columns */
     1178int
     1179ClpNetworkMatrix::appendMatrix(int number, int type,
     1180                                    const CoinBigIndex * starts, const int * index,
     1181                                    const double * element, int numberOther)
     1182{
     1183  int numberErrors=0;
     1184  // make into CoinPackedVector
     1185  CoinPackedVectorBase ** vectors=
     1186    new CoinPackedVectorBase * [number];
     1187  int iVector;
     1188  for (iVector=0;iVector<number;iVector++) {
     1189    int iStart = starts[iVector];
     1190    vectors[iVector] =
     1191      new CoinPackedVector(starts[iVector+1]-iStart,
     1192                           index+iStart,element+iStart);
     1193  }
     1194  if (type==0) {
     1195    // rows
     1196    appendRows(number,vectors);
     1197  } else {
     1198    // columns
     1199    appendCols(number,vectors);
     1200  }
     1201  for (iVector=0;iVector<number;iVector++)
     1202    delete vectors[iVector];
     1203  delete [] vectors;
     1204  return numberErrors;
     1205}
     1206#endif
     1207/* Subset clone (without gaps).  Duplicates are allowed
     1208   and order is as given */
     1209ClpMatrixBase *
     1210ClpNetworkMatrix::subsetClone (int numberRows, const int * whichRows,
     1211                              int numberColumns,
     1212                              const int * whichColumns) const
     1213{
     1214  return new ClpNetworkMatrix(*this, numberRows, whichRows,
     1215                                   numberColumns, whichColumns);
     1216}
     1217/* Subset constructor (without gaps).  Duplicates are allowed
     1218   and order is as given */
     1219ClpNetworkMatrix::ClpNetworkMatrix (
     1220                       const ClpNetworkMatrix & rhs,
     1221                       int numberRows, const int * whichRow,
     1222                       int numberColumns, const int * whichColumn)
     1223: ClpMatrixBase(rhs)
     1224
     1225  setType(11);
     1226  matrix_ = NULL;
     1227  lengths_=NULL;
     1228  indices_=new int[2*numberColumns];;
     1229  numberRows_=numberRows;
     1230  numberColumns_=numberColumns;
     1231  trueNetwork_=true;
     1232  int iColumn;
     1233  int numberBad=0;
     1234  int * which = new int [rhs.numberRows_];
     1235  int iRow;
     1236  for (iRow=0;iRow<rhs.numberRows_;iRow++)
     1237    which[iRow]=-1;
     1238  int n=0;
     1239  for (iRow=0;iRow<numberRows;iRow++) {
     1240    int jRow=whichRow[iRow];
     1241    assert (jRow>=0&&jRow<rhs.numberRows_);
     1242    which[jRow]=n++;
     1243  }
     1244  for (iColumn=0;iColumn<numberColumns;iColumn++) {
     1245    CoinBigIndex start;
     1246    CoinBigIndex i;
     1247    start = 2*iColumn;
     1248    CoinBigIndex offset = 2*whichColumn[iColumn]-start;
     1249    for (i=start;i<start+2;i++) {
     1250      int iRow = rhs.indices_[i+offset];
     1251      iRow = which[iRow];
     1252      if (iRow<0)
     1253        numberBad++;
     1254      else
     1255        indices_[i]=iRow;
     1256    }
     1257  }
     1258  if (numberBad)
     1259    throw CoinError("Invalid rows", "subsetConstructor", "ClpNetworkMatrix");
     1260}
Note: See TracChangeset for help on using the changeset viewer.