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

Last change on this file since 1902 was 1902, checked in by stefan, 5 years ago

sync with trunk rev 1901

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1/* $Id: CbcSolver.hpp 1902 2013-04-10 16:58:16Z stefan $ */
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 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 --
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} CbcSolverUsefulData;
254
255
256/*! \brief A class to allow the use of unknown user functionality
257
258    For example, access to a modelling language (CbcAmpl).
259*/
260class CbcUser {
261
262public:
263    ///@name import/export methods
264    //@{
265    /*! \brief Import - gets full command arguments
266
267      \return
268      - -1 - no action
269      -  0 - data read in without error
270      -  1 - errors
271    */
272    virtual int importData(CbcSolver * /*model*/, int & /*argc*/, char ** /*argv[]*/) {
273        return -1;
274    }
275
276    /*! \brief Export
277
278      Values for mode:
279      - 1 OsiClpSolver
280      - 2 CbcModel
281      - add 10 if infeasible from odd situation
282    */
283    virtual void exportSolution(CbcSolver * /*model*/,
284                                int /*mode*/, const char * /*message*/ = NULL) {}
285
286    /// Export Data (i.e. at very end)
287    virtual void exportData(CbcSolver * /*model*/) {}
288
289    /// Get useful stuff
290    virtual void fillInformation(CbcSolver * /*model*/,
291                                 CbcSolverUsefulData & /*info*/) {}
292    //@}
293
294    ///@name usage methods
295    //@{
296    /// CoinModel if valid
297    inline CoinModel *coinModel() const {
298        return coinModel_;
299    }
300    /// Other info - needs expanding
301    virtual void * stuff() {
302        return NULL;
303    }
304    /// Name
305    inline std::string name() const {
306        return userName_;
307    }
308    /// Solve (whatever that means)
309    virtual void solve(CbcSolver * model, const char * options) = 0;
310    /// Returns true if function knows about option
311    virtual bool canDo(const char * options) = 0;
312    //@}
313
314    ///@name Constructors and destructors etc
315    //@{
316    /// Default Constructor
317    CbcUser();
318
319    /// Copy constructor
320    CbcUser(const CbcUser & rhs);
321
322    /// Assignment operator
323    CbcUser & operator=(const CbcUser& rhs);
324
325    /// Clone
326    virtual CbcUser * clone() const = 0;
327
328    /// Destructor
329    virtual ~CbcUser ();
330    //@}
331
332protected:
333    ///@name Private member data
334    //@{
335
336    /// CoinModel
337    CoinModel * coinModel_;
338
339    /// Name of user function
340    std::string userName_;
341
342//@}
343};
344//#############################################################################
345
346/*! \brief Support the use of a call back class to decide whether to stop
347
348  Definitely under construction.
349*/
350
351class CbcStopNow {
352
353public:
354    ///@name Decision methods
355    //@{
356    /*! \brief Import
357
358      Values for whereFrom:
359       - 1 after initial solve by dualsimplex etc
360       - 2 after preprocessing
361       - 3 just before branchAndBound (so user can override)
362       - 4 just after branchAndBound (before postprocessing)
363       - 5 after postprocessing
364       - 6 after a user called heuristic phase
365
366      \return 0 if good
367       nonzero return code to stop
368    */
369    virtual int callBack(CbcModel * /*currentSolver*/, int /*whereFrom*/) {
370        return 0;
371    }
372    //@}
373
374    ///@name Constructors and destructors etc
375    //@{
376    /// Default Constructor
377    CbcStopNow();
378
379    /** Copy constructor .
380     */
381    CbcStopNow(const CbcStopNow & rhs);
382
383    /// Assignment operator
384    CbcStopNow & operator=(const CbcStopNow& rhs);
385
386    /// Clone
387    virtual CbcStopNow * clone() const;
388
389    /// Destructor
390    virtual ~CbcStopNow ();
391    //@}
392
393private:
394    ///@name Private member data
395    //@{
396//@}
397};
398#endif
399
Note: See TracBrowser for help on using the repository browser.