source: branches/sandbox/Cbc/src/CbcSolver.hpp @ 1389

Last change on this file since 1389 was 1383, checked in by bjarni, 10 years ago

Extracted crunchIt, fixVubs, doHeuristics from CbcSolver?.cpp and placed it in CbcSolverHeuristics?.cpp

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