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

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

Allow export model - what a kludge

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