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

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

Breaking out whileIterating

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.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// This is only in to use OsiDblParam etc
12#include "OsiSolverInterface.hpp"
13#include "ClpMatrixBase.hpp"
14#include "OsiMessageHandler.hpp"
15
16
17/** This is the base class for Linear Models
18    This knows nothing about the algorithm, but it seems to
19    have a reasonable amount of information
20
21    I would welcome suggestions for what should be in this and
22    how it relates to OsiSolverInterface.  Some methods look
23    very similar.
24
25*/
26
27class ClpModel {
28
29public:
30
31  /**@name Constructors and destructor
32     Note - copy methods copy ALL data so can chew up memory
33     until other copy is freed
34   */
35  //@{
36  /// Default constructor
37    ClpModel (  );
38
39  /// Copy constructor.
40  ClpModel(const ClpModel &);
41  /// Assignment operator. This copies the data
42    ClpModel & operator=(const ClpModel & rhs);
43  /// Destructor
44   ~ClpModel (  );
45  //@}
46
47  /**@name Load model - loads some stuff and initializes others */
48  //@{
49    /** Loads a problem (the constraints on the
50        rows are given by lower and upper bounds). If a pointer is 0 then the
51        following values are the default:
52        <ul>
53          <li> <code>colub</code>: all columns have upper bound infinity
54          <li> <code>collb</code>: all columns have lower bound 0
55          <li> <code>rowub</code>: all rows have upper bound infinity
56          <li> <code>rowlb</code>: all rows have lower bound -infinity
57          <li> <code>obj</code>: all variables have 0 objective coefficient
58        </ul>
59    */
60  void loadProblem (  const ClpMatrixBase& matrix,
61                     const double* collb, const double* colub,   
62                     const double* obj,
63                     const double* rowlb, const double* rowub,
64                      const double * rowObjective=NULL);
65  void loadProblem (  const OsiPackedMatrix& matrix,
66                     const double* collb, const double* colub,   
67                     const double* obj,
68                     const double* rowlb, const double* rowub,
69                      const double * rowObjective=NULL);
70
71  /** Just like the other loadProblem() method except that the matrix is
72        given in a standard column major ordered format (without gaps). */
73  void loadProblem (  const int numcols, const int numrows,
74                     const int* start, const int* index,
75                     const double* value,
76                     const double* collb, const double* colub,   
77                     const double* obj,
78                      const double* rowlb, const double* rowub,
79                      const double * rowObjective=NULL);
80  /// Read an mps file from the given filename
81  int readMps(const char *filename,
82              bool keepNames=false,
83              bool ignoreErrors = false);
84  /// Resizes rim part of model
85  void resize (int newNumberRows, int newNumberColumns);
86  /// Deletes rows
87  void deleteRows(int number, const int * which);
88  /// Deletes columns
89 
90  void deleteColumns(int number, const int * which);
91  /** Borrow model.  This is so we dont have to copy large amounts
92      of data around.  It assumes a derived class wants to overwrite
93      an empty model with a real one - while it does an algorithm */
94  void borrowModel(ClpModel & otherModel);
95  /** Return model - nulls all arrays so can be deleted safely
96      also updates any scalars */
97  void returnModel(ClpModel & otherModel);
98 
99  //@}
100  /**@name gets and sets */
101  //@{
102  /// Number of rows
103  inline int numberRows() const
104    { return numberRows_;};
105  inline int getNumRows() const
106    { return numberRows_;};
107  /// Number of columns
108  inline int getNumCols() const
109    { return numberColumns_;};
110  inline int numberColumns() const
111    { return numberColumns_;};
112  /// Primal tolerance to use
113  inline double primalTolerance() const 
114          { return dblParam_[OsiPrimalTolerance];} ;
115  void setPrimalTolerance( double value) ;
116  /// Dual tolerance to use
117  inline double dualTolerance() const 
118          { return dblParam_[OsiDualTolerance];} ;
119  void setDualTolerance( double value) ;
120  /// Number of iterations
121  inline int numberIterations() const
122    { return numberIterations_;};
123  inline int getIterationCount() const
124    { return numberIterations_;};
125  /// Maximum number of iterations
126  inline int maximumIterations() const
127    { return maximumIterations_;};
128  void setMaximumIterations(int value);
129  /** Status of problem:
130      0 - optimal
131      1 - primal infeasible
132      2 - dual infeasible
133      3 - stopped on iterations etc
134      4 - stopped due to errors
135  */
136  inline int status() const
137  { return problemStatus_;};
138  /// Are there a numerical difficulties?
139  bool isAbandoned() const 
140  { return problemStatus_==4;};
141  /// Is optimality proven?
142  bool isProvenOptimal() const 
143  { return problemStatus_==0;};
144  /// Is primal infeasiblity proven?
145  bool isProvenPrimalInfeasible() const 
146  { return problemStatus_==1;};
147  /// Is dual infeasiblity proven?
148  bool isProvenDualInfeasible() const 
149  { return problemStatus_==2;};
150  /// Is the given primal objective limit reached?
151  bool isPrimalObjectiveLimitReached() const 
152  { return false;}; // not implemented yet
153  /// Is the given dual objective limit reached?
154  bool isDualObjectiveLimitReached() const 
155  { return false;}; // not implememented yet
156  /// Iteration limit reached?
157  bool isIterationLimitReached() const 
158  { return problemStatus_==3;};
159  /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
160  inline int optimizationDirection() const
161          { return (int) optimizationDirection_;};
162  inline double getObjSense() const
163          { return optimizationDirection_;};
164  void setOptimizationDirection(int value);
165  /// Primal row solution
166  inline double * primalRowSolution() const 
167          { return rowActivity_;} ;
168  inline const double * getRowActivity() const 
169          { return rowActivity_;} ;
170  /// Primal column solution
171  inline double * primalColumnSolution() const 
172          { return columnActivity_;} ;
173  inline const double * getColSolution() const 
174          { return columnActivity_;} ;
175  /// Dual row solution
176  inline double * dualRowSolution() const 
177          { return dual_;} ;
178  inline const double * getRowPrice() const 
179          { return dual_;} ;
180  /// Reduced costs
181  inline double * dualColumnSolution() const 
182          { return reducedCost_;} ;
183  inline const double * getReducedCost() const 
184          { return reducedCost_;} ;
185  /// Row lower
186  inline double* rowLower() const
187          { return rowLower_;};
188  inline const double* getRowLower() const
189          { return rowLower_;};
190  /// Row upper
191  inline double* rowUpper() const
192          { return rowUpper_;};
193  inline const double* getRowUpper() const
194          { return rowUpper_;};
195  /// Objective
196  inline double * objective() const
197          { return objective_;};
198  inline const double * getObjCoefficients() const
199          { return objective_;};
200  /// Row Objective
201  inline double * rowObjective() const
202          { return rowObjective_;};
203  inline const double * getRowObjCoefficients() const
204          { return rowObjective_;};
205  /// Column Lower
206  inline double * columnLower() const
207          { return columnLower_;};
208  inline const double * getColLower() const
209          { return columnLower_;};
210  /// Column Upper
211  inline double * columnUpper() const
212          { return columnUpper_;};
213  inline const double * getColUpper() const
214          { return columnUpper_;};
215  /// Matrix (if not ClpPackedmatrix be careful about memory leak
216  inline OsiPackedMatrix * matrix() const
217          { return matrix_->getPackedMatrix();};
218  /// Row Matrix
219  inline ClpMatrixBase * rowCopy() const
220          { return rowCopy_;};
221  /// Clp Matrix
222  inline ClpMatrixBase * clpMatrix() const
223          { return matrix_;};
224  /// Objective value
225  inline double objectiveValue() const
226  { return objectiveValue_*optimizationDirection_ - dblParam_[OsiObjOffset];};
227  inline double getObjValue() const
228  { return objectiveValue_*optimizationDirection_ - dblParam_[OsiObjOffset];};
229  /** Infeasibility/unbounded ray (NULL returned if none/wrong)
230      Up to user to use delete [] on these arrays.  */
231  double * infeasibilityRay() const;
232  double * unboundedRay() const;
233  //@}
234  /**@name Message handling */
235  //@{
236  /// Pass in Message handler (not deleted at end)
237  void passInMessageHandler(OsiMessageHandler * handler);
238  /// Set language
239  void newLanguage(OsiMessages::Language language);
240  void setLanguage(OsiMessages::Language language)
241  {newLanguage(language);};
242  /// Return handler
243  OsiMessageHandler * messageHandler() const
244  {return handler_;};
245  /// Return messages
246  OsiMessages messages() const
247  {return messages_;};
248  /// Return pointer to messages
249  OsiMessages * messagesPointer() 
250  {return & messages_;};
251  /// Log level
252  void setLogLevel(int value)
253  {handler_->setLogLevel(value);};
254  int logLevel() const
255  { return handler_->logLevel();};
256  /// length of names (0 means no names0
257  inline int lengthNames() const
258  {return lengthNames_;};
259  /// Row names
260  const std::vector<std::string> * rowNames() const
261  {return &rowNames_;};
262  /// Column names
263  const std::vector<std::string> * columnNames() const
264  {return &columnNames_;};
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(OsiIntParam key, int value) ;
288    /// Set an double parameter
289    bool setDblParam(OsiDblParam key, double value) ;
290    /// Set an string parameter
291    bool setStrParam(OsiStrParam key, const std::string & value);
292    // Get an integer parameter
293    inline bool getIntParam(OsiIntParam key, int& value) const {
294      if (key!=OsiLastIntParam) {
295        value = intParam_[key];
296        return true;
297      } else {
298        return false;
299      }
300    }
301    // Get an double parameter
302    inline bool getDblParam(OsiDblParam key, double& value) const {
303      if (key!=OsiLastDblParam) {
304        value = dblParam_[key];
305        return true;
306      } else {
307        return false;
308      }
309    }
310    // Get a string parameter
311    inline bool getStrParam(OsiStrParam key, std::string& value) const {
312      if (key!=OsiLastStrParam) {
313        value = strParam_[key];
314        return true;
315      } else {
316        return false;
317      }
318    }
319  //@}
320
321  /**@name private or protected methods */
322  //@{
323private:
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);
329protected:
330  /// gets lower and upper bounds on rows
331  void getRowBound(int iRow, double& lower, double& upper) const;
332  /// puts in format I like - 4 array matrix - may make row copy
333  void gutsOfLoadModel ( int numberRows, int numberColumns,
334                     const double* collb, const double* colub,   
335                     const double* obj,
336                     const double* rowlb, const double* rowub,
337                      const double * rowObjective=NULL);
338  //@}
339
340
341////////////////// data //////////////////
342protected:
343
344  /**@name data */
345  //@{
346  /// Direction of optimization (1 - minimize, -1 - maximize, 0 - ignore
347  double optimizationDirection_;
348  /// Number of rows
349  int numberRows_;
350  /// Number of columns
351  int numberColumns_;
352  /// Row activities
353  double * rowActivity_;
354  /// Column activities
355  double * columnActivity_;
356  /// Duals
357  double * dual_;
358  /// Reduced costs
359  double * reducedCost_;
360  /// Row lower
361  double* rowLower_;
362  /// Row upper
363  double* rowUpper_;
364  /// Objective
365  double * objective_;
366  /// Row Objective (? sign)  - may be NULL
367  double * rowObjective_;
368  /// Column Lower
369  double * columnLower_;
370  /// Column Upper
371  double * columnUpper_;
372  /// Packed matrix
373  ClpMatrixBase * matrix_;
374  /// Row copy if wanted
375  ClpMatrixBase * rowCopy_;
376  /// Infeasible/unbounded ray
377  double * ray_;
378  /// Array of integer parameters
379  int intParam_[OsiLastIntParam];
380  /// Array of double parameters
381  double dblParam_[OsiLastDblParam];
382  /// Array of string parameters
383  std::string strParam_[OsiLastDblParam];
384  /// Objective value
385  double objectiveValue_;
386  /// Number of iterations
387  int numberIterations_;
388  /// Status of problem
389  int problemStatus_;
390  /// Maximum number of iterations
391  int maximumIterations_;
392  /// Message handler
393  OsiMessageHandler * handler_;
394  /// Flag to say if default handler (so delete)
395  bool defaultHandler_;
396  /// Messages
397  OsiMessages messages_;
398  /// length of names (0 means no names0
399  int lengthNames_;
400  /// Row names
401  std::vector<std::string> rowNames_;
402  /// Column names
403  std::vector<std::string> columnNames_;
404  //@}
405};
406#endif
Note: See TracBrowser for help on using the repository browser.