source: trunk/Cbc/src/CbcEventHandler.hpp @ 1954

Last change on this file since 1954 was 1954, checked in by forrest, 6 years ago

new event plus flexible output format

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
RevLine 
[1600]1/*
[277]2  Copyright (C) 2006, International Business Machines Corporation and others.
3  All Rights Reserved.
[1573]4
5  This code is licensed under the terms of the Eclipse Public License (EPL).
6
7  $Id: CbcEventHandler.hpp 1954 2013-08-06 19:19:32Z forrest $
[277]8*/
9
10#ifndef CbcEventHandler_H
11#define CbcEventHandler_H
12
13/*! \file CbcEventHandler.hpp
14    \brief Event handling for cbc
15
16  This file contains the declaration of CbcEventHandler, used for event
17  handling in cbc.
18
19  The central method is CbcEventHandler::event(). The default semantics of
20  this call are `ask for the action to take in reponse to this event'. The
21  call is made at the point in the code where the event occurs (<i>e.g.</i>,
22  when a solution is found, or when a node is added to or removed from the
23  search tree). The return value specifies the action to perform in response
24  to the event (<i>e.g.</i>, continue, or stop).
25
26  This is a lazy class. Initially, it knows nothing about specific events,
27  and returns dfltAction_ for any event. This makes for a trivial constructor
28  and fast startup. The only place where the list of known events or actions
29  is hardwired is in the enum definitions for CbcEvent and CbcAction,
30  respectively.
31
32  At the first call to setAction, a map is created to hold (Event,Action)
33  pairs, and this map will be consulted ever after. Events not in the map
34  will still return the default value.
35
36  For serious extensions, derive a subclass and replace event() with a
37  function that suits you better.  The function has access to the CbcModel
38  via a pointer held in the CbcEventHandler object, and can do as much
39  thinking as it likes before returning an answer.  You can also print as
40  much information as you want. The model is held as a const, however, so
41  you can't alter reality.
42
43  The design of the class deliberately matches ClpEventHandler, so that other
44  solvers can participate in cbc without breaking the patterns set by
45  clp-specific code.
46
47*/
48
49#include <map>
50
51/* May well already be declared, but can't hurt. */
52
53class CbcModel ;
54
55/*
56  cvs/svn: $Id: CbcEventHandler.hpp 1954 2013-08-06 19:19:32Z forrest $
57*/
58
59/*! \class CbcEventHandler
60    \brief Base class for Cbc event handling.
61
62  Up front: We're not talking about unanticipated events here. We're talking
63  about anticipated events, in the sense that the code is going to make a call
64  to event() and is prepared to obey the return value that it receives.
65
66  The general pattern for usage is as follows:
67  <ol>
68    <li> Create a CbcEventHandler object. This will be initialised with a set
69         of default actions for every recognised event.
70
71    <li> Attach the event handler to the CbcModel object.
72
73    <li> When execution reaches the point where an event occurs, call the
74         event handler as CbcEventHandler::event(the event). The return value
75         will specify what the code should do in response to the event.
76  </ol>
77
78  The return value associated with an event can be changed at any time.
79*/
80
[1286]81class CbcEventHandler {
82
[277]83public:
84
[1286]85    /*! \brief Events known to cbc */
[277]86
[1286]87    enum CbcEvent { /*! Processing of the current node is complete. */
88        node = 200,
89        /*! A tree status interval has arrived. */
90        treeStatus,
91        /*! A solution has been found. */
92        solution,
93        /*! A heuristic solution has been found. */
94        heuristicSolution,
95        /*! A solution will be found unless user takes action (first check). */
96        beforeSolution1,
97        /*! A solution will be found unless user takes action (thorough check). */
98        beforeSolution2,
[1590]99        /*! After failed heuristic. */
100        afterHeuristic,
[1943]101        /*! On entry to small branch and bound. */
102        smallBranchAndBound,
[1954]103        /*! After a pass of heuristic. */
104        heuristicPass,
[1286]105        /*! End of search. */
106        endSearch
107    } ;
[277]108
[1286]109    /*! \brief Action codes returned by the event handler.
[277]110
[1286]111        Specific values are chosen to match ClpEventHandler return codes.
112    */
[277]113
[1286]114    enum CbcAction { /*! Continue --- no action required. */
115        noAction = -1,
116        /*! Stop --- abort the current run at the next opportunity. */
117        stop = 0,
118        /*! Restart --- restart branch-and-cut search; do not undo root node
119        processing.
120        */
121        restart,
122        /*! RestartRoot --- undo root node and start branch-and-cut afresh. */
123        restartRoot,
124        /*! Add special cuts. */
125        addCuts,
126        /*! Pretend solution never happened. */
[1943]127        killSolution,
128        /*! Take action on modified data. */
129        takeAction
[1286]130
131    } ;
132
133    /*! \brief Data type for event/action pairs */
134
135    typedef std::map<CbcEvent, CbcAction> eaMapPair ;
136
137
138    /*! \name Event Processing */
139    //@{
140
141    /*! \brief Return the action to be taken for an event.
142
143      Return the action that should be taken in response to the event passed as
144      the parameter. The default implementation simply reads a return code
145      from a map.
[277]146    */
[1286]147    virtual CbcAction event(CbcEvent whichEvent) ;
[277]148
[1943]149    /*! \brief Return the action to be taken for an event - and modify data.
150
151      Return the action that should be taken in response to the event passed as
152      the parameter. The default implementation simply reads a return code
153      from a map.
154    */
155    virtual CbcAction event(CbcEvent whichEvent, void * data) ;
156
[1286]157    //@}
[277]158
159
[1286]160    /*! \name Constructors and destructors */
161    //@{
[277]162
[1286]163    /*! \brief Default constructor. */
[277]164
[1632]165    CbcEventHandler(CbcModel *model = 0 /* was NULL but 4.6 complains */) ;
[277]166
[1286]167    /*! \brief Copy constructor. */
[277]168
[1286]169    CbcEventHandler(const CbcEventHandler &orig) ;
[277]170
[1286]171    /*! \brief Assignment. */
[277]172
[1286]173    CbcEventHandler& operator=(const CbcEventHandler &rhs) ;
[277]174
[1286]175    /*! \brief Clone (virtual) constructor. */
[277]176
[1286]177    virtual CbcEventHandler* clone() const ;
[277]178
[1286]179    /*! \brief Destructor. */
[277]180
[1286]181    virtual ~CbcEventHandler() ;
[277]182
[1286]183    //@}
[277]184
[1286]185    /*! \name Set/Get methods */
186    //@{
[277]187
[1286]188    /*! \brief Set model. */
[277]189
[1286]190    inline void setModel(CbcModel *model) {
191        model_ = model ;
192    }
[277]193
[1286]194    /*! \brief Get model. */
[277]195
[1286]196    inline const CbcModel* getModel() const {
197        return model_ ;
198    }
[277]199
[1286]200    /*! \brief Set the default action */
[277]201
[1286]202    inline void setDfltAction(CbcAction action) {
203        dfltAction_ = action ;
204    }
[277]205
[1286]206    /*! \brief Set the action code associated with an event */
[277]207
[1286]208    inline void setAction(CbcEvent event, CbcAction action) {
209        if (eaMap_ == 0) {
210            eaMap_ = new eaMapPair ;
211        }
212        (*eaMap_)[event] = action ;
213    }
[277]214
[1286]215    //@}
[277]216
217
218protected:
219
[1286]220    /*! \name Data members
[277]221
[1286]222       Protected (as opposed to private) to allow access by derived classes.
223    */
224    //@{
[277]225
[1286]226    /*! \brief Pointer to associated CbcModel */
[277]227
[1286]228    CbcModel *model_ ;
[277]229
[1286]230    /*! \brief Default action */
[277]231
[1286]232    CbcAction dfltAction_ ;
[277]233
[1286]234    /*! \brief Pointer to a map that holds non-default event/action pairs */
[277]235
[1286]236    eaMapPair *eaMap_ ;
[277]237
[1286]238    //@}
[277]239} ;
240
241#endif
[1432]242
Note: See TracBrowser for help on using the repository browser.