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-2024 Zuse Institute Berlin (ZIB) */
7 /* */
8 /* Licensed under the Apache License, Version 2.0 (the "License"); */
9 /* you may not use this file except in compliance with the License. */
10 /* You may obtain a copy of the License at */
11 /* */
12 /* http://www.apache.org/licenses/LICENSE-2.0 */
13 /* */
14 /* Unless required by applicable law or agreed to in writing, software */
15 /* distributed under the License is distributed on an "AS IS" BASIS, */
16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17 /* See the License for the specific language governing permissions and */
18 /* limitations under the License. */
19 /* */
20 /* You should have received a copy of the Apache-2.0 license */
21 /* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22 /* */
23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 
25 /**@file examples/Eventhdlr/src/event_bestsol.c
26  * @brief eventhdlr for best solution found
27  * @author Stefan Heinz
28  * @author Michael Winkler
29  */
30 
31 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32 
33 #include "event_bestsol.h"
34 
35 #include <string.h>
36 
37 #define EVENTHDLR_NAME "bestsol"
38 #define EVENTHDLR_DESC "event handler for best solutions found"
39 
40 /** copy method for event handler plugins (called when SCIP copies plugins) */
41 static
42 SCIP_DECL_EVENTCOPY(eventCopyBestsol)
43 { /*lint --e{715}*/
44  assert(scip != NULL);
45  assert(eventhdlr != NULL);
46  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
47 
48  /* call inclusion method of event handler */
50 
51  return SCIP_OKAY;
52 }
53 
54 /** initialization method of event handler (called after problem was transformed) */
55 static
56 SCIP_DECL_EVENTINIT(eventInitBestsol)
57 { /*lint --e{715}*/
58  assert(scip != NULL);
59  assert(eventhdlr != NULL);
60  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
61 
62  /* notify SCIP that your event handler wants to react on the event type best solution found */
64 
65  return SCIP_OKAY;
66 }
67 
68 /** deinitialization method of event handler (called before transformed problem is freed) */
69 static
70 SCIP_DECL_EVENTEXIT(eventExitBestsol)
71 { /*lint --e{715}*/
72  assert(scip != NULL);
73  assert(eventhdlr != NULL);
74  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
75 
76  /* notify SCIP that your event handler wants to drop the event type best solution found */
78 
79  return SCIP_OKAY;
80 }
81 
82 /** execution method of event handler */
83 static
84 SCIP_DECL_EVENTEXEC(eventExecBestsol)
85 { /*lint --e{715}*/
86  SCIP_SOL* bestsol;
87  SCIP_Real solvalue;
88 
89  assert(eventhdlr != NULL);
90  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
91  assert(event != NULL);
92  assert(scip != NULL);
94 
95  SCIPdebugMsg(scip, "exec method of event handler for best solution found\n");
96 
97  bestsol = SCIPgetBestSol(scip);
98  assert(bestsol != NULL);
99  solvalue = SCIPgetSolOrigObj(scip, bestsol);
100 
101  /* print best solution value */
102  SCIPinfoMessage(scip, NULL, "found new best solution with solution value <%g> in SCIP <%s>\n",
103  solvalue, SCIPgetProbName(scip) );
104 
105  return SCIP_OKAY;
106 }
107 
108 /** includes event handler for best solution found */
110  SCIP* scip /**< SCIP data structure */
111  )
112 {
113  SCIP_EVENTHDLRDATA* eventhdlrdata;
114  SCIP_EVENTHDLR* eventhdlr;
115  eventhdlrdata = NULL;
116 
117  eventhdlr = NULL;
118  /* create event handler for events on watched variables */
119  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecBestsol, eventhdlrdata) );
120  assert(eventhdlr != NULL);
121 
122  SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopyBestsol) );
123  SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitBestsol) );
124  SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitBestsol) );
125 
126  return SCIP_OKAY;
127 }
#define NULL
Definition: def.h:267
#define EVENTHDLR_NAME
Definition: event_bestsol.c:37
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
Definition: scip_event.c:178
static SCIP_DECL_EVENTINIT(eventInitBestsol)
Definition: event_bestsol.c:56
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:104
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:155
static SCIP_DECL_EVENTEXIT(eventExitBestsol)
Definition: event_bestsol.c:70
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:324
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
#define SCIPdebugMsg
Definition: scip_message.h:78
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:208
const char * SCIPgetProbName(SCIP *scip)
Definition: scip_prob.c:1067
SCIP_RETCODE SCIPsetEventhdlrCopy(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTCOPY((*eventcopy)))
Definition: scip_event.c:136
#define EVENTHDLR_DESC
Definition: event_bestsol.c:38
SCIP_RETCODE SCIPsetEventhdlrInit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTINIT((*eventinit)))
Definition: scip_event.c:164
#define SCIP_CALL(x)
Definition: def.h:380
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:286
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:1030
eventhdlr for best solution found
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:320
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1300
static SCIP_DECL_EVENTCOPY(eventCopyBestsol)
Definition: event_bestsol.c:42
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:105
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2169
SCIP_RETCODE SCIPincludeEventHdlrBestsol(SCIP *scip)
#define SCIP_Real
Definition: def.h:173
static SCIP_DECL_EVENTEXEC(eventExecBestsol)
Definition: event_bestsol.c:84