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

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

For presolve

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 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  //@}
233  /**@name Message handling */
234  //@{
235   /// Pass in Message handler (not deleted at end)
236   void passInMessageHandler(CoinMessageHandler * handler);
237   /// Set language
238   void newLanguage(CoinMessages::Language language);
239   void setLanguage(CoinMessages::Language language) { newLanguage(language); }
240   /// Return handler
241   CoinMessageHandler * messageHandler() const       { return handler_; }
242   /// Return messages
243   CoinMessages messages() const                     { return messages_; }
244   /// Return pointer to messages
245   CoinMessages * messagesPointer()                  { return & messages_; }
246   /// Log level
247   void setLogLevel(int value)    { handler_->setLogLevel(value); }
248   int logLevel() const           { return handler_->logLevel(); }
249   /// length of names (0 means no names0
250   inline int lengthNames() const { return lengthNames_; }
251   /// Row names
252   const std::vector<std::string> * rowNames() const {
253      return &rowNames_;
254   }
255   const std::string& rowName(int iRow) const {
256      return rowNames_[iRow];
257   }
258   /// Column names
259   const std::vector<std::string> * columnNames() const {
260      return &columnNames_;
261   }
262   const std::string& columnName(int iColumn) const {
263      return columnNames_[iColumn];
264   }
265  //@}
266
267
268  //---------------------------------------------------------------------------
269  /**@name Parameter set/get methods
270
271     The set methods return true if the parameter was set to the given value,
272     false otherwise. There can be various reasons for failure: the given
273     parameter is not applicable for the solver (e.g., refactorization
274     frequency for the volume algorithm), the parameter is not yet implemented
275     for the solver or simply the value of the parameter is out of the range
276     the solver accepts. If a parameter setting call returns false check the
277     details of your solver.
278
279     The get methods return true if the given parameter is applicable for the
280     solver and is implemented. In this case the value of the parameter is
281     returned in the second argument. Otherwise they return false.
282
283     ** once it has been decided where solver sits this may be redone
284  */
285  //@{
286    /// Set an integer parameter
287    bool setIntParam(ClpIntParam key, int value) ;
288    /// Set an double parameter
289    bool setDblParam(ClpDblParam key, double value) ;
290    /// Set an string parameter
291    bool setStrParam(ClpStrParam key, const std::string & value);
292    // Get an integer parameter
293    inline bool getIntParam(ClpIntParam key, int& value) const {
294      if (key!=ClpLastIntParam) {
295        value = intParam_[key];
296        return true;
297      } else {
298        return false;
299      }
300    }
301    // Get an double parameter
302    inline bool getDblParam(ClpDblParam key, double& value) const {
303      if (key!=ClpLastDblParam) {
304        value = dblParam_[key];
305        return true;
306      } else {
307        return false;
308      }
309    }
310    // Get a string parameter
311    inline bool getStrParam(ClpStrParam key, std::string& value) const {
312      if (key!=ClpLastStrParam) {
313        value = strParam_[key];
314        return true;
315      } else {
316        return false;
317      }
318    }
319  //@}
320
321  /**@name private or protected methods */
322  //@{
323protected:
324  /// Does most of deletion
325  void gutsOfDelete();
326  /** Does most of copying
327      If trueCopy false then just points to arrays */
328  void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
329  /// gets lower and upper bounds on rows
330  void getRowBound(int iRow, double& lower, double& upper) const;
331  /// puts in format I like - 4 array matrix - may make row copy
332  void gutsOfLoadModel ( int numberRows, int numberColumns,
333                     const double* collb, const double* colub,   
334                     const double* obj,
335                     const double* rowlb, const double* rowub,
336                      const double * rowObjective=NULL);
337  //@}
338
339
340////////////////// data //////////////////
341protected:
342
343  /**@name data */
344  //@{
345  /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
346  double optimizationDirection_;
347  /// Number of rows
348  int numberRows_;
349  /// Number of columns
350  int numberColumns_;
351  /// Row activities
352  double * rowActivity_;
353  /// Column activities
354  double * columnActivity_;
355  /// Duals
356  double * dual_;
357  /// Reduced costs
358  double * reducedCost_;
359  /// Row lower
360  double* rowLower_;
361  /// Row upper
362  double* rowUpper_;
363  /// Objective
364  double * objective_;
365  /// Row Objective (? sign)  - may be NULL
366  double * rowObjective_;
367  /// Column Lower
368  double * columnLower_;
369  /// Column Upper
370  double * columnUpper_;
371  /// Packed matrix
372  ClpMatrixBase * matrix_;
373  /// Row copy if wanted
374  ClpMatrixBase * rowCopy_;
375  /// Infeasible/unbounded ray
376  double * ray_;
377  /// Array of integer parameters
378  int intParam_[ClpLastIntParam];
379  /// Array of double parameters
380  double dblParam_[ClpLastDblParam];
381  /// Array of string parameters
382  std::string strParam_[ClpLastStrParam];
383  /// Objective value
384  double objectiveValue_;
385  /// Number of iterations
386  int numberIterations_;
387  /// Status of problem
388  int problemStatus_;
389  /// Maximum number of iterations
390  int maximumIterations_;
391  /// Message handler
392  CoinMessageHandler * handler_;
393  /// Flag to say if default handler (so delete)
394  bool defaultHandler_;
395  /// Messages
396  CoinMessages messages_;
397  /// length of names (0 means no names)
398  int lengthNames_;
399  /// Row names
400  std::vector<std::string> rowNames_;
401  /// Column names
402  std::vector<std::string> columnNames_;
403  /// Integer information
404  char * integerType_;
405  //@}
406};
407#endif
Note: See TracBrowser for help on using the repository browser.