source: trunk/Clp/src/ClpPackedMatrix.hpp @ 800

Last change on this file since 800 was 800, checked in by ladanyi, 14 years ago

finishing conversion to svn

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#ifndef ClpPackedMatrix_H
4#define ClpPackedMatrix_H
5
6
7#include "CoinPragma.hpp"
8
9#include "ClpMatrixBase.hpp"
10
11/** This implements CoinPackedMatrix as derived from ClpMatrixBase.
12
13    It adds a few methods that know about model as well as matrix
14
15    For details see CoinPackedMatrix */
16
17class ClpPackedMatrix2;
18class ClpPackedMatrix : public ClpMatrixBase {
19 
20public:
21  /**@name Useful methods */
22   //@{
23   /// Return a complete CoinPackedMatrix
24  virtual CoinPackedMatrix * getPackedMatrix() const { return matrix_;};
25    /** Whether the packed matrix is column major ordered or not. */
26    virtual bool isColOrdered() const { return matrix_->isColOrdered(); }
27   /** Number of entries in the packed matrix. */
28  virtual  CoinBigIndex getNumElements() const 
29  { return matrix_->getNumElements(); }
30   /** Number of columns. */
31   virtual int getNumCols() const { return matrix_->getNumCols(); }
32   /** Number of rows. */
33  virtual int getNumRows() const { return matrix_->getNumRows(); };
34
35   /** A vector containing the elements in the packed matrix. Note that there
36        might be gaps in this list, entries that do not belong to any
37        major-dimension vector. To get the actual elements one should look at
38        this vector together with vectorStarts and vectorLengths. */
39   virtual const double * getElements() const 
40  { return matrix_->getElements();};
41   /** A vector containing the minor indices of the elements in the packed
42        matrix. Note that there might be gaps in this list, entries that do not
43        belong to any major-dimension vector. To get the actual elements one
44        should look at this vector together with vectorStarts and
45        vectorLengths. */
46   virtual const int * getIndices() const 
47  { return matrix_->getIndices();};
48
49   virtual const CoinBigIndex * getVectorStarts() const 
50  { return matrix_->getVectorStarts();};
51   /** The lengths of the major-dimension vectors. */
52   virtual const int * getVectorLengths() const 
53  { return matrix_->getVectorLengths();} ;
54  /** The length of a single major-dimension vector. */
55  virtual int getVectorLength(int index) const 
56  { return matrix_->getVectorSize(index);};
57
58    /** Delete the columns whose indices are listed in <code>indDel</code>. */
59  virtual void deleteCols(const int numDel, const int * indDel);
60    /** Delete the rows whose indices are listed in <code>indDel</code>. */
61  virtual void deleteRows(const int numDel, const int * indDel);
62#ifndef CLP_NO_VECTOR
63  /// Append Columns
64  virtual void appendCols(int number, const CoinPackedVectorBase * const * columns);
65  /// Append Rows
66  virtual void appendRows(int number, const CoinPackedVectorBase * const * rows);
67#endif
68  /** Append a set of rows/columns to the end of the matrix. Returns number of errors
69      i.e. if any of the new rows/columns contain an index that's larger than the
70      number of columns-1/rows-1 (if numberOther>0) or duplicates
71      If 0 then rows, 1 if columns */
72  virtual int appendMatrix(int number, int type,
73                           const CoinBigIndex * starts, const int * index,
74                           const double * element, int numberOther=-1);
75  /** Replace the elements of a vector.  The indices remain the same.
76      This is only needed if scaling and a row copy is used.
77      At most the number specified will be replaced.
78      The index is between 0 and major dimension of matrix */
79  virtual void replaceVector(const int index,
80                       const int numReplace, const double * newElements)
81      {matrix_->replaceVector(index,numReplace,newElements);};
82  /** Modify one element of packed matrix.  An element may be added.
83      This works for either ordering If the new element is zero it will be
84      deleted unless keepZero true */
85  virtual void modifyCoefficient(int row, int column, double newElement,
86                           bool keepZero=false)
87       {matrix_->modifyCoefficient(row, column, newElement, keepZero);}
88  /** Returns a new matrix in reverse order without gaps */
89  virtual ClpMatrixBase * reverseOrderedCopy() const;
90  /// Returns number of elements in column part of basis
91  virtual CoinBigIndex countBasis(ClpSimplex * model,
92                                 const int * whichColumn, 
93                                 int numberRowBasic,
94                                  int & numberColumnBasic);
95  /// Fills in column part of basis
96  virtual void fillBasis(ClpSimplex * model,
97                                 const int * whichColumn, 
98                                 int & numberColumnBasic,
99                                 int * row, int * start,
100                                 int * rowCount, int * columnCount,
101                                 double * element);
102  /** Creates scales for column copy (rowCopy in model may be modified)
103      returns non-zero if no scaling done */
104  virtual int scale(ClpModel * model) const ;
105  /** Scales rowCopy if column copy scaled
106      Only called if scales already exist */
107  virtual void scaleRowCopy(ClpModel * model) const ;
108  /** Realy really scales column copy
109      Only called if scales already exist.
110      Up to user ro delete */
111  virtual ClpMatrixBase * scaledColumnCopy(ClpModel * model) const ;
112  /** Checks if all elements are in valid range.  Can just
113      return true if you are not paranoid.  For Clp I will
114      probably expect no zeros.  Code can modify matrix to get rid of
115      small elements.
116      check bits (can be turned off to save time) :
117      1 - check if matrix has gaps
118      2 - check if zero elements
119      4 - check and compress duplicates
120      8 - report on large and small
121  */
122  virtual bool allElementsInRange(ClpModel * model,
123                                  double smallest, double largest,
124                                  int check=15);
125  /** Returns largest and smallest elements of both signs.
126      Largest refers to largest absolute value.
127  */
128  virtual void rangeOfElements(double & smallestNegative, double & largestNegative,
129                       double & smallestPositive, double & largestPositive);
130
131  /** Unpacks a column into an CoinIndexedvector
132   */
133  virtual void unpack(const ClpSimplex * model,CoinIndexedVector * rowArray,
134                   int column) const ;
135  /** Unpacks a column into an CoinIndexedvector
136   ** in packed foramt
137      Note that model is NOT const.  Bounds and objective could
138      be modified if doing column generation (just for this variable) */
139  virtual void unpackPacked(ClpSimplex * model,
140                            CoinIndexedVector * rowArray,
141                            int column) const;
142  /** Adds multiple of a column into an CoinIndexedvector
143      You can use quickAdd to add to vector */
144  virtual void add(const ClpSimplex * model,CoinIndexedVector * rowArray,
145                   int column, double multiplier) const ;
146  /** Adds multiple of a column into an array */
147  virtual void add(const ClpSimplex * model,double * array,
148                   int column, double multiplier) const;
149   /// Allow any parts of a created CoinPackedMatrix to be deleted
150   virtual void releasePackedMatrix() const { };
151  /** Given positive integer weights for each row fills in sum of weights
152      for each column (and slack).
153      Returns weights vector
154  */
155  virtual CoinBigIndex * dubiousWeights(const ClpSimplex * model,int * inputWeights) const;
156  /// Says whether it can do partial pricing
157  virtual bool canDoPartialPricing() const;
158  /// Partial pricing
159  virtual void partialPricing(ClpSimplex * model, double start, double end,
160                      int & bestSequence, int & numberWanted);
161  /// makes sure active columns correct
162  virtual int refresh(ClpSimplex * model);
163  // Really scale matrix
164  virtual void reallyScale(const double * rowScale, const double * columnScale);
165  /** Set the dimensions of the matrix. In effect, append new empty
166      columns/rows to the matrix. A negative number for either dimension
167      means that that dimension doesn't change. Otherwise the new dimensions
168      MUST be at least as large as the current ones otherwise an exception
169      is thrown. */
170  virtual void setDimensions(int numrows, int numcols);
171   //@}
172
173  /**@name Matrix times vector methods */
174  //@{
175    /** Return <code>y + A * scalar *x</code> in <code>y</code>.
176        @pre <code>x</code> must be of size <code>numColumns()</code>
177        @pre <code>y</code> must be of size <code>numRows()</code> */
178  virtual void times(double scalar,
179                       const double * x, double * y) const;
180  /// And for scaling
181  virtual void times(double scalar,
182                     const double * x, double * y,
183                     const double * rowScale, 
184                     const double * columnScale) const;
185    /** Return <code>y + x * scalar * A</code> in <code>y</code>.
186        @pre <code>x</code> must be of size <code>numRows()</code>
187        @pre <code>y</code> must be of size <code>numColumns()</code> */
188    virtual void transposeTimes(double scalar,
189                                const double * x, double * y) const;
190  /// And for scaling
191    virtual void transposeTimes(double scalar,
192                                const double * x, double * y,
193                                const double * rowScale, 
194                                const double * columnScale,
195                                double * spare=NULL) const;
196    /** Return <code>x * scalar * A + y</code> in <code>z</code>.
197        Can use y as temporary array (will be empty at end)
198        Note - If x packed mode - then z packed mode
199        Squashes small elements and knows about ClpSimplex */
200  virtual void transposeTimes(const ClpSimplex * model, double scalar,
201                              const CoinIndexedVector * x,
202                              CoinIndexedVector * y,
203                              CoinIndexedVector * z) const;
204    /** Return <code>x * scalar * A + y</code> in <code>z</code>.
205        Note - If x packed mode - then z packed mode
206        This does by column and knows no gaps
207        Squashes small elements and knows about ClpSimplex */
208  void transposeTimesByColumn(const ClpSimplex * model, double scalar,
209                              const CoinIndexedVector * x,
210                              CoinIndexedVector * y,
211                              CoinIndexedVector * z) const;
212    /** Return <code>x * scalar * A + y</code> in <code>z</code>.
213        Can use y as temporary array (will be empty at end)
214        Note - If x packed mode - then z packed mode
215        Squashes small elements and knows about ClpSimplex.
216    This version uses row copy*/
217  virtual void transposeTimesByRow(const ClpSimplex * model, double scalar,
218                              const CoinIndexedVector * x,
219                              CoinIndexedVector * y,
220                              CoinIndexedVector * z) const;
221    /** Return <code>x *A</code> in <code>z</code> but
222        just for indices in y.
223        Note - z always packed mode */
224  virtual void subsetTransposeTimes(const ClpSimplex * model,
225                                    const CoinIndexedVector * x,
226                                    const CoinIndexedVector * y,
227                                    CoinIndexedVector * z) const;
228  /** Returns true if can combine transposeTimes and subsetTransposeTimes
229      and if it would be faster */
230  virtual bool canCombine(const ClpSimplex * model,
231                          const CoinIndexedVector * pi) const;
232  /// Updates two arrays for steepest
233  virtual void transposeTimes2(const ClpSimplex * model,
234                               const CoinIndexedVector * pi1, CoinIndexedVector * dj1,
235                               const CoinIndexedVector * pi2, CoinIndexedVector * dj2,
236                               CoinIndexedVector * spare,
237                               double referenceIn, double devex,
238                               // Array for exact devex to say what is in reference framework
239                               unsigned int * reference,
240                               double * weights, double scaleFactor);
241  /// Updates second array for steepest and does devex weights
242  virtual void subsetTimes2(const ClpSimplex * model,
243                                CoinIndexedVector * dj1,
244                               const CoinIndexedVector * pi2, CoinIndexedVector * dj2,
245                               double referenceIn, double devex,
246                               // Array for exact devex to say what is in reference framework
247                               unsigned int * reference,
248                               double * weights, double scaleFactor);
249  /// Sets up an effective RHS
250  void useEffectiveRhs(ClpSimplex * model);
251//@}
252
253  /**@name Other */
254   //@{
255  /// Returns CoinPackedMatrix (non const)
256  inline CoinPackedMatrix * matrix() const { return matrix_;};
257  /** Just sets matrix_ to NULL so it can be used elsewhere.
258      used in GUB
259  */
260  inline void setMatrixNull()
261  { matrix_=NULL;};
262   //@}
263
264
265  /**@name Constructors, destructor */
266   //@{
267   /** Default constructor. */
268   ClpPackedMatrix();
269   /** Destructor */
270   virtual ~ClpPackedMatrix();
271   //@}
272
273   /**@name Copy method */
274   //@{
275   /** The copy constructor. */
276   ClpPackedMatrix(const ClpPackedMatrix&);
277   /** The copy constructor from an CoinPackedMatrix. */
278   ClpPackedMatrix(const CoinPackedMatrix&);
279  /** Subset constructor (without gaps).  Duplicates are allowed
280      and order is as given */
281  ClpPackedMatrix (const ClpPackedMatrix & wholeModel,
282                    int numberRows, const int * whichRows,
283                    int numberColumns, const int * whichColumns);
284  ClpPackedMatrix (const CoinPackedMatrix & wholeModel,
285                    int numberRows, const int * whichRows,
286                    int numberColumns, const int * whichColumns);
287
288  /** This takes over ownership (for space reasons) */
289   ClpPackedMatrix(CoinPackedMatrix * matrix);
290
291   ClpPackedMatrix& operator=(const ClpPackedMatrix&);
292  /// Clone
293  virtual ClpMatrixBase * clone() const ;
294  /** Subset clone (without gaps).  Duplicates are allowed
295      and order is as given */
296  virtual ClpMatrixBase * subsetClone (
297                    int numberRows, const int * whichRows,
298                    int numberColumns, const int * whichColumns) const ;
299  /// make special row copy
300  void specialRowCopy(ClpSimplex * model,const ClpMatrixBase * rowCopy); 
301   //@}
302   
303   
304protected:
305   /**@name Data members
306      The data members are protected to allow access for derived classes. */
307   //@{
308  /// Data
309  CoinPackedMatrix * matrix_;
310  /// number of active columns (normally same as number of columns)
311  int numberActiveColumns_;
312  /// Zero element flag - set true if any zero elements
313  bool zeroElements_;
314  /// Gaps flag - set true if column start and length don't say contiguous
315  bool hasGaps_;
316  /// Special row copy
317  ClpPackedMatrix2 * rowCopy_;
318   //@}
319};
320#ifdef THREAD
321#include <pthread.h>
322typedef struct {
323  double acceptablePivot;
324  const ClpSimplex * model;
325  double * spare;
326  int * spareIndex;
327  double * arrayTemp;
328  int * indexTemp;
329  int * numberInPtr;
330  double * bestPossiblePtr;
331  double * upperThetaPtr;
332  int * posFreePtr;
333  double * freePivotPtr;
334  int * numberOutPtr;
335  const unsigned short * count;
336  const double * pi;
337  const CoinBigIndex * rowStart;
338  const double * element;
339  const unsigned short * column;
340  int offset;
341  int numberInRowArray;
342  int numberLook;
343} dualColumn0Struct;
344#endif
345class ClpPackedMatrix2 {
346 
347public:
348  /**@name Useful methods */
349  //@{
350    /** Return <code>x * -1 * A in <code>z</code>.
351        Note - x packed and z will be packed mode
352        Squashes small elements and knows about ClpSimplex */
353  void transposeTimes(const ClpSimplex * model,
354                      const CoinPackedMatrix * rowCopy,
355                      const CoinIndexedVector * x,
356                      CoinIndexedVector * spareArray,
357                      CoinIndexedVector * z) const;
358  /// Returns true if copy has useful information
359  inline bool usefulInfo() const
360  { return rowStart_!=NULL;};
361  //@}
362
363
364  /**@name Constructors, destructor */
365   //@{
366   /** Default constructor. */
367   ClpPackedMatrix2();
368   /** Constructor from copy. */
369   ClpPackedMatrix2(ClpSimplex * model,const CoinPackedMatrix * rowCopy);
370   /** Destructor */
371   virtual ~ClpPackedMatrix2();
372   //@}
373
374   /**@name Copy method */
375   //@{
376   /** The copy constructor. */
377   ClpPackedMatrix2(const ClpPackedMatrix2&);
378   ClpPackedMatrix2& operator=(const ClpPackedMatrix2&);
379   //@}
380   
381   
382protected:
383   /**@name Data members
384      The data members are protected to allow access for derived classes. */
385   //@{
386  /// Number of blocks
387  int numberBlocks_;
388  /// Number of rows
389  int numberRows_;
390  /// Column offset for each block (plus one at end)
391  int * offset_;
392  /// Counts of elements in each part of row
393  mutable unsigned short * count_;
394  /// Row starts
395  mutable CoinBigIndex * rowStart_;
396  /// columns within block
397  unsigned short * column_;
398  /// work arrays
399  double * work_;
400#ifdef THREAD
401  pthread_t * threadId_;
402  dualColumn0Struct * info_;
403#endif
404   //@}
405};
406
407#endif
Note: See TracBrowser for help on using the repository browser.