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

Last change on this file since 2464 was 2464, checked in by unxusr, 10 months ago

.clang-format with proposal for formatting code

  • 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 2464 2019-01-03 19:03:23Z unxusr $ */
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  CbcOrClpParam *parameters_;
237  /// Number of parameters
238  int numberParameters_;
239  /// Whether to do miplib test
240  bool doMiplib_;
241  /// Whether to print to std::cout
242  bool noPrinting_;
243  /// Where to start reading commands
244  int readMode_;
245  //@}
246};
247//#############################################################################
248
249/// Structure to hold useful arrays
250typedef struct {
251  // Priorities
252  int *priorities_;
253  // SOS priorities
254  int *sosPriority_;
255  // Direction to branch first
256  int *branchDirection_;
257  // Input solution
258  double *primalSolution_;
259  // Down pseudo costs
260  double *pseudoDown_;
261  // Up pseudo costs
262  double *pseudoUp_;
263} CbcSolverUsefulData2;
264
265//#############################################################################
266
267/**
268   The CbcSolver class was taken out at a 9/12/09 meeting
269   This is a feeble replacement.
270   At present everything is public
271*/
272class CbcSolverUsefulData {
273
274public:
275  ///@name Constructors and destructors etc
276  //@{
277  /// Default Constructor
278  CbcSolverUsefulData();
279
280  /** Copy constructor .
281     */
282  CbcSolverUsefulData(const CbcSolverUsefulData &rhs);
283
284  /// Assignment operator
285  CbcSolverUsefulData &operator=(const CbcSolverUsefulData &rhs);
286
287  /// Destructor
288  ~CbcSolverUsefulData();
289  //@}
290
291  ///@name Member data
292  //@{
293  // For time
294  double totalTime_;
295  // Parameters
296  CbcOrClpParam parameters_[CBCMAXPARAMETERS];
297  // Printing
298  bool noPrinting_;
299  // Whether to use signal handler
300  bool useSignalHandler_;
301  // Number of Parameters
302  int numberParameters_;
303  // Default pump tuning
304  int initialPumpTune_;
305  //@}
306};
307/// And this uses it
308// When we want to load up CbcModel with options first
309void CbcMain0(CbcModel &babSolver, CbcSolverUsefulData &solverData);
310int CbcMain1(int argc, const char *argv[], CbcModel &babSolver, int(CbcModel *currentSolver, int whereFrom), CbcSolverUsefulData &solverData);
311
312//#############################################################################
313
314/*! \brief A class to allow the use of unknown user functionality
315
316    For example, access to a modelling language (CbcAmpl).
317*/
318class CbcUser {
319
320public:
321  ///@name import/export methods
322  //@{
323  /*! \brief Import - gets full command arguments
324
325      \return
326      - -1 - no action
327      -  0 - data read in without error
328      -  1 - errors
329    */
330  virtual int importData(CbcSolver * /*model*/, int & /*argc*/, char ** /*argv[]*/)
331  {
332    return -1;
333  }
334
335  /*! \brief Export
336
337      Values for mode:
338      - 1 OsiClpSolver
339      - 2 CbcModel
340      - add 10 if infeasible from odd situation
341    */
342  virtual void exportSolution(CbcSolver * /*model*/,
343    int /*mode*/, const char * /*message*/ = NULL) {}
344
345  /// Export Data (i.e. at very end)
346  virtual void exportData(CbcSolver * /*model*/) {}
347
348  /// Get useful stuff
349  virtual void fillInformation(CbcSolver * /*model*/,
350    CbcSolverUsefulData & /*info*/) {}
351  //@}
352
353  ///@name usage methods
354  //@{
355  /// CoinModel if valid
356  inline CoinModel *coinModel() const
357  {
358    return coinModel_;
359  }
360  /// Other info - needs expanding
361  virtual void *stuff()
362  {
363    return NULL;
364  }
365  /// Name
366  inline std::string name() const
367  {
368    return userName_;
369  }
370  /// Solve (whatever that means)
371  virtual void solve(CbcSolver *model, const char *options) = 0;
372  /// Returns true if function knows about option
373  virtual bool canDo(const char *options) = 0;
374  //@}
375
376  ///@name Constructors and destructors etc
377  //@{
378  /// Default Constructor
379  CbcUser();
380
381  /// Copy constructor
382  CbcUser(const CbcUser &rhs);
383
384  /// Assignment operator
385  CbcUser &operator=(const CbcUser &rhs);
386
387  /// Clone
388  virtual CbcUser *clone() const = 0;
389
390  /// Destructor
391  virtual ~CbcUser();
392  //@}
393
394protected:
395  ///@name Private member data
396  //@{
397
398  /// CoinModel
399  CoinModel *coinModel_;
400
401  /// Name of user function
402  std::string userName_;
403
404  //@}
405};
406//#############################################################################
407
408/*! \brief Support the use of a call back class to decide whether to stop
409
410  Definitely under construction.
411*/
412
413class CbcStopNow {
414
415public:
416  ///@name Decision methods
417  //@{
418  /*! \brief Import
419
420      Values for whereFrom:
421       - 1 after initial solve by dualsimplex etc
422       - 2 after preprocessing
423       - 3 just before branchAndBound (so user can override)
424       - 4 just after branchAndBound (before postprocessing)
425       - 5 after postprocessing
426       - 6 after a user called heuristic phase
427
428      \return 0 if good
429       nonzero return code to stop
430    */
431  virtual int callBack(CbcModel * /*currentSolver*/, int /*whereFrom*/)
432  {
433    return 0;
434  }
435  //@}
436
437  ///@name Constructors and destructors etc
438  //@{
439  /// Default Constructor
440  CbcStopNow();
441
442  /** Copy constructor .
443     */
444  CbcStopNow(const CbcStopNow &rhs);
445
446  /// Assignment operator
447  CbcStopNow &operator=(const CbcStopNow &rhs);
448
449  /// Clone
450  virtual CbcStopNow *clone() const;
451
452  /// Destructor
453  virtual ~CbcStopNow();
454  //@}
455
456private:
457  ///@name Private member data
458  //@{
459  //@}
460};
461#endif
Note: See TracBrowser for help on using the repository browser.