source: trunk/Cbc/src/CbcSolver.hpp

Last change on this file was 2479, checked in by unxusr, 4 months ago

simplify handling of parameters and remove staticParameterData which made CbcMain0 and CbcMain1 not thread safe

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1/* $Id: CbcSolver.hpp 2479 2019-02-07 19:50:37Z forrest $ */
2// Copyright (C) 2007, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6/*! \file CbcSolver.hpp
7    \brief Defines CbcSolver, the proposed top-level class for the new-style
8    cbc solver.
9
10    This class is currently an orphan. With the removal of all code flagged
11    with the NEW_STYLE_SOLVER, this class is never instantiated (and cannot
12    be instantiated). It is available to be coopted as a top-level object
13    wrapping the current CbcMain0 and CbcMain1, should that appear to be a
14    desireable path forward.  -- lh, 091211 --
15*/
16
17#ifndef CbcSolver_H
18#define CbcSolver_H
19
20#include <string>
21#include <vector>
22#include "CoinMessageHandler.hpp"
23#include "OsiClpSolverInterface.hpp"
24
25#if CBC_OTHER_SOLVER == 1
26#include "OsiCpxSolverInterface.hpp"
27#endif
28
29#include "CbcModel.hpp"
30#include "CbcOrClpParam.hpp"
31
32class CbcUser;
33class CbcStopNow;
34class CglCutGenerator;
35
36//#############################################################################
37
38/*! \brief This allows the use of the standalone solver in a flexible manner.
39
40    It has an original OsiClpSolverInterface and CbcModel which it can use
41    repeatedly, e.g., to get a heuristic solution and then start again.
42
43    So I [jjf] will need a primitive scripting language which can then call
44    solve and manipulate solution value and solution arrays.
45
46    Also provides for user callback functions. Currently two ideas in
47    gestation, CbcUser and CbcStopNow. The latter seems limited to deciding
48    whether or not to stop. The former seems completely general, with a notion
49    of importing and exporting, and a `solve', which should be interpreted as
50    `do whatever this user function does'.
51
52    Parameter initialisation is at last centralised in fillParameters().
53*/
54
55class CbcSolver {
56
57public:
58  ///@name Solve method
59  //@{
60  /** This takes a list of commands, does "stuff" and returns
61        returnMode -
62        0 model and solver untouched - babModel updated
63        1 model updated - just with solution basis etc
64        2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
65    */
66  int solve(int argc, const char *argv[], int returnMode);
67  /** This takes a list of commands, does "stuff" and returns
68        returnMode -
69        0 model and solver untouched - babModel updated
70        1 model updated - just with solution basis etc
71        2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
72    */
73  int solve(const char *input, int returnMode);
74  //@}
75  ///@name Constructors and destructors etc
76  //@{
77  /// Default Constructor
78  CbcSolver();
79
80  /// Constructor from solver
81  CbcSolver(const OsiClpSolverInterface &);
82
83  /// Constructor from model
84  CbcSolver(const CbcModel &);
85
86  /** Copy constructor .
87     */
88  CbcSolver(const CbcSolver &rhs);
89
90  /// Assignment operator
91  CbcSolver &operator=(const CbcSolver &rhs);
92
93  /// Destructor
94  ~CbcSolver();
95  /// Fill with standard parameters
96  void fillParameters();
97  /*! \brief Set default values in solvers from parameters
98
99      Misleading. The current code actually reads default values from
100      the underlying solvers and installs them as default values for a subset of
101      parameters in #parameters_.
102    */
103  void fillValuesInSolver();
104  /// Add user function
105  void addUserFunction(CbcUser *function);
106  /// Set user call back
107  void setUserCallBack(CbcStopNow *function);
108  /// Add cut generator
109  void addCutGenerator(CglCutGenerator *generator);
110  //@}
111  ///@name miscellaneous methods to line up with old
112  //@{
113  // analyze model
114  int *analyze(OsiClpSolverInterface *solverMod, int &numberChanged, double &increment,
115    bool changeInt, CoinMessageHandler *generalMessageHandler);
116  /** 1 - add heuristics to model
117        2 - do heuristics (and set cutoff and best solution)
118        3 - for miplib test so skip some
119        (out model later)
120    */
121  //int doHeuristics(CbcModel * model, int type);
122  /** Updates model_ from babModel_ according to returnMode
123        returnMode -
124        0 model and solver untouched - babModel updated
125        1 model updated - just with solution basis etc
126        2 model updated i.e. as babModel (babModel NULL) (only use without preprocessing)
127    */
128  void updateModel(ClpSimplex *model2, int returnMode);
129  //@}
130  ///@name useful stuff
131  //@{
132  /// Get int value
133  int intValue(CbcOrClpParameterType type) const;
134  /// Set int value
135  void setIntValue(CbcOrClpParameterType type, int value);
136  /// Get double value
137  double doubleValue(CbcOrClpParameterType type) const;
138  /// Set double value
139  void setDoubleValue(CbcOrClpParameterType type, double value);
140  /// User function (NULL if no match)
141  CbcUser *userFunction(const char *name) const;
142  /// Return original Cbc model
143  inline CbcModel *model()
144  {
145    return &model_;
146  }
147  /// Return updated Cbc model
148  inline CbcModel *babModel()
149  {
150    return babModel_;
151  }
152  /// Number of userFunctions
153  inline int numberUserFunctions() const
154  {
155    return numberUserFunctions_;
156  }
157  /// User function array
158  inline CbcUser **userFunctionArray() const
159  {
160    return userFunction_;
161  }
162  /// Copy of model on initial load (will contain output solutions)
163  inline OsiClpSolverInterface *originalSolver() const
164  {
165    return originalSolver_;
166  }
167  /// Copy of model on initial load
168  inline CoinModel *originalCoinModel() const
169  {
170    return originalCoinModel_;
171  }
172  /// Copy of model on initial load (will contain output solutions)
173  void setOriginalSolver(OsiClpSolverInterface *originalSolver);
174  /// Copy of model on initial load
175  void setOriginalCoinModel(CoinModel *originalCoinModel);
176  /// Number of cutgenerators
177  inline int numberCutGenerators() const
178  {
179    return numberCutGenerators_;
180  }
181  /// Cut generator array
182  inline CglCutGenerator **cutGeneratorArray() const
183  {
184    return cutGenerator_;
185  }
186  /// Start time
187  inline double startTime() const
188  {
189    return startTime_;
190  }
191  /// Whether to print to std::cout
192  inline void setPrinting(bool onOff)
193  {
194    noPrinting_ = !onOff;
195  }
196  /// Where to start reading commands
197  inline void setReadMode(int value)
198  {
199    readMode_ = value;
200  }
201  //@}
202private:
203  ///@name Private member data
204  //@{
205
206  /// Reference model
207  CbcModel model_;
208
209  /// Updated model
210  CbcModel *babModel_;
211
212  /// User functions
213  CbcUser **userFunction_;
214  /** Status of user functions
215        0 - not used
216        1 - needs cbc_load
217        2 - available - data in coinModel
218        3 - data loaded - can do cbc_save
219    */
220  int *statusUserFunction_;
221  /// Copy of model on initial load (will contain output solutions)
222  OsiClpSolverInterface *originalSolver_;
223  /// Copy of model on initial load
224  CoinModel *originalCoinModel_;
225  /// Cut generators
226  CglCutGenerator **cutGenerator_;
227  /// Number of user functions
228  int numberUserFunctions_;
229  /// Number of cut generators
230  int numberCutGenerators_;
231  /// Stop now stuff
232  CbcStopNow *callBack_;
233  /// Cpu time at instantiation
234  double startTime_;
235  /// Parameters and values
236  std::vector< CbcOrClpParam > parameters_;
237  /// Whether to do miplib test
238  bool doMiplib_;
239  /// Whether to print to std::cout
240  bool noPrinting_;
241  /// Where to start reading commands
242  int readMode_;
243  //@}
244};
245//#############################################################################
246
247/// Structure to hold useful arrays
248typedef struct {
249  // Priorities
250  int *priorities_;
251  // SOS priorities
252  int *sosPriority_;
253  // Direction to branch first
254  int *branchDirection_;
255  // Input solution
256  double *primalSolution_;
257  // Down pseudo costs
258  double *pseudoDown_;
259  // Up pseudo costs
260  double *pseudoUp_;
261} CbcSolverUsefulData2;
262
263//#############################################################################
264
265/**
266   The CbcSolver class was taken out at a 9/12/09 meeting
267   This is a feeble replacement.
268   At present everything is public
269*/
270class CbcSolverUsefulData {
271
272public:
273  ///@name Constructors and destructors etc
274  //@{
275  /// Default Constructor
276  CbcSolverUsefulData();
277
278  /** Copy constructor .
279     */
280  CbcSolverUsefulData(const CbcSolverUsefulData &rhs);
281
282  /// Assignment operator
283  CbcSolverUsefulData &operator=(const CbcSolverUsefulData &rhs);
284
285  /// Destructor
286  ~CbcSolverUsefulData();
287  //@}
288
289  ///@name Member data
290  //@{
291  // For time
292  double totalTime_;
293  // Parameters
294  std::vector<CbcOrClpParam> parameters_;
295  // Printing
296  bool noPrinting_;
297  // Whether to use signal handler
298  bool useSignalHandler_;
299  // Default pump tuning
300  int initialPumpTune_;
301  //@}
302};
303/// And this uses it
304// When we want to load up CbcModel with options first
305void CbcMain0(CbcModel &babSolver, CbcSolverUsefulData &solverData);
306int CbcMain1(int argc, const char *argv[], CbcModel &babSolver, int(CbcModel *currentSolver, int whereFrom), CbcSolverUsefulData &solverData);
307
308//#############################################################################
309
310/*! \brief A class to allow the use of unknown user functionality
311
312    For example, access to a modelling language (CbcAmpl).
313*/
314class CbcUser {
315
316public:
317  ///@name import/export methods
318  //@{
319  /*! \brief Import - gets full command arguments
320
321      \return
322      - -1 - no action
323      -  0 - data read in without error
324      -  1 - errors
325    */
326  virtual int importData(CbcSolver * /*model*/, int & /*argc*/, char ** /*argv[]*/)
327  {
328    return -1;
329  }
330
331  /*! \brief Export
332
333      Values for mode:
334      - 1 OsiClpSolver
335      - 2 CbcModel
336      - add 10 if infeasible from odd situation
337    */
338  virtual void exportSolution(CbcSolver * /*model*/,
339    int /*mode*/, const char * /*message*/ = NULL) {}
340
341  /// Export Data (i.e. at very end)
342  virtual void exportData(CbcSolver * /*model*/) {}
343
344  /// Get useful stuff
345  virtual void fillInformation(CbcSolver * /*model*/,
346    CbcSolverUsefulData & /*info*/) {}
347  //@}
348
349  ///@name usage methods
350  //@{
351  /// CoinModel if valid
352  inline CoinModel *coinModel() const
353  {
354    return coinModel_;
355  }
356  /// Other info - needs expanding
357  virtual void *stuff()
358  {
359    return NULL;
360  }
361  /// Name
362  inline std::string name() const
363  {
364    return userName_;
365  }
366  /// Solve (whatever that means)
367  virtual void solve(CbcSolver *model, const char *options) = 0;
368  /// Returns true if function knows about option
369  virtual bool canDo(const char *options) = 0;
370  //@}
371
372  ///@name Constructors and destructors etc
373  //@{
374  /// Default Constructor
375  CbcUser();
376
377  /// Copy constructor
378  CbcUser(const CbcUser &rhs);
379
380  /// Assignment operator
381  CbcUser &operator=(const CbcUser &rhs);
382
383  /// Clone
384  virtual CbcUser *clone() const = 0;
385
386  /// Destructor
387  virtual ~CbcUser();
388  //@}
389
390protected:
391  ///@name Private member data
392  //@{
393
394  /// CoinModel
395  CoinModel *coinModel_;
396
397  /// Name of user function
398  std::string userName_;
399
400  //@}
401};
402//#############################################################################
403
404/*! \brief Support the use of a call back class to decide whether to stop
405
406  Definitely under construction.
407*/
408
409class CbcStopNow {
410
411public:
412  ///@name Decision methods
413  //@{
414  /*! \brief Import
415
416      Values for whereFrom:
417       - 1 after initial solve by dualsimplex etc
418       - 2 after preprocessing
419       - 3 just before branchAndBound (so user can override)
420       - 4 just after branchAndBound (before postprocessing)
421       - 5 after postprocessing
422       - 6 after a user called heuristic phase
423
424      \return 0 if good
425       nonzero return code to stop
426    */
427  virtual int callBack(CbcModel * /*currentSolver*/, int /*whereFrom*/)
428  {
429    return 0;
430  }
431  //@}
432
433  ///@name Constructors and destructors etc
434  //@{
435  /// Default Constructor
436  CbcStopNow();
437
438  /** Copy constructor .
439     */
440  CbcStopNow(const CbcStopNow &rhs);
441
442  /// Assignment operator
443  CbcStopNow &operator=(const CbcStopNow &rhs);
444
445  /// Clone
446  virtual CbcStopNow *clone() const;
447
448  /// Destructor
449  virtual ~CbcStopNow();
450  //@}
451
452private:
453  ///@name Private member data
454  //@{
455  //@}
456};
457#endif
458
459/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
460*/
Note: See TracBrowser for help on using the repository browser.