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

Last change on this file since 36 was 36, checked in by forrest, 19 years ago

Forgot to pass back status after presolve

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 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  inline void setNumberIterations(int numberIterations)
144  { numberIterations_ = numberIterations;};
145   /// Maximum number of iterations
146   inline int maximumIterations() const { return maximumIterations_; }
147   void setMaximumIterations(int value);
148   /** Status of problem:
149       0 - optimal
150       1 - primal infeasible
151       2 - dual infeasible
152       3 - stopped on iterations etc
153       4 - stopped due to errors
154   */
155   inline int status() const            { return problemStatus_; }
156  /// Set problem status
157  inline void setProblemStatus(int problemStatus)
158  { problemStatus_ = problemStatus;};
159   /// Are there a numerical difficulties?
160   bool isAbandoned() const             { return problemStatus_==4; }
161   /// Is optimality proven?
162   bool isProvenOptimal() const         { return problemStatus_==0; }
163   /// Is primal infeasiblity proven?
164   bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
165   /// Is dual infeasiblity proven?
166   bool isProvenDualInfeasible() const  { return problemStatus_==2; }
167   /// Is the given primal objective limit reached?
168   bool isPrimalObjectiveLimitReached() const ;
169   /// Is the given dual objective limit reached?
170   bool isDualObjectiveLimitReached() const ;
171   /// Iteration limit reached?
172   bool isIterationLimitReached() const { return problemStatus_==3; }
173   /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
174   inline int optimizationDirection() const {
175      return (int) optimizationDirection_;
176   }
177   inline double getObjSense() const    { return optimizationDirection_; }
178   void setOptimizationDirection(int value);
179   /// Primal row solution
180   inline double * primalRowSolution() const    { return rowActivity_; }
181   inline const double * getRowActivity() const { return rowActivity_; }
182   /// Primal column solution
183   inline double * primalColumnSolution() const { return columnActivity_; }
184   inline const double * getColSolution() const { return columnActivity_; }
185   inline void setColSolution(const double * input)
186   { memcpy(columnActivity_,input,numberColumns_*sizeof(double));};
187   /// Dual row solution
188   inline double * dualRowSolution() const      { return dual_; }
189   inline const double * getRowPrice() const    { return dual_; }
190   /// Reduced costs
191   inline double * dualColumnSolution() const   { return reducedCost_; }
192   inline const double * getReducedCost() const { return reducedCost_; }
193   /// Row lower
194   inline double* rowLower() const              { return rowLower_; }
195   inline const double* getRowLower() const     { return rowLower_; }
196   /// Row upper
197   inline double* rowUpper() const              { return rowUpper_; }
198   inline const double* getRowUpper() const     { return rowUpper_; }
199   /// Objective
200   inline double * objective() const            { return objective_; }
201   inline const double * getObjCoefficients() const { return objective_; }
202   /// Row Objective
203   inline double * rowObjective() const         { return rowObjective_; }
204   inline const double * getRowObjCoefficients() const {
205      return rowObjective_;
206   }
207   /// Column Lower
208   inline double * columnLower() const          { return columnLower_; }
209   inline const double * getColLower() const    { return columnLower_; }
210   /// Column Upper
211   inline double * columnUpper() const          { return columnUpper_; }
212   inline const double * getColUpper() const    { return columnUpper_; }
213   /// Matrix (if not ClpPackedmatrix be careful about memory leak
214   inline CoinPackedMatrix * matrix() const {
215      return matrix_->getPackedMatrix();
216   }
217   /// Number of elements in matrix
218   inline int getNumElements() const 
219     { return matrix_->getNumElements();};
220   /// Row Matrix
221   inline ClpMatrixBase * rowCopy() const       { return rowCopy_; }
222   /// Clp Matrix
223   inline ClpMatrixBase * clpMatrix() const     { return matrix_; }
224   /// Objective value
225   inline double objectiveValue() const {
226      return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
227   }
228   inline double getObjValue() const {
229      return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
230   }
231   /// Integer information
232   inline char * integerInformation() const     { return integerType_; }
233   /** Infeasibility/unbounded ray (NULL returned if none/wrong)
234       Up to user to use delete [] on these arrays.  */
235   double * infeasibilityRay() const;
236   double * unboundedRay() const;
237  /// See if status array exists (partly for OsiClp)
238  inline bool statusExists() const
239  { return (status_!=NULL);};
240  /// Return address of status array (char[numberRows+numberColumns])
241  inline unsigned char *  statusArray() const
242  { return status_;};
243  /** Return copy of status array (char[numberRows+numberColumns]),
244      use delete [] */
245  unsigned char *  statusCopy() const;
246  /// Copy in status vector
247  void copyinStatus(const unsigned char * statusArray);
248
249  //@}
250  /**@name Message handling */
251  //@{
252   /// Pass in Message handler (not deleted at end)
253   void passInMessageHandler(CoinMessageHandler * handler);
254   /// Set language
255   void newLanguage(CoinMessages::Language language);
256   void setLanguage(CoinMessages::Language language) { newLanguage(language); }
257   /// Return handler
258   CoinMessageHandler * messageHandler() const       { return handler_; }
259   /// Return messages
260   CoinMessages messages() const                     { return messages_; }
261   /// Return pointer to messages
262   CoinMessages * messagesPointer()                  { return & messages_; }
263   /// Log level
264   void setLogLevel(int value)    { handler_->setLogLevel(value); }
265   int logLevel() const           { return handler_->logLevel(); }
266   /// length of names (0 means no names0
267   inline int lengthNames() const { return lengthNames_; }
268   /// Row names
269   const std::vector<std::string> * rowNames() const {
270      return &rowNames_;
271   }
272   const std::string& rowName(int iRow) const {
273      return rowNames_[iRow];
274   }
275   /// Column names
276   const std::vector<std::string> * columnNames() const {
277      return &columnNames_;
278   }
279   const std::string& columnName(int iColumn) const {
280      return columnNames_[iColumn];
281   }
282  //@}
283
284
285  //---------------------------------------------------------------------------
286  /**@name Parameter set/get methods
287
288     The set methods return true if the parameter was set to the given value,
289     false otherwise. There can be various reasons for failure: the given
290     parameter is not applicable for the solver (e.g., refactorization
291     frequency for the volume algorithm), the parameter is not yet implemented
292     for the solver or simply the value of the parameter is out of the range
293     the solver accepts. If a parameter setting call returns false check the
294     details of your solver.
295
296     The get methods return true if the given parameter is applicable for the
297     solver and is implemented. In this case the value of the parameter is
298     returned in the second argument. Otherwise they return false.
299
300     ** once it has been decided where solver sits this may be redone
301  */
302  //@{
303    /// Set an integer parameter
304    bool setIntParam(ClpIntParam key, int value) ;
305    /// Set an double parameter
306    bool setDblParam(ClpDblParam key, double value) ;
307    /// Set an string parameter
308    bool setStrParam(ClpStrParam key, const std::string & value);
309    // Get an integer parameter
310    inline bool getIntParam(ClpIntParam key, int& value) const {
311      if (key!=ClpLastIntParam) {
312        value = intParam_[key];
313        return true;
314      } else {
315        return false;
316      }
317    }
318    // Get an double parameter
319    inline bool getDblParam(ClpDblParam key, double& value) const {
320      if (key!=ClpLastDblParam) {
321        value = dblParam_[key];
322        return true;
323      } else {
324        return false;
325      }
326    }
327    // Get a string parameter
328    inline bool getStrParam(ClpStrParam key, std::string& value) const {
329      if (key!=ClpLastStrParam) {
330        value = strParam_[key];
331        return true;
332      } else {
333        return false;
334      }
335    }
336  //@}
337
338  /**@name private or protected methods */
339  //@{
340protected:
341  /// Does most of deletion
342  void gutsOfDelete();
343  /** Does most of copying
344      If trueCopy false then just points to arrays */
345  void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
346  /// gets lower and upper bounds on rows
347  void getRowBound(int iRow, double& lower, double& upper) const;
348  /// puts in format I like - 4 array matrix - may make row copy
349  void gutsOfLoadModel ( int numberRows, int numberColumns,
350                     const double* collb, const double* colub,   
351                     const double* obj,
352                     const double* rowlb, const double* rowub,
353                      const double * rowObjective=NULL);
354  //@}
355
356
357////////////////// data //////////////////
358protected:
359
360  /**@name data */
361  //@{
362  /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
363  double optimizationDirection_;
364  /// Number of rows
365  int numberRows_;
366  /// Number of columns
367  int numberColumns_;
368  /// Row activities
369  double * rowActivity_;
370  /// Column activities
371  double * columnActivity_;
372  /// Duals
373  double * dual_;
374  /// Reduced costs
375  double * reducedCost_;
376  /// Row lower
377  double* rowLower_;
378  /// Row upper
379  double* rowUpper_;
380  /// Objective
381  double * objective_;
382  /// Row Objective (? sign)  - may be NULL
383  double * rowObjective_;
384  /// Column Lower
385  double * columnLower_;
386  /// Column Upper
387  double * columnUpper_;
388  /// Packed matrix
389  ClpMatrixBase * matrix_;
390  /// Row copy if wanted
391  ClpMatrixBase * rowCopy_;
392  /// Infeasible/unbounded ray
393  double * ray_;
394  /// Array of integer parameters
395  int intParam_[ClpLastIntParam];
396  /// Array of double parameters
397  double dblParam_[ClpLastDblParam];
398  /// Array of string parameters
399  std::string strParam_[ClpLastStrParam];
400  /// Objective value
401  double objectiveValue_;
402  /// Number of iterations
403  int numberIterations_;
404  /// Status of problem
405  int problemStatus_;
406  /// Maximum number of iterations
407  int maximumIterations_;
408  /// Message handler
409  CoinMessageHandler * handler_;
410  /// Flag to say if default handler (so delete)
411  bool defaultHandler_;
412  /// Messages
413  CoinMessages messages_;
414  /** Status Region.  I know that not all algorithms need a status
415      array, but it made sense for things like crossover and put
416      all permanent stuff in one place.  No assumption is made
417      about what is in status array (although it might be good to reserve
418      bottom 3 bits (i.e. 0-7 numeric) for classic status).  This
419      is number of columns + number of rows long (in that order).
420  */
421  unsigned char * status_;
422  /// length of names (0 means no names)
423  int lengthNames_;
424  /// Row names
425  std::vector<std::string> rowNames_;
426  /// Column names
427  std::vector<std::string> columnNames_;
428  /// Integer information
429  char * integerType_;
430  //@}
431};
432#endif
Note: See TracBrowser for help on using the repository browser.