source: branches/devel/Cbc/src/CbcEventHandler.hpp @ 417

Last change on this file since 417 was 277, checked in by lou, 14 years ago

Revise cbc build process for better control of solvers included in build
(COIN_USE_XXX -> CBC_USE_XXX). Add CbcEventHandler? for independence from
clp.

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