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

Last change on this file since 1573 was 1573, checked in by lou, 8 years ago

Change to EPL license notice.

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