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

Last change on this file since 1132 was 1132, checked in by forrest, 11 years ago

chnages to try and make faster

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