source: branches/devel-1/include/ClpModel.hpp @ 33

Last change on this file since 33 was 33, checked in by forrest, 17 years ago

Presolve in as option

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#ifndef ClpModel_H
4#define ClpModel_H
5
6#include <iostream>
7#include <cassert>
8#include <cmath>
9#include <vector>
10#include <string>
11
12#include "ClpMatrixBase.hpp"
13#include "CoinMessageHandler.hpp"
14#include "ClpParameters.hpp"
15
16#define CLP_INFINITY 1e30
17
18/** This is the base class for Linear Models
19    This knows nothing about the algorithm, but it seems to
20    have a reasonable amount of information
21
22    I would welcome suggestions for what should be in this and
23    how it relates to OsiSolverInterface.  Some methods look
24    very similar.
25
26*/
27
28class ClpModel {
29
30public:
31
32  /**@name Constructors and destructor
33     Note - copy methods copy ALL data so can chew up memory
34     until other copy is freed
35   */
36  //@{
37    /// Default constructor
38    ClpModel (  );
39
40    /// Copy constructor.
41    ClpModel(const ClpModel &);
42    /// Assignment operator. This copies the data
43    ClpModel & operator=(const ClpModel & rhs);
44    /// Destructor
45    ~ClpModel (  );
46  //@}
47
48  /**@name Load model - loads some stuff and initializes others */
49  //@{
50    /** Loads a problem (the constraints on the
51        rows are given by lower and upper bounds). If a pointer is 0 then the
52        following values are the default:
53        <ul>
54          <li> <code>colub</code>: all columns have upper bound infinity
55          <li> <code>collb</code>: all columns have lower bound 0
56          <li> <code>rowub</code>: all rows have upper bound infinity
57          <li> <code>rowlb</code>: all rows have lower bound -infinity
58          <li> <code>obj</code>: all variables have 0 objective coefficient
59        </ul>
60    */
61  void loadProblem (  const ClpMatrixBase& matrix,
62                     const double* collb, const double* colub,   
63                     const double* obj,
64                     const double* rowlb, const double* rowub,
65                      const double * rowObjective=NULL);
66  void loadProblem (  const CoinPackedMatrix& matrix,
67                     const double* collb, const double* colub,   
68                     const double* obj,
69                     const double* rowlb, const double* rowub,
70                      const double * rowObjective=NULL);
71
72  /** Just like the other loadProblem() method except that the matrix is
73        given in a standard column major ordered format (without gaps). */
74  void loadProblem (  const int numcols, const int numrows,
75                     const int* start, const int* index,
76                     const double* value,
77                     const double* collb, const double* colub,   
78                     const double* obj,
79                      const double* rowlb, const double* rowub,
80                      const double * rowObjective=NULL);
81  /// This one is for after presolve to save memory
82  void loadProblem (  const int numcols, const int numrows,
83                     const int* start, const int* index,
84                      const double* value,const int * length,
85                     const double* collb, const double* colub,   
86                     const double* obj,
87                      const double* rowlb, const double* rowub,
88                      const double * rowObjective=NULL);
89  /// Read an mps file from the given filename
90  int readMps(const char *filename,
91              bool keepNames=false,
92              bool ignoreErrors = false);
93  /// Copy in integer informations
94  void copyInIntegerInformation(const char * information);
95  /// Drop integer informations
96  void deleteIntegerInformation();
97  /// Resizes rim part of model
98  void resize (int newNumberRows, int newNumberColumns);
99  /// Deletes rows
100  void deleteRows(int number, const int * which);
101  /// Deletes columns
102 
103  void deleteColumns(int number, const int * which);
104  /** Borrow model.  This is so we dont have to copy large amounts
105      of data around.  It assumes a derived class wants to overwrite
106      an empty model with a real one - while it does an algorithm */
107  void borrowModel(ClpModel & otherModel);
108  /** Return model - nulls all arrays so can be deleted safely
109      also updates any scalars */
110  void returnModel(ClpModel & otherModel);
111
112  /// Drops names - makes lengthnames 0 and names empty
113  void dropNames();
114 
115  //@}
116  /**@name gets and sets */
117  //@{
118   /// Number of rows
119   inline int numberRows() const {
120      return numberRows_;
121   }
122   inline int getNumRows() const {
123      return numberRows_;
124   }
125   /// Number of columns
126   inline int getNumCols() const {
127      return numberColumns_;
128   }
129   inline int numberColumns() const {
130      return numberColumns_;
131   }
132   /// Primal tolerance to use
133   inline double primalTolerance() const {
134      return dblParam_[ClpPrimalTolerance];
135   }
136   void setPrimalTolerance( double value) ;
137   /// Dual tolerance to use
138   inline double dualTolerance() const  { return dblParam_[ClpDualTolerance]; }
139   void setDualTolerance( double value) ;
140   /// Number of iterations
141   inline int numberIterations() const  { return numberIterations_; }
142   inline int getIterationCount() const { return numberIterations_; }
143   /// Maximum number of iterations
144   inline int maximumIterations() const { return maximumIterations_; }
145   void setMaximumIterations(int value);
146   /** Status of problem:
147       0 - optimal
148       1 - primal infeasible
149       2 - dual infeasible
150       3 - stopped on iterations etc
151       4 - stopped due to errors
152   */
153   inline int status() const            { return problemStatus_; }
154   /// Are there a numerical difficulties?
155   bool isAbandoned() const             { return problemStatus_==4; }
156   /// Is optimality proven?
157   bool isProvenOptimal() const         { return problemStatus_==0; }
158   /// Is primal infeasiblity proven?
159   bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
160   /// Is dual infeasiblity proven?
161   bool isProvenDualInfeasible() const  { return problemStatus_==2; }
162   /// Is the given primal objective limit reached?
163   bool isPrimalObjectiveLimitReached() const ;
164   /// Is the given dual objective limit reached?
165   bool isDualObjectiveLimitReached() const ;
166   /// Iteration limit reached?
167   bool isIterationLimitReached() const { return problemStatus_==3; }
168   /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
169   inline int optimizationDirection() const {
170      return (int) optimizationDirection_;
171   }
172   inline double getObjSense() const    { return optimizationDirection_; }
173   void setOptimizationDirection(int value);
174   /// Primal row solution
175   inline double * primalRowSolution() const    { return rowActivity_; }
176   inline const double * getRowActivity() const { return rowActivity_; }
177   /// Primal column solution
178   inline double * primalColumnSolution() const { return columnActivity_; }
179   inline const double * getColSolution() const { return columnActivity_; }
180   inline void setColSolution(const double * input)
181   { memcpy(columnActivity_,input,numberColumns_*sizeof(double));};
182   /// Dual row solution
183   inline double * dualRowSolution() const      { return dual_; }
184   inline const double * getRowPrice() const    { return dual_; }
185   /// Reduced costs
186   inline double * dualColumnSolution() const   { return reducedCost_; }
187   inline const double * getReducedCost() const { return reducedCost_; }
188   /// Row lower
189   inline double* rowLower() const              { return rowLower_; }
190   inline const double* getRowLower() const     { return rowLower_; }
191   /// Row upper
192   inline double* rowUpper() const              { return rowUpper_; }
193   inline const double* getRowUpper() const     { return rowUpper_; }
194   /// Objective
195   inline double * objective() const            { return objective_; }
196   inline const double * getObjCoefficients() const { return objective_; }
197   /// Row Objective
198   inline double * rowObjective() const         { return rowObjective_; }
199   inline const double * getRowObjCoefficients() const {
200      return rowObjective_;
201   }
202   /// Column Lower
203   inline double * columnLower() const          { return columnLower_; }
204   inline const double * getColLower() const    { return columnLower_; }
205   /// Column Upper
206   inline double * columnUpper() const          { return columnUpper_; }
207   inline const double * getColUpper() const    { return columnUpper_; }
208   /// Matrix (if not ClpPackedmatrix be careful about memory leak
209   inline CoinPackedMatrix * matrix() const {
210      return matrix_->getPackedMatrix();
211   }
212   /// Number of elements in matrix
213   inline int getNumElements() const 
214     { return matrix_->getNumElements();};
215   /// Row Matrix
216   inline ClpMatrixBase * rowCopy() const       { return rowCopy_; }
217   /// Clp Matrix
218   inline ClpMatrixBase * clpMatrix() const     { return matrix_; }
219   /// Objective value
220   inline double objectiveValue() const {
221      return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
222   }
223   inline double getObjValue() const {
224      return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
225   }
226   /// Integer information
227   inline char * integerInformation() const     { return integerType_; }
228   /** Infeasibility/unbounded ray (NULL returned if none/wrong)
229       Up to user to use delete [] on these arrays.  */
230   double * infeasibilityRay() const;
231   double * unboundedRay() const;
232  /// See if status array exists (partly for OsiClp)
233  inline bool statusExists() const
234  { return (status_!=NULL);};
235  /// Return address of status array (char[numberRows+numberColumns])
236  inline unsigned char *  statusArray() const
237  { return status_;};
238  /** Return copy of status array (char[numberRows+numberColumns]),
239      use delete [] */
240  unsigned char *  statusCopy() const;
241  /// Copy in status vector
242  void copyinStatus(const unsigned char * statusArray);
243
244  //@}
245  /**@name Message handling */
246  //@{
247   /// Pass in Message handler (not deleted at end)
248   void passInMessageHandler(CoinMessageHandler * handler);
249   /// Set language
250   void newLanguage(CoinMessages::Language language);
251   void setLanguage(CoinMessages::Language language) { newLanguage(language); }
252   /// Return handler
253   CoinMessageHandler * messageHandler() const       { return handler_; }
254   /// Return messages
255   CoinMessages messages() const                     { return messages_; }
256   /// Return pointer to messages
257   CoinMessages * messagesPointer()                  { return & messages_; }
258   /// Log level
259   void setLogLevel(int value)    { handler_->setLogLevel(value); }
260   int logLevel() const           { return handler_->logLevel(); }
261   /// length of names (0 means no names0
262   inline int lengthNames() const { return lengthNames_; }
263   /// Row names
264   const std::vector<std::string> * rowNames() const {
265      return &rowNames_;
266   }
267   const std::string& rowName(int iRow) const {
268      return rowNames_[iRow];
269   }
270   /// Column names
271   const std::vector<std::string> * columnNames() const {
272      return &columnNames_;
273   }
274   const std::string& columnName(int iColumn) const {
275      return columnNames_[iColumn];
276   }
277  //@}
278
279
280  //---------------------------------------------------------------------------
281  /**@name Parameter set/get methods
282
283     The set methods return true if the parameter was set to the given value,
284     false otherwise. There can be various reasons for failure: the given
285     parameter is not applicable for the solver (e.g., refactorization
286     frequency for the volume algorithm), the parameter is not yet implemented
287     for the solver or simply the value of the parameter is out of the range
288     the solver accepts. If a parameter setting call returns false check the
289     details of your solver.
290
291     The get methods return true if the given parameter is applicable for the
292     solver and is implemented. In this case the value of the parameter is
293     returned in the second argument. Otherwise they return false.
294
295     ** once it has been decided where solver sits this may be redone
296  */
297  //@{
298    /// Set an integer parameter
299    bool setIntParam(ClpIntParam key, int value) ;
300    /// Set an double parameter
301    bool setDblParam(ClpDblParam key, double value) ;
302    /// Set an string parameter
303    bool setStrParam(ClpStrParam key, const std::string & value);
304    // Get an integer parameter
305    inline bool getIntParam(ClpIntParam key, int& value) const {
306      if (key!=ClpLastIntParam) {
307        value = intParam_[key];
308        return true;
309      } else {
310        return false;
311      }
312    }
313    // Get an double parameter
314    inline bool getDblParam(ClpDblParam key, double& value) const {
315      if (key!=ClpLastDblParam) {
316        value = dblParam_[key];
317        return true;
318      } else {
319        return false;
320      }
321    }
322    // Get a string parameter
323    inline bool getStrParam(ClpStrParam key, std::string& value) const {
324      if (key!=ClpLastStrParam) {
325        value = strParam_[key];
326        return true;
327      } else {
328        return false;
329      }
330    }
331  //@}
332
333  /**@name private or protected methods */
334  //@{
335protected:
336  /// Does most of deletion
337  void gutsOfDelete();
338  /** Does most of copying
339      If trueCopy false then just points to arrays */
340  void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
341  /// gets lower and upper bounds on rows
342  void getRowBound(int iRow, double& lower, double& upper) const;
343  /// puts in format I like - 4 array matrix - may make row copy
344  void gutsOfLoadModel ( int numberRows, int numberColumns,
345                     const double* collb, const double* colub,   
346                     const double* obj,
347                     const double* rowlb, const double* rowub,
348                      const double * rowObjective=NULL);
349  //@}
350
351
352////////////////// data //////////////////
353protected:
354
355  /**@name data */
356  //@{
357  /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
358  double optimizationDirection_;
359  /// Number of rows
360  int numberRows_;
361  /// Number of columns
362  int numberColumns_;
363  /// Row activities
364  double * rowActivity_;
365  /// Column activities
366  double * columnActivity_;
367  /// Duals
368  double * dual_;
369  /// Reduced costs
370  double * reducedCost_;
371  /// Row lower
372  double* rowLower_;
373  /// Row upper
374  double* rowUpper_;
375  /// Objective
376  double * objective_;
377  /// Row Objective (? sign)  - may be NULL
378  double * rowObjective_;
379  /// Column Lower
380  double * columnLower_;
381  /// Column Upper
382  double * columnUpper_;
383  /// Packed matrix
384  ClpMatrixBase * matrix_;
385  /// Row copy if wanted
386  ClpMatrixBase * rowCopy_;
387  /// Infeasible/unbounded ray
388  double * ray_;
389  /// Array of integer parameters
390  int intParam_[ClpLastIntParam];
391  /// Array of double parameters
392  double dblParam_[ClpLastDblParam];
393  /// Array of string parameters
394  std::string strParam_[ClpLastStrParam];
395  /// Objective value
396  double objectiveValue_;
397  /// Number of iterations
398  int numberIterations_;
399  /// Status of problem
400  int problemStatus_;
401  /// Maximum number of iterations
402  int maximumIterations_;
403  /// Message handler
404  CoinMessageHandler * handler_;
405  /// Flag to say if default handler (so delete)
406  bool defaultHandler_;
407  /// Messages
408  CoinMessages messages_;
409  /** Status Region.  I know that not all algorithms need a status
410      array, but it made sense for things like crossover and put
411      all permanent stuff in one place.  No assumption is made
412      about what is in status array (although it might be good to reserve
413      bottom 3 bits (i.e. 0-7 numeric) for classic status).  This
414      is number of columns + number of rows long (in that order).
415  */
416  unsigned char * status_;
417  /// length of names (0 means no names)
418  int lengthNames_;
419  /// Row names
420  std::vector<std::string> rowNames_;
421  /// Column names
422  std::vector<std::string> columnNames_;
423  /// Integer information
424  char * integerType_;
425  //@}
426};
427#endif
Note: See TracBrowser for help on using the repository browser.