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

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

Changes to make more reliable if problem size changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 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  /// Resizes rim part of model
94  void resize (int newNumberRows, int newNumberColumns);
95  /// Deletes rows
96  void deleteRows(int number, const int * which);
97  /// Deletes columns
98 
99  void deleteColumns(int number, const int * which);
100  /** Borrow model.  This is so we dont have to copy large amounts
101      of data around.  It assumes a derived class wants to overwrite
102      an empty model with a real one - while it does an algorithm */
103  void borrowModel(ClpModel & otherModel);
104  /** Return model - nulls all arrays so can be deleted safely
105      also updates any scalars */
106  void returnModel(ClpModel & otherModel);
107 
108  //@}
109  /**@name gets and sets */
110  //@{
111   /// Number of rows
112   inline int numberRows() const {
113      return numberRows_;
114   }
115   inline int getNumRows() const {
116      return numberRows_;
117   }
118   /// Number of columns
119   inline int getNumCols() const {
120      return numberColumns_;
121   }
122   inline int numberColumns() const {
123      return numberColumns_;
124   }
125   /// Primal tolerance to use
126   inline double primalTolerance() const {
127      return dblParam_[ClpPrimalTolerance];
128   }
129   void setPrimalTolerance( double value) ;
130   /// Dual tolerance to use
131   inline double dualTolerance() const  { return dblParam_[ClpDualTolerance]; }
132   void setDualTolerance( double value) ;
133   /// Number of iterations
134   inline int numberIterations() const  { return numberIterations_; }
135   inline int getIterationCount() const { return numberIterations_; }
136   /// Maximum number of iterations
137   inline int maximumIterations() const { return maximumIterations_; }
138   void setMaximumIterations(int value);
139   /** Status of problem:
140       0 - optimal
141       1 - primal infeasible
142       2 - dual infeasible
143       3 - stopped on iterations etc
144       4 - stopped due to errors
145   */
146   inline int status() const            { return problemStatus_; }
147   /// Are there a numerical difficulties?
148   bool isAbandoned() const             { return problemStatus_==4; }
149   /// Is optimality proven?
150   bool isProvenOptimal() const         { return problemStatus_==0; }
151   /// Is primal infeasiblity proven?
152   bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
153   /// Is dual infeasiblity proven?
154   bool isProvenDualInfeasible() const  { return problemStatus_==2; }
155   /// Is the given primal objective limit reached?
156   bool isPrimalObjectiveLimitReached() const ;
157   /// Is the given dual objective limit reached?
158   bool isDualObjectiveLimitReached() const ;
159   /// Iteration limit reached?
160   bool isIterationLimitReached() const { return problemStatus_==3; }
161   /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
162   inline int optimizationDirection() const {
163      return (int) optimizationDirection_;
164   }
165   inline double getObjSense() const    { return optimizationDirection_; }
166   void setOptimizationDirection(int value);
167   /// Primal row solution
168   inline double * primalRowSolution() const    { return rowActivity_; }
169   inline const double * getRowActivity() const { return rowActivity_; }
170   /// Primal column solution
171   inline double * primalColumnSolution() const { return columnActivity_; }
172   inline const double * getColSolution() const { return columnActivity_; }
173   inline void setColSolution(const double * input)
174   { memcpy(columnActivity_,input,numberColumns_*sizeof(double));};
175   /// Dual row solution
176   inline double * dualRowSolution() const      { return dual_; }
177   inline const double * getRowPrice() const    { return dual_; }
178   /// Reduced costs
179   inline double * dualColumnSolution() const   { return reducedCost_; }
180   inline const double * getReducedCost() const { return reducedCost_; }
181   /// Row lower
182   inline double* rowLower() const              { return rowLower_; }
183   inline const double* getRowLower() const     { return rowLower_; }
184   /// Row upper
185   inline double* rowUpper() const              { return rowUpper_; }
186   inline const double* getRowUpper() const     { return rowUpper_; }
187   /// Objective
188   inline double * objective() const            { return objective_; }
189   inline const double * getObjCoefficients() const { return objective_; }
190   /// Row Objective
191   inline double * rowObjective() const         { return rowObjective_; }
192   inline const double * getRowObjCoefficients() const {
193      return rowObjective_;
194   }
195   /// Column Lower
196   inline double * columnLower() const          { return columnLower_; }
197   inline const double * getColLower() const    { return columnLower_; }
198   /// Column Upper
199   inline double * columnUpper() const          { return columnUpper_; }
200   inline const double * getColUpper() const    { return columnUpper_; }
201   /// Matrix (if not ClpPackedmatrix be careful about memory leak
202   inline CoinPackedMatrix * matrix() const {
203      return matrix_->getPackedMatrix();
204   }
205   /// Number of elements in matrix
206   inline int getNumElements() const 
207     { return matrix_->getNumElements();};
208   /// Row Matrix
209   inline ClpMatrixBase * rowCopy() const       { return rowCopy_; }
210   /// Clp Matrix
211   inline ClpMatrixBase * clpMatrix() const     { return matrix_; }
212   /// Objective value
213   inline double objectiveValue() const {
214      return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
215   }
216   inline double getObjValue() const {
217      return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
218   }
219   /// Integer information
220   inline char * integerInformation() const     { return integerType_; }
221   /** Infeasibility/unbounded ray (NULL returned if none/wrong)
222       Up to user to use delete [] on these arrays.  */
223   double * infeasibilityRay() const;
224   double * unboundedRay() const;
225  //@}
226  /**@name Message handling */
227  //@{
228   /// Pass in Message handler (not deleted at end)
229   void passInMessageHandler(CoinMessageHandler * handler);
230   /// Set language
231   void newLanguage(CoinMessages::Language language);
232   void setLanguage(CoinMessages::Language language) { newLanguage(language); }
233   /// Return handler
234   CoinMessageHandler * messageHandler() const       { return handler_; }
235   /// Return messages
236   CoinMessages messages() const                     { return messages_; }
237   /// Return pointer to messages
238   CoinMessages * messagesPointer()                  { return & messages_; }
239   /// Log level
240   void setLogLevel(int value)    { handler_->setLogLevel(value); }
241   int logLevel() const           { return handler_->logLevel(); }
242   /// length of names (0 means no names0
243   inline int lengthNames() const { return lengthNames_; }
244   /// Row names
245   const std::vector<std::string> * rowNames() const {
246      return &rowNames_;
247   }
248   const std::string& rowName(int iRow) const {
249      return rowNames_[iRow];
250   }
251   /// Column names
252   const std::vector<std::string> * columnNames() const {
253      return &columnNames_;
254   }
255   const std::string& columnName(int iColumn) const {
256      return columnNames_[iColumn];
257   }
258  //@}
259
260
261  //---------------------------------------------------------------------------
262  /**@name Parameter set/get methods
263
264     The set methods return true if the parameter was set to the given value,
265     false otherwise. There can be various reasons for failure: the given
266     parameter is not applicable for the solver (e.g., refactorization
267     frequency for the volume algorithm), the parameter is not yet implemented
268     for the solver or simply the value of the parameter is out of the range
269     the solver accepts. If a parameter setting call returns false check the
270     details of your solver.
271
272     The get methods return true if the given parameter is applicable for the
273     solver and is implemented. In this case the value of the parameter is
274     returned in the second argument. Otherwise they return false.
275
276     ** once it has been decided where solver sits this may be redone
277  */
278  //@{
279    /// Set an integer parameter
280    bool setIntParam(ClpIntParam key, int value) ;
281    /// Set an double parameter
282    bool setDblParam(ClpDblParam key, double value) ;
283    /// Set an string parameter
284    bool setStrParam(ClpStrParam key, const std::string & value);
285    // Get an integer parameter
286    inline bool getIntParam(ClpIntParam key, int& value) const {
287      if (key!=ClpLastIntParam) {
288        value = intParam_[key];
289        return true;
290      } else {
291        return false;
292      }
293    }
294    // Get an double parameter
295    inline bool getDblParam(ClpDblParam key, double& value) const {
296      if (key!=ClpLastDblParam) {
297        value = dblParam_[key];
298        return true;
299      } else {
300        return false;
301      }
302    }
303    // Get a string parameter
304    inline bool getStrParam(ClpStrParam key, std::string& value) const {
305      if (key!=ClpLastStrParam) {
306        value = strParam_[key];
307        return true;
308      } else {
309        return false;
310      }
311    }
312  //@}
313
314  /**@name private or protected methods */
315  //@{
316protected:
317  /// Does most of deletion
318  void gutsOfDelete();
319  /** Does most of copying
320      If trueCopy false then just points to arrays */
321  void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
322  /// gets lower and upper bounds on rows
323  void getRowBound(int iRow, double& lower, double& upper) const;
324  /// puts in format I like - 4 array matrix - may make row copy
325  void gutsOfLoadModel ( int numberRows, int numberColumns,
326                     const double* collb, const double* colub,   
327                     const double* obj,
328                     const double* rowlb, const double* rowub,
329                      const double * rowObjective=NULL);
330  //@}
331
332
333////////////////// data //////////////////
334protected:
335
336  /**@name data */
337  //@{
338  /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
339  double optimizationDirection_;
340  /// Number of rows
341  int numberRows_;
342  /// Number of columns
343  int numberColumns_;
344  /// Row activities
345  double * rowActivity_;
346  /// Column activities
347  double * columnActivity_;
348  /// Duals
349  double * dual_;
350  /// Reduced costs
351  double * reducedCost_;
352  /// Row lower
353  double* rowLower_;
354  /// Row upper
355  double* rowUpper_;
356  /// Objective
357  double * objective_;
358  /// Row Objective (? sign)  - may be NULL
359  double * rowObjective_;
360  /// Column Lower
361  double * columnLower_;
362  /// Column Upper
363  double * columnUpper_;
364  /// Packed matrix
365  ClpMatrixBase * matrix_;
366  /// Row copy if wanted
367  ClpMatrixBase * rowCopy_;
368  /// Infeasible/unbounded ray
369  double * ray_;
370  /// Array of integer parameters
371  int intParam_[ClpLastIntParam];
372  /// Array of double parameters
373  double dblParam_[ClpLastDblParam];
374  /// Array of string parameters
375  std::string strParam_[ClpLastStrParam];
376  /// Objective value
377  double objectiveValue_;
378  /// Number of iterations
379  int numberIterations_;
380  /// Status of problem
381  int problemStatus_;
382  /// Maximum number of iterations
383  int maximumIterations_;
384  /// Message handler
385  CoinMessageHandler * handler_;
386  /// Flag to say if default handler (so delete)
387  bool defaultHandler_;
388  /// Messages
389  CoinMessages messages_;
390  /// length of names (0 means no names)
391  int lengthNames_;
392  /// Row names
393  std::vector<std::string> rowNames_;
394  /// Column names
395  std::vector<std::string> columnNames_;
396  /// Integer information
397  char * integerType_;
398  //@}
399};
400#endif
Note: See TracBrowser for help on using the repository browser.