Scippy

SCIP

Solving Constraint Integer Programs

event_bestsol.c
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-2018 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 examples/Eventhdlr/src/event_bestsol.c
17  * @brief eventhdlr for best solution found
18  * @author Stefan Heinz
19  * @author Michael Winkler
20  */
21 
22 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "event_bestsol.h"
25 
26 #include <string.h>
27 
28 #define EVENTHDLR_NAME "bestsol"
29 #define EVENTHDLR_DESC "event handler for best solutions found"
30 
31 /** copy method for event handler plugins (called when SCIP copies plugins) */
32 static
33 SCIP_DECL_EVENTCOPY(eventCopyBestsol)
34 { /*lint --e{715}*/
35  assert(scip != NULL);
36  assert(eventhdlr != NULL);
37  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
38 
39  /* call inclusion method of event handler */
41 
42  return SCIP_OKAY;
43 }
44 
45 /** initialization method of event handler (called after problem was transformed) */
46 static
47 SCIP_DECL_EVENTINIT(eventInitBestsol)
48 { /*lint --e{715}*/
49  assert(scip != NULL);
50  assert(eventhdlr != NULL);
51  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
52 
53  /* notify SCIP that your event handler wants to react on the event type best solution found */
55 
56  return SCIP_OKAY;
57 }
58 
59 /** deinitialization method of event handler (called before transformed problem is freed) */
60 static
61 SCIP_DECL_EVENTEXIT(eventExitBestsol)
62 { /*lint --e{715}*/
63  assert(scip != NULL);
64  assert(eventhdlr != NULL);
65  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
66 
67  /* notify SCIP that your event handler wants to drop the event type best solution found */
69 
70  return SCIP_OKAY;
71 }
72 
73 /** execution method of event handler */
74 static
75 SCIP_DECL_EVENTEXEC(eventExecBestsol)
76 { /*lint --e{715}*/
77  SCIP_SOL* bestsol;
78  SCIP_Real solvalue;
79 
80  assert(eventhdlr != NULL);
81  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
82  assert(event != NULL);
83  assert(scip != NULL);
85 
86  SCIPdebugMsg(scip, "exec method of event handler for best solution found\n");
87 
88  bestsol = SCIPgetBestSol(scip);
89  assert(bestsol != NULL);
90  solvalue = SCIPgetSolOrigObj(scip, bestsol);
91 
92  /* print best solution value */
93  SCIPinfoMessage(scip, NULL, "found new best solution with solution value <%g> in SCIP <%s>\n",
94  solvalue, SCIPgetProbName(scip) );
95 
96  return SCIP_OKAY;
97 }
98 
99 /** includes event handler for best solution found */
101  SCIP* scip /**< SCIP data structure */
102  )
103 {
104  SCIP_EVENTHDLRDATA* eventhdlrdata;
105  SCIP_EVENTHDLR* eventhdlr;
106  eventhdlrdata = NULL;
107 
108  eventhdlr = NULL;
109  /* create event handler for events on watched variables */
110  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecBestsol, eventhdlrdata) );
111  assert(eventhdlr != NULL);
112 
113  SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopyBestsol) );
114  SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitBestsol) );
115  SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitBestsol) );
116 
117  return SCIP_OKAY;
118 }
#define NULL
Definition: def.h:239
#define EVENTHDLR_NAME
Definition: event_bestsol.c:28
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
Definition: scip_event.c:246
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip_event.c:172
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:138
static SCIP_DECL_EVENTINIT(eventInitBestsol)
Definition: event_bestsol.c:47
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:314
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define SCIPdebugMsg
Definition: scip_message.h:88
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:279
const char * SCIPgetProbName(SCIP *scip)
Definition: scip_prob.c:1123
SCIP_RETCODE SCIPsetEventhdlrCopy(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTCOPY((*eventcopy)))
Definition: scip_event.c:204
SCIP_RETCODE SCIPsetEventhdlrInit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTINIT((*eventinit)))
Definition: scip_event.c:232
#define SCIP_CALL(x)
Definition: def.h:351
static SCIP_DECL_EVENTEXEC(eventExecBestsol)
Definition: event_bestsol.c:75
#define EVENTHDLR_DESC
Definition: event_bestsol.c:29
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:354
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:995
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:388
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1493
static SCIP_DECL_EVENTCOPY(eventCopyBestsol)
Definition: event_bestsol.c:33
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:88
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2379
static SCIP_DECL_EVENTEXIT(eventExitBestsol)
Definition: event_bestsol.c:61
#define SCIP_Real
Definition: def.h:150
SCIP_RETCODE SCIPincludeEventHdlrBestsol(SCIP *scip)
eventhdlr for best solution found