1 | /*! \legal |
---|
2 | Copyright (C) 2006, International Business Machines Corporation and others. |
---|
3 | All Rights Reserved. |
---|
4 | |
---|
5 | This code is licensed under the terms of the Eclipse Public License (EPL). |
---|
6 | |
---|
7 | $Id: CbcEventHandler.hpp 1590 2011-02-03 15:46:48Z forrest $ |
---|
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 | |
---|
53 | class CbcModel ; |
---|
54 | |
---|
55 | /* |
---|
56 | cvs/svn: $Id: CbcEventHandler.hpp 1590 2011-02-03 15:46:48Z 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 | |
---|
81 | class CbcEventHandler { |
---|
82 | |
---|
83 | public: |
---|
84 | |
---|
85 | /*! \brief Events known to cbc */ |
---|
86 | |
---|
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, |
---|
99 | /*! After failed heuristic. */ |
---|
100 | afterHeuristic, |
---|
101 | /*! End of search. */ |
---|
102 | endSearch |
---|
103 | } ; |
---|
104 | |
---|
105 | /*! \brief Action codes returned by the event handler. |
---|
106 | |
---|
107 | Specific values are chosen to match ClpEventHandler return codes. |
---|
108 | */ |
---|
109 | |
---|
110 | enum CbcAction { /*! Continue --- no action required. */ |
---|
111 | noAction = -1, |
---|
112 | /*! Stop --- abort the current run at the next opportunity. */ |
---|
113 | stop = 0, |
---|
114 | /*! Restart --- restart branch-and-cut search; do not undo root node |
---|
115 | processing. |
---|
116 | */ |
---|
117 | restart, |
---|
118 | /*! RestartRoot --- undo root node and start branch-and-cut afresh. */ |
---|
119 | restartRoot, |
---|
120 | /*! Add special cuts. */ |
---|
121 | addCuts, |
---|
122 | /*! Pretend solution never happened. */ |
---|
123 | killSolution |
---|
124 | |
---|
125 | } ; |
---|
126 | |
---|
127 | /*! \brief Data type for event/action pairs */ |
---|
128 | |
---|
129 | typedef std::map<CbcEvent, CbcAction> eaMapPair ; |
---|
130 | |
---|
131 | |
---|
132 | /*! \name Event Processing */ |
---|
133 | //@{ |
---|
134 | |
---|
135 | /*! \brief Return the action to be taken for an event. |
---|
136 | |
---|
137 | Return the action that should be taken in response to the event passed as |
---|
138 | the parameter. The default implementation simply reads a return code |
---|
139 | from a map. |
---|
140 | */ |
---|
141 | virtual CbcAction event(CbcEvent whichEvent) ; |
---|
142 | |
---|
143 | //@} |
---|
144 | |
---|
145 | |
---|
146 | /*! \name Constructors and destructors */ |
---|
147 | //@{ |
---|
148 | |
---|
149 | /*! \brief Default constructor. */ |
---|
150 | |
---|
151 | CbcEventHandler(CbcModel *model = NULL) ; |
---|
152 | |
---|
153 | /*! \brief Copy constructor. */ |
---|
154 | |
---|
155 | CbcEventHandler(const CbcEventHandler &orig) ; |
---|
156 | |
---|
157 | /*! \brief Assignment. */ |
---|
158 | |
---|
159 | CbcEventHandler& operator=(const CbcEventHandler &rhs) ; |
---|
160 | |
---|
161 | /*! \brief Clone (virtual) constructor. */ |
---|
162 | |
---|
163 | virtual CbcEventHandler* clone() const ; |
---|
164 | |
---|
165 | /*! \brief Destructor. */ |
---|
166 | |
---|
167 | virtual ~CbcEventHandler() ; |
---|
168 | |
---|
169 | //@} |
---|
170 | |
---|
171 | /*! \name Set/Get methods */ |
---|
172 | //@{ |
---|
173 | |
---|
174 | /*! \brief Set model. */ |
---|
175 | |
---|
176 | inline void setModel(CbcModel *model) { |
---|
177 | model_ = model ; |
---|
178 | } |
---|
179 | |
---|
180 | /*! \brief Get model. */ |
---|
181 | |
---|
182 | inline const CbcModel* getModel() const { |
---|
183 | return model_ ; |
---|
184 | } |
---|
185 | |
---|
186 | /*! \brief Set the default action */ |
---|
187 | |
---|
188 | inline void setDfltAction(CbcAction action) { |
---|
189 | dfltAction_ = action ; |
---|
190 | } |
---|
191 | |
---|
192 | /*! \brief Set the action code associated with an event */ |
---|
193 | |
---|
194 | inline void setAction(CbcEvent event, CbcAction action) { |
---|
195 | if (eaMap_ == 0) { |
---|
196 | eaMap_ = new eaMapPair ; |
---|
197 | } |
---|
198 | (*eaMap_)[event] = action ; |
---|
199 | } |
---|
200 | |
---|
201 | //@} |
---|
202 | |
---|
203 | |
---|
204 | protected: |
---|
205 | |
---|
206 | /*! \name Data members |
---|
207 | |
---|
208 | Protected (as opposed to private) to allow access by derived classes. |
---|
209 | */ |
---|
210 | //@{ |
---|
211 | |
---|
212 | /*! \brief Pointer to associated CbcModel */ |
---|
213 | |
---|
214 | CbcModel *model_ ; |
---|
215 | |
---|
216 | /*! \brief Default action */ |
---|
217 | |
---|
218 | CbcAction dfltAction_ ; |
---|
219 | |
---|
220 | /*! \brief Pointer to a map that holds non-default event/action pairs */ |
---|
221 | |
---|
222 | eaMapPair *eaMap_ ; |
---|
223 | |
---|
224 | //@} |
---|
225 | } ; |
---|
226 | |
---|
227 | #endif |
---|
228 | |
---|