source: trunk/Cbc/src/CbcSolver.hpp @ 862

Last change on this file since 862 was 847, checked in by forrest, 12 years ago

access babModel

File size: 9.1 KB
Line 
1// Copyright (C) 2007, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#ifndef CbcSolver_H
4#define CbcSolver_H
5#include <string>
6#include <vector>
7#include "CoinFinite.hpp"
8#include "CoinMessageHandler.hpp"
9#include "OsiClpSolverInterface.hpp"
10#include "CbcModel.hpp"
11#include "CbcOrClpParam.hpp"
12class CbcUser;
13class CbcStopNow;
14class CglCutGenerator;
15
16//#############################################################################
17
18/** This allows the use of the standalone solver in a flexible manner
19    It has an original OsiClpSolverInterface and CbcModel which
20    it can use repeatedly e.g. get a heuristic solution and then start again
21
22    So I will need a primitive scripting language which can then call solve
23    and manipulate solution value and solution arrays.
24   
25*/
26
27class CbcSolver  {
28 
29public:
30  ///@name Solve method
31  //@{
32  /** This takes a list of commands, does "stuff" and returns
33      returnMode -
34      0 model and solver untouched - babModel updated
35      1 model updated - just with solution basis etc
36      2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
37  */
38  int solve(int argc, const char * argv[], int returnMode); 
39  /** This takes a list of commands, does "stuff" and returns
40      returnMode -
41      0 model and solver untouched - babModel updated
42      1 model updated - just with solution basis etc
43      2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
44  */
45  int solve(const char * input, int returnMode); 
46  //@}
47  ///@name Constructors and destructors etc
48  //@{
49  /// Default Constructor
50  CbcSolver(); 
51 
52  /// Constructor from solver
53  CbcSolver(const OsiClpSolverInterface &);
54 
55  /// Constructor from model
56  CbcSolver(const CbcModel &);
57 
58  /** Copy constructor .
59   */ 
60  CbcSolver(const CbcSolver & rhs);
61 
62  /// Assignment operator
63  CbcSolver & operator=(const CbcSolver& rhs);
64 
65  /// Destructor
66  ~CbcSolver ();
67  /// Fill with standard parameters
68  void fillParameters();
69  /// Set default values in solvers from parameters
70  void fillValuesInSolver();
71  /// Add user function
72  void addUserFunction(CbcUser * function);
73  /// Set user call back
74  void setUserCallBack(CbcStopNow * function);
75  /// Add cut generator
76  void addCutGenerator(CglCutGenerator * generator);
77  //@}
78  ///@name miscellaneous methods to line up with old
79  //@{
80  // analyze model
81  int * analyze(OsiClpSolverInterface * solverMod, int & numberChanged, double & increment,
82                bool changeInt,  CoinMessageHandler * generalMessageHandler);
83  /** 1 - add heuristics to model
84      2 - do heuristics (and set cutoff and best solution)
85      3 - for miplib test so skip some
86      (out model later)
87  */
88  int doHeuristics(CbcModel * model, int type);
89  /** Updates model_ from babModel_ according to returnMode
90      returnMode -
91      0 model and solver untouched - babModel updated
92      1 model updated - just with solution basis etc
93      2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
94  */
95  void updateModel(ClpSimplex * model2, int returnMode);
96  //@}
97  ///@name useful stuff
98  //@{
99  /// Get int value
100  int intValue(CbcOrClpParameterType type) const;
101  /// Set int value
102  void setIntValue(CbcOrClpParameterType type,int value);
103  /// Get double value
104  double doubleValue(CbcOrClpParameterType type) const;
105  /// Set double value
106  void setDoubleValue(CbcOrClpParameterType type,double value);
107  /// User function (NULL if no match)
108  CbcUser * userFunction(const char * name) const;
109  /// Return original Cbc model
110  inline CbcModel * model() 
111  { return &model_;}
112  /// Return updated Cbc model
113  inline CbcModel * babModel() 
114  { return babModel_;}
115  /// Number of userFunctions
116  inline int numberUserFunctions() const
117  { return numberUserFunctions_;}
118  /// User function array
119  inline CbcUser ** userFunctionArray() const
120  { return userFunction_;}
121  /// Copy of model on initial load (will contain output solutions)
122  inline OsiClpSolverInterface * originalSolver() const
123  { return originalSolver_;}
124  /// Copy of model on initial load
125  inline CoinModel * originalCoinModel() const
126  { return originalCoinModel_;}
127  /// Copy of model on initial load (will contain output solutions)
128  void setOriginalSolver(OsiClpSolverInterface * originalSolver);
129  /// Copy of model on initial load
130  void setOriginalCoinModel(CoinModel * originalCoinModel);
131  /// Number of cutgenerators
132  inline int numberCutGenerators() const
133  { return numberCutGenerators_;}
134  /// Cut generator array
135  inline CglCutGenerator ** cutGeneratorArray() const
136  { return cutGenerator_;}
137  /// Start time
138  inline double startTime() const
139  { return startTime_;}
140  /// Whether to print to std::cout
141  inline void setPrinting(bool onOff)
142  { noPrinting_= !onOff;}
143  /// Where to start reading commands
144  inline void setReadMode(int value)
145  { readMode_ = value;}
146  //@}
147private:
148  ///@name Private member data
149  //@{
150 
151  /// Reference model
152  CbcModel model_;
153 
154  /// Updated model
155  CbcModel * babModel_;
156 
157  /// User functions
158  CbcUser ** userFunction_;
159  /** Status of user functions
160      0 - not used
161      1 - needs cbc_load
162      2 - available - data in coinModel
163      3 - data loaded - can do cbc_save
164  */
165  int * statusUserFunction_;
166  /// Copy of model on initial load (will contain output solutions)
167  OsiClpSolverInterface * originalSolver_;
168  /// Copy of model on initial load
169  CoinModel * originalCoinModel_;
170  /// Cut generators
171  CglCutGenerator ** cutGenerator_;
172  /// Number of user functions
173  int numberUserFunctions_;
174  /// Number of cut generators
175  int numberCutGenerators_;
176  /// Stop now stuff
177  CbcStopNow * callBack_;
178  /// Cpu time at instantiation
179  double startTime_;
180  /// Parameters and values
181  CbcOrClpParam * parameters_;
182  /// Number of parameters
183  int numberParameters_ ;
184  /// Whether to do miplib test
185  bool doMiplib_;
186  /// Whether to print to std::cout
187  bool noPrinting_;
188  /// Where to start reading commands
189  int readMode_;
190  //@}
191};
192//#############################################################################
193/// Structure to hold useful arrays
194typedef struct {
195  // Priorities
196  int * priorities_;
197  // SOS priorities
198  int * sosPriority_;
199  // Direction to branch first
200  int * branchDirection_;
201  // Input solution
202  double * primalSolution_;
203  // Down pseudo costs
204  double * pseudoDown_; 
205  // Up pseudo costs
206  double * pseudoUp_; 
207} CbcSolverUsefulData;
208/** This allows the use of an unknown user stuff including modeling languages
209 */
210
211class CbcUser  {
212 
213public:
214  ///@name import/export methods
215  //@{
216  /** Import - gets full command arguments
217      Returns -1 - no action
218               0 - data read in without error
219               1 - errors
220  */
221  virtual int importData(CbcSolver * model, int & argc, char * argv[]) {return -1;}
222  /// Export 1 OsiClpSolver, 2 CbcModel - add 10 if infeasible from odd situation
223  virtual void exportSolution(CbcSolver * model, int mode,const char * message=NULL) {}
224  /// Export Data (i.e. at very end)
225  virtual void exportData(CbcSolver * model) {}
226  /// Get useful stuff
227  virtual void fillInformation(CbcSolver * model,
228                               CbcSolverUsefulData & info) {}
229
230  //@}
231  ///@name usage methods
232  //@{
233  /// CoinModel if valid
234  inline CoinModel * coinModel() const
235  { return coinModel_;}
236  /// Other info - needs expanding
237  virtual void * stuff() {return NULL;}
238  /// Name
239  inline std::string name() const
240  { return userName_;}
241  /// Solve (whatever that means)
242  virtual void solve(CbcSolver * model, const char * options) = 0;
243  /// Returns true if function knows about option
244  virtual bool canDo(const char * options) = 0;
245  //@}
246  ///@name Constructors and destructors etc
247  //@{
248  /// Default Constructor
249  CbcUser(); 
250 
251  /** Copy constructor .
252   */ 
253  CbcUser(const CbcUser & rhs);
254 
255  /// Assignment operator
256  CbcUser & operator=(const CbcUser& rhs);
257
258  /// Clone
259  virtual CbcUser * clone() const = 0;
260 
261  /// Destructor
262  virtual ~CbcUser ();
263  //@}
264 
265protected:
266  ///@name Private member data
267  //@{
268 
269  /// CoinModel
270  CoinModel * coinModel_;
271 
272  /// Name of user function
273  std::string userName_;
274
275 //@}
276};
277//#############################################################################
278
279/** This allows the use of a call back class to decide whether to stop
280 */
281
282class CbcStopNow  {
283 
284public:
285  ///@name Decision methods
286  //@{
287  /// Import - 0 if good
288  /** Meaning of whereFrom:
289     1 after initial solve by dualsimplex etc
290     2 after preprocessing
291     3 just before branchAndBound (so user can override)
292     4 just after branchAndBound (before postprocessing)
293     5 after postprocessing
294     6 after a user called heuristic phase
295     nonzero return code to stop
296  */
297  virtual int callBack(CbcModel * currentSolver, int whereFrom) {return 0;}
298  //@}
299  ///@name Constructors and destructors etc
300  //@{
301  /// Default Constructor
302  CbcStopNow(); 
303 
304  /** Copy constructor .
305   */ 
306  CbcStopNow(const CbcStopNow & rhs);
307 
308  /// Assignment operator
309  CbcStopNow & operator=(const CbcStopNow& rhs);
310
311  /// Clone
312  virtual CbcStopNow * clone() const;
313 
314  /// Destructor
315  virtual ~CbcStopNow ();
316  //@}
317 
318private:
319  ///@name Private member data
320  //@{
321 //@}
322};
323#endif
Note: See TracBrowser for help on using the repository browser.