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

Last change on this file since 862 was 741, checked in by forrest, 12 years ago

event handling

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