source: stable/2.8/Cbc/src/CbcSolver.hpp @ 2004

Last change on this file since 2004 was 2004, checked in by forrest, 5 years ago

more for threadsafe

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