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

Last change on this file since 1401 was 1401, checked in by lou, 10 years ago

A final commit to make it clear that CbcSolver? is an unused class in the
current code. See comments in CbcSolver?.hpp.

File size: 11.1 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 proposed top-level class for the new-style
7    cbc solver.
8
9    This class is currently an orphan. With the removal of all code flagged
10    with the NEWS_STYLE_SOLVER, this class is never instantiated (and cannot
11    be instantiated). It is available to be coopted as a top-level object
12    wrapping the current CbcMain0 and CbcMain1, should that appear to be a
13    desireable path forward.  -- lh, 091211 --
14*/
15
16#ifndef CbcSolver_H
17#define CbcSolver_H
18
19#include <string>
20#include <vector>
21#include "CoinFinite.hpp"
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        return &model_;
145    }
146    /// Return updated Cbc model
147    inline CbcModel * babModel() {
148        return babModel_;
149    }
150    /// Number of userFunctions
151    inline int numberUserFunctions() const {
152        return numberUserFunctions_;
153    }
154    /// User function array
155    inline CbcUser ** userFunctionArray() const {
156        return userFunction_;
157    }
158    /// Copy of model on initial load (will contain output solutions)
159    inline OsiClpSolverInterface * originalSolver() const {
160        return originalSolver_;
161    }
162    /// Copy of model on initial load
163    inline CoinModel * originalCoinModel() const {
164        return originalCoinModel_;
165    }
166    /// Copy of model on initial load (will contain output solutions)
167    void setOriginalSolver(OsiClpSolverInterface * originalSolver);
168    /// Copy of model on initial load
169    void setOriginalCoinModel(CoinModel * originalCoinModel);
170    /// Number of cutgenerators
171    inline int numberCutGenerators() const {
172        return numberCutGenerators_;
173    }
174    /// Cut generator array
175    inline CglCutGenerator ** cutGeneratorArray() const {
176        return cutGenerator_;
177    }
178    /// Start time
179    inline double startTime() const {
180        return startTime_;
181    }
182    /// Whether to print to std::cout
183    inline void setPrinting(bool onOff) {
184        noPrinting_ = !onOff;
185    }
186    /// Where to start reading commands
187    inline void setReadMode(int value) {
188        readMode_ = value;
189    }
190    //@}
191private:
192    ///@name Private member data
193    //@{
194
195    /// Reference model
196    CbcModel model_;
197
198    /// Updated model
199    CbcModel * babModel_;
200
201    /// User functions
202    CbcUser ** userFunction_;
203    /** Status of user functions
204        0 - not used
205        1 - needs cbc_load
206        2 - available - data in coinModel
207        3 - data loaded - can do cbc_save
208    */
209    int * statusUserFunction_;
210    /// Copy of model on initial load (will contain output solutions)
211    OsiClpSolverInterface * originalSolver_;
212    /// Copy of model on initial load
213    CoinModel * originalCoinModel_;
214    /// Cut generators
215    CglCutGenerator ** cutGenerator_;
216    /// Number of user functions
217    int numberUserFunctions_;
218    /// Number of cut generators
219    int numberCutGenerators_;
220    /// Stop now stuff
221    CbcStopNow * callBack_;
222    /// Cpu time at instantiation
223    double startTime_;
224    /// Parameters and values
225    CbcOrClpParam * parameters_;
226    /// Number of parameters
227    int numberParameters_ ;
228    /// Whether to do miplib test
229    bool doMiplib_;
230    /// Whether to print to std::cout
231    bool noPrinting_;
232    /// Where to start reading commands
233    int readMode_;
234    //@}
235};
236//#############################################################################
237
238/// Structure to hold useful arrays
239typedef struct {
240    // Priorities
241    int * priorities_;
242    // SOS priorities
243    int * sosPriority_;
244    // Direction to branch first
245    int * branchDirection_;
246    // Input solution
247    double * primalSolution_;
248    // Down pseudo costs
249    double * pseudoDown_;
250    // Up pseudo costs
251    double * pseudoUp_;
252} CbcSolverUsefulData;
253
254
255/*! \brief A class to allow the use of unknown user functionality
256
257    For example, access to a modelling language (CbcAmpl).
258*/
259class CbcUser {
260
261public:
262    ///@name import/export methods
263    //@{
264    /*! \brief Import - gets full command arguments
265
266      \return
267      - -1 - no action
268      -  0 - data read in without error
269      -  1 - errors
270    */
271    virtual int importData(CbcSolver * /*model*/, int & /*argc*/, char ** /*argv[]*/) {
272        return -1;
273    }
274
275    /*! \brief Export
276
277      \param mode
278      - 1 OsiClpSolver
279      - 2 CbcModel
280      - add 10 if infeasible from odd situation
281    */
282    virtual void exportSolution(CbcSolver * /*model*/,
283                                int /*mode*/, const char * /*message*/ = NULL) {}
284
285    /// Export Data (i.e. at very end)
286    virtual void exportData(CbcSolver * /*model*/) {}
287
288    /// Get useful stuff
289    virtual void fillInformation(CbcSolver * /*model*/,
290                                 CbcSolverUsefulData & /*info*/) {}
291    //@}
292
293    ///@name usage methods
294    //@{
295    /// CoinModel if valid
296    inline CoinModel *coinModel() const {
297        return coinModel_;
298    }
299    /// Other info - needs expanding
300    virtual void * stuff() {
301        return NULL;
302    }
303    /// Name
304    inline std::string name() const {
305        return userName_;
306    }
307    /// Solve (whatever that means)
308    virtual void solve(CbcSolver * model, const char * options) = 0;
309    /// Returns true if function knows about option
310    virtual bool canDo(const char * options) = 0;
311    //@}
312
313    ///@name Constructors and destructors etc
314    //@{
315    /// Default Constructor
316    CbcUser();
317
318    /// Copy constructor
319    CbcUser(const CbcUser & rhs);
320
321    /// Assignment operator
322    CbcUser & operator=(const CbcUser& rhs);
323
324    /// Clone
325    virtual CbcUser * clone() const = 0;
326
327    /// Destructor
328    virtual ~CbcUser ();
329    //@}
330
331protected:
332    ///@name Private member data
333    //@{
334
335    /// CoinModel
336    CoinModel * coinModel_;
337
338    /// Name of user function
339    std::string userName_;
340
341//@}
342};
343//#############################################################################
344
345/*! \brief Support the use of a call back class to decide whether to stop
346
347  Definitely under construction.
348*/
349
350class CbcStopNow {
351
352public:
353    ///@name Decision methods
354    //@{
355    /*! \brief Import
356
357      \param whereFrom
358       - 1 after initial solve by dualsimplex etc
359       - 2 after preprocessing
360       - 3 just before branchAndBound (so user can override)
361       - 4 just after branchAndBound (before postprocessing)
362       - 5 after postprocessing
363       - 6 after a user called heuristic phase
364
365      \return 0 if good
366       nonzero return code to stop
367    */
368    virtual int callBack(CbcModel * /*currentSolver*/, int /*whereFrom*/) {
369        return 0;
370    }
371    //@}
372
373    ///@name Constructors and destructors etc
374    //@{
375    /// Default Constructor
376    CbcStopNow();
377
378    /** Copy constructor .
379     */
380    CbcStopNow(const CbcStopNow & rhs);
381
382    /// Assignment operator
383    CbcStopNow & operator=(const CbcStopNow& rhs);
384
385    /// Clone
386    virtual CbcStopNow * clone() const;
387
388    /// Destructor
389    virtual ~CbcStopNow ();
390    //@}
391
392private:
393    ///@name Private member data
394    //@{
395//@}
396};
397#endif
Note: See TracBrowser for help on using the repository browser.