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

Last change on this file since 1998 was 1998, checked in by forrest, 4 years ago

for threadsafe

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