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

Last change on this file since 1212 was 1212, checked in by forrest, 10 years ago

fixes

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