source: stable/2.4/Cbc/src/CbcEventHandler.hpp @ 1271

Last change on this file since 1271 was 1271, checked in by forrest, 10 years ago

Creating new stable branch 2.4 from trunk (rev 1270)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 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 1271 2009-11-05 15:57:25Z 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    /*! A solution will be found unless user takes action (first check). */
94    beforeSolution1,
95    /*! A solution will be found unless user takes action (thorough check). */
96    beforeSolution2,
97    /*! End of search. */
98    endSearch
99  } ;
100
101  /*! \brief Action codes returned by the event handler.
102
103      Specific values are chosen to match ClpEventHandler return codes.
104  */
105
106  enum CbcAction
107  { /*! Continue --- no action required. */
108    noAction = -1,
109    /*! Stop --- abort the current run at the next opportunity. */
110    stop = 0,
111    /*! Restart --- restart branch-and-cut search; do not undo root node
112        processing.
113    */
114    restart,
115    /*! RestartRoot --- undo root node and start branch-and-cut afresh. */
116    restartRoot,
117    /*! Add special cuts. */
118    addCuts,
119    /*! Pretend solution never happened. */
120    killSolution
121     
122  } ;
123
124  /*! \brief Data type for event/action pairs */
125
126  typedef std::map<CbcEvent,CbcAction> eaMapPair ;
127
128
129  /*! \name Event Processing */
130  //@{
131
132  /*! \brief Return the action to be taken for an event.
133 
134    Return the action that should be taken in response to the event passed as
135    the parameter. The default implementation simply reads a return code
136    from a map.
137  */
138  virtual CbcAction event(CbcEvent whichEvent) ;
139
140  //@}
141 
142 
143  /*! \name Constructors and destructors */
144  //@{
145
146  /*! \brief Default constructor. */
147
148  CbcEventHandler(CbcModel *model = NULL) ;
149
150  /*! \brief Copy constructor. */
151
152  CbcEventHandler(const CbcEventHandler &orig) ;
153
154  /*! \brief Assignment. */
155
156  CbcEventHandler& operator=(const CbcEventHandler &rhs) ;
157
158  /*! \brief Clone (virtual) constructor. */
159
160  virtual CbcEventHandler* clone() const ;
161
162  /*! \brief Destructor. */
163
164  virtual ~CbcEventHandler() ;
165
166  //@}
167 
168  /*! \name Set/Get methods */
169  //@{
170
171  /*! \brief Set model. */
172
173  inline void setModel(CbcModel *model)
174  { model_ = model ; }
175
176  /*! \brief Get model. */
177
178  inline const CbcModel* getModel() const
179  { return model_ ; } 
180
181  /*! \brief Set the default action */
182
183  inline void setDfltAction(CbcAction action)
184  { dfltAction_ = action ; }
185
186  /*! \brief Set the action code associated with an event */
187
188  inline void setAction(CbcEvent event, CbcAction action)
189  { if (eaMap_ == 0)
190    { eaMap_ = new eaMapPair ; }
191    (*eaMap_)[event] = action ; }
192
193  //@}
194 
195 
196protected:
197
198  /*! \name Data members
199
200     Protected (as opposed to private) to allow access by derived classes.
201  */
202  //@{
203
204  /*! \brief Pointer to associated CbcModel */
205
206  CbcModel *model_ ;
207
208  /*! \brief Default action */
209
210  CbcAction dfltAction_ ;
211
212  /*! \brief Pointer to a map that holds non-default event/action pairs */
213
214  eaMapPair *eaMap_ ;
215
216  //@}
217} ;
218
219#endif
Note: See TracBrowser for help on using the repository browser.