1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2021 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 scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15
16 /**@file event_newsol.c
17 * @brief eventhdlr that adds new solutions to the candidate pool for the exchange heuristic
18 * @author Leon Eifler
19 */
20
21 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22
23 #include <assert.h>
24 #include <string.h>
25
26 #include "event_newsol.h"
27 #include "heur_cyckerlin.h"
28 #include "probdata_cyc.h"
29
30 #define EVENTHDLR_NAME "newsol"
31 #define EVENTHDLR_DESC "event handler for solution events"
32
33 /** copy method for event handler plugins (called when SCIP copies plugins) */
34 static
SCIP_DECL_EVENTCOPY(eventCopyNewsol)35 SCIP_DECL_EVENTCOPY(eventCopyNewsol)
36 { /*lint --e{715}*/
37 assert(scip != NULL);
38 assert(eventhdlr != NULL);
39 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
40
41 /* call inclusion method of event handler */
42 SCIP_CALL( SCIPincludeEventHdlrNewsol(scip) );
43
44 return SCIP_OKAY;
45 }
46
47 /** initialization method of event handler (called after problem was transformed) */
48 static
SCIP_DECL_EVENTINIT(eventInitNewsol)49 SCIP_DECL_EVENTINIT(eventInitNewsol)
50 { /*lint --e{715}*/
51 assert(scip != NULL);
52 assert(eventhdlr != NULL);
53 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
54
55 /* notify SCIP that your event handler wants to react on the event type best solution found */
56 SCIP_CALL( SCIPcatchEvent( scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, NULL) );
57
58 return SCIP_OKAY;
59 }
60
61 /** deinitialization method of event handler (called before transformed problem is freed) */
62 static
SCIP_DECL_EVENTEXIT(eventExitNewsol)63 SCIP_DECL_EVENTEXIT(eventExitNewsol)
64 { /*lint --e{715}*/
65 assert(scip != NULL);
66 assert(eventhdlr != NULL);
67 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
68
69 /* notify SCIP that your event handler wants to drop the event type best solution found */
70 SCIP_CALL( SCIPdropEvent( scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, -1) );
71
72 return SCIP_OKAY;
73 }
74
75 /** execution method of event handler */
76 static
SCIP_DECL_EVENTEXEC(eventExecNewsol)77 SCIP_DECL_EVENTEXEC(eventExecNewsol)
78 { /*lint --e{715}*/
79 SCIP_SOL* newsol;
80
81 assert(eventhdlr != NULL);
82 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
83 assert(event != NULL);
84 assert(scip != NULL);
85 assert(SCIPeventGetType(event) == SCIP_EVENTTYPE_BESTSOLFOUND);
86
87 if( SCIPgetStage(scip) != SCIP_STAGE_SOLVING )
88 return SCIP_OKAY;
89
90 SCIPdebugMsg(scip, "exec method of event handler for newsol solution found\n");
91
92 newsol = SCIPeventGetSol(event);
93 assert(newsol != NULL);
94
95 SCIPdebugMsg(scip, "catch event for solution %p with obj=%g.\n", (void*) newsol, SCIPgetSolOrigObj(scip, newsol));
96
97 SCIP_CALL( addCandSolCyckerlin(scip, newsol) );
98
99 return SCIP_OKAY;
100 }
101
102 /** includes event handler for best solution found */
SCIPincludeEventHdlrNewsol(SCIP * scip)103 SCIP_RETCODE SCIPincludeEventHdlrNewsol(
104 SCIP* scip /**< SCIP data structure */
105 )
106 {
107 SCIP_EVENTHDLRDATA* eventhdlrdata;
108 SCIP_EVENTHDLR* eventhdlr;
109 eventhdlrdata = NULL;
110
111 eventhdlr = NULL;
112 /* create event handler for events on watched variables */
113 SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC,
114 eventExecNewsol, eventhdlrdata) );
115
116 assert(eventhdlr != NULL);
117
118 SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopyNewsol) );
119 SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitNewsol) );
120 SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitNewsol) );
121
122 return SCIP_OKAY;
123 }
124