Scippy

SCIP

Solving Constraint Integer Programs

objeventhdlr.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License. */
12 /* along with SCIP; see the file COPYING. If not visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file objeventhdlr.cpp
17  * @brief C++ wrapper for event handlers
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <cassert>
24 
25 #include "objeventhdlr.h"
26 
27 
28 
29 
30 /*
31  * Data structures
32  */
33 
34 /** event handler data */
35 struct SCIP_EventhdlrData
36 {
37  scip::ObjEventhdlr* objeventhdlr; /**< event handler object */
38  SCIP_Bool deleteobject; /**< should the event handler object be deleted when eventhdlristic is freed? */
39 };
40 
41 
42 
43 
44 /*
45  * Callback methods of event handler
46  */
47 
48 extern "C"
49 {
50 
51 /** copy method for event handler plugins (called when SCIP copies plugins) */
52 static
53 SCIP_DECL_EVENTCOPY(eventhdlrCopyObj)
54 { /*lint --e{715}*/
55  SCIP_EVENTHDLRDATA* eventhdlrdata;
56 
57  assert(scip != NULL);
58 
59  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
60  assert(eventhdlrdata != NULL);
61  assert(eventhdlrdata->objeventhdlr != NULL);
62  assert(eventhdlrdata->objeventhdlr->scip_ != scip);
63 
64  if( eventhdlrdata->objeventhdlr->iscloneable() )
65  {
66  scip::ObjEventhdlr* newobjeventhdlr;
67  newobjeventhdlr = dynamic_cast<scip::ObjEventhdlr*> (eventhdlrdata->objeventhdlr->clone(scip));
68 
69  /* call include method of event handler object */
70  SCIP_CALL( SCIPincludeObjEventhdlr(scip, newobjeventhdlr, TRUE) );
71  }
72 
73  return SCIP_OKAY;
74 }
75 
76 /** destructor of event handler to free user data (called when SCIP is exiting) */
77 static
78 SCIP_DECL_EVENTFREE(eventhdlrFreeObj)
79 { /*lint --e{715}*/
80  SCIP_EVENTHDLRDATA* eventhdlrdata;
81 
82  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
83  assert(eventhdlrdata != NULL);
84  assert(eventhdlrdata->objeventhdlr != NULL);
85  assert(eventhdlrdata->objeventhdlr->scip_ == scip);
86 
87  /* call virtual method of eventhdlr object */
88  SCIP_CALL( eventhdlrdata->objeventhdlr->scip_free(scip, eventhdlr) );
89 
90  /* free eventhdlr object */
91  if( eventhdlrdata->deleteobject )
92  delete eventhdlrdata->objeventhdlr;
93 
94  /* free eventhdlr data */
95  delete eventhdlrdata;
96  SCIPeventhdlrSetData(eventhdlr, NULL); /*lint !e64*/
97 
98  return SCIP_OKAY;
99 }
100 
101 
102 /** initialization method of event handler (called after problem was transformed) */
103 static
104 SCIP_DECL_EVENTINIT(eventhdlrInitObj)
105 { /*lint --e{715}*/
106  SCIP_EVENTHDLRDATA* eventhdlrdata;
107 
108  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
109  assert(eventhdlrdata != NULL);
110  assert(eventhdlrdata->objeventhdlr != NULL);
111  assert(eventhdlrdata->objeventhdlr->scip_ == scip);
112 
113  /* call virtual method of eventhdlr object */
114  SCIP_CALL( eventhdlrdata->objeventhdlr->scip_init(scip, eventhdlr) );
115 
116  return SCIP_OKAY;
117 }
118 
119 
120 /** deinitialization method of event handler (called before transformed problem is freed) */
121 static
122 SCIP_DECL_EVENTEXIT(eventhdlrExitObj)
123 { /*lint --e{715}*/
124  SCIP_EVENTHDLRDATA* eventhdlrdata;
125 
126  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
127  assert(eventhdlrdata != NULL);
128  assert(eventhdlrdata->objeventhdlr != NULL);
129 
130  /* call virtual method of eventhdlr object */
131  SCIP_CALL( eventhdlrdata->objeventhdlr->scip_exit(scip, eventhdlr) );
132 
133  return SCIP_OKAY;
134 }
135 
136 
137 /** solving process initialization method of event handler (called when branch and bound process is about to begin) */
138 static
139 SCIP_DECL_EVENTINITSOL(eventhdlrInitsolObj)
140 { /*lint --e{715}*/
141  SCIP_EVENTHDLRDATA* eventhdlrdata;
142 
143  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
144  assert(eventhdlrdata != NULL);
145  assert(eventhdlrdata->objeventhdlr != NULL);
146 
147  /* call virtual method of eventhdlr object */
148  SCIP_CALL( eventhdlrdata->objeventhdlr->scip_initsol(scip, eventhdlr) );
149 
150  return SCIP_OKAY;
151 }
152 
153 
154 /** solving process deinitialization method of event handler (called before branch and bound process data is freed) */
155 static
156 SCIP_DECL_EVENTEXITSOL(eventhdlrExitsolObj)
157 { /*lint --e{715}*/
158  SCIP_EVENTHDLRDATA* eventhdlrdata;
159 
160  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
161  assert(eventhdlrdata != NULL);
162  assert(eventhdlrdata->objeventhdlr != NULL);
163 
164  /* call virtual method of eventhdlr object */
165  SCIP_CALL( eventhdlrdata->objeventhdlr->scip_exitsol(scip, eventhdlr) );
166 
167  return SCIP_OKAY;
168 }
169 
170 
171 /** frees specific constraint data */
172 static
173 SCIP_DECL_EVENTDELETE(eventhdlrDeleteObj)
174 { /*lint --e{715}*/
175  SCIP_EVENTHDLRDATA* eventhdlrdata;
176 
177  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
178  assert(eventhdlrdata != NULL);
179  assert(eventhdlrdata->objeventhdlr != NULL);
180 
181  /* call virtual method of eventhdlr object */
182  SCIP_CALL( eventhdlrdata->objeventhdlr->scip_delete(scip, eventhdlr, eventdata) );
183 
184  return SCIP_OKAY;
185 }
186 
187 
188 /** execution method of event handler */
189 static
190 SCIP_DECL_EVENTEXEC(eventhdlrExecObj)
191 { /*lint --e{715}*/
192  SCIP_EVENTHDLRDATA* eventhdlrdata;
193 
194  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
195  assert(eventhdlrdata != NULL);
196  assert(eventhdlrdata->objeventhdlr != NULL);
197 
198  /* call virtual method of eventhdlr object */
199  SCIP_CALL( eventhdlrdata->objeventhdlr->scip_exec(scip, eventhdlr, event, eventdata) );
200 
201  return SCIP_OKAY;
202 }
203 }
204 
205 
206 
207 /*
208  * event handler specific interface methods
209  */
210 
211 /** creates the event handler for the given event handler object and includes it in SCIP */
213  SCIP* scip, /**< SCIP data structure */
214  scip::ObjEventhdlr* objeventhdlr, /**< event handler object */
215  SCIP_Bool deleteobject /**< should the event handler object be deleted when eventhdlristic is freed? */
216  )
217 {
218  SCIP_EVENTHDLRDATA* eventhdlrdata;
219 
220  assert(scip != NULL);
221  assert(objeventhdlr != NULL);
222 
223  /* create event handler data */
224  eventhdlrdata = new SCIP_EVENTHDLRDATA;
225  eventhdlrdata->objeventhdlr = objeventhdlr;
226  eventhdlrdata->deleteobject = deleteobject;
227 
228  /* include event handler */
229  SCIP_CALL( SCIPincludeEventhdlr(scip, objeventhdlr->scip_name_, objeventhdlr->scip_desc_,
230  eventhdlrCopyObj,
231  eventhdlrFreeObj, eventhdlrInitObj, eventhdlrExitObj,
232  eventhdlrInitsolObj, eventhdlrExitsolObj, eventhdlrDeleteObj, eventhdlrExecObj,
233  eventhdlrdata) ); /*lint !e429*/
234 
235  return SCIP_OKAY; /*lint !e429*/
236 }
237 
238 /** returns the eventhdlr object of the given name, or 0 if not existing */
240  SCIP* scip, /**< SCIP data structure */
241  const char* name /**< name of event handler */
242  )
243 {
244  SCIP_EVENTHDLR* eventhdlr;
245  SCIP_EVENTHDLRDATA* eventhdlrdata;
246 
247  eventhdlr = SCIPfindEventhdlr(scip, name);
248  if( eventhdlr == NULL )
249  return 0;
250 
251  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
252  assert(eventhdlrdata != NULL);
253 
254  return eventhdlrdata->objeventhdlr;
255 }
256 
257 /** returns the eventhdlr object for the given event handler */
259  SCIP* scip, /**< SCIP data structure */
260  SCIP_EVENTHDLR* eventhdlr /**< event handler */
261  )
262 {
263  SCIP_EVENTHDLRDATA* eventhdlrdata;
264 
265  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
266  assert(eventhdlrdata != NULL);
267 
268  return eventhdlrdata->objeventhdlr;
269 }
void SCIPeventhdlrSetData(SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: event.c:334
#define NULL
Definition: def.h:253
static SCIP_DECL_EVENTEXIT(eventhdlrExitObj)
SCIP_RETCODE SCIPincludeObjEventhdlr(SCIP *scip, scip::ObjEventhdlr *objeventhdlr, SCIP_Bool deleteobject)
SCIP_EVENTHDLR * SCIPfindEventhdlr(SCIP *scip, const char *name)
Definition: scip_event.c:224
C++ wrapper for event handlers.
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:138
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
static SCIP_DECL_EVENTEXITSOL(eventhdlrExitsolObj)
static SCIP_DECL_EVENTINIT(eventhdlrInitObj)
SCIP_EVENTHDLRDATA * SCIPeventhdlrGetData(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:324
static SCIP_DECL_EVENTINITSOL(eventhdlrInitsolObj)
static SCIP_DECL_EVENTEXEC(eventhdlrExecObj)
scip::ObjEventhdlr * SCIPgetObjEventhdlr(SCIP *scip, SCIP_EVENTHDLR *eventhdlr)
static SCIP_DECL_EVENTCOPY(eventhdlrCopyObj)
#define SCIP_CALL(x)
Definition: def.h:365
scip::ObjEventhdlr * SCIPfindObjEventhdlr(SCIP *scip, const char *name)
#define SCIP_Bool
Definition: def.h:70
C++ wrapper for event handlers.
Definition: objeventhdlr.h:42
SCIP_RETCODE SCIPincludeEventhdlr(SCIP *scip, const char *name, const char *desc, SCIP_DECL_EVENTCOPY((*eventcopy)), SCIP_DECL_EVENTFREE((*eventfree)), SCIP_DECL_EVENTINIT((*eventinit)), SCIP_DECL_EVENTEXIT((*eventexit)), SCIP_DECL_EVENTINITSOL((*eventinitsol)), SCIP_DECL_EVENTEXITSOL((*eventexitsol)), SCIP_DECL_EVENTDELETE((*eventdelete)), SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip_event.c:53
static SCIP_DECL_EVENTFREE(eventhdlrFreeObj)
static SCIP_DECL_EVENTDELETE(eventhdlrDeleteObj)