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

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

trying to move ampl stuff away

File size: 9.0 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  inline CbcModel * model() 
110  { return &model_;}
111  /// Number of userFunctions
112  inline int numberUserFunctions() const
113  { return numberUserFunctions_;}
114  /// User function array
115  inline CbcUser ** userFunctionArray() const
116  { return userFunction_;}
117  /// Copy of model on initial load (will contain output solutions)
118  inline OsiClpSolverInterface * originalSolver() const
119  { return originalSolver_;}
120  /// Copy of model on initial load
121  inline CoinModel * originalCoinModel() const
122  { return originalCoinModel_;}
123  /// Copy of model on initial load (will contain output solutions)
124  void setOriginalSolver(OsiClpSolverInterface * originalSolver);
125  /// Copy of model on initial load
126  void setOriginalCoinModel(CoinModel * originalCoinModel);
127  /// Number of cutgenerators
128  inline int numberCutGenerators() const
129  { return numberCutGenerators_;}
130  /// Cut generator array
131  inline CglCutGenerator ** cutGeneratorArray() const
132  { return cutGenerator_;}
133  /// Start time
134  inline double startTime() const
135  { return startTime_;}
136  /// Whether to print to std::cout
137  inline void setPrinting(bool onOff)
138  { noPrinting_= !onOff;}
139  /// Where to start reading commands
140  inline void setReadMode(int value)
141  { readMode_ = value;}
142  //@}
143private:
144  ///@name Private member data
145  //@{
146 
147  /// Reference model
148  CbcModel model_;
149 
150  /// Updated model
151  CbcModel * babModel_;
152 
153  /// User functions
154  CbcUser ** userFunction_;
155  /** Status of user functions
156      0 - not used
157      1 - needs cbc_load
158      2 - available - data in coinModel
159      3 - data loaded - can do cbc_save
160  */
161  int * statusUserFunction_;
162  /// Copy of model on initial load (will contain output solutions)
163  OsiClpSolverInterface * originalSolver_;
164  /// Copy of model on initial load
165  CoinModel * originalCoinModel_;
166  /// Cut generators
167  CglCutGenerator ** cutGenerator_;
168  /// Number of user functions
169  int numberUserFunctions_;
170  /// Number of cut generators
171  int numberCutGenerators_;
172  /// Stop now stuff
173  CbcStopNow * callBack_;
174  /// Cpu time at instantiation
175  double startTime_;
176  /// Parameters and values
177  CbcOrClpParam * parameters_;
178  /// Number of parameters
179  int numberParameters_ ;
180  /// Whether to do miplib test
181  bool doMiplib_;
182  /// Whether to print to std::cout
183  bool noPrinting_;
184  /// Where to start reading commands
185  int readMode_;
186  //@}
187};
188//#############################################################################
189/// Structure to hold useful arrays
190typedef struct {
191  // Priorities
192  int * priorities_;
193  // SOS priorities
194  int * sosPriority_;
195  // Direction to branch first
196  int * branchDirection_;
197  // Input solution
198  double * primalSolution_;
199  // Down pseudo costs
200  double * pseudoDown_; 
201  // Up pseudo costs
202  double * pseudoUp_; 
203} CbcSolverUsefulData;
204/** This allows the use of an unknown user stuff including modeling languages
205 */
206
207class CbcUser  {
208 
209public:
210  ///@name import/export methods
211  //@{
212  /** Import - gets full command arguments
213      Returns -1 - no action
214               0 - data read in without error
215               1 - errors
216  */
217  virtual int importData(CbcSolver * model, int & argc, char * argv[]) {return -1;}
218  /// Export 1 OsiClpSolver, 2 CbcModel - add 10 if infeasible from odd situation
219  virtual void exportSolution(CbcSolver * model, int mode,const char * message=NULL) {}
220  /// Export Data (i.e. at very end)
221  virtual void exportData(CbcSolver * model) {}
222  /// Get useful stuff
223  virtual void fillInformation(CbcSolver * model,
224                               CbcSolverUsefulData & info) {}
225
226  //@}
227  ///@name usage methods
228  //@{
229  /// CoinModel if valid
230  inline CoinModel * coinModel() const
231  { return coinModel_;}
232  /// Other info - needs expanding
233  virtual void * stuff() {return NULL;}
234  /// Name
235  inline std::string name() const
236  { return userName_;}
237  /// Solve (whatever that means)
238  virtual void solve(CbcSolver * model, const char * options) = 0;
239  /// Returns true if function knows about option
240  virtual bool canDo(const char * options) = 0;
241  //@}
242  ///@name Constructors and destructors etc
243  //@{
244  /// Default Constructor
245  CbcUser(); 
246 
247  /** Copy constructor .
248   */ 
249  CbcUser(const CbcUser & rhs);
250 
251  /// Assignment operator
252  CbcUser & operator=(const CbcUser& rhs);
253
254  /// Clone
255  virtual CbcUser * clone() const = 0;
256 
257  /// Destructor
258  virtual ~CbcUser ();
259  //@}
260 
261protected:
262  ///@name Private member data
263  //@{
264 
265  /// CoinModel
266  CoinModel * coinModel_;
267 
268  /// Name of user function
269  std::string userName_;
270
271 //@}
272};
273//#############################################################################
274
275/** This allows the use of a call back class to decide whether to stop
276 */
277
278class CbcStopNow  {
279 
280public:
281  ///@name Decision methods
282  //@{
283  /// Import - 0 if good
284  /** Meaning of whereFrom:
285     1 after initial solve by dualsimplex etc
286     2 after preprocessing
287     3 just before branchAndBound (so user can override)
288     4 just after branchAndBound (before postprocessing)
289     5 after postprocessing
290     6 after a user called heuristic phase
291     nonzero return code to stop
292  */
293  virtual int callBack(CbcModel * currentSolver, int whereFrom) {return 0;}
294  //@}
295  ///@name Constructors and destructors etc
296  //@{
297  /// Default Constructor
298  CbcStopNow(); 
299 
300  /** Copy constructor .
301   */ 
302  CbcStopNow(const CbcStopNow & rhs);
303 
304  /// Assignment operator
305  CbcStopNow & operator=(const CbcStopNow& rhs);
306
307  /// Clone
308  virtual CbcStopNow * clone() const;
309 
310  /// Destructor
311  virtual ~CbcStopNow ();
312  //@}
313 
314private:
315  ///@name Private member data
316  //@{
317 //@}
318};
319#endif
Note: See TracBrowser for help on using the repository browser.